Skip to content

Commit

Permalink
pallet asset-conversion additional quote tests (#1371)
Browse files Browse the repository at this point in the history
* added identity quote test (only possible if fees are not included in quote)
* add tests that compare quoted price to actual execution
  • Loading branch information
gilescope authored and Ank4n committed Sep 8, 2023
1 parent c725bbe commit 5e56b1a
Showing 1 changed file with 261 additions and 0 deletions.
261 changes: 261 additions & 0 deletions substrate/frame/asset-conversion/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,16 @@ fn can_quote_price() {
),
Some(60)
);
// including fee so should get less out...
assert_eq!(
AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
3000,
true,
),
Some(46)
);
// Check it still gives same price:
// (if the above accidentally exchanged then it would not give same quote as before)
assert_eq!(
Expand All @@ -580,6 +590,16 @@ fn can_quote_price() {
),
Some(60)
);
// including fee so should get less out...
assert_eq!(
AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
3000,
true,
),
Some(46)
);

// Check inverse:
assert_eq!(
Expand All @@ -591,6 +611,247 @@ fn can_quote_price() {
),
Some(3000)
);
// including fee so should get less out...
assert_eq!(
AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Asset(2),
NativeOrAssetId::Native,
60,
true,
),
Some(2302)
);

//
// same tests as above but for quote_price_tokens_for_exact_tokens:
//
assert_eq!(
AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
60,
false,
),
Some(3000)
);
// including fee so should need to put more in...
assert_eq!(
AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
60,
true,
),
Some(4299)
);
// Check it still gives same price:
// (if the above accidentally exchanged then it would not give same quote as before)
assert_eq!(
AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
60,
false,
),
Some(3000)
);
// including fee so should need to put more in...
assert_eq!(
AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
60,
true,
),
Some(4299)
);

// Check inverse:
assert_eq!(
AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Asset(2),
NativeOrAssetId::Native,
3000,
false,
),
Some(60)
);
// including fee so should need to put more in...
assert_eq!(
AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Asset(2),
NativeOrAssetId::Native,
3000,
true,
),
Some(86)
);

//
// roundtrip: Without fees one should get the original number
//
let amount_in = 100;

assert_eq!(
AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Asset(2),
NativeOrAssetId::Native,
amount_in,
false,
)
.and_then(|amount| AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
amount,
false,
)),
Some(amount_in)
);
assert_eq!(
AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
amount_in,
false,
)
.and_then(|amount| AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Asset(2),
NativeOrAssetId::Native,
amount,
false,
)),
Some(amount_in)
);

assert_eq!(
AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Asset(2),
NativeOrAssetId::Native,
amount_in,
false,
)
.and_then(|amount| AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
amount,
false,
)),
Some(amount_in)
);
assert_eq!(
AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(2),
amount_in,
false,
)
.and_then(|amount| AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Asset(2),
NativeOrAssetId::Native,
amount,
false,
)),
Some(amount_in)
);
});
}

#[test]
fn quote_price_exact_tokens_for_tokens_matches_execution() {
new_test_ext().execute_with(|| {
let user = 1;
let user2 = 2;
let token_1 = NativeOrAssetId::Native;
let token_2 = NativeOrAssetId::Asset(2);

create_tokens(user, vec![token_2]);
assert_ok!(AssetConversion::create_pool(RuntimeOrigin::signed(user), token_1, token_2));

assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), user, 100000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), 2, user, 1000));

assert_ok!(AssetConversion::add_liquidity(
RuntimeOrigin::signed(user),
token_1,
token_2,
10000,
200,
1,
1,
user,
));

let amount = 1;
let quoted_price = 49;
assert_eq!(
AssetConversion::quote_price_exact_tokens_for_tokens(token_2, token_1, amount, true,),
Some(quoted_price)
);

assert_ok!(Assets::mint(RuntimeOrigin::signed(user), 2, user2, amount));
let prior_dot_balance = 20000;
assert_eq!(prior_dot_balance, balance(user2, token_1));
assert_ok!(AssetConversion::swap_exact_tokens_for_tokens(
RuntimeOrigin::signed(user2),
bvec![token_2, token_1],
amount,
1,
user2,
false,
));

assert_eq!(prior_dot_balance + quoted_price, balance(user2, token_1));
});
}

#[test]
fn quote_price_tokens_for_exact_tokens_matches_execution() {
new_test_ext().execute_with(|| {
let user = 1;
let user2 = 2;
let token_1 = NativeOrAssetId::Native;
let token_2 = NativeOrAssetId::Asset(2);

create_tokens(user, vec![token_2]);
assert_ok!(AssetConversion::create_pool(RuntimeOrigin::signed(user), token_1, token_2));

assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), user, 100000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), 2, user, 1000));

assert_ok!(AssetConversion::add_liquidity(
RuntimeOrigin::signed(user),
token_1,
token_2,
10000,
200,
1,
1,
user,
));

let amount = 49;
let quoted_price = 1;
assert_eq!(
AssetConversion::quote_price_tokens_for_exact_tokens(token_2, token_1, amount, true,),
Some(quoted_price)
);

assert_ok!(Assets::mint(RuntimeOrigin::signed(user), 2, user2, amount));
let prior_dot_balance = 20000;
assert_eq!(prior_dot_balance, balance(user2, token_1));
let prior_asset_balance = 49;
assert_eq!(prior_asset_balance, balance(user2, token_2));
assert_ok!(AssetConversion::swap_tokens_for_exact_tokens(
RuntimeOrigin::signed(user2),
bvec![token_2, token_1],
amount,
1,
user2,
false,
));

assert_eq!(prior_dot_balance + amount, balance(user2, token_1));
assert_eq!(prior_asset_balance - quoted_price, balance(user2, token_2));
});
}

Expand Down

0 comments on commit 5e56b1a

Please sign in to comment.