Skip to content

Commit

Permalink
Girazoki fix index in transfer multicurrencies (#696)
Browse files Browse the repository at this point in the history
* Fix index for fee in transfer_multi_currencies

* Add removed event from the last refactor

* Fix event

* Fix event
  • Loading branch information
girazoki committed Feb 15, 2022
1 parent b31124e commit 4a66b29
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
28 changes: 21 additions & 7 deletions xtokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,17 @@ 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::<T>::TransferredMultiAssetWithFee {
sender: who,
asset,
fee,
dest,
});

Ok(())
}
Expand All @@ -522,18 +529,25 @@ 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::<T>::AssetIndexNonExistent)?;

for (currency_id, amount) in &currencies {
let location: MultiLocation = T::CurrencyIdConvert::convert(currency_id.clone())
.ok_or(Error::<T>::NotCrossChainTransferableCurrency)?;
// Push contains saturated addition, so we should be able to use it safely
assets.push((location, (*amount).into()).into())
}

// We first grab the fee
let fee = assets
.get(fee_item as usize)
.ok_or(Error::<T>::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::<T>::NotCrossChainTransferableCurrency)?;

let fee: MultiAsset = (fee_location, (*fee_amount).into()).into();

Self::do_transfer_multiassets(who.clone(), assets, fee, dest.clone(), dest_weight, false)?;

Expand Down
46 changes: 46 additions & 0 deletions xtokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 4a66b29

Please sign in to comment.