From d04987353b6bb08b115842927b1e4644c6d0fe02 Mon Sep 17 00:00:00 2001 From: Vlad Proshchavaiev <32250097+F3Joule@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:24:58 +0300 Subject: [PATCH 1/2] Make proxy pay with its own energy From now on, if proxy (caller) account has enough energy it will pay for himself. If not, proxy's real account will be energy provider. A fallback is a caller account native token balance --- pallets/energy/src/lib.rs | 8 ++++---- pallets/energy/src/tests.rs | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/pallets/energy/src/lib.rs b/pallets/energy/src/lib.rs index f8e5d6e8..c62b0f38 100644 --- a/pallets/energy/src/lib.rs +++ b/pallets/energy/src/lib.rs @@ -386,10 +386,10 @@ pub mod pallet { let real_account = T::Lookup::lookup(real.clone())?; is_who_a_proxy = pallet_proxy::Pallet::::find_proxy(&real_account, who, None).is_ok(); - if is_who_a_proxy { - real_account - } else { - who.clone() + match is_who_a_proxy { + true if Self::energy_balance(&who) >= energy_fee => who.clone(), + true => real_account, + false => who.clone(), } } _ => who.clone(), diff --git a/pallets/energy/src/tests.rs b/pallets/energy/src/tests.rs index c8a621df..0c7770f9 100644 --- a/pallets/energy/src/tests.rs +++ b/pallets/energy/src/tests.rs @@ -363,6 +363,45 @@ fn charge_transaction_should_pay_with_energy_if_enough() { #[test] fn charge_transaction_should_pay_with_energy_if_proxy_caller() { + ExtBuilder::default().value_coefficient(2f64).build().execute_with(|| { + let real_account = account(1); + let proxy_account = account(2); + + assert_ok!(pallet_proxy::Pallet::::add_proxy_delegate( + &real_account, + proxy_account, + MockProxyType::Any, + Zero::zero(), + )); + + set_native_balance(proxy_account, 1000); + set_energy_balance(proxy_account, 1000); + + assert_ok!(charge_transaction_with_proxy(&proxy_account, real_account, 150, 100, 20, || { + // subtract the expected fees / coefficient from real account + assert_energy_balance!(proxy_account, 1000 - div_coeff!(150, 2)); + // tip subtracted from the native balance of proxy account + assert_balance!(proxy_account, 1000 - 20); + + assert!( + get_captured_withdraw_fee_args().is_none(), + "Shouldn't go through the fallback OnChargeTransaction" + ); + },),); + + assert_energy_balance!(proxy_account, 1000 - div_coeff!(100, 2)); + + // subtract the actual (fees + tip) / coefficient + assert_balance!(proxy_account, 1000 - 20); // tip subtracted from the native balance + assert!( + get_corrected_and_deposit_fee_args().is_none(), + "Shouldn't go through the fallback OnChargeTransaction" + ); + }); +} + +#[test] +fn charge_transaction_should_pay_with_real_account_energy_if_proxy_balance_low() { ExtBuilder::default().value_coefficient(2f64).build().execute_with(|| { let real_account = account(1); let proxy_account = account(2); From 7b30e626400cbcb0f9910b9dcc1bec564effce8e Mon Sep 17 00:00:00 2001 From: Vlad Proshchavaiev <32250097+F3Joule@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:25:14 +0300 Subject: [PATCH 2/2] Update spec_version to 44 --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index df077b74..2ab91fc2 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -189,7 +189,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("subsocial-parachain"), impl_name: create_runtime_str!("subsocial-parachain"), authoring_version: 1, - spec_version: 43, + spec_version: 44, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 9,