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: Add call filter for Energy pallet #154

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
9 changes: 7 additions & 2 deletions pallets/energy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod pallet {
pallet_prelude::*,
traits::{tokens::Balance, Currency, ExistenceRequirement, WithdrawReasons},
};
use frame_support::traits::Contains;
use frame_system::pallet_prelude::*;
use pallet_transaction_payment::OnChargeTransaction;
use sp_runtime::{
Expand Down Expand Up @@ -67,6 +68,9 @@ pub mod pallet {
/// The minimum amount of energy required to keep an account.
type ExistentialDeposit: Get<Self::Balance>;

/// Filter that determines if the call fees can be paid by energy.
type CallFilter: Contains<Self::Call>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -356,8 +360,9 @@ pub mod pallet {
let fee_without_tip = fee.saturating_sub(tip);
let energy_fee = Self::native_token_to_energy(fee_without_tip);

// if we don't have enough energy then fallback to paying with native token.
if Self::energy_balance(&who) < energy_fee {
// if the call cannot be paid by energy or we don't have enough energy then fallback
// to paying with native token.
if !T::CallFilter::contains(call) || Self::energy_balance(&who) < energy_fee {
return T::NativeOnChargeTransaction::withdraw_fee(
who,
call,
Expand Down
1 change: 1 addition & 0 deletions pallets/energy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ impl pallet_energy::Config for Test {
type UpdateOrigin = EnsureAccount<TestUpdateOrigin, AccountId>;
type NativeOnChargeTransaction = ProxiedOnChargeTransaction<CurrencyAdapter<Balances, ()>>;
type ExistentialDeposit = EnergyExistentialDeposit;
type CallFilter = Everything;
type WeightInfo = ();
}

Expand Down
Loading