Skip to content

Commit fb59233

Browse files
committed
feat(tokens): OnDeposit, OnTransfer, OnSlash hooks
1 parent f90248d commit fb59233

File tree

9 files changed

+68
-1
lines changed

9 files changed

+68
-1
lines changed

asset-registry/src/mock/para.rs

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ impl orml_tokens::Config for Runtime {
9393
type WeightInfo = ();
9494
type ExistentialDeposits = ExistentialDeposits;
9595
type OnDust = ();
96+
type OnSlash = ();
97+
type OnDeposit = ();
98+
type OnTransfer = ();
9699
type ReserveIdentifier = [u8; 8];
97100
type MaxReserves = ();
98101
type MaxLocks = ConstU32<50>;

currencies/src/mock.rs

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ impl orml_tokens::Config for Runtime {
8181
type WeightInfo = ();
8282
type ExistentialDeposits = ExistentialDeposits;
8383
type OnDust = orml_tokens::TransferDust<Runtime, DustAccount>;
84+
type OnSlash = ();
85+
type OnDeposit = ();
86+
type OnTransfer = ();
8487
type MaxLocks = ConstU32<100_000>;
8588
type MaxReserves = ConstU32<100_000>;
8689
type ReserveIdentifier = ReserveIdentifier;

payments/src/mock.rs

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ impl orml_tokens::Config for Test {
9999
type Event = Event;
100100
type ExistentialDeposits = ExistentialDeposits;
101101
type OnDust = ();
102+
type OnSlash = ();
103+
type OnDeposit = ();
104+
type OnTransfer = ();
102105
type WeightInfo = ();
103106
type MaxLocks = MaxLocks;
104107
type DustRemovalWhitelist = MockDustRemovalWhitelist;

tokens/src/lib.rs

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

6767
use orml_traits::{
6868
arithmetic::{self, Signed},
69-
currency::TransferAll,
69+
currency::{OnDeposit, OnSlash, OnTransfer, TransferAll},
7070
BalanceStatus, GetByKey, Happened, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency,
7171
MultiReservableCurrency, NamedMultiReservableCurrency, OnDust,
7272
};
@@ -173,6 +173,8 @@ pub use module::*;
173173

174174
#[frame_support::pallet]
175175
pub mod module {
176+
use orml_traits::currency::{OnDeposit, OnSlash, OnTransfer};
177+
176178
use super::*;
177179

178180
#[pallet::config]
@@ -216,6 +218,15 @@ pub mod module {
216218
/// Handler to burn or transfer account's dust
217219
type OnDust: OnDust<Self::AccountId, Self::CurrencyId, Self::Balance>;
218220

221+
/// Hook to run before slashing an account.
222+
type OnSlash: OnSlash<Self::AccountId, Self::CurrencyId, Self::Balance>;
223+
224+
/// Hook to run before depositing into an account.
225+
type OnDeposit: OnDeposit<Self::AccountId, Self::CurrencyId, Self::Balance>;
226+
227+
/// Hook to run before transferring from an account to another.
228+
type OnTransfer: OnTransfer<Self::AccountId, Self::CurrencyId, Self::Balance>;
229+
219230
/// Handler for when an account was created
220231
type OnNewTokenAccount: Happened<(Self::AccountId, Self::CurrencyId)>;
221232

@@ -890,6 +901,7 @@ impl<T: Config> Pallet<T> {
890901
amount: T::Balance,
891902
existence_requirement: ExistenceRequirement,
892903
) -> DispatchResult {
904+
T::OnTransfer::on_transfer(currency_id, from, to, amount)?;
893905
if amount.is_zero() || from == to {
894906
return Ok(());
895907
}
@@ -1015,6 +1027,7 @@ impl<T: Config> Pallet<T> {
10151027
require_existed: bool,
10161028
change_total_issuance: bool,
10171029
) -> DispatchResult {
1030+
T::OnDeposit::on_deposit(currency_id, who, amount)?;
10181031
if amount.is_zero() {
10191032
return Ok(());
10201033
}
@@ -1110,6 +1123,7 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
11101123
/// reserved funds, however we err on the side of punishment if things
11111124
/// are inconsistent or `can_slash` wasn't used appropriately.
11121125
fn slash(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> Self::Balance {
1126+
T::OnSlash::on_slash(currency_id, who, amount);
11131127
if amount.is_zero() {
11141128
return amount;
11151129
}
@@ -1276,6 +1290,7 @@ impl<T: Config> MultiReservableCurrency<T::AccountId> for Pallet<T> {
12761290
///
12771291
/// Is a no-op if the value to be slashed is zero.
12781292
fn slash_reserved(currency_id: Self::CurrencyId, who: &T::AccountId, value: Self::Balance) -> Self::Balance {
1293+
T::OnSlash::on_slash(currency_id, who, value);
12791294
if value.is_zero() {
12801295
return value;
12811296
}

tokens/src/mock.rs

+3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ impl Config for Runtime {
280280
type WeightInfo = ();
281281
type ExistentialDeposits = ExistentialDeposits;
282282
type OnDust = TransferDust<Runtime, DustReceiver>;
283+
type OnSlash = ();
284+
type OnDeposit = ();
285+
type OnTransfer = ();
283286
type OnNewTokenAccount = TrackCreatedAccounts;
284287
type OnKilledTokenAccount = TrackKilledAccounts;
285288
type MaxLocks = ConstU32<2>;

traits/src/currency.rs

+31
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,34 @@ impl<AccountId> TransferAll<AccountId> for Tuple {
657657
Ok(())
658658
}
659659
}
660+
661+
/// Hook to run before slashing an account.
662+
pub trait OnSlash<AccountId, CurrencyId, Balance> {
663+
fn on_slash(currency_id: CurrencyId, who: &AccountId, amount: Balance);
664+
}
665+
666+
impl<AccountId, CurrencyId, Balance> OnSlash<AccountId, CurrencyId, Balance> for () {
667+
fn on_slash(_: CurrencyId, _: &AccountId, _: Balance) {}
668+
}
669+
670+
/// Hook to run before depositing into an account.
671+
pub trait OnDeposit<AccountId, CurrencyId, Balance> {
672+
fn on_deposit(currency_id: CurrencyId, who: &AccountId, amount: Balance) -> DispatchResult;
673+
}
674+
675+
impl<AccountId, CurrencyId, Balance> OnDeposit<AccountId, CurrencyId, Balance> for () {
676+
fn on_deposit(_: CurrencyId, _: &AccountId, _: Balance) -> DispatchResult {
677+
Ok(())
678+
}
679+
}
680+
681+
/// Hook to run before transferring from an account to another.
682+
pub trait OnTransfer<AccountId, CurrencyId, Balance> {
683+
fn on_transfer(currency_id: CurrencyId, from: &AccountId, to: &AccountId, amount: Balance) -> DispatchResult;
684+
}
685+
686+
impl<AccountId, CurrencyId, Balance> OnTransfer<AccountId, CurrencyId, Balance> for () {
687+
fn on_transfer(_: CurrencyId, _: &AccountId, _: &AccountId, _: Balance) -> DispatchResult {
688+
Ok(())
689+
}
690+
}

xtokens/src/mock/para.rs

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ impl orml_tokens::Config for Runtime {
8484
type WeightInfo = ();
8585
type ExistentialDeposits = ExistentialDeposits;
8686
type OnDust = ();
87+
type OnSlash = ();
88+
type OnDeposit = ();
89+
type OnTransfer = ();
8790
type MaxLocks = ConstU32<50>;
8891
type MaxReserves = ConstU32<50>;
8992
type ReserveIdentifier = [u8; 8];

xtokens/src/mock/para_relative_view.rs

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ impl orml_tokens::Config for Runtime {
8787
type WeightInfo = ();
8888
type ExistentialDeposits = ExistentialDeposits;
8989
type OnDust = ();
90+
type OnSlash = ();
91+
type OnDeposit = ();
92+
type OnTransfer = ();
9093
type MaxLocks = ConstU32<50>;
9194
type MaxReserves = ConstU32<50>;
9295
type ReserveIdentifier = [u8; 8];

xtokens/src/mock/para_teleport.rs

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ impl orml_tokens::Config for Runtime {
8585
type WeightInfo = ();
8686
type ExistentialDeposits = ExistentialDeposits;
8787
type OnDust = ();
88+
type OnSlash = ();
89+
type OnDeposit = ();
90+
type OnTransfer = ();
8891
type MaxLocks = ConstU32<50>;
8992
type MaxReserves = ConstU32<50>;
9093
type ReserveIdentifier = [u8; 8];

0 commit comments

Comments
 (0)