From fb238ff8a5d05605bbb31a04e88c844fcfa5305b Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 3 Sep 2023 13:09:41 +0100 Subject: [PATCH 1/5] additional tests --- substrate/frame/asset-conversion/src/tests.rs | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/substrate/frame/asset-conversion/src/tests.rs b/substrate/frame/asset-conversion/src/tests.rs index 190e4fb62147..5f4aad467e44 100644 --- a/substrate/frame/asset-conversion/src/tests.rs +++ b/substrate/frame/asset-conversion/src/tests.rs @@ -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!( @@ -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!( @@ -591,6 +611,81 @@ 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) + ); }); } From d62b437bf281500267a6e013e4b96ba4202bce32 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 5 Sep 2023 21:39:49 +0100 Subject: [PATCH 2/5] added identity quote test (only possible if fees are not included in quote) --- substrate/frame/asset-conversion/src/tests.rs | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/substrate/frame/asset-conversion/src/tests.rs b/substrate/frame/asset-conversion/src/tests.rs index 5f4aad467e44..ca563e33e57c 100644 --- a/substrate/frame/asset-conversion/src/tests.rs +++ b/substrate/frame/asset-conversion/src/tests.rs @@ -686,6 +686,79 @@ fn can_quote_price() { ), Some(86) ); + + // + // roundtrip: Without fees one should get the original number + // + let amount_in = 60; + assert_eq!( + AssetConversion::quote_price_exact_tokens_for_tokens( + NativeOrAssetId::Asset(2), + NativeOrAssetId::Native, + amount_in, + false, + ) + .map(|amount| AssetConversion::quote_price_exact_tokens_for_tokens( + NativeOrAssetId::Native, + NativeOrAssetId::Asset(2), + amount, + false, + )) + .flatten(), + Some(amount_in) + ); + let amount_in = 3000; + assert_eq!( + AssetConversion::quote_price_exact_tokens_for_tokens( + NativeOrAssetId::Native, + NativeOrAssetId::Asset(2), + amount_in, + false, + ) + .map(|amount| AssetConversion::quote_price_exact_tokens_for_tokens( + NativeOrAssetId::Asset(2), + NativeOrAssetId::Native, + amount, + false, + )) + .flatten(), + Some(amount_in) + ); + + let amount_in = 3000; + assert_eq!( + AssetConversion::quote_price_tokens_for_exact_tokens( + NativeOrAssetId::Asset(2), + NativeOrAssetId::Native, + amount_in, + false, + ) + .map(|amount| AssetConversion::quote_price_tokens_for_exact_tokens( + NativeOrAssetId::Native, + NativeOrAssetId::Asset(2), + amount, + false, + )) + .flatten(), + Some(amount_in) + ); + let amount_in = 60; + assert_eq!( + AssetConversion::quote_price_tokens_for_exact_tokens( + NativeOrAssetId::Native, + NativeOrAssetId::Asset(2), + amount_in, + false, + ) + .map(|amount| AssetConversion::quote_price_tokens_for_exact_tokens( + NativeOrAssetId::Asset(2), + NativeOrAssetId::Native, + amount, + false, + )) + .flatten(), + Some(amount_in) + ); }); } From a5214a01fc5694504cee71f857d898693f74fe56 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 5 Sep 2023 21:54:43 +0100 Subject: [PATCH 3/5] clippy knoweth best --- substrate/frame/asset-conversion/src/tests.rs | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/substrate/frame/asset-conversion/src/tests.rs b/substrate/frame/asset-conversion/src/tests.rs index ca563e33e57c..d57c0033e2ef 100644 --- a/substrate/frame/asset-conversion/src/tests.rs +++ b/substrate/frame/asset-conversion/src/tests.rs @@ -698,13 +698,12 @@ fn can_quote_price() { amount_in, false, ) - .map(|amount| AssetConversion::quote_price_exact_tokens_for_tokens( + .and_then(|amount| AssetConversion::quote_price_exact_tokens_for_tokens( NativeOrAssetId::Native, NativeOrAssetId::Asset(2), amount, false, - )) - .flatten(), + )), Some(amount_in) ); let amount_in = 3000; @@ -715,13 +714,12 @@ fn can_quote_price() { amount_in, false, ) - .map(|amount| AssetConversion::quote_price_exact_tokens_for_tokens( + .and_then(|amount| AssetConversion::quote_price_exact_tokens_for_tokens( NativeOrAssetId::Asset(2), NativeOrAssetId::Native, amount, false, - )) - .flatten(), + )), Some(amount_in) ); @@ -733,13 +731,12 @@ fn can_quote_price() { amount_in, false, ) - .map(|amount| AssetConversion::quote_price_tokens_for_exact_tokens( + .and_then(|amount| AssetConversion::quote_price_tokens_for_exact_tokens( NativeOrAssetId::Native, NativeOrAssetId::Asset(2), amount, false, - )) - .flatten(), + )), Some(amount_in) ); let amount_in = 60; @@ -750,13 +747,12 @@ fn can_quote_price() { amount_in, false, ) - .map(|amount| AssetConversion::quote_price_tokens_for_exact_tokens( + .and_then(|amount| AssetConversion::quote_price_tokens_for_exact_tokens( NativeOrAssetId::Asset(2), NativeOrAssetId::Native, amount, false, - )) - .flatten(), + )), Some(amount_in) ); }); From 368243997188b4f14a6a91f9a05287b589b40d40 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 5 Sep 2023 22:01:52 +0100 Subject: [PATCH 4/5] simplify --- substrate/frame/asset-conversion/src/tests.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/substrate/frame/asset-conversion/src/tests.rs b/substrate/frame/asset-conversion/src/tests.rs index d57c0033e2ef..4c73b098c0a9 100644 --- a/substrate/frame/asset-conversion/src/tests.rs +++ b/substrate/frame/asset-conversion/src/tests.rs @@ -690,7 +690,8 @@ fn can_quote_price() { // // roundtrip: Without fees one should get the original number // - let amount_in = 60; + let amount_in = 100; + assert_eq!( AssetConversion::quote_price_exact_tokens_for_tokens( NativeOrAssetId::Asset(2), @@ -706,7 +707,6 @@ fn can_quote_price() { )), Some(amount_in) ); - let amount_in = 3000; assert_eq!( AssetConversion::quote_price_exact_tokens_for_tokens( NativeOrAssetId::Native, @@ -723,7 +723,6 @@ fn can_quote_price() { Some(amount_in) ); - let amount_in = 3000; assert_eq!( AssetConversion::quote_price_tokens_for_exact_tokens( NativeOrAssetId::Asset(2), @@ -739,7 +738,6 @@ fn can_quote_price() { )), Some(amount_in) ); - let amount_in = 60; assert_eq!( AssetConversion::quote_price_tokens_for_exact_tokens( NativeOrAssetId::Native, From 4cdd189cc0124fee188e5233fe65318c3cf859f2 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Wed, 6 Sep 2023 11:26:14 +0100 Subject: [PATCH 5/5] add tests that compare quoted price to actual execution --- substrate/frame/asset-conversion/src/tests.rs | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/substrate/frame/asset-conversion/src/tests.rs b/substrate/frame/asset-conversion/src/tests.rs index 4c73b098c0a9..5fd5129c5866 100644 --- a/substrate/frame/asset-conversion/src/tests.rs +++ b/substrate/frame/asset-conversion/src/tests.rs @@ -756,6 +756,105 @@ fn can_quote_price() { }); } +#[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)); + }); +} + #[test] fn can_swap_with_native() { new_test_ext().execute_with(|| {