Skip to content

Commit

Permalink
fix pallet-asset-conversion-tx-payment
Browse files Browse the repository at this point in the history
  • Loading branch information
dianpopa committed Apr 27, 2024
1 parent c2dc223 commit 7f3c742
Showing 1 changed file with 52 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ fn default_post_info() -> PostDispatchInfo {
fn setup_lp(asset_id: u32, balance_factor: u64) {
let lp_provider = 5;
let ed = Balances::minimum_balance();
let ed_asset = Assets::minimum_balance(asset_id);
let ed_asset = Assets::minimum_balance(&asset_id);
assert_ok!(Balances::force_set_balance(
RuntimeOrigin::root(),
lp_provider,
10_000 * balance_factor + ed,
));
let lp_provider_account = <Runtime as system::Config>::Lookup::unlookup(lp_provider);
assert_ok!(Assets::mint_into(
asset_id.into(),
&asset_id,
&lp_provider_account,
10_000 * balance_factor + ed_asset
));
Expand Down Expand Up @@ -217,8 +217,8 @@ fn transaction_payment_in_asset_possible() {
let beneficiary = <Runtime as system::Config>::Lookup::unlookup(caller);
let balance = 1000;

assert_ok!(Assets::mint_into(asset_id.into(), &beneficiary, balance));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_ok!(Assets::mint_into(&asset_id, &beneficiary, balance));
assert_eq!(Assets::balance(&asset_id, caller), balance);

let len = 10;
let tx_weight = 5;
Expand All @@ -227,15 +227,15 @@ fn transaction_payment_in_asset_possible() {

let fee_in_native = base_weight + tx_weight + len as u64;
let input_quote = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
&NativeOrWithId::WithId(asset_id),
&NativeOrWithId::Native,
fee_in_native,
true,
);
assert_eq!(input_quote, Some(201));

let fee_in_asset = input_quote.unwrap();
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_eq!(Assets::balance(&asset_id, caller), balance);

let pre = ChargeAssetTxPayment::<Runtime>::from(0, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(WEIGHT_5), len)
Expand All @@ -244,7 +244,7 @@ fn transaction_payment_in_asset_possible() {
assert_eq!(Balances::free_balance(caller), 10 * balance_factor);

// check that fee was charged in the given asset
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);
assert_eq!(Assets::balance(&asset_id, caller), balance - fee_in_asset);

assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
Expand All @@ -254,7 +254,7 @@ fn transaction_payment_in_asset_possible() {
&Ok(())
));

assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);
assert_eq!(Assets::balance(&asset_id, caller), balance - fee_in_asset);
assert_eq!(TipUnbalancedAmount::get(), 0);
assert_eq!(FeeUnbalancedAmount::get(), fee_in_native);
});
Expand Down Expand Up @@ -285,8 +285,8 @@ fn transaction_payment_in_asset_fails_if_no_pool_for_that_asset() {
let beneficiary = <Runtime as system::Config>::Lookup::unlookup(caller);
let balance = 1000;

assert_ok!(Assets::mint_into(asset_id.into(), &beneficiary, balance));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_ok!(Assets::mint_into(&asset_id, &beneficiary, balance));
assert_eq!(Assets::balance(&asset_id, caller), balance);

let len = 10;
let pre = ChargeAssetTxPayment::<Runtime>::from(0, Some(asset_id)).pre_dispatch(
Expand Down Expand Up @@ -329,15 +329,15 @@ fn transaction_payment_without_fee() {

// mint into the caller account
let beneficiary = <Runtime as system::Config>::Lookup::unlookup(caller);
assert_ok!(Assets::mint_into(asset_id.into(), &beneficiary, balance));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_ok!(Assets::mint_into(&asset_id, &beneficiary, balance));
assert_eq!(Assets::balance(&asset_id, caller), balance);

let weight = 5;
let len = 10;
let fee_in_native = base_weight + weight + len as u64;
let input_quote = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
&NativeOrWithId::WithId(asset_id),
&NativeOrWithId::Native,
fee_in_native,
true,
);
Expand All @@ -351,11 +351,11 @@ fn transaction_payment_without_fee() {
// assert that native balance is not used
assert_eq!(Balances::free_balance(caller), 10 * balance_factor);
// check that fee was charged in the given asset
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);
assert_eq!(Assets::balance(&asset_id, caller), balance - fee_in_asset);

let refund = AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrWithId::Native,
NativeOrWithId::WithId(asset_id),
&NativeOrWithId::Native,
&NativeOrWithId::WithId(asset_id),
fee_in_native,
true,
)
Expand All @@ -371,7 +371,7 @@ fn transaction_payment_without_fee() {
));

// caller should get refunded
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset + refund);
assert_eq!(Assets::balance(&asset_id, caller), balance - fee_in_asset + refund);
assert_eq!(Balances::free_balance(caller), 10 * balance_factor);
});
}
Expand Down Expand Up @@ -403,16 +403,16 @@ fn asset_transaction_payment_with_tip_and_refund() {
let beneficiary = <Runtime as system::Config>::Lookup::unlookup(caller);
let balance = 10000;

assert_ok!(Assets::mint_into(asset_id.into(), &beneficiary, balance));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_ok!(Assets::mint_into(&asset_id, &beneficiary, balance));
assert_eq!(Assets::balance(&asset_id, caller), balance);

let weight = 100;
let tip = 5;
let len = 10;
let fee_in_native = base_weight + weight + len as u64 + tip;
let input_quote = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
&NativeOrWithId::WithId(asset_id),
&NativeOrWithId::Native,
fee_in_native,
true,
);
Expand All @@ -422,13 +422,13 @@ fn asset_transaction_payment_with_tip_and_refund() {
let pre = ChargeAssetTxPayment::<Runtime>::from(tip, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(WEIGHT_100), len)
.unwrap();
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);
assert_eq!(Assets::balance(&asset_id, caller), balance - fee_in_asset);

let final_weight = 50;
let expected_fee = fee_in_native - final_weight - tip;
let expected_token_refund = AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrWithId::Native,
NativeOrWithId::WithId(asset_id),
&NativeOrWithId::Native,
&NativeOrWithId::WithId(asset_id),
fee_in_native - expected_fee - tip,
true,
)
Expand All @@ -447,7 +447,7 @@ fn asset_transaction_payment_with_tip_and_refund() {

// caller should get refunded
assert_eq!(
Assets::balance(asset_id, caller),
Assets::balance(&asset_id, caller),
balance - fee_in_asset + expected_token_refund
);
assert_eq!(Balances::free_balance(caller), 20 * balance_factor);
Expand Down Expand Up @@ -481,8 +481,8 @@ fn payment_from_account_with_only_assets() {
let beneficiary = <Runtime as system::Config>::Lookup::unlookup(caller);
let balance = 1000;

assert_ok!(Assets::mint_into(asset_id.into(), &beneficiary, balance));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_ok!(Assets::mint_into(&asset_id, &beneficiary, balance));
assert_eq!(Assets::balance(&asset_id, caller), balance);

// assert that native balance is not necessary
assert_eq!(Balances::free_balance(caller), 0);
Expand All @@ -492,8 +492,8 @@ fn payment_from_account_with_only_assets() {
let fee_in_native = base_weight + weight + len as u64;
let ed = Balances::minimum_balance();
let fee_in_asset = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
&NativeOrWithId::WithId(asset_id),
&NativeOrWithId::Native,
fee_in_native + ed,
true,
)
Expand All @@ -505,11 +505,11 @@ fn payment_from_account_with_only_assets() {
.unwrap();
assert_eq!(Balances::free_balance(caller), ed);
// check that fee was charged in the given asset
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);
assert_eq!(Assets::balance(&asset_id, caller), balance - fee_in_asset);

let refund = AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrWithId::Native,
NativeOrWithId::WithId(asset_id),
&NativeOrWithId::Native,
&NativeOrWithId::WithId(asset_id),
ed,
true,
)
Expand All @@ -522,7 +522,7 @@ fn payment_from_account_with_only_assets() {
len,
&Ok(())
));
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset + refund);
assert_eq!(Assets::balance(&asset_id, caller), balance - fee_in_asset + refund);
assert_eq!(Balances::free_balance(caller), 0);

assert_eq!(TipUnbalancedAmount::get(), 0);
Expand Down Expand Up @@ -557,8 +557,8 @@ fn converted_fee_is_never_zero_if_input_fee_is_not() {
let beneficiary = <Runtime as system::Config>::Lookup::unlookup(caller);
let balance = 1000;

assert_ok!(Assets::mint_into(asset_id.into(), &beneficiary, balance));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_ok!(Assets::mint_into(&asset_id, &beneficiary, balance));
assert_eq!(Assets::balance(&asset_id, caller), balance);

let weight = 1;
let len = 1;
Expand All @@ -569,7 +569,7 @@ fn converted_fee_is_never_zero_if_input_fee_is_not() {
.pre_dispatch(&caller, CALL, &info_from_pays(Pays::No), len)
.unwrap();
// `Pays::No` implies there are no fees
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_eq!(Assets::balance(&asset_id, caller), balance);

assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
Expand All @@ -578,14 +578,15 @@ fn converted_fee_is_never_zero_if_input_fee_is_not() {
len,
&Ok(())
));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_eq!(Assets::balance(&asset_id, caller), balance);
}

// validate even a small fee gets converted to asset.
let fee_in_native = base_weight + weight + len as u64;

let fee_in_asset = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
&NativeOrWithId::WithId(asset_id),
&NativeOrWithId::Native,
fee_in_native,
true,
)
Expand All @@ -594,7 +595,7 @@ fn converted_fee_is_never_zero_if_input_fee_is_not() {
let pre = ChargeAssetTxPayment::<Runtime>::from(0, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(Weight::from_parts(weight, 0)), len)
.unwrap();
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);
assert_eq!(Assets::balance(&asset_id, caller), balance - fee_in_asset);

assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
Expand All @@ -603,7 +604,7 @@ fn converted_fee_is_never_zero_if_input_fee_is_not() {
len,
&Ok(())
));
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);
assert_eq!(Assets::balance(&asset_id, caller), balance - fee_in_asset);
});
}

Expand Down Expand Up @@ -631,8 +632,8 @@ fn post_dispatch_fee_is_zero_if_pre_dispatch_fee_is_zero() {
let beneficiary = <Runtime as system::Config>::Lookup::unlookup(caller);
let balance = 1000;

assert_ok!(Assets::mint_into(asset_id.into(), &beneficiary, balance));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_ok!(Assets::mint_into(&asset_id, &beneficiary, balance));
assert_eq!(Assets::balance(&asset_id, caller), balance);

let weight = 1;
let len = 1;
Expand All @@ -646,7 +647,7 @@ fn post_dispatch_fee_is_zero_if_pre_dispatch_fee_is_zero() {
.unwrap();
// `Pays::No` implies no pre-dispatch fees

assert_eq!(Assets::balance(asset_id, caller), balance);
assert_eq!(Assets::balance(&asset_id, caller), balance);

let (_tip, _who, initial_payment, _asset_id) = &pre;
let not_paying = match initial_payment {
Expand All @@ -664,7 +665,7 @@ fn post_dispatch_fee_is_zero_if_pre_dispatch_fee_is_zero() {
len,
&Ok(())
));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_eq!(Assets::balance(&asset_id, caller), balance);
});
}

Expand Down Expand Up @@ -692,8 +693,8 @@ fn post_dispatch_fee_is_zero_if_unsigned_pre_dispatch_fee_is_zero() {
let beneficiary = <Runtime as system::Config>::Lookup::unlookup(caller);
let balance = 1000;

assert_ok!(Assets::mint_into(asset_id.into(), &beneficiary, balance));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_ok!(Assets::mint_into(&asset_id, &beneficiary, balance));
assert_eq!(Assets::balance(&asset_id, caller), balance);

let weight = 1;
let len = 1;
Expand All @@ -704,7 +705,7 @@ fn post_dispatch_fee_is_zero_if_unsigned_pre_dispatch_fee_is_zero() {
)
.unwrap();

assert_eq!(Assets::balance(asset_id, caller), balance);
assert_eq!(Assets::balance(&asset_id, caller), balance);

// `Pays::Yes` on post-dispatch does not mean we pay (we never charge more than the
// initial fee)
Expand All @@ -715,6 +716,6 @@ fn post_dispatch_fee_is_zero_if_unsigned_pre_dispatch_fee_is_zero() {
len,
&Ok(())
));
assert_eq!(Assets::balance(asset_id, caller), balance);
assert_eq!(Assets::balance(&asset_id, caller), balance);
});
}

0 comments on commit 7f3c742

Please sign in to comment.