Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use relaychain asset as fee #700

Merged
merged 27 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions xtokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,53 @@ pub mod module {

Self::do_transfer_multiassets(who, assets.clone(), fee.clone(), dest, dest_weight, true)
}

/// Transfer currency and use relay-chain asset as fee
zqhxuyuan marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::weight(Pallet::<T>::weight_of_transfer(currency_id.clone(), *amount, dest))]
#[transactional]
pub fn transfer_using_relaychain_as_fee(
origin: OriginFor<T>,
currency_id: T::CurrencyId,
amount: T::Balance,
dest: Box<VersionedMultiLocation>,
dest_weight: Weight,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let dest: MultiLocation = (*dest).try_into().map_err(|()| Error::<T>::BadVersion)?;

let location: MultiLocation = T::CurrencyIdConvert::convert(currency_id.clone())
.ok_or(Error::<T>::NotCrossChainTransferableCurrency)?;
let asset: MultiAsset = (location, amount.into()).into();

let fee = MultiAsset {
id: AssetId::Concrete(MultiLocation::new(1, Junctions::Here)),
zqhxuyuan marked this conversation as resolved.
Show resolved Hide resolved
fun: Fungibility::Fungible(dest_weight.into()),
};

let mut assets = MultiAssets::new();
assets.push(asset.clone());
assets.push(fee.clone());

let (_, dest, reserve, recipient) = Self::transfer_kind(&asset, &dest)?;
ensure!(dest == reserve, "Asset should match to destination!");
zqhxuyuan marked this conversation as resolved.
Show resolved Hide resolved
let mut msg = Self::transfer_to_reserve(assets, fee, dest.clone(), recipient, dest_weight)?;

let origin_location = T::AccountIdToMultiLocation::convert(who.clone());
let weight = T::Weigher::weight(&mut msg).map_err(|()| Error::<T>::UnweighableMessage)?;
T::XcmExecutor::execute_xcm_in_credit(origin_location, msg, weight, weight)
.ensure_complete()
.map_err(|error| {
log::error!("Failed execute transfer message with {:?}", error);
Error::<T>::XcmExecutionFailed
})?;
Self::deposit_event(Event::<T>::Transferred {
sender: who,
currency_id,
amount,
dest,
});
Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down
2 changes: 1 addition & 1 deletion xtokens/src/mock/para.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl Convert<AccountId, MultiLocation> for AccountIdToMultiLocation {

parameter_types! {
pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));
pub const BaseXcmWeight: Weight = 100_000_000;
pub const BaseXcmWeight: Weight = 10;
zqhxuyuan marked this conversation as resolved.
Show resolved Hide resolved
pub const MaxAssetsForTransfer: usize = 2;
}

Expand Down
43 changes: 43 additions & 0 deletions xtokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,49 @@ fn sending_assets_with_different_reserve_should_fail() {
});
}

#[test]
fn sending_assets_with_relaychain_as_fee() {
TestNet::reset();

ParaA::execute_with(|| {
assert_ok!(ParaTokens::deposit(CurrencyId::B, &ALICE, 1_000));
});

ParaB::execute_with(|| {
assert_ok!(ParaTokens::deposit(CurrencyId::B, &sibling_a_account(), 1_000));
assert_ok!(ParaTokens::deposit(CurrencyId::R, &sibling_a_account(), 1_000));
});

ParaA::execute_with(|| {
assert_ok!(ParaXTokens::transfer_using_relaychain_as_fee(
Some(ALICE).into(),
CurrencyId::B,
500,
Box::new(
(
Parent,
Parachain(2),
Junction::AccountId32 {
network: NetworkId::Any,
id: BOB.into(),
},
)
.into()
),
40,
));

assert_eq!(500, ParaTokens::free_balance(CurrencyId::B, &ALICE));
assert_eq!(960, ParaTokens::free_balance(CurrencyId::R, &ALICE));
});

ParaB::execute_with(|| {
assert_eq!(500, ParaTokens::free_balance(CurrencyId::B, &BOB));
assert_eq!(500, ParaTokens::free_balance(CurrencyId::B, &sibling_a_account()));
assert_eq!(960, ParaTokens::free_balance(CurrencyId::R, &sibling_a_account()));
});
}

#[test]
fn specifying_a_non_existent_asset_index_should_fail() {
TestNet::reset();
Expand Down