From 9b9c50ad8a36c12f9587a2c940b66130ef31a3a3 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Thu, 14 Mar 2024 10:31:49 -0500 Subject: [PATCH 01/12] update pallet --- Cargo.lock | 1 + pallets/transaction-multi-payment/Cargo.toml | 1 + pallets/transaction-multi-payment/src/lib.rs | 27 ++++++++++++------- pallets/transaction-multi-payment/src/mock.rs | 8 ++++++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 68814b784..4376137ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8601,6 +8601,7 @@ dependencies = [ "pallet-currencies", "pallet-evm", "pallet-transaction-payment", + "pallet-utility", "parity-scale-codec", "primitives", "scale-info", diff --git a/pallets/transaction-multi-payment/Cargo.toml b/pallets/transaction-multi-payment/Cargo.toml index c4ed31d2c..08a23d185 100644 --- a/pallets/transaction-multi-payment/Cargo.toml +++ b/pallets/transaction-multi-payment/Cargo.toml @@ -30,6 +30,7 @@ sp-core = { workspace = true } sp-std = { workspace = true } sp-runtime = { workspace = true } pallet-transaction-payment = { workspace = true } +pallet-utility = { workspace = true } pallet-evm = { workspace = true, optional = true } diff --git a/pallets/transaction-multi-payment/src/lib.rs b/pallets/transaction-multi-payment/src/lib.rs index 1a11a346f..7c9421c20 100644 --- a/pallets/transaction-multi-payment/src/lib.rs +++ b/pallets/transaction-multi-payment/src/lib.rs @@ -361,13 +361,14 @@ pub struct TransferFees(PhantomData<(MC, DF, FR)>); impl OnChargeTransaction for TransferFees where - T: Config, + T: Config + pallet_utility::Config, MC: MultiCurrency<::AccountId>, AssetIdOf: Into, MC::Balance: FixedPointOperand, FR: Get, DF: DepositFee, - ::RuntimeCall: IsSubType>, + ::RuntimeCall: IsSubType> + IsSubType>, + ::RuntimeCall: IsSubType>, BalanceOf: FixedPointOperand, { type LiquidityInfo = Option, Price>>; @@ -378,8 +379,8 @@ where /// Note: The `fee` already includes the `tip`. fn withdraw_fee( who: &T::AccountId, - call: &T::RuntimeCall, - _info: &DispatchInfoOf, + call: &::RuntimeCall, + _info: &DispatchInfoOf<::RuntimeCall>, fee: Self::Balance, _tip: Self::Balance, ) -> Result { @@ -387,9 +388,17 @@ where return Ok(None); } - let currency = match call.is_sub_type() { - Some(Call::set_currency { currency }) => *currency, - _ => Pallet::::account_currency(who), + let currency = if let Some(Call::set_currency { currency }) = call.is_sub_type() { + *currency + } else if let Some(pallet_utility::pallet::Call::::batch { calls }) = call.is_sub_type() { + let first_call = &calls[0]; + let currency = match first_call.is_sub_type() { + Some(Call::set_currency { currency }) => *currency, + _ => Pallet::::account_currency(who), + }; + currency + } else { + Pallet::::account_currency(who) }; let price = Pallet::::get_currency_price(currency) @@ -416,8 +425,8 @@ where /// Note: The `fee` already includes the `tip`. fn correct_and_deposit_fee( who: &T::AccountId, - _dispatch_info: &DispatchInfoOf, - _post_info: &PostDispatchInfoOf, + _dispatch_info: &DispatchInfoOf<::RuntimeCall>, + _post_info: &PostDispatchInfoOf<::RuntimeCall>, corrected_fee: Self::Balance, tip: Self::Balance, already_withdrawn: Self::LiquidityInfo, diff --git a/pallets/transaction-multi-payment/src/mock.rs b/pallets/transaction-multi-payment/src/mock.rs index 2654888b5..73a78c078 100644 --- a/pallets/transaction-multi-payment/src/mock.rs +++ b/pallets/transaction-multi-payment/src/mock.rs @@ -85,6 +85,7 @@ frame_support::construct_runtime!( Balances: pallet_balances, Currencies: pallet_currencies, Tokens: orml_tokens, + Utility: pallet_utility, } ); @@ -261,6 +262,13 @@ impl pallet_currencies::Config for Test { type WeightInfo = (); } +impl pallet_utility::Config for Test { + type RuntimeEvent = RuntimeEvent; + type RuntimeCall = RuntimeCall; + type PalletsOrigin = OriginCaller; + type WeightInfo = (); +} + pub struct ExtBuilder { base_weight: Weight, native_balances: Vec<(AccountId, Balance)>, From e074e43701bc0cf81ee05e35c28f938503290a10 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Thu, 14 Mar 2024 10:48:24 -0500 Subject: [PATCH 02/12] write integration tests --- integration-tests/src/non_native_fee.rs | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/integration-tests/src/non_native_fee.rs b/integration-tests/src/non_native_fee.rs index 8eb455bbe..a48dd486e 100644 --- a/integration-tests/src/non_native_fee.rs +++ b/integration-tests/src/non_native_fee.rs @@ -79,6 +79,76 @@ fn non_native_fee_payment_works_with_oracle_price_based_on_onchain_route() { }); } +#[test] +fn set_currency_should_work_in_batch_transaction_when_first_tx() { + TestNet::reset(); + + Hydra::execute_with(|| { + let first_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( + pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, + ); + let second_inner_call = hydradx_runtime::RuntimeCall::System( + frame_system::Call::remark { remark: vec![] }, + ); + let call = hydradx_runtime::RuntimeCall::Utility( + pallet_utility::Call::batch { calls: vec![first_inner_call, second_inner_call] }, + ); + + let info = DispatchInfo { + weight: Weight::from_parts(106_957_000, 0), + ..Default::default() + }; + let len: usize = 10; + + assert_ok!( + pallet_transaction_payment::ChargeTransactionPayment::::from(0).pre_dispatch( + &AccountId::from(BOB), + &call, + &info, + len, + ) + ); + let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); + assert_eq!(bob_balance, 999991); + }); +} + +#[test] +fn set_currency_should_not_work_in_batch_transaction_when_not_first_tx() { + TestNet::reset(); + + Hydra::execute_with(|| { + let first_inner_call = hydradx_runtime::RuntimeCall::System( + frame_system::Call::remark { remark: vec![] }, + ); + let second_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( + pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, + ); + let call = hydradx_runtime::RuntimeCall::Utility( + pallet_utility::Call::batch { calls: vec![first_inner_call, second_inner_call] }, + ); + + let info = DispatchInfo { + weight: Weight::from_parts(106_957_000, 0), + ..Default::default() + }; + let len: usize = 10; + + let bob_initial_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); + + assert_ok!( + pallet_transaction_payment::ChargeTransactionPayment::::from(0).pre_dispatch( + &AccountId::from(BOB), + &call, + &info, + len, + ) + ); + let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); + assert_eq!(bob_balance, bob_initial_balance); + }); +} + const HITCHHIKER: [u8; 32] = [42u8; 32]; #[test] From de6ccd32c5e20507644c759a7151c3f3e21ac2d9 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Thu, 14 Mar 2024 11:04:15 -0500 Subject: [PATCH 03/12] handle set_currency from batch_all --- integration-tests/src/non_native_fee.rs | 80 +++++++++++++++++--- pallets/transaction-multi-payment/src/lib.rs | 7 ++ 2 files changed, 78 insertions(+), 9 deletions(-) diff --git a/integration-tests/src/non_native_fee.rs b/integration-tests/src/non_native_fee.rs index a48dd486e..54b209210 100644 --- a/integration-tests/src/non_native_fee.rs +++ b/integration-tests/src/non_native_fee.rs @@ -87,12 +87,42 @@ fn set_currency_should_work_in_batch_transaction_when_first_tx() { let first_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, ); - let second_inner_call = hydradx_runtime::RuntimeCall::System( - frame_system::Call::remark { remark: vec![] }, + let second_inner_call = hydradx_runtime::RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let call = hydradx_runtime::RuntimeCall::Utility(pallet_utility::Call::batch { + calls: vec![first_inner_call, second_inner_call], + }); + + let info = DispatchInfo { + weight: Weight::from_parts(106_957_000, 0), + ..Default::default() + }; + let len: usize = 10; + + assert_ok!( + pallet_transaction_payment::ChargeTransactionPayment::::from(0).pre_dispatch( + &AccountId::from(BOB), + &call, + &info, + len, + ) ); - let call = hydradx_runtime::RuntimeCall::Utility( - pallet_utility::Call::batch { calls: vec![first_inner_call, second_inner_call] }, + let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); + assert_eq!(bob_balance, 999991); + }); +} + +#[test] +fn set_currency_should_work_in_batch_all_transaction_when_first_tx() { + TestNet::reset(); + + Hydra::execute_with(|| { + let first_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( + pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, ); + let second_inner_call = hydradx_runtime::RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let call = hydradx_runtime::RuntimeCall::Utility(pallet_utility::Call::batch_all { + calls: vec![first_inner_call, second_inner_call], + }); let info = DispatchInfo { weight: Weight::from_parts(106_957_000, 0), @@ -118,15 +148,47 @@ fn set_currency_should_not_work_in_batch_transaction_when_not_first_tx() { TestNet::reset(); Hydra::execute_with(|| { - let first_inner_call = hydradx_runtime::RuntimeCall::System( - frame_system::Call::remark { remark: vec![] }, - ); + let first_inner_call = hydradx_runtime::RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); let second_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, ); - let call = hydradx_runtime::RuntimeCall::Utility( - pallet_utility::Call::batch { calls: vec![first_inner_call, second_inner_call] }, + let call = hydradx_runtime::RuntimeCall::Utility(pallet_utility::Call::batch { + calls: vec![first_inner_call, second_inner_call], + }); + + let info = DispatchInfo { + weight: Weight::from_parts(106_957_000, 0), + ..Default::default() + }; + let len: usize = 10; + + let bob_initial_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); + + assert_ok!( + pallet_transaction_payment::ChargeTransactionPayment::::from(0).pre_dispatch( + &AccountId::from(BOB), + &call, + &info, + len, + ) + ); + let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); + assert_eq!(bob_balance, bob_initial_balance); + }); +} + +#[test] +fn set_currency_should_not_work_in_batch_all_transaction_when_not_first_tx() { + TestNet::reset(); + + Hydra::execute_with(|| { + let first_inner_call = hydradx_runtime::RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let second_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( + pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, ); + let call = hydradx_runtime::RuntimeCall::Utility(pallet_utility::Call::batch_all { + calls: vec![first_inner_call, second_inner_call], + }); let info = DispatchInfo { weight: Weight::from_parts(106_957_000, 0), diff --git a/pallets/transaction-multi-payment/src/lib.rs b/pallets/transaction-multi-payment/src/lib.rs index 7c9421c20..d0447bc18 100644 --- a/pallets/transaction-multi-payment/src/lib.rs +++ b/pallets/transaction-multi-payment/src/lib.rs @@ -397,6 +397,13 @@ where _ => Pallet::::account_currency(who), }; currency + } else if let Some(pallet_utility::pallet::Call::::batch_all { calls }) = call.is_sub_type() { + let first_call = &calls[0]; + let currency = match first_call.is_sub_type() { + Some(Call::set_currency { currency }) => *currency, + _ => Pallet::::account_currency(who), + }; + currency } else { Pallet::::account_currency(who) }; From a9d85655d76d1c80d26c9e259b30fa16ab4a0aa4 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Thu, 14 Mar 2024 11:25:49 -0500 Subject: [PATCH 04/12] refactor integration tests --- integration-tests/src/non_native_fee.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/integration-tests/src/non_native_fee.rs b/integration-tests/src/non_native_fee.rs index 54b209210..41f67c55c 100644 --- a/integration-tests/src/non_native_fee.rs +++ b/integration-tests/src/non_native_fee.rs @@ -83,6 +83,7 @@ fn non_native_fee_payment_works_with_oracle_price_based_on_onchain_route() { fn set_currency_should_work_in_batch_transaction_when_first_tx() { TestNet::reset(); + // batch Hydra::execute_with(|| { let first_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, @@ -109,12 +110,10 @@ fn set_currency_should_work_in_batch_transaction_when_first_tx() { let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); assert_eq!(bob_balance, 999991); }); -} -#[test] -fn set_currency_should_work_in_batch_all_transaction_when_first_tx() { TestNet::reset(); + // batch_all Hydra::execute_with(|| { let first_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, @@ -147,6 +146,7 @@ fn set_currency_should_work_in_batch_all_transaction_when_first_tx() { fn set_currency_should_not_work_in_batch_transaction_when_not_first_tx() { TestNet::reset(); + // batch Hydra::execute_with(|| { let first_inner_call = hydradx_runtime::RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); let second_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( @@ -175,12 +175,10 @@ fn set_currency_should_not_work_in_batch_transaction_when_not_first_tx() { let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); assert_eq!(bob_balance, bob_initial_balance); }); -} -#[test] -fn set_currency_should_not_work_in_batch_all_transaction_when_not_first_tx() { TestNet::reset(); + // batch_all Hydra::execute_with(|| { let first_inner_call = hydradx_runtime::RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); let second_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( From 5d059a23aea15ca887e73958e014194f026f2f45 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Thu, 14 Mar 2024 15:15:39 -0500 Subject: [PATCH 05/12] write unit tests --- .../transaction-multi-payment/src/tests.rs | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/pallets/transaction-multi-payment/src/tests.rs b/pallets/transaction-multi-payment/src/tests.rs index fbec2762f..1e0cdc855 100644 --- a/pallets/transaction-multi-payment/src/tests.rs +++ b/pallets/transaction-multi-payment/src/tests.rs @@ -127,6 +127,142 @@ fn set_supported_currency_without_spot_price_should_charge_fee_in_correct_curren }); } +#[test] +fn set_supported_currency_in_batch_should_charge_fee_in_correct_currency() { + // batch + ExtBuilder::default().base_weight(5).build().execute_with(|| { + let first_inner_call = RuntimeCall::PaymentPallet(crate::Call::set_currency { + currency: SUPPORTED_CURRENCY, + }); + let second_inner_call = RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let call = RuntimeCall::Utility(pallet_utility::Call::batch { + calls: vec![first_inner_call, second_inner_call], + }); + + let len = 10; + let info = info_from_weight(Weight::from_parts(5, 0)); + + let pre = ChargeTransactionPayment::::from(0).pre_dispatch(&ALICE, &call, &info, len); + assert!(pre.is_ok()); + + assert_eq!( + Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), + 999_999_999_999_970 + ); + + assert_ok!(ChargeTransactionPayment::::post_dispatch( + Some(pre.unwrap()), + &info, + &default_post_info(), + len, + &Ok(()) + )); + assert_eq!(Currencies::free_balance(SUPPORTED_CURRENCY, &FEE_RECEIVER), 30); + }); + + // batch_all + ExtBuilder::default().base_weight(5).build().execute_with(|| { + let first_inner_call = RuntimeCall::PaymentPallet(crate::Call::set_currency { + currency: SUPPORTED_CURRENCY, + }); + let second_inner_call = RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let call = RuntimeCall::Utility(pallet_utility::Call::batch_all { + calls: vec![first_inner_call, second_inner_call], + }); + + let len = 10; + let info = info_from_weight(Weight::from_parts(5, 0)); + + let pre = ChargeTransactionPayment::::from(0).pre_dispatch(&ALICE, &call, &info, len); + assert!(pre.is_ok()); + + assert_eq!( + Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), + 999_999_999_999_970 + ); + + assert_ok!(ChargeTransactionPayment::::post_dispatch( + Some(pre.unwrap()), + &info, + &default_post_info(), + len, + &Ok(()) + )); + assert_eq!(Currencies::free_balance(SUPPORTED_CURRENCY, &FEE_RECEIVER), 30); + }); +} + +#[test] +fn set_supported_currency_in_batch_should_not_work_if_not_first_transaction() { + // batch + ExtBuilder::default().base_weight(5).build().execute_with(|| { + let first_inner_call = RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let second_inner_call = RuntimeCall::PaymentPallet(crate::Call::set_currency { + currency: SUPPORTED_CURRENCY, + }); + let call = RuntimeCall::Utility(pallet_utility::Call::batch { + calls: vec![first_inner_call, second_inner_call], + }); + + let len = 10; + let info = info_from_weight(Weight::from_parts(5, 0)); + + let alice_initial_non_native_balance = Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE); + + let pre = ChargeTransactionPayment::::from(0).pre_dispatch(&ALICE, &call, &info, len); + assert!(pre.is_ok()); + + assert_eq!( + Currencies::free_balance(HDX, &ALICE), + 999_999_999_999_980 + ); + + assert_ok!(ChargeTransactionPayment::::post_dispatch( + Some(pre.unwrap()), + &info, + &default_post_info(), + len, + &Ok(()) + )); + assert_eq!(Currencies::free_balance(HDX, &FEE_RECEIVER), 20); + assert_eq!(Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), alice_initial_non_native_balance); + }); + + // batch_all + ExtBuilder::default().base_weight(5).build().execute_with(|| { + let first_inner_call = RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let second_inner_call = RuntimeCall::PaymentPallet(crate::Call::set_currency { + currency: SUPPORTED_CURRENCY, + }); + let call = RuntimeCall::Utility(pallet_utility::Call::batch_all { + calls: vec![first_inner_call, second_inner_call], + }); + + let len = 10; + let info = info_from_weight(Weight::from_parts(5, 0)); + + let alice_initial_non_native_balance = Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE); + + let pre = ChargeTransactionPayment::::from(0).pre_dispatch(&ALICE, &call, &info, len); + assert!(pre.is_ok()); + + assert_eq!( + Currencies::free_balance(HDX, &ALICE), + 999_999_999_999_980 + ); + + assert_ok!(ChargeTransactionPayment::::post_dispatch( + Some(pre.unwrap()), + &info, + &default_post_info(), + len, + &Ok(()) + )); + assert_eq!(Currencies::free_balance(HDX, &FEE_RECEIVER), 20); + assert_eq!(Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), alice_initial_non_native_balance); + }); +} + #[test] fn set_supported_currency_with_spot_price_should_charge_fee_in_correct_currency() { ExtBuilder::default().base_weight(5).build().execute_with(|| { From 057c892290526d59510d54074708bc94a82c8790 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Thu, 14 Mar 2024 15:19:31 -0500 Subject: [PATCH 06/12] formatting --- .../transaction-multi-payment/src/tests.rs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pallets/transaction-multi-payment/src/tests.rs b/pallets/transaction-multi-payment/src/tests.rs index 1e0cdc855..d7e1e5a7d 100644 --- a/pallets/transaction-multi-payment/src/tests.rs +++ b/pallets/transaction-multi-payment/src/tests.rs @@ -212,10 +212,7 @@ fn set_supported_currency_in_batch_should_not_work_if_not_first_transaction() { let pre = ChargeTransactionPayment::::from(0).pre_dispatch(&ALICE, &call, &info, len); assert!(pre.is_ok()); - assert_eq!( - Currencies::free_balance(HDX, &ALICE), - 999_999_999_999_980 - ); + assert_eq!(Currencies::free_balance(HDX, &ALICE), 999_999_999_999_980); assert_ok!(ChargeTransactionPayment::::post_dispatch( Some(pre.unwrap()), @@ -225,7 +222,10 @@ fn set_supported_currency_in_batch_should_not_work_if_not_first_transaction() { &Ok(()) )); assert_eq!(Currencies::free_balance(HDX, &FEE_RECEIVER), 20); - assert_eq!(Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), alice_initial_non_native_balance); + assert_eq!( + Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), + alice_initial_non_native_balance + ); }); // batch_all @@ -246,10 +246,7 @@ fn set_supported_currency_in_batch_should_not_work_if_not_first_transaction() { let pre = ChargeTransactionPayment::::from(0).pre_dispatch(&ALICE, &call, &info, len); assert!(pre.is_ok()); - assert_eq!( - Currencies::free_balance(HDX, &ALICE), - 999_999_999_999_980 - ); + assert_eq!(Currencies::free_balance(HDX, &ALICE), 999_999_999_999_980); assert_ok!(ChargeTransactionPayment::::post_dispatch( Some(pre.unwrap()), @@ -259,7 +256,10 @@ fn set_supported_currency_in_batch_should_not_work_if_not_first_transaction() { &Ok(()) )); assert_eq!(Currencies::free_balance(HDX, &FEE_RECEIVER), 20); - assert_eq!(Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), alice_initial_non_native_balance); + assert_eq!( + Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), + alice_initial_non_native_balance + ); }); } From 6812e07b18bf1c0e71bb9a19b35fefe2862eaabf Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Thu, 14 Mar 2024 15:20:05 -0500 Subject: [PATCH 07/12] bump crate versions --- Cargo.lock | 6 +++--- integration-tests/Cargo.toml | 2 +- pallets/transaction-multi-payment/Cargo.toml | 2 +- runtime/hydradx/Cargo.toml | 2 +- runtime/hydradx/src/lib.rs | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ba03c27df..467dd7b9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4621,7 +4621,7 @@ dependencies = [ [[package]] name = "hydradx-runtime" -version = "222.0.0" +version = "223.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", @@ -8589,7 +8589,7 @@ dependencies = [ [[package]] name = "pallet-transaction-multi-payment" -version = "9.3.1" +version = "9.4.0" dependencies = [ "frame-support", "frame-system", @@ -11364,7 +11364,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.19.8" +version = "1.19.9" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index c0e678965..4e6999ae0 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runtime-integration-tests" -version = "1.19.8" +version = "1.19.9" description = "Integration tests" authors = ["GalacticCouncil"] edition = "2021" diff --git a/pallets/transaction-multi-payment/Cargo.toml b/pallets/transaction-multi-payment/Cargo.toml index 08a23d185..342ee70ca 100644 --- a/pallets/transaction-multi-payment/Cargo.toml +++ b/pallets/transaction-multi-payment/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-transaction-multi-payment" -version = "9.3.1" +version = "9.4.0" description = "Transaction multi currency payment support module" authors = ["GalacticCoucil"] edition = "2021" diff --git a/runtime/hydradx/Cargo.toml b/runtime/hydradx/Cargo.toml index 3ccdfd0b1..e3c818bbe 100644 --- a/runtime/hydradx/Cargo.toml +++ b/runtime/hydradx/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydradx-runtime" -version = "222.0.0" +version = "223.0.0" authors = ["GalacticCouncil"] edition = "2021" license = "Apache 2.0" diff --git a/runtime/hydradx/src/lib.rs b/runtime/hydradx/src/lib.rs index 25304c6f1..08ef12903 100644 --- a/runtime/hydradx/src/lib.rs +++ b/runtime/hydradx/src/lib.rs @@ -108,7 +108,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("hydradx"), impl_name: create_runtime_str!("hydradx"), authoring_version: 1, - spec_version: 222, + spec_version: 223, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From 1e91118f6d2f1a118ed7d8c70849fbb984f7a0ec Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Fri, 15 Mar 2024 10:33:32 -0500 Subject: [PATCH 08/12] add support for force_batch --- integration-tests/src/non_native_fee.rs | 62 ++++++++++++++++++ pallets/transaction-multi-payment/src/lib.rs | 7 ++ .../transaction-multi-payment/src/tests.rs | 65 +++++++++++++++++++ 3 files changed, 134 insertions(+) diff --git a/integration-tests/src/non_native_fee.rs b/integration-tests/src/non_native_fee.rs index 41f67c55c..a373ac29e 100644 --- a/integration-tests/src/non_native_fee.rs +++ b/integration-tests/src/non_native_fee.rs @@ -140,6 +140,36 @@ fn set_currency_should_work_in_batch_transaction_when_first_tx() { let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); assert_eq!(bob_balance, 999991); }); + + TestNet::reset(); + + // batch_all + Hydra::execute_with(|| { + let first_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( + pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, + ); + let second_inner_call = hydradx_runtime::RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let call = hydradx_runtime::RuntimeCall::Utility(pallet_utility::Call::force_batch { + calls: vec![first_inner_call, second_inner_call], + }); + + let info = DispatchInfo { + weight: Weight::from_parts(106_957_000, 0), + ..Default::default() + }; + let len: usize = 10; + + assert_ok!( + pallet_transaction_payment::ChargeTransactionPayment::::from(0).pre_dispatch( + &AccountId::from(BOB), + &call, + &info, + len, + ) + ); + let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); + assert_eq!(bob_balance, 999991); + }); } #[test] @@ -207,6 +237,38 @@ fn set_currency_should_not_work_in_batch_transaction_when_not_first_tx() { let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); assert_eq!(bob_balance, bob_initial_balance); }); + + TestNet::reset(); + + // batch_all + Hydra::execute_with(|| { + let first_inner_call = hydradx_runtime::RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let second_inner_call = hydradx_runtime::RuntimeCall::MultiTransactionPayment( + pallet_transaction_multi_payment::Call::set_currency { currency: BTC }, + ); + let call = hydradx_runtime::RuntimeCall::Utility(pallet_utility::Call::force_batch { + calls: vec![first_inner_call, second_inner_call], + }); + + let info = DispatchInfo { + weight: Weight::from_parts(106_957_000, 0), + ..Default::default() + }; + let len: usize = 10; + + let bob_initial_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); + + assert_ok!( + pallet_transaction_payment::ChargeTransactionPayment::::from(0).pre_dispatch( + &AccountId::from(BOB), + &call, + &info, + len, + ) + ); + let bob_balance = hydradx_runtime::Tokens::free_balance(BTC, &AccountId::from(BOB)); + assert_eq!(bob_balance, bob_initial_balance); + }); } const HITCHHIKER: [u8; 32] = [42u8; 32]; diff --git a/pallets/transaction-multi-payment/src/lib.rs b/pallets/transaction-multi-payment/src/lib.rs index d0447bc18..3cda7dd83 100644 --- a/pallets/transaction-multi-payment/src/lib.rs +++ b/pallets/transaction-multi-payment/src/lib.rs @@ -404,6 +404,13 @@ where _ => Pallet::::account_currency(who), }; currency + } else if let Some(pallet_utility::pallet::Call::::force_batch { calls }) = call.is_sub_type() { + let first_call = &calls[0]; + let currency = match first_call.is_sub_type() { + Some(Call::set_currency { currency }) => *currency, + _ => Pallet::::account_currency(who), + }; + currency } else { Pallet::::account_currency(who) }; diff --git a/pallets/transaction-multi-payment/src/tests.rs b/pallets/transaction-multi-payment/src/tests.rs index d7e1e5a7d..78185717c 100644 --- a/pallets/transaction-multi-payment/src/tests.rs +++ b/pallets/transaction-multi-payment/src/tests.rs @@ -190,6 +190,37 @@ fn set_supported_currency_in_batch_should_charge_fee_in_correct_currency() { )); assert_eq!(Currencies::free_balance(SUPPORTED_CURRENCY, &FEE_RECEIVER), 30); }); + + // batch_all + ExtBuilder::default().base_weight(5).build().execute_with(|| { + let first_inner_call = RuntimeCall::PaymentPallet(crate::Call::set_currency { + currency: SUPPORTED_CURRENCY, + }); + let second_inner_call = RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let call = RuntimeCall::Utility(pallet_utility::Call::force_batch { + calls: vec![first_inner_call, second_inner_call], + }); + + let len = 10; + let info = info_from_weight(Weight::from_parts(5, 0)); + + let pre = ChargeTransactionPayment::::from(0).pre_dispatch(&ALICE, &call, &info, len); + assert!(pre.is_ok()); + + assert_eq!( + Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), + 999_999_999_999_970 + ); + + assert_ok!(ChargeTransactionPayment::::post_dispatch( + Some(pre.unwrap()), + &info, + &default_post_info(), + len, + &Ok(()) + )); + assert_eq!(Currencies::free_balance(SUPPORTED_CURRENCY, &FEE_RECEIVER), 30); + }); } #[test] @@ -261,6 +292,40 @@ fn set_supported_currency_in_batch_should_not_work_if_not_first_transaction() { alice_initial_non_native_balance ); }); + + // batch_all + ExtBuilder::default().base_weight(5).build().execute_with(|| { + let first_inner_call = RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); + let second_inner_call = RuntimeCall::PaymentPallet(crate::Call::set_currency { + currency: SUPPORTED_CURRENCY, + }); + let call = RuntimeCall::Utility(pallet_utility::Call::force_batch { + calls: vec![first_inner_call, second_inner_call], + }); + + let len = 10; + let info = info_from_weight(Weight::from_parts(5, 0)); + + let alice_initial_non_native_balance = Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE); + + let pre = ChargeTransactionPayment::::from(0).pre_dispatch(&ALICE, &call, &info, len); + assert!(pre.is_ok()); + + assert_eq!(Currencies::free_balance(HDX, &ALICE), 999_999_999_999_980); + + assert_ok!(ChargeTransactionPayment::::post_dispatch( + Some(pre.unwrap()), + &info, + &default_post_info(), + len, + &Ok(()) + )); + assert_eq!(Currencies::free_balance(HDX, &FEE_RECEIVER), 20); + assert_eq!( + Currencies::free_balance(SUPPORTED_CURRENCY, &ALICE), + alice_initial_non_native_balance + ); + }); } #[test] From cc3c0c70d9c03a11473159533df4307174600775 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Fri, 15 Mar 2024 13:16:00 -0500 Subject: [PATCH 09/12] refactoring --- pallets/transaction-multi-payment/src/lib.rs | 33 ++++++++------------ 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/pallets/transaction-multi-payment/src/lib.rs b/pallets/transaction-multi-payment/src/lib.rs index 3cda7dd83..99ac18dd4 100644 --- a/pallets/transaction-multi-payment/src/lib.rs +++ b/pallets/transaction-multi-payment/src/lib.rs @@ -390,27 +390,20 @@ where let currency = if let Some(Call::set_currency { currency }) = call.is_sub_type() { *currency - } else if let Some(pallet_utility::pallet::Call::::batch { calls }) = call.is_sub_type() { - let first_call = &calls[0]; - let currency = match first_call.is_sub_type() { - Some(Call::set_currency { currency }) => *currency, - _ => Pallet::::account_currency(who), - }; - currency - } else if let Some(pallet_utility::pallet::Call::::batch_all { calls }) = call.is_sub_type() { - let first_call = &calls[0]; - let currency = match first_call.is_sub_type() { - Some(Call::set_currency { currency }) => *currency, - _ => Pallet::::account_currency(who), - }; - currency - } else if let Some(pallet_utility::pallet::Call::::force_batch { calls }) = call.is_sub_type() { - let first_call = &calls[0]; - let currency = match first_call.is_sub_type() { - Some(Call::set_currency { currency }) => *currency, + } else if let Some(pallet_utility::pallet::Call::batch { calls }) + | Some(pallet_utility::pallet::Call::batch_all { calls }) + | Some(pallet_utility::pallet::Call::force_batch { calls }) = call.is_sub_type() + { + // `calls` can be empty Vec + match calls.first() { + Some(first_call) => { + match first_call.is_sub_type() { + Some(Call::set_currency { currency }) => *currency, + _ => Pallet::::account_currency(who), + } + } _ => Pallet::::account_currency(who), - }; - currency + } } else { Pallet::::account_currency(who) }; From 722d4b6935e5f060c7ba0f708766168808c35e28 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Mon, 18 Mar 2024 08:46:56 -0500 Subject: [PATCH 10/12] bump crate versions --- Cargo.lock | 4 ++-- integration-tests/Cargo.toml | 2 +- pallets/transaction-multi-payment/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index baceeb44d..0f470fea0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8590,7 +8590,7 @@ dependencies = [ [[package]] name = "pallet-transaction-multi-payment" -version = "9.4.0" +version = "9.5.0" dependencies = [ "frame-support", "frame-system", @@ -11366,7 +11366,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.19.9" +version = "1.19.10" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index 4e6999ae0..7684cc98a 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runtime-integration-tests" -version = "1.19.9" +version = "1.19.10" description = "Integration tests" authors = ["GalacticCouncil"] edition = "2021" diff --git a/pallets/transaction-multi-payment/Cargo.toml b/pallets/transaction-multi-payment/Cargo.toml index 84a62c2e0..493d3b5eb 100644 --- a/pallets/transaction-multi-payment/Cargo.toml +++ b/pallets/transaction-multi-payment/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-transaction-multi-payment" -version = "9.4.0" +version = "9.5.0" description = "Transaction multi currency payment support module" authors = ["GalacticCoucil"] edition = "2021" From 0d7dbdd4193442d408f2a566497bff69cd0aa7e5 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Mon, 18 Mar 2024 08:47:03 -0500 Subject: [PATCH 11/12] formatting --- pallets/transaction-multi-payment/src/lib.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pallets/transaction-multi-payment/src/lib.rs b/pallets/transaction-multi-payment/src/lib.rs index 4ea46fe9e..42fd58552 100644 --- a/pallets/transaction-multi-payment/src/lib.rs +++ b/pallets/transaction-multi-payment/src/lib.rs @@ -440,12 +440,10 @@ where { // `calls` can be empty Vec match calls.first() { - Some(first_call) => { - match first_call.is_sub_type() { - Some(Call::set_currency { currency }) => *currency, - _ => Pallet::::account_currency(who), - } - } + Some(first_call) => match first_call.is_sub_type() { + Some(Call::set_currency { currency }) => *currency, + _ => Pallet::::account_currency(who), + }, _ => Pallet::::account_currency(who), } } else { From ea0482d6cb877a58e606d63bffd9994f3ea14cfd Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Tue, 19 Mar 2024 10:46:55 -0500 Subject: [PATCH 12/12] bump crate versions --- Cargo.lock | 4 ++-- integration-tests/Cargo.toml | 2 +- runtime/hydradx/Cargo.toml | 2 +- runtime/hydradx/src/lib.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6136968d9..a84663ef5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4621,7 +4621,7 @@ dependencies = [ [[package]] name = "hydradx-runtime" -version = "224.0.0" +version = "225.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", @@ -11366,7 +11366,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.19.10" +version = "1.19.11" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index 7684cc98a..9c63774af 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runtime-integration-tests" -version = "1.19.10" +version = "1.19.11" description = "Integration tests" authors = ["GalacticCouncil"] edition = "2021" diff --git a/runtime/hydradx/Cargo.toml b/runtime/hydradx/Cargo.toml index c90d4e2a5..aceda94c3 100644 --- a/runtime/hydradx/Cargo.toml +++ b/runtime/hydradx/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydradx-runtime" -version = "224.0.0" +version = "225.0.0" authors = ["GalacticCouncil"] edition = "2021" license = "Apache 2.0" diff --git a/runtime/hydradx/src/lib.rs b/runtime/hydradx/src/lib.rs index f6586e87d..8e2faf1c4 100644 --- a/runtime/hydradx/src/lib.rs +++ b/runtime/hydradx/src/lib.rs @@ -109,7 +109,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("hydradx"), impl_name: create_runtime_str!("hydradx"), authoring_version: 1, - spec_version: 224, + spec_version: 225, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1,