Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Add a new transact example #27

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
72 changes: 55 additions & 17 deletions examples/src/expects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tests {
use xcm::latest::prelude::*;
use xcm_simulator::TestExt;

const AMOUNT: u128 = 10;
const AMOUNT: u128 = 50 * CENTS;
const QUERY_ID: u64 = 1234;

/// Scenario:
Expand All @@ -19,30 +19,41 @@ mod tests {
#[test]
fn expect_asset() {
MockNet::reset();

let message_fee = relay_chain::estimate_message_fee(5);

ParaA::execute_with(|| {
let message = Xcm(vec![
WithdrawAsset((Here, AMOUNT).into()),
BuyExecution { fees: (Here, AMOUNT).into(), weight_limit: WeightLimit::Unlimited },
WithdrawAsset((Here, AMOUNT + message_fee).into()),
BuyExecution {
fees: (Here, message_fee).into(),
weight_limit: WeightLimit::Unlimited,
},
// Set the instructions that are executed when ExpectAsset does not pass.
// In this case, reporting back an error to the Parachain.
SetErrorHandler(Xcm(vec![ReportError(QueryResponseInfo {
destination: Parachain(1).into(),
query_id: QUERY_ID,
max_weight: Weight::from_all(0),
})])),
ExpectAsset((Here, AMOUNT + 10).into()),
ExpectAsset((Here, AMOUNT + 10 * CENTS).into()),
// Add Instructions that do something with assets in holding when ExpectAsset passes.
]);
assert_ok!(ParachainPalletXcm::send_xcm(Here, Parent, message.clone(),));
});

let instruction_index_that_errored = 3;

// Check that QueryResponse message with ExpectationFalse error was received.
ParaA::execute_with(|| {
assert_eq!(
parachain::MsgQueue::received_dmp(),
vec![Xcm(vec![QueryResponse {
query_id: QUERY_ID,
response: Response::ExecutionResult(Some((3, XcmError::ExpectationFalse))),
response: Response::ExecutionResult(Some((
instruction_index_that_errored,
XcmError::ExpectationFalse
))),
max_weight: Weight::from_all(0),
querier: Some(Here.into()),
}])],
Expand All @@ -58,10 +69,15 @@ mod tests {
fn expect_origin() {
MockNet::reset();

let message_fee = relay_chain::estimate_message_fee(6);

ParaA::execute_with(|| {
let message = Xcm(vec![
WithdrawAsset((Here, AMOUNT).into()),
BuyExecution { fees: (Here, AMOUNT).into(), weight_limit: WeightLimit::Unlimited },
WithdrawAsset((Here, AMOUNT + message_fee).into()),
BuyExecution {
fees: (Here, message_fee).into(),
weight_limit: WeightLimit::Unlimited,
},
// Set the instructions that are executed when ExpectOrigin does not pass.
// In this case, reporting back an error to the Parachain.
SetErrorHandler(Xcm(vec![ReportError(QueryResponseInfo {
Expand All @@ -76,13 +92,18 @@ mod tests {
assert_ok!(ParachainPalletXcm::send_xcm(Here, Parent, message.clone(),));
});

let instruction_index_that_errored = 4;

// Check that QueryResponse message with ExpectationFalse error was received.
ParaA::execute_with(|| {
assert_eq!(
parachain::MsgQueue::received_dmp(),
vec![Xcm(vec![QueryResponse {
query_id: QUERY_ID,
response: Response::ExecutionResult(Some((4, XcmError::ExpectationFalse))),
response: Response::ExecutionResult(Some((
instruction_index_that_errored,
XcmError::ExpectationFalse
))),
max_weight: Weight::from_all(0),
querier: None,
}])],
Expand All @@ -98,10 +119,15 @@ mod tests {
fn expect_pallet() {
MockNet::reset();

let message_fee = relay_chain::estimate_message_fee(5);

ParaA::execute_with(|| {
let message = Xcm(vec![
WithdrawAsset((Here, AMOUNT).into()),
BuyExecution { fees: (Here, AMOUNT).into(), weight_limit: WeightLimit::Unlimited },
WithdrawAsset((Here, message_fee).into()),
BuyExecution {
fees: (Here, message_fee).into(),
weight_limit: WeightLimit::Unlimited,
},
// Set the instructions that are executed when ExpectPallet does not pass.
// In this case, reporting back an error to the Parachain.
SetErrorHandler(Xcm(vec![ReportError(QueryResponseInfo {
Expand Down Expand Up @@ -147,10 +173,15 @@ mod tests {
fn expect_error() {
MockNet::reset();

let message_fee = relay_chain::estimate_message_fee(6);

ParaA::execute_with(|| {
let message = Xcm(vec![
WithdrawAsset((Here, AMOUNT).into()),
BuyExecution { fees: (Here, AMOUNT).into(), weight_limit: WeightLimit::Unlimited },
WithdrawAsset((Here, message_fee).into()),
BuyExecution {
fees: (Here, message_fee).into(),
weight_limit: WeightLimit::Unlimited,
},
// ReportError is only executed if the thrown error is the `VersionIncompatible` error.
SetErrorHandler(Xcm(vec![
ExpectError(Some((1, XcmError::VersionIncompatible))),
Expand All @@ -174,7 +205,7 @@ mod tests {

// Does not receive a message as the incorrect error was thrown during execution.
ParaA::execute_with(|| {
assert_eq!(parachain::MsgQueue::received_dmp(), vec![],);
assert_eq!(parachain::MsgQueue::received_dmp(), vec![]);
});
}

Expand All @@ -189,6 +220,7 @@ mod tests {
#[test]
fn expect_transact_status() {
MockNet::reset();

// Runtime call dispatched by the Transact instruction.
// set_balance requires root origin.
let call = relay_chain::RuntimeCall::Balances(pallet_balances::Call::<
Expand All @@ -199,24 +231,30 @@ mod tests {
new_reserved: 0,
});

let message_fee = relay_chain::estimate_message_fee(6);
let set_balance_weight_estimation = Weight::from_parts(1_000_000_000, 10_000);
let set_balance_fee_estimation =
relay_chain::estimate_fee_for_weight(set_balance_weight_estimation);
let fees = message_fee + set_balance_fee_estimation;

let message = Xcm(vec![
WithdrawAsset((Here, AMOUNT).into()),
BuyExecution { fees: (Here, AMOUNT).into(), weight_limit: WeightLimit::Unlimited },
WithdrawAsset((Here, fees).into()),
BuyExecution { fees: (Here, fees).into(), weight_limit: WeightLimit::Unlimited },
SetErrorHandler(Xcm(vec![ReportTransactStatus(QueryResponseInfo {
destination: Parachain(1).into(),
query_id: QUERY_ID,
max_weight: Weight::from_all(0),
})])),
Transact {
origin_kind: OriginKind::SovereignAccount,
require_weight_at_most: Weight::from_parts(INITIAL_BALANCE as u64, 1024 * 1024),
require_weight_at_most: set_balance_weight_estimation,
call: call.encode().into(),
},
ExpectTransactStatus(MaybeErrorCode::Success),
]);

ParaA::execute_with(|| {
assert_ok!(ParachainPalletXcm::send_xcm(Here, Parent, message.clone(),));
assert_ok!(ParachainPalletXcm::send_xcm(Here, Parent, message.clone()));
});

// The execution of set_balance does not succeed, and error is reported back to the parachain.
Expand Down
11 changes: 7 additions & 4 deletions examples/src/first_look.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ mod tests {

ParaA::execute_with(|| {
// Amount to transfer.
let amount: u128 = 10;
let amount: u128 = 10 * CENTS;
// Check that the balance of Alice is equal to the `INITIAL_BALANCE`.
assert_eq!(ParachainBalances::free_balance(&ALICE), INITIAL_BALANCE);

let fee = parachain::estimate_message_fee(3);

// The XCM used to transfer funds from Alice to Bob.
let message = Xcm(vec![
WithdrawAsset((Here, amount).into()),
BuyExecution { fees: (Here, amount).into(), weight_limit: WeightLimit::Unlimited },
WithdrawAsset(vec![(Here, amount).into(), (Parent, fee).into()].into()),
BuyExecution { fees: (Parent, fee).into(), weight_limit: WeightLimit::Unlimited },
DepositAsset {
assets: All.into(),
beneficiary: MultiLocation {
Expand All @@ -36,11 +38,12 @@ mod tests {
assert_ok!(ParachainPalletXcm::execute(
parachain::RuntimeOrigin::signed(ALICE),
Box::new(xcm::VersionedXcm::from(message.clone())),
10.into()
(100_000_000_000, 100_000_000_000).into()
));

// Check if the funds are subtracted from the account of Alice and added to the account of Bob.
assert_eq!(ParachainBalances::free_balance(ALICE), INITIAL_BALANCE - amount);
assert_eq!(parachain::Assets::balance(0, ALICE), INITIAL_BALANCE - fee);
assert_eq!(ParachainBalances::free_balance(BOB), amount);
});
}
Expand Down
10 changes: 5 additions & 5 deletions examples/src/holding_modifiers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ mod tests {
#[test]
fn burn_assets() {
let message = Xcm(vec![
UnpaidExecution { weight_limit: WeightLimit::Unlimited, check_origin: None },
WithdrawAsset((Here, 10 * CENTS).into()),
BuyExecution { fees: (Here, CENTS).into(), weight_limit: WeightLimit::Unlimited },
BurnAsset((Here, 4 * CENTS).into()),
ReportHolding {
response_info: QueryResponseInfo {
Expand Down Expand Up @@ -58,8 +58,8 @@ mod tests {
parachain::set_exchange_assets(assets_in_exchange);

let message = Xcm(vec![
UnpaidExecution { weight_limit: WeightLimit::Unlimited, check_origin: None },
WithdrawAsset((Here, 10 * CENTS).into()),
BuyExecution { fees: (Here, CENTS).into(), weight_limit: WeightLimit::Unlimited },
// Maximal field set to true.
ExchangeAsset {
give: Definite((Here, 5 * CENTS).into()),
Expand All @@ -82,7 +82,7 @@ mod tests {

ParaA::execute_with(|| {
assert_eq!(parachain::exchange_assets(), vec![(Here, 5 * CENTS).into()].into());
assert_eq!(ParachainAssets::balance(1u128, &ALICE), INITIAL_BALANCE + 10 * CENTS);
assert_eq!(ParachainAssets::balance(0, &ALICE), INITIAL_BALANCE + 10 * CENTS);
assert_eq!(ParachainBalances::free_balance(ALICE), INITIAL_BALANCE + 5 * CENTS);
})
}
Expand All @@ -103,8 +103,8 @@ mod tests {
parachain::set_exchange_assets(assets_in_exchange);

let message = Xcm(vec![
UnpaidExecution { weight_limit: WeightLimit::Unlimited, check_origin: None },
WithdrawAsset((Here, 10 * CENTS).into()),
BuyExecution { fees: (Here, CENTS).into(), weight_limit: WeightLimit::Unlimited },
// Maximal field set to false.
ExchangeAsset {
give: Definite((Here, 5 * CENTS).into()),
Expand All @@ -130,7 +130,7 @@ mod tests {
parachain::exchange_assets(),
vec![(Parent, 5 * CENTS).into(), (Here, 5 * CENTS).into()].into()
);
assert_eq!(ParachainAssets::balance(1u128, &ALICE), INITIAL_BALANCE + 5 * CENTS);
assert_eq!(ParachainAssets::balance(0, &ALICE), INITIAL_BALANCE + 5 * CENTS);
assert_eq!(ParachainBalances::free_balance(ALICE), INITIAL_BALANCE + 5 * CENTS);
})
}
Expand Down
Loading