Skip to content

Commit 83b063c

Browse files
authored
add general OnDust implemention (#328)
1 parent 9fe934d commit 83b063c

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

currencies/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ orml-utilities = { path = "../utilities", version = "0.3.3-dev", default-feature
2323
[dev-dependencies]
2424
sp-core = { version = "2.0.0", default-features = false }
2525
pallet-balances = "2.0.0"
26-
tokens = { package = "orml-tokens", path = "../tokens" }
26+
orml_tokens = { package = "orml-tokens", path = "../tokens", version = "0.3.3-dev" }
2727

2828
clear_on_drop = { version = "0.2.4", features = ["no_cc"] } # https://github.com/paritytech/substrate/issues/4179
2929

currencies/src/mock.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![cfg(test)]
44

55
use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
6-
use orml_traits::{parameter_type_with_key, OnDust};
6+
use orml_traits::parameter_type_with_key;
77
use pallet_balances;
88
use sp_core::H256;
99
use sp_runtime::{
@@ -12,8 +12,6 @@ use sp_runtime::{
1212
AccountId32, ModuleId, Perbill,
1313
};
1414

15-
use tokens;
16-
1715
use super::*;
1816

1917
mod currencies {
@@ -24,7 +22,7 @@ impl_outer_event! {
2422
pub enum TestEvent for Runtime {
2523
frame_system<T>,
2624
currencies<T>,
27-
tokens<T>,
25+
orml_tokens<T>,
2826
pallet_balances<T>,
2927
}
3028
}
@@ -91,13 +89,6 @@ impl pallet_balances::Trait for Runtime {
9189
}
9290
pub type PalletBalances = pallet_balances::Module<Runtime>;
9391

94-
pub struct MockOnDust;
95-
impl OnDust<AccountId, CurrencyId, Balance> for MockOnDust {
96-
fn on_dust(who: &AccountId, currency_id: CurrencyId, amount: Balance) {
97-
let _ = <Tokens as MultiCurrency<_>>::transfer(currency_id, who, &DustAccount::get(), amount);
98-
}
99-
}
100-
10192
parameter_type_with_key! {
10293
pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance {
10394
Default::default()
@@ -108,16 +99,16 @@ parameter_types! {
10899
pub DustAccount: AccountId = ModuleId(*b"orml/dst").into_account();
109100
}
110101

111-
impl tokens::Trait for Runtime {
102+
impl orml_tokens::Trait for Runtime {
112103
type Event = TestEvent;
113104
type Balance = Balance;
114105
type Amount = i64;
115106
type CurrencyId = CurrencyId;
116107
type WeightInfo = ();
117108
type ExistentialDeposits = ExistentialDeposits;
118-
type OnDust = MockOnDust;
109+
type OnDust = orml_tokens::TransferDust<Runtime, DustAccount>;
119110
}
120-
pub type Tokens = tokens::Module<Runtime>;
111+
pub type Tokens = orml_tokens::Module<Runtime>;
121112

122113
pub const NATIVE_CURRENCY_ID: CurrencyId = 1;
123114
pub const X_TOKEN_ID: CurrencyId = 2;
@@ -186,7 +177,7 @@ impl ExtBuilder {
186177
.assimilate_storage(&mut t)
187178
.unwrap();
188179

189-
tokens::GenesisConfig::<Runtime> {
180+
orml_tokens::GenesisConfig::<Runtime> {
190181
endowed_accounts: self
191182
.endowed_accounts
192183
.into_iter()

tokens/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ pub trait WeightInfo {
8585
fn transfer_all() -> Weight;
8686
}
8787

88+
pub struct TransferDust<T, GetAccountId>(marker::PhantomData<(T, GetAccountId)>);
89+
impl<T, GetAccountId> OnDust<T::AccountId, T::CurrencyId, T::Balance> for TransferDust<T, GetAccountId>
90+
where
91+
T: Trait,
92+
GetAccountId: Get<T::AccountId>,
93+
{
94+
fn on_dust(who: &T::AccountId, currency_id: T::CurrencyId, amount: T::Balance) {
95+
// transfer the dust to treasury account, ignore the result,
96+
// if failed will leave some dust which still could be recycled.
97+
let _ = <Module<T> as MultiCurrency<T::AccountId>>::transfer(currency_id, who, &GetAccountId::get(), amount);
98+
}
99+
}
100+
101+
pub struct BurnDust<T>(marker::PhantomData<T>);
102+
impl<T: Trait> OnDust<T::AccountId, T::CurrencyId, T::Balance> for BurnDust<T> {
103+
fn on_dust(who: &T::AccountId, currency_id: T::CurrencyId, amount: T::Balance) {
104+
// burn the dust, ignore the result,
105+
// if failed will leave some dust which still could be recycled.
106+
let _ = Module::<T>::withdraw(currency_id, who, amount);
107+
}
108+
}
109+
88110
pub trait Trait: frame_system::Trait {
89111
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
90112

tokens/src/mock.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,6 @@ impl pallet_elections_phragmen::Trait for Runtime {
270270
type WeightInfo = ();
271271
}
272272

273-
pub struct MockOnDust;
274-
impl OnDust<AccountId, CurrencyId, Balance> for MockOnDust {
275-
fn on_dust(who: &AccountId, currency_id: CurrencyId, amount: Balance) {
276-
let _ = <Tokens as MultiCurrency<_>>::transfer(currency_id, who, &DustAccount::get(), amount);
277-
}
278-
}
279-
280273
parameter_type_with_key! {
281274
pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance {
282275
match currency_id {
@@ -298,7 +291,7 @@ impl Trait for Runtime {
298291
type CurrencyId = CurrencyId;
299292
type WeightInfo = ();
300293
type ExistentialDeposits = ExistentialDeposits;
301-
type OnDust = MockOnDust;
294+
type OnDust = TransferDust<Runtime, DustAccount>;
302295
}
303296
pub type Tokens = Module<Runtime>;
304297
pub type TreasuryCurrencyAdapter = <Runtime as pallet_treasury::Trait>::Currency;

0 commit comments

Comments
 (0)