From 26dbb91df0d19751547be7766297b660db10cc7a Mon Sep 17 00:00:00 2001 From: gorka Date: Tue, 15 Feb 2022 14:52:32 +0100 Subject: [PATCH 1/4] Fix index for fee in transfer_multi_currencies --- xtokens/src/lib.rs | 17 +++++++++++----- xtokens/src/tests.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/xtokens/src/lib.rs b/xtokens/src/lib.rs index 1248f0f79..158157222 100644 --- a/xtokens/src/lib.rs +++ b/xtokens/src/lib.rs @@ -522,6 +522,12 @@ pub mod module { dest_weight: Weight, ) -> DispatchResult { let mut assets = MultiAssets::new(); + + // Lets grab the fee amount and location first + let (fee_currency_id, fee_amount) = currencies + .get(fee_item as usize) + .ok_or(Error::::AssetIndexNonExistent)?; + for (currency_id, amount) in ¤cies { let location: MultiLocation = T::CurrencyIdConvert::convert(currency_id.clone()) .ok_or(Error::::NotCrossChainTransferableCurrency)?; @@ -529,11 +535,12 @@ pub mod module { assets.push((location, (*amount).into()).into()) } - // We first grab the fee - let fee = assets - .get(fee_item as usize) - .ok_or(Error::::AssetIndexNonExistent)? - .clone(); + // We construct the fee now, since getting it from assets wont work as assets + // sorts it + let fee_location: MultiLocation = T::CurrencyIdConvert::convert(fee_currency_id.clone()) + .ok_or(Error::::NotCrossChainTransferableCurrency)?; + + let fee: MultiAsset = (fee_location, (*fee_amount).into()).into(); Self::do_transfer_multiassets(who.clone(), assets, fee, dest.clone(), dest_weight, false)?; diff --git a/xtokens/src/tests.rs b/xtokens/src/tests.rs index 5b83a48e9..4e835d225 100644 --- a/xtokens/src/tests.rs +++ b/xtokens/src/tests.rs @@ -351,6 +351,52 @@ fn send_sibling_asset_to_reserve_sibling_with_distinc_fee() { assert_ok!(ParaTokens::deposit(CurrencyId::B1, &sibling_a_account(), 1_000)); }); + ParaA::execute_with(|| { + assert_ok!(ParaXTokens::transfer_multicurrencies( + Some(ALICE).into(), + vec![(CurrencyId::B1, 50), (CurrencyId::B, 450)], + 0, + Box::new( + ( + Parent, + Parachain(2), + Junction::AccountId32 { + network: NetworkId::Any, + id: BOB.into(), + }, + ) + .into() + ), + 40, + )); + + assert_eq!(ParaTokens::free_balance(CurrencyId::B, &ALICE), 550); + assert_eq!(ParaTokens::free_balance(CurrencyId::B1, &ALICE), 950); + }); + + // It should use 40 for weight, so 450 B and 10 B1 should reach destination + ParaB::execute_with(|| { + assert_eq!(ParaTokens::free_balance(CurrencyId::B, &sibling_a_account()), 550); + assert_eq!(ParaTokens::free_balance(CurrencyId::B1, &sibling_a_account()), 950); + assert_eq!(ParaTokens::free_balance(CurrencyId::B, &BOB), 450); + assert_eq!(ParaTokens::free_balance(CurrencyId::B1, &BOB), 10); + }); +} + +#[test] +fn send_sibling_asset_to_reserve_sibling_with_distinc_fee_index_works() { + TestNet::reset(); + + ParaA::execute_with(|| { + assert_ok!(ParaTokens::deposit(CurrencyId::B, &ALICE, 1_000)); + assert_ok!(ParaTokens::deposit(CurrencyId::B1, &ALICE, 1_000)); + }); + + ParaB::execute_with(|| { + assert_ok!(ParaTokens::deposit(CurrencyId::B, &sibling_a_account(), 1_000)); + assert_ok!(ParaTokens::deposit(CurrencyId::B1, &sibling_a_account(), 1_000)); + }); + ParaA::execute_with(|| { assert_ok!(ParaXTokens::transfer_multicurrencies( Some(ALICE).into(), From 32a0850f7cc53a8b2c84d5083c5f3b36c4fdf611 Mon Sep 17 00:00:00 2001 From: gorka Date: Tue, 15 Feb 2022 14:56:55 +0100 Subject: [PATCH 2/4] Add removed event from the last refactor --- xtokens/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xtokens/src/lib.rs b/xtokens/src/lib.rs index 158157222..638c8a3cc 100644 --- a/xtokens/src/lib.rs +++ b/xtokens/src/lib.rs @@ -511,6 +511,8 @@ pub mod module { Self::do_transfer_multiassets(who, assets, fee, dest, dest_weight, false)?; + Self::deposit_event(Event::::TransferredMultiAssetWithFee(who, asset, fee, dest)); + Ok(()) } From 3b7ab3ac6a73e48ac958af0367111f443c3bd6b9 Mon Sep 17 00:00:00 2001 From: gorka Date: Tue, 15 Feb 2022 15:43:28 +0100 Subject: [PATCH 3/4] Fix event --- xtokens/src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/xtokens/src/lib.rs b/xtokens/src/lib.rs index 638c8a3cc..1c9015eb3 100644 --- a/xtokens/src/lib.rs +++ b/xtokens/src/lib.rs @@ -511,7 +511,12 @@ pub mod module { Self::do_transfer_multiassets(who, assets, fee, dest, dest_weight, false)?; - Self::deposit_event(Event::::TransferredMultiAssetWithFee(who, asset, fee, dest)); + Self::deposit_event(Event::::TransferredMultiAssetWithFee { + sender: who, + asset, + fee, + dest, + }); Ok(()) } From 50df512fb005a136b13bbf81bd049723f4a762c5 Mon Sep 17 00:00:00 2001 From: gorka Date: Tue, 15 Feb 2022 18:19:03 +0100 Subject: [PATCH 4/4] Fix event --- xtokens/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xtokens/src/lib.rs b/xtokens/src/lib.rs index 1c9015eb3..64f3e1977 100644 --- a/xtokens/src/lib.rs +++ b/xtokens/src/lib.rs @@ -506,10 +506,10 @@ pub mod module { // Push contains saturated addition, so we should be able to use it safely let mut assets = MultiAssets::new(); - assets.push(asset); + assets.push(asset.clone()); assets.push(fee.clone()); - Self::do_transfer_multiassets(who, assets, fee, dest, dest_weight, false)?; + Self::do_transfer_multiassets(who.clone(), assets, fee.clone(), dest.clone(), dest_weight, false)?; Self::deposit_event(Event::::TransferredMultiAssetWithFee { sender: who,