Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(energy): Make proxy caller to pay with his own energy #261

Merged
merged 2 commits into from
Apr 12, 2024
Merged
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
8 changes: 4 additions & 4 deletions pallets/energy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,10 @@ pub mod pallet {
let real_account = T::Lookup::lookup(real.clone())?;
is_who_a_proxy = pallet_proxy::Pallet::<T>::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(),
Expand Down
39 changes: 39 additions & 0 deletions pallets/energy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Test>::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);
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading