Skip to content

Commit

Permalink
feat: fix holds
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Feb 7, 2024
1 parent 5aa0433 commit 035feda
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 34 deletions.
2 changes: 1 addition & 1 deletion chain-extensions/pallet-assets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<10>;
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<0>;
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/astar-xcm-benchmarks/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl xcm_executor::Config for XcmConfig {
type UniversalAliases = Nothing;
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = ();
type Aliasers = Nothing;
}

impl pallet_xcm_benchmarks::Config for Test {
Expand Down
2 changes: 1 addition & 1 deletion pallets/inflation/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sp_runtime::{
BuildStorage, Perquintill,
};

use astar_primitives::{testing::Header, Balance, BlockNumber};
use astar_primitives::{Balance, BlockNumber};
pub(crate) type AccountId = u64;

/// Initial inflation params set by the mock.
Expand Down
2 changes: 1 addition & 1 deletion precompiles/xcm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ impl xcm_executor::Config for XcmConfig {
type UniversalAliases = Nothing;
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = ();
type Aliasers = Nothing;
}

parameter_types! {
Expand Down
83 changes: 53 additions & 30 deletions primitives/src/xcm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

use super::*;
use frame_support::assert_ok;
use sp_runtime::traits::Zero;
use xcm_executor::traits::Convert;
use sp_runtime::traits::{MaybeEquivalence, Zero};
use xcm_builder::{DescribeAllTerminal, DescribeFamily, HashedDescription};
use xcm_executor::traits::ConvertLocation;

type AssetId = u128;

Expand Down Expand Up @@ -79,45 +80,45 @@ fn execution_fee(weight: Weight, units_per_second: u128) -> u128 {
fn asset_location_to_id() {
// Test cases where the MultiLocation is valid
assert_eq!(
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert_ref(PARENT),
Ok(u128::MAX)
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert(&PARENT),
Some(u128::MAX)
);
assert_eq!(
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert_ref(PARACHAIN),
Ok(20)
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert(&PARACHAIN),
Some(20)
);
assert_eq!(
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert_ref(GENERAL_INDEX),
Ok(30)
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert(&GENERAL_INDEX),
Some(30)
);

// Test case where MultiLocation isn't supported
assert_eq!(
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert_ref(MultiLocation::here()),
Err(())
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert(&MultiLocation::here()),
None
);
}

#[test]
fn asset_id_to_location() {
// Test cases where the AssetId is valid
assert_eq!(
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::reverse_ref(u128::MAX),
Ok(PARENT)
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert_back(&u128::MAX),
Some(PARENT)
);
assert_eq!(
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::reverse_ref(20),
Ok(PARACHAIN)
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert_back(&20),
Some(PARACHAIN)
);
assert_eq!(
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::reverse_ref(30),
Ok(GENERAL_INDEX)
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert_back(&30),
Some(GENERAL_INDEX)
);

// Test case where the AssetId isn't supported
assert_eq!(
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::reverse_ref(0),
Err(())
AssetLocationIdConverter::<AssetId, AssetLocationMapper>::convert_back(&0),
None
);
}

Expand All @@ -132,6 +133,12 @@ fn fixed_rate_of_foreign_asset_buy_is_ok() {
fun: Fungibility::Fungible(total_payment),
};
let weight: Weight = Weight::from_parts(1_000_000_000, 0);
let ctx = XcmContext {
// arbitary ML
origin: Some(MultiLocation::here()),
message_id: XcmHash::default(),
topic: None,
};

// Calculate the expected execution fee for the execution weight
let expected_execution_fee = execution_fee(
Expand All @@ -141,7 +148,7 @@ fn fixed_rate_of_foreign_asset_buy_is_ok() {
assert!(expected_execution_fee > 0); // sanity check

// 1. Buy weight and expect it to be successful
let result = fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into());
let result = fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into(), &ctx);
if let Ok(assets) = result {
// We expect only one unused payment asset and specific amount
assert_eq!(assets.len(), 1);
Expand Down Expand Up @@ -172,7 +179,7 @@ fn fixed_rate_of_foreign_asset_buy_is_ok() {
);
assert!(expected_execution_fee > 0); // sanity check

let result = fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into());
let result = fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into(), &ctx);
if let Ok(assets) = result {
// We expect only one unused payment asset and specific amount
assert_eq!(assets.len(), 1);
Expand Down Expand Up @@ -213,7 +220,7 @@ fn fixed_rate_of_foreign_asset_buy_is_ok() {
);
assert!(expected_execution_fee > 0); // sanity check

let result = fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into());
let result = fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into(), &ctx);
if let Ok(assets) = result {
// We expect only one unused payment asset and specific amount
assert_eq!(assets.len(), 1);
Expand Down Expand Up @@ -248,6 +255,12 @@ fn fixed_rate_of_foreign_asset_buy_execution_fails() {
fun: Fungibility::Fungible(total_payment),
};
let weight: Weight = Weight::from_parts(3_000_000_000, 0);
let ctx = XcmContext {
// arbitary ML
origin: Some(MultiLocation::here()),
message_id: XcmHash::default(),
topic: None,
};

// Calculate the expected execution fee for the execution weight
let expected_execution_fee = execution_fee(
Expand All @@ -259,7 +272,7 @@ fn fixed_rate_of_foreign_asset_buy_execution_fails() {

// Expect failure because we lack the required funds
assert_eq!(
fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into()),
fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into(), &ctx),
Err(XcmError::TooExpensive)
);

Expand All @@ -269,7 +282,7 @@ fn fixed_rate_of_foreign_asset_buy_execution_fails() {
fun: Fungibility::Fungible(total_payment),
};
assert_eq!(
fixed_rate_trader.buy_weight(Weight::zero(), payment_multi_asset.clone().into()),
fixed_rate_trader.buy_weight(Weight::zero(), payment_multi_asset.clone().into(), &ctx),
Err(XcmError::TooExpensive)
);
}
Expand All @@ -285,21 +298,27 @@ fn fixed_rate_of_foreign_asset_refund_is_ok() {
fun: Fungibility::Fungible(total_payment),
};
let weight: Weight = Weight::from_parts(1_000_000_000, 0);
let ctx = XcmContext {
// arbitary ML
origin: Some(MultiLocation::here()),
message_id: XcmHash::default(),
topic: None,
};

// Calculate the expected execution fee for the execution weight and buy it
let expected_execution_fee = execution_fee(
weight,
ExecutionPayment::get_units_per_second(PARENT).unwrap(),
);
assert!(expected_execution_fee > 0); // sanity check
assert_ok!(fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into()));
assert_ok!(fixed_rate_trader.buy_weight(weight, payment_multi_asset.clone().into(), &ctx));

// Refund quarter and expect it to pass
let weight_to_refund = weight / 4;
let assets_to_refund = expected_execution_fee / 4;
let (old_weight, old_consumed) = (fixed_rate_trader.weight, fixed_rate_trader.consumed);

let result = fixed_rate_trader.refund_weight(weight_to_refund);
let result = fixed_rate_trader.refund_weight(weight_to_refund, &ctx);
if let Some(asset_location) = result {
assert_eq!(asset_location, (PARENT, assets_to_refund).into());

Expand All @@ -310,7 +329,7 @@ fn fixed_rate_of_foreign_asset_refund_is_ok() {
// Refund more than remains and expect it to pass (saturated)
let assets_to_refund = fixed_rate_trader.consumed;

let result = fixed_rate_trader.refund_weight(weight + Weight::from_parts(10000, 0));
let result = fixed_rate_trader.refund_weight(weight + Weight::from_parts(10000, 0), &ctx);
if let Some(asset_location) = result {
assert_eq!(asset_location, (PARENT, assets_to_refund).into());

Expand Down Expand Up @@ -423,10 +442,12 @@ fn hashed_description_sanity_check() {
};
// Ensure derived value is same as it would be using `polkadot-v0.9.44` code.
let derived_account =
HashedDescription::<[u8; 32], DescribeFamily<DescribeAllTerminal>>::convert(acc_key_20_mul);
HashedDescription::<[u8; 32], DescribeFamily<DescribeAllTerminal>>::convert_location(
&acc_key_20_mul,
);
assert_eq!(
derived_account,
Ok([
Some([
61_u8, 117, 247, 231, 100, 219, 128, 176, 180, 200, 187, 102, 93, 107, 187, 145, 25,
146, 50, 248, 244, 153, 83, 95, 207, 165, 90, 10, 220, 39, 23, 49
])
Expand All @@ -444,10 +465,12 @@ fn hashed_description_sanity_check() {
};
// Ensure derived value is same as it would be using `polkadot-v0.9.44` code.
let derived_account =
HashedDescription::<[u8; 32], DescribeFamily<DescribeAllTerminal>>::convert(acc_id_32_mul);
HashedDescription::<[u8; 32], DescribeFamily<DescribeAllTerminal>>::convert_location(
&acc_id_32_mul,
);
assert_eq!(
derived_account,
Ok([
Some([
123, 171, 79, 159, 78, 47, 62, 233, 108, 149, 131, 249, 23, 192, 178, 52, 235, 133,
147, 145, 152, 89, 129, 92, 63, 79, 211, 235, 213, 152, 201, 205
])
Expand Down

0 comments on commit 035feda

Please sign in to comment.