Skip to content

Commit

Permalink
Support LiquidCroadloan and ForeignAsset for TradingPair (#1703)
Browse files Browse the repository at this point in the history
* enable LiquidCroadloan and ForeignAsset for TradingPair

* fix tests
  • Loading branch information
zjb0807 authored Dec 15, 2021
1 parent b8a0480 commit 5e075a5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
15 changes: 15 additions & 0 deletions primitives/src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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 {
Expand Down
77 changes: 77 additions & 0 deletions runtime/integration-tests/src/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
});
}

0 comments on commit 5e075a5

Please sign in to comment.