diff --git a/primitives/src/currency.rs b/primitives/src/currency.rs index e528a8df61..8a2a73b7c3 100644 --- a/primitives/src/currency.rs +++ b/primitives/src/currency.rs @@ -251,6 +251,21 @@ impl CurrencyId { matches!(self, CurrencyId::Erc20(_)) } + pub fn is_liquid_croadloan_currency_id(&self) -> bool { + matches!(self, CurrencyId::LiquidCroadloan(_)) + } + + pub fn is_foreign_asset_currency_id(&self) -> bool { + matches!(self, CurrencyId::ForeignAsset(_)) + } + + pub fn is_trading_pair_currency_id(&self) -> bool { + matches!( + self, + CurrencyId::Token(_) | CurrencyId::Erc20(_) | CurrencyId::LiquidCroadloan(_) | CurrencyId::ForeignAsset(_) + ) + } + pub fn split_dex_share_currency_id(&self) -> Option<(Self, Self)> { match self { CurrencyId::DexShare(dex_share_0, dex_share_1) => { diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 8cbc5d78bc..e9fec1ae29 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -125,8 +125,8 @@ pub struct TradingPair(CurrencyId, CurrencyId); impl TradingPair { pub fn from_currency_ids(currency_id_a: CurrencyId, currency_id_b: CurrencyId) -> Option { - if (currency_id_a.is_token_currency_id() || currency_id_a.is_erc20_currency_id()) - && (currency_id_b.is_token_currency_id() || currency_id_b.is_erc20_currency_id()) + if currency_id_a.is_trading_pair_currency_id() + && currency_id_b.is_trading_pair_currency_id() && currency_id_a != currency_id_b { if currency_id_a > currency_id_b { diff --git a/runtime/integration-tests/src/dex.rs b/runtime/integration-tests/src/dex.rs index 02f3a0ff53..b5179715df 100644 --- a/runtime/integration-tests/src/dex.rs +++ b/runtime/integration-tests/src/dex.rs @@ -164,3 +164,80 @@ fn test_dex_module() { assert_eq!(Currencies::total_issuance(LPTOKEN), 20_005_999_999_999_999_995); }); } + +#[test] +fn test_trading_pair() { + ExtBuilder::default() + .balances(vec![ + ( + AccountId::from(ALICE), + USD_CURRENCY, + 1_000_000_000 * dollar(NATIVE_CURRENCY), + ), + ( + AccountId::from(ALICE), + RELAY_CHAIN_CURRENCY, + 1_000_000_000 * dollar(NATIVE_CURRENCY), + ), + (AccountId::from(BOB), USD_CURRENCY, 1_000_000 * dollar(NATIVE_CURRENCY)), + ( + AccountId::from(BOB), + RELAY_CHAIN_CURRENCY, + 1_000_000_000 * dollar(NATIVE_CURRENCY), + ), + ]) + .build() + .execute_with(|| { + assert_eq!(Dex::get_liquidity_pool(RELAY_CHAIN_CURRENCY, USD_CURRENCY), (0, 0)); + assert_eq!(Currencies::total_issuance(LPTOKEN), 0); + assert_eq!(Currencies::free_balance(LPTOKEN, &AccountId::from(ALICE)), 0); + + // CurrencyId::DexShare(Token, LiquidCroadloan) + assert_ok!(Dex::list_provisioning( + Origin::root(), + USD_CURRENCY, + CurrencyId::LiquidCroadloan(1), + 10, + 100, + 100, + 1000, + 0, + )); + + // CurrencyId::DexShare(LiquidCroadloan, Token) + assert_ok!(Dex::list_provisioning( + Origin::root(), + CurrencyId::LiquidCroadloan(2), + USD_CURRENCY, + 10, + 100, + 100, + 1000, + 0, + )); + + // CurrencyId::DexShare(Token, ForeignAsset) + assert_ok!(Dex::list_provisioning( + Origin::root(), + USD_CURRENCY, + CurrencyId::ForeignAsset(1), + 10, + 100, + 100, + 1000, + 0, + )); + + // CurrencyId::DexShare(ForeignAsset, Token) + assert_ok!(Dex::list_provisioning( + Origin::root(), + CurrencyId::ForeignAsset(2), + USD_CURRENCY, + 10, + 100, + 100, + 1000, + 0, + )); + }); +}