Skip to content

Commit 5803a01

Browse files
authored
Token hook refactoring; Posthooks for deposit and transfer (#834)
* feat(tokens): Refactor hooks into single trait * feat(tokens): Add post-hooks for Transfer and Deposit * chore(tokens): improve mutation hook naming
1 parent 9d54d0e commit 5803a01

File tree

11 files changed

+242
-125
lines changed

11 files changed

+242
-125
lines changed

asset-registry/src/mock/para.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,11 @@ impl orml_tokens::Config for Runtime {
9292
type CurrencyId = CurrencyId;
9393
type WeightInfo = ();
9494
type ExistentialDeposits = ExistentialDeposits;
95-
type OnDust = ();
96-
type OnSlash = ();
97-
type OnDeposit = ();
98-
type OnTransfer = ();
95+
type CurrencyHooks = ();
9996
type ReserveIdentifier = [u8; 8];
10097
type MaxReserves = ();
10198
type MaxLocks = ConstU32<50>;
10299
type DustRemovalWhitelist = Nothing;
103-
type OnNewTokenAccount = ();
104-
type OnKilledTokenAccount = ();
105100
}
106101

107102
#[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, Debug)]

currencies/src/mock.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use frame_support::{
88
traits::{ConstU32, ConstU64, Everything, Nothing},
99
PalletId,
1010
};
11-
use orml_traits::parameter_type_with_key;
11+
use orml_traits::{currency::MutationHooks, parameter_type_with_key};
1212
use sp_core::H256;
1313
use sp_runtime::{
1414
testing::Header,
@@ -73,23 +73,33 @@ parameter_types! {
7373
pub DustAccount: AccountId = PalletId(*b"orml/dst").into_account_truncating();
7474
}
7575

76+
pub struct CurrencyHooks<T>(marker::PhantomData<T>);
77+
impl<T: orml_tokens::Config> MutationHooks<T::AccountId, T::CurrencyId, T::Balance> for CurrencyHooks<T>
78+
where
79+
T::AccountId: From<AccountId32>,
80+
{
81+
type OnDust = orml_tokens::TransferDust<T, DustAccount>;
82+
type OnSlash = ();
83+
type PreDeposit = ();
84+
type PostDeposit = ();
85+
type PreTransfer = ();
86+
type PostTransfer = ();
87+
type OnNewTokenAccount = ();
88+
type OnKilledTokenAccount = ();
89+
}
90+
7691
impl orml_tokens::Config for Runtime {
7792
type RuntimeEvent = RuntimeEvent;
7893
type Balance = Balance;
7994
type Amount = i64;
8095
type CurrencyId = CurrencyId;
8196
type WeightInfo = ();
8297
type ExistentialDeposits = ExistentialDeposits;
83-
type OnDust = orml_tokens::TransferDust<Runtime, DustAccount>;
84-
type OnSlash = ();
85-
type OnDeposit = ();
86-
type OnTransfer = ();
98+
type CurrencyHooks = CurrencyHooks<Runtime>;
8799
type MaxLocks = ConstU32<100_000>;
88100
type MaxReserves = ConstU32<100_000>;
89101
type ReserveIdentifier = ReserveIdentifier;
90102
type DustRemovalWhitelist = Nothing;
91-
type OnNewTokenAccount = ();
92-
type OnKilledTokenAccount = ();
93103
}
94104

95105
pub const NATIVE_CURRENCY_ID: CurrencyId = 1;

payments/src/mock.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,12 @@ impl orml_tokens::Config for Test {
9898
type CurrencyId = u32;
9999
type RuntimeEvent = RuntimeEvent;
100100
type ExistentialDeposits = ExistentialDeposits;
101-
type OnDust = ();
102-
type OnSlash = ();
103-
type OnDeposit = ();
104-
type OnTransfer = ();
101+
type CurrencyHooks = ();
105102
type WeightInfo = ();
106103
type MaxLocks = MaxLocks;
107104
type DustRemovalWhitelist = MockDustRemovalWhitelist;
108105
type MaxReserves = ConstU32<2>;
109106
type ReserveIdentifier = ReserveIdentifier;
110-
type OnNewTokenAccount = ();
111-
type OnKilledTokenAccount = ();
112107
}
113108

114109
pub struct MockDisputeResolver;

tokens/src/lib.rs

+41-27
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ use sp_std::{cmp, convert::Infallible, marker, prelude::*, vec::Vec};
6666

6767
use orml_traits::{
6868
arithmetic::{self, Signed},
69-
currency::{OnDeposit, OnSlash, OnTransfer, TransferAll},
69+
currency::{MutationHooks, OnDeposit, OnDust, OnSlash, OnTransfer, TransferAll},
7070
BalanceStatus, GetByKey, Happened, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency,
71-
MultiReservableCurrency, NamedMultiReservableCurrency, OnDust,
71+
MultiReservableCurrency, NamedMultiReservableCurrency,
7272
};
7373

7474
mod imbalances;
@@ -173,7 +173,7 @@ pub use module::*;
173173

174174
#[frame_support::pallet]
175175
pub mod module {
176-
use orml_traits::currency::{OnDeposit, OnSlash, OnTransfer};
176+
use orml_traits::currency::MutationHooks;
177177

178178
use super::*;
179179

@@ -216,23 +216,9 @@ pub mod module {
216216
/// System::AccountInfo, zero ED may cause some problems.
217217
type ExistentialDeposits: GetByKey<Self::CurrencyId, Self::Balance>;
218218

219-
/// Handler to burn or transfer account's dust
220-
type OnDust: OnDust<Self::AccountId, Self::CurrencyId, Self::Balance>;
221-
222-
/// Hook to run before slashing an account.
223-
type OnSlash: OnSlash<Self::AccountId, Self::CurrencyId, Self::Balance>;
224-
225-
/// Hook to run before depositing into an account.
226-
type OnDeposit: OnDeposit<Self::AccountId, Self::CurrencyId, Self::Balance>;
227-
228-
/// Hook to run before transferring from an account to another.
229-
type OnTransfer: OnTransfer<Self::AccountId, Self::CurrencyId, Self::Balance>;
230-
231-
/// Handler for when an account was created
232-
type OnNewTokenAccount: Happened<(Self::AccountId, Self::CurrencyId)>;
233-
234-
/// Handler for when an account was created
235-
type OnKilledTokenAccount: Happened<(Self::AccountId, Self::CurrencyId)>;
219+
/// Hooks are actions that are executed on certain events.
220+
/// For example: OnDust, OnNewTokenAccount
221+
type CurrencyHooks: MutationHooks<Self::AccountId, Self::CurrencyId, Self::Balance>;
236222

237223
#[pallet::constant]
238224
type MaxLocks: Get<u32>;
@@ -765,11 +751,11 @@ impl<T: Config> Pallet<T> {
765751
// Ignore the result, because if it failed then there are remaining consumers,
766752
// and the account storage in frame_system shouldn't be reaped.
767753
let _ = frame_system::Pallet::<T>::dec_providers(who);
768-
T::OnKilledTokenAccount::happened(&(who.clone(), currency_id));
754+
<T::CurrencyHooks as MutationHooks<T::AccountId, T::CurrencyId, T::Balance>>::OnKilledTokenAccount::happened(&(who.clone(), currency_id));
769755
} else if !existed && exists {
770756
// if new, increase account provider
771757
frame_system::Pallet::<T>::inc_providers(who);
772-
T::OnNewTokenAccount::happened(&(who.clone(), currency_id));
758+
<T::CurrencyHooks as MutationHooks<T::AccountId, T::CurrencyId, T::Balance>>::OnNewTokenAccount::happened(&(who.clone(), currency_id));
773759
}
774760

775761
if let Some(endowed) = maybe_endowed {
@@ -783,7 +769,7 @@ impl<T: Config> Pallet<T> {
783769
if let Some(dust_amount) = maybe_dust {
784770
// `OnDust` maybe get/set storage `Accounts` of `who`, trigger handler here
785771
// to avoid some unexpected errors.
786-
T::OnDust::on_dust(who, currency_id, dust_amount);
772+
<T::CurrencyHooks as MutationHooks<T::AccountId, T::CurrencyId, T::Balance>>::OnDust::on_dust(who, currency_id, dust_amount);
787773

788774
Self::deposit_event(Event::DustLost {
789775
currency_id,
@@ -906,7 +892,12 @@ impl<T: Config> Pallet<T> {
906892
return Ok(());
907893
}
908894

909-
T::OnTransfer::on_transfer(currency_id, from, to, amount)?;
895+
<T::CurrencyHooks as MutationHooks<T::AccountId, T::CurrencyId, T::Balance>>::PreTransfer::on_transfer(
896+
currency_id,
897+
from,
898+
to,
899+
amount,
900+
)?;
910901
Self::try_mutate_account(to, currency_id, |to_account, _existed| -> DispatchResult {
911902
Self::try_mutate_account(from, currency_id, |from_account, _existed| -> DispatchResult {
912903
from_account.free = from_account
@@ -946,6 +937,12 @@ impl<T: Config> Pallet<T> {
946937
Ok(())
947938
})?;
948939

940+
<T::CurrencyHooks as MutationHooks<T::AccountId, T::CurrencyId, T::Balance>>::PostTransfer::on_transfer(
941+
currency_id,
942+
from,
943+
to,
944+
amount,
945+
)?;
949946
Self::deposit_event(Event::Transfer {
950947
currency_id,
951948
from: from.clone(),
@@ -1032,7 +1029,11 @@ impl<T: Config> Pallet<T> {
10321029
return Ok(());
10331030
}
10341031

1035-
T::OnDeposit::on_deposit(currency_id, who, amount)?;
1032+
<T::CurrencyHooks as MutationHooks<T::AccountId, T::CurrencyId, T::Balance>>::PreDeposit::on_deposit(
1033+
currency_id,
1034+
who,
1035+
amount,
1036+
)?;
10361037
Self::try_mutate_account(who, currency_id, |account, existed| -> DispatchResult {
10371038
if require_existed {
10381039
ensure!(existed, Error::<T>::DeadAccount);
@@ -1054,6 +1055,11 @@ impl<T: Config> Pallet<T> {
10541055
}
10551056
account.free = account.free.defensive_saturating_add(amount);
10561057

1058+
<T::CurrencyHooks as MutationHooks<T::AccountId, T::CurrencyId, T::Balance>>::PostDeposit::on_deposit(
1059+
currency_id,
1060+
who,
1061+
amount,
1062+
)?;
10571063
Self::deposit_event(Event::Deposited {
10581064
currency_id,
10591065
who: who.clone(),
@@ -1128,7 +1134,11 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
11281134
return amount;
11291135
}
11301136

1131-
T::OnSlash::on_slash(currency_id, who, amount);
1137+
<T::CurrencyHooks as MutationHooks<T::AccountId, T::CurrencyId, T::Balance>>::OnSlash::on_slash(
1138+
currency_id,
1139+
who,
1140+
amount,
1141+
);
11321142
let account = Self::accounts(who, currency_id);
11331143
let free_slashed_amount = account.free.min(amount);
11341144
// Cannot underflow because free_slashed_amount can never be greater than amount
@@ -1306,7 +1316,11 @@ impl<T: Config> MultiReservableCurrency<T::AccountId> for Pallet<T> {
13061316
return value;
13071317
}
13081318

1309-
T::OnSlash::on_slash(currency_id, who, value);
1319+
<T::CurrencyHooks as MutationHooks<T::AccountId, T::CurrencyId, T::Balance>>::OnSlash::on_slash(
1320+
currency_id,
1321+
who,
1322+
value,
1323+
);
13101324
let reserved_balance = Self::reserved_balance(currency_id, who);
13111325
let actual = reserved_balance.min(value);
13121326
Self::mutate_account(who, currency_id, |account, _| {

0 commit comments

Comments
 (0)