@@ -66,9 +66,9 @@ use sp_std::{cmp, convert::Infallible, marker, prelude::*, vec::Vec};
66
66
67
67
use orml_traits:: {
68
68
arithmetic:: { self , Signed } ,
69
- currency:: { OnDeposit , OnSlash , OnTransfer , TransferAll } ,
69
+ currency:: { MutationHooks , OnDeposit , OnDust , OnSlash , OnTransfer , TransferAll } ,
70
70
BalanceStatus , GetByKey , Happened , LockIdentifier , MultiCurrency , MultiCurrencyExtended , MultiLockableCurrency ,
71
- MultiReservableCurrency , NamedMultiReservableCurrency , OnDust ,
71
+ MultiReservableCurrency , NamedMultiReservableCurrency ,
72
72
} ;
73
73
74
74
mod imbalances;
@@ -173,7 +173,7 @@ pub use module::*;
173
173
174
174
#[ frame_support:: pallet]
175
175
pub mod module {
176
- use orml_traits:: currency:: { OnDeposit , OnSlash , OnTransfer } ;
176
+ use orml_traits:: currency:: MutationHooks ;
177
177
178
178
use super :: * ;
179
179
@@ -216,23 +216,9 @@ pub mod module {
216
216
/// System::AccountInfo, zero ED may cause some problems.
217
217
type ExistentialDeposits : GetByKey < Self :: CurrencyId , Self :: Balance > ;
218
218
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 > ;
236
222
237
223
#[ pallet:: constant]
238
224
type MaxLocks : Get < u32 > ;
@@ -765,11 +751,11 @@ impl<T: Config> Pallet<T> {
765
751
// Ignore the result, because if it failed then there are remaining consumers,
766
752
// and the account storage in frame_system shouldn't be reaped.
767
753
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) ) ;
769
755
} else if !existed && exists {
770
756
// if new, increase account provider
771
757
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) ) ;
773
759
}
774
760
775
761
if let Some ( endowed) = maybe_endowed {
@@ -783,7 +769,7 @@ impl<T: Config> Pallet<T> {
783
769
if let Some ( dust_amount) = maybe_dust {
784
770
// `OnDust` maybe get/set storage `Accounts` of `who`, trigger handler here
785
771
// 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) ;
787
773
788
774
Self :: deposit_event ( Event :: DustLost {
789
775
currency_id,
@@ -906,7 +892,12 @@ impl<T: Config> Pallet<T> {
906
892
return Ok ( ( ) ) ;
907
893
}
908
894
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
+ ) ?;
910
901
Self :: try_mutate_account ( to, currency_id, |to_account, _existed| -> DispatchResult {
911
902
Self :: try_mutate_account ( from, currency_id, |from_account, _existed| -> DispatchResult {
912
903
from_account. free = from_account
@@ -946,6 +937,12 @@ impl<T: Config> Pallet<T> {
946
937
Ok ( ( ) )
947
938
} ) ?;
948
939
940
+ <T :: CurrencyHooks as MutationHooks < T :: AccountId , T :: CurrencyId , T :: Balance > >:: PostTransfer :: on_transfer (
941
+ currency_id,
942
+ from,
943
+ to,
944
+ amount,
945
+ ) ?;
949
946
Self :: deposit_event ( Event :: Transfer {
950
947
currency_id,
951
948
from : from. clone ( ) ,
@@ -1032,7 +1029,11 @@ impl<T: Config> Pallet<T> {
1032
1029
return Ok ( ( ) ) ;
1033
1030
}
1034
1031
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
+ ) ?;
1036
1037
Self :: try_mutate_account ( who, currency_id, |account, existed| -> DispatchResult {
1037
1038
if require_existed {
1038
1039
ensure ! ( existed, Error :: <T >:: DeadAccount ) ;
@@ -1054,6 +1055,11 @@ impl<T: Config> Pallet<T> {
1054
1055
}
1055
1056
account. free = account. free . defensive_saturating_add ( amount) ;
1056
1057
1058
+ <T :: CurrencyHooks as MutationHooks < T :: AccountId , T :: CurrencyId , T :: Balance > >:: PostDeposit :: on_deposit (
1059
+ currency_id,
1060
+ who,
1061
+ amount,
1062
+ ) ?;
1057
1063
Self :: deposit_event ( Event :: Deposited {
1058
1064
currency_id,
1059
1065
who : who. clone ( ) ,
@@ -1128,7 +1134,11 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
1128
1134
return amount;
1129
1135
}
1130
1136
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
+ ) ;
1132
1142
let account = Self :: accounts ( who, currency_id) ;
1133
1143
let free_slashed_amount = account. free . min ( amount) ;
1134
1144
// 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> {
1306
1316
return value;
1307
1317
}
1308
1318
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
+ ) ;
1310
1324
let reserved_balance = Self :: reserved_balance ( currency_id, who) ;
1311
1325
let actual = reserved_balance. min ( value) ;
1312
1326
Self :: mutate_account ( who, currency_id, |account, _| {
0 commit comments