Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:sora-xor/sora2-network into trus…
Browse files Browse the repository at this point in the history
…tless-evm-bridge
  • Loading branch information
vovac12 committed Apr 5, 2023
2 parents 0e05464 + 044b95b commit 5652c2d
Show file tree
Hide file tree
Showing 35 changed files with 367 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ sp-std = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot
static_assertions = "1.1.0"
hex = { version = "*", default-features = false }
hex-literal = "0.3.1"
env_logger = { version = "0.9.0", optional = true }

[dev-dependencies]
serde_json = "1.0.41"
Expand Down Expand Up @@ -73,4 +74,4 @@ std = [
'hex/std',
"fixnum/std",
]
test = []
test = ["env_logger"]
2 changes: 1 addition & 1 deletion common/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ impl Default for RewardReason {
#[derive(Encode, Decode, Clone, RuntimeDebug, Default, scale_info::TypeInfo)]
pub struct PswapRemintInfo {
pub liquidity_providers: Balance,
pub parliament: Balance,
pub buy_back_xst: Balance,
pub vesting: Balance,
}

Expand Down
4 changes: 4 additions & 0 deletions common/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ macro_rules! assert_noop_msg {
);
};
}

pub fn init_logger() {
let _ = env_logger::builder().is_test(true).try_init();
}
40 changes: 40 additions & 0 deletions common/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,43 @@ impl<DEXId: PartialEq + Copy, AccountId, AssetId> LiquidityProxyTrait<DEXId, Acc
pub trait IsValid {
fn is_valid(&self) -> bool;
}

pub trait BuyBackHandler<AccountId, AssetId> {
/// Mint `amount` of `mint_asset_id`, exchange to `buy_back_asset_id` and burn result amount
///
/// Returns burned amount
fn mint_buy_back_and_burn(
mint_asset_id: &AssetId,
buy_back_asset_id: &AssetId,
amount: Balance,
) -> Result<Balance, DispatchError>;

/// Exchange `amount` of `asset_id` from `account_id` to `buy_back_asset_id` and burn result amount
///
/// Returns burned amount
fn buy_back_and_burn(
account_id: &AccountId,
asset_id: &AssetId,
buy_back_asset_id: &AssetId,
amount: Balance,
) -> Result<Balance, DispatchError>;
}

impl<AssetId, AccountId> BuyBackHandler<AccountId, AssetId> for () {
fn mint_buy_back_and_burn(
_mint_asset_id: &AssetId,
_buy_back_asset_id: &AssetId,
_amount: Balance,
) -> Result<Balance, DispatchError> {
Ok(0)
}

fn buy_back_and_burn(
_account_id: &AccountId,
_asset_id: &AssetId,
_buy_back_asset_id: &AssetId,
_amount: Balance,
) -> Result<Balance, DispatchError> {
Ok(0)
}
}
2 changes: 2 additions & 0 deletions pallets/ceres-governance-platform/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl pswap_distribution::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -203,6 +204,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl technical::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/ceres-launchpad/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl pswap_distribution::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -244,6 +245,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl technical::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/ceres-liquidity-locker/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl pswap_distribution::Config for Runtime {
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type RuntimeEvent = RuntimeEvent;
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -233,6 +234,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl pallet_timestamp::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/ceres-liquidity-locker/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ impl pswap_distribution::Config for Runtime {
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type RuntimeEvent = RuntimeEvent;
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -241,6 +242,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl ceres_liquidity_locker::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/ceres-token-locker/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl pswap_distribution::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -187,6 +188,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl pallet_timestamp::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/demeter-farming-platform/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl pswap_distribution::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -233,6 +234,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl technical::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/demeter-farming-platform/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ impl pswap_distribution::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -230,6 +231,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl technical::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/dex-api/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ impl pswap_distribution::Config for Runtime {
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type RuntimeEvent = RuntimeEvent;
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -309,6 +310,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl pallet_timestamp::Config for Runtime {
Expand Down
2 changes: 1 addition & 1 deletion pallets/eth-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ pub mod pallet {

#[pallet::type_value]
pub fn DefaultForBridgeSignatureVersion() -> BridgeSignatureVersion {
BridgeSignatureVersion::V1
BridgeSignatureVersion::V3
}

#[pallet::storage]
Expand Down
2 changes: 2 additions & 0 deletions pallets/farming/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ impl pswap_distribution::Config for Runtime {
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type RuntimeEvent = RuntimeEvent;
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -305,6 +306,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl multicollateral_bonding_curve_pool::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/hermes-governance-platform/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl pswap_distribution::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -212,6 +213,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl technical::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/liquidity-proxy/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ impl pswap_distribution::Config for Runtime {
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type RuntimeEvent = RuntimeEvent;
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = liquidity_proxy::Pallet<Runtime>;
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -509,6 +510,7 @@ impl pswap_distribution::Config for Runtime {
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type PoolXykPallet = PoolXYK;
type BuyBackHandler = ();
}

impl price_tools::Config for Runtime {
Expand Down
48 changes: 46 additions & 2 deletions pallets/liquidity-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@

extern crate core;

use core::marker::PhantomData;

use codec::{Decode, Encode};

use assets::AssetIdOf;
use assets::WeightInfo as _;
use common::prelude::fixnum::ops::{Bounded, Zero as _};
use common::prelude::{Balance, FixedWrapper, QuoteAmount, SwapAmount, SwapOutcome, SwapVariant};
use common::{
balance, fixed_wrapper, AccountIdOf, DEXInfo, DexIdOf, FilterMode, Fixed, GetMarketInfo,
GetPoolReserves, LiquidityProxyTrait, LiquidityRegistry, LiquiditySource,
balance, fixed_wrapper, AccountIdOf, BuyBackHandler, DEXInfo, DexIdOf, FilterMode, Fixed,
GetMarketInfo, GetPoolReserves, LiquidityProxyTrait, LiquidityRegistry, LiquiditySource,
LiquiditySourceFilter, LiquiditySourceId, LiquiditySourceType, RewardReason, TradingPair,
VestedRewardsPallet, XSTUSD,
};
Expand Down Expand Up @@ -2073,6 +2075,48 @@ impl<AssetId, DEXId, AccountId> SwapBatchInfo<AssetId, DEXId, AccountId> {
}
}

pub struct LiquidityProxyBuyBackHandler<T, GetDEXId>(PhantomData<(T, GetDEXId)>);

impl<T: Config, GetDEXId: Get<T::DEXId>> BuyBackHandler<T::AccountId, T::AssetId>
for LiquidityProxyBuyBackHandler<T, GetDEXId>
{
fn mint_buy_back_and_burn(
mint_asset_id: &T::AssetId,
buy_back_asset_id: &T::AssetId,
amount: Balance,
) -> Result<Balance, DispatchError> {
let owner = assets::Pallet::<T>::asset_owner(&mint_asset_id)
.ok_or(assets::Error::<T>::AssetIdNotExists)?;
let transit = T::GetTechnicalAccountId::get();
assets::Pallet::<T>::mint_to(mint_asset_id, &owner, &transit, amount)?;
let amount = Self::buy_back_and_burn(&transit, mint_asset_id, buy_back_asset_id, amount)?;
Ok(amount)
}

fn buy_back_and_burn(
account_id: &T::AccountId,
asset_id: &T::AssetId,
buy_back_asset_id: &T::AssetId,
amount: Balance,
) -> Result<Balance, DispatchError> {
let dex_id = GetDEXId::get();
let outcome = Pallet::<T>::exchange(
dex_id,
account_id,
account_id,
asset_id,
buy_back_asset_id,
SwapAmount::with_desired_input(amount, 0),
LiquiditySourceFilter::with_forbidden(
dex_id,
vec![LiquiditySourceType::MulticollateralBondingCurvePool],
),
)?;
assets::Pallet::<T>::burn_from(buy_back_asset_id, account_id, account_id, outcome.amount)?;
Ok(outcome.amount)
}
}

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down
6 changes: 4 additions & 2 deletions pallets/liquidity-proxy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::{self as liquidity_proxy, Config};
use crate::{self as liquidity_proxy, Config, LiquidityProxyBuyBackHandler};
use common::mock::ExistentialDeposits;
use common::{
self, balance, fixed, fixed_from_basis_points, fixed_wrapper, hash, Amount, AssetId32,
Expand Down Expand Up @@ -230,7 +230,7 @@ parameter_types! {
pub const GetBuyBackAccountId: AccountId = AccountId::new(hex!(
"0000000000000000000000000000000000000000000000000000000000000023"
));
pub const GetBuyBackDexId: DEXId = 0;
pub const GetBuyBackDexId: DEXId = DEX_A_ID;
}

impl assets::Config for Runtime {
Expand Down Expand Up @@ -331,6 +331,7 @@ impl pswap_distribution::Config for Runtime {
const PSWAP_BURN_PERCENT: Percent = Percent::from_percent(3);
type RuntimeEvent = RuntimeEvent;
type GetIncentiveAssetId = GetIncentiveAssetId;
type GetXSTAssetId = GetBuyBackAssetId;
type LiquidityProxy = ();
type CompatBalance = Balance;
type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
Expand All @@ -341,6 +342,7 @@ impl pswap_distribution::Config for Runtime {
type PoolXykPallet = pool_xyk::Pallet<Runtime>;
type WeightInfo = ();
type GetParliamentAccountId = GetParliamentAccountId;
type BuyBackHandler = LiquidityProxyBuyBackHandler<Runtime, GetBuyBackDexId>;
}

impl demeter_farming_platform::Config for Runtime {
Expand Down
Loading

0 comments on commit 5652c2d

Please sign in to comment.