mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 23:57:56 +00:00
Different XCM builders, default one requires fee payment (#2253)
Adding on top of the new builder pattern for creating XCM programs, I'm adding some more APIs: ```rust let paying_fees: Xcm<()> = Xcm::builder() // Only allow paying for fees .withdraw_asset() // First instruction has to load the holding register .buy_execution() // Second instruction has to be `buy_execution` .build(); let paying_fees_invalid: Xcm<()> = Xcm::builder() .withdraw_asset() .build(); // Invalid, need to pay for fees let not_paying_fees: Xcm<()> = Xcm::builder_unpaid() .unpaid_execution() // Needed .withdraw_asset() .deposit_asset() .build(); let all_goes: Xcm<()> = Xcm::builder_unsafe() // You can do anything .withdraw_asset() .deposit_asset() .build(); ``` The invalid bits are because the methods don't even exist on the types that you'd want to call them on. --------- Co-authored-by: command-bot <>
This commit is contained in:
committed by
GitHub
parent
b25d29a502
commit
b3841b6b71
@@ -0,0 +1,81 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test the methods generated by the Builder derive macro.
|
||||
//! Tests directly on the actual Xcm struct and Instruction enum.
|
||||
|
||||
use xcm::latest::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn builder_pattern_works() {
|
||||
let asset: MultiAsset = (Here, 100u128).into();
|
||||
let beneficiary: MultiLocation = AccountId32 { id: [0u8; 32], network: None }.into();
|
||||
let message: Xcm<()> = Xcm::builder()
|
||||
.receive_teleported_asset(asset.clone().into())
|
||||
.buy_execution(asset.clone(), Unlimited)
|
||||
.deposit_asset(asset.clone().into(), beneficiary)
|
||||
.build();
|
||||
assert_eq!(
|
||||
message,
|
||||
Xcm(vec![
|
||||
ReceiveTeleportedAsset(asset.clone().into()),
|
||||
BuyExecution { fees: asset.clone(), weight_limit: Unlimited },
|
||||
DepositAsset { assets: asset.into(), beneficiary },
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_builder_requires_buy_execution() {
|
||||
let asset: MultiAsset = (Here, 100u128).into();
|
||||
let beneficiary: MultiLocation = AccountId32 { id: [0u8; 32], network: None }.into();
|
||||
// This is invalid, since it doesn't pay for fees.
|
||||
// This is enforced by the runtime, because the build() method doesn't exist
|
||||
// on the resulting type.
|
||||
// let message: Xcm<()> = Xcm::builder()
|
||||
// .withdraw_asset(asset.clone().into())
|
||||
// .deposit_asset(asset.into(), beneficiary)
|
||||
// .build();
|
||||
|
||||
// To be able to do that, we need to use the explicitly unpaid variant
|
||||
let message: Xcm<()> = Xcm::builder_unpaid()
|
||||
.unpaid_execution(Unlimited, None)
|
||||
.withdraw_asset(asset.clone().into())
|
||||
.deposit_asset(asset.clone().into(), beneficiary)
|
||||
.build(); // This works
|
||||
assert_eq!(
|
||||
message,
|
||||
Xcm(vec![
|
||||
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
|
||||
WithdrawAsset(asset.clone().into()),
|
||||
DepositAsset { assets: asset.clone().into(), beneficiary },
|
||||
])
|
||||
);
|
||||
|
||||
// The other option doesn't have any limits whatsoever, so it should
|
||||
// only be used when you really know what you're doing.
|
||||
let message: Xcm<()> = Xcm::builder_unsafe()
|
||||
.withdraw_asset(asset.clone().into())
|
||||
.deposit_asset(asset.clone().into(), beneficiary)
|
||||
.build();
|
||||
assert_eq!(
|
||||
message,
|
||||
Xcm(vec![
|
||||
WithdrawAsset(asset.clone().into()),
|
||||
DepositAsset { assets: asset.clone().into(), beneficiary },
|
||||
])
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user