Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #26 from jsidorenko/js-multi-swaps
Browse files Browse the repository at this point in the history
Add path param to token swap methods
  • Loading branch information
jsidorenko committed Dec 20, 2022
2 parents 49ca641 + 9cb0779 commit 318d488
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 89 deletions.
1 change: 1 addition & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,7 @@ impl pallet_dex::Config for Runtime {
type PalletId = DexPalletId;
type WeightInfo = pallet_dex::weights::SubstrateWeight<Runtime>;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = ConstU32<4>;
}

parameter_types! {
Expand Down
59 changes: 45 additions & 14 deletions frame/dex/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use super::*;
use frame_benchmarking::{benchmarks, whitelisted_caller};
use frame_support::{
assert_ok,
storage::bounded_vec::BoundedVec,
traits::{
fungible::Unbalanced,
fungibles::{Create, Mutate},
Expand Down Expand Up @@ -162,64 +163,94 @@ benchmarks! {
swap_exact_tokens_for_tokens {
let asset1 = MultiAssetId::Native;
let asset2 = MultiAssetId::Asset(0.into());
let (lp_token, caller, _) = create_asset_and_pool::<T>(asset1, asset2);
let asset3 = MultiAssetId::Asset(1.into());
let (_, caller, _) = create_asset_and_pool::<T>(asset1, asset2);
let (_, _) = create_asset::<T>(asset3);
Dex::<T>::create_pool(SystemOrigin::Signed(caller.clone()).into(), asset2, asset3)?;
let deadline = T::BlockNumber::max_value();
let path: BoundedVec<_, T::MaxSwapPathLength> =
BoundedVec::try_from(vec![asset1, asset2, asset3]).unwrap();

Dex::<T>::add_liquidity(
SystemOrigin::Signed(caller.clone()).into(),
asset1,
asset2,
10000.into(),
200.into(),
10.into(),
10.into(),
caller.clone(),
deadline,
false,
)?;
Dex::<T>::add_liquidity(
SystemOrigin::Signed(caller.clone()).into(),
asset2,
asset3,
200.into(),
2000.into(),
10.into(),
10.into(),
caller.clone(),
deadline,
false,
)?;
}: _(SystemOrigin::Signed(caller.clone()), asset1, asset2, 5.into(), 1.into(), caller.clone(), deadline, false)
}: _(SystemOrigin::Signed(caller.clone()), path.clone(), 500.into(), 80.into(), caller.clone(), deadline, false)
verify {
let pool_id = (asset1, asset2);
assert_last_event::<T>(Event::SwapExecuted {
who: caller.clone(),
send_to: caller.clone(),
asset1,
asset2,
pool_id,
amount_in: 5.into(),
amount_out: 3.into(),
path,
amount_in: 500.into(),
amount_out: 85.into(),
}.into());
}

swap_tokens_for_exact_tokens {
let asset1 = MultiAssetId::Native;
let asset2 = MultiAssetId::Asset(0.into());
let (lp_token, caller, _) = create_asset_and_pool::<T>(asset1, asset2);
let asset3 = MultiAssetId::Asset(1.into());
let (_, caller, _) = create_asset_and_pool::<T>(asset1, asset2);
let (_, _) = create_asset::<T>(asset3);
Dex::<T>::create_pool(SystemOrigin::Signed(caller.clone()).into(), asset2, asset3)?;
let deadline = T::BlockNumber::max_value();
let path: BoundedVec<_, T::MaxSwapPathLength> =
BoundedVec::try_from(vec![asset1, asset2, asset3]).unwrap();

Dex::<T>::add_liquidity(
SystemOrigin::Signed(caller.clone()).into(),
asset1,
asset2,
10000.into(),
200.into(),
10.into(),
10.into(),
caller.clone(),
deadline,
false,
)?;
Dex::<T>::add_liquidity(
SystemOrigin::Signed(caller.clone()).into(),
asset2,
asset3,
200.into(),
2000.into(),
10.into(),
10.into(),
caller.clone(),
deadline,
false,
)?;
}: _(SystemOrigin::Signed(caller.clone()), asset1, asset2, 3.into(), 8.into(), caller.clone(), deadline, false)
}: _(SystemOrigin::Signed(caller.clone()), path.clone(), 100.into(), 1000.into(), caller.clone(), deadline, false)
verify {
let pool_id = (asset1, asset2);
assert_last_event::<T>(Event::SwapExecuted {
who: caller.clone(),
send_to: caller.clone(),
asset1,
asset2,
pool_id,
amount_in: 5.into(),
amount_out: 3.into(),
path,
amount_in: 584.into(),
amount_out: 100.into(),
}.into());
}

Expand Down
Loading

0 comments on commit 318d488

Please sign in to comment.