From a032a4712f8cbe9abd16b4821c35aec89dd60b84 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Sat, 28 May 2022 13:45:27 +0200 Subject: [PATCH 1/9] add OnNewAccount and OnKilledAccount to orml-tokens --- tokens/src/lib.rs | 10 +++++++++- tokens/src/mock.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ tokens/src/tests.rs | 2 ++ traits/src/currency.rs | 18 ++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index c7d73d7dc..13782f2c9 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -68,7 +68,7 @@ use orml_traits::{ arithmetic::{self, Signed}, currency::TransferAll, BalanceStatus, GetByKey, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, - MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, + MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewAccount, OnKilledAccount, }; mod imbalances; @@ -216,6 +216,12 @@ pub mod module { /// Handler to burn or transfer account's dust type OnDust: OnDust; + /// Handler for when an account was created + type OnNewAccount: OnNewAccount; + + /// Handler for when an account was created + type OnKilledAccount: OnKilledAccount; + #[pallet::constant] type MaxLocks: Get; @@ -747,9 +753,11 @@ impl Pallet { // Ignore the result, because if it failed then there are remaining consumers, // and the account storage in frame_system shouldn't be reaped. let _ = frame_system::Pallet::::dec_providers(who); + T::OnKilledAccount::on_killed_account(&who, currency_id); } else if !existed && exists { // if new, increase account provider frame_system::Pallet::::inc_providers(who); + T::OnNewAccount::on_new_account(&who, currency_id); } if let Some(endowed) = maybe_endowed { diff --git a/tokens/src/mock.rs b/tokens/src/mock.rs index 7be814521..bd799b9c5 100644 --- a/tokens/src/mock.rs +++ b/tokens/src/mock.rs @@ -220,6 +220,43 @@ parameter_type_with_key! { }; } +thread_local! { + pub static CREATED: RefCell> = RefCell::new(vec![]); + pub static KILLED: RefCell> = RefCell::new(vec![]); +} + +pub struct TrackCreatedAccounts; +impl TrackCreatedAccounts { + pub fn accounts() -> Vec<(AccountId, CurrencyId)> { + CREATED.clone() + } + + pub fn reset() { + *CREATED = RefCell::new(vec![]); + } +} +impl OnNewAccount for TrackCreatedAccounts { + fn on_new_account(who: &AccountId, currency: CurrencyId) { + CREATED.push((who, CurrencyId)); + } +} + +pub struct TrackKilledAccounts; +impl TrackKilledAccounts { + pub fn accounts() -> Vec<(AccountId, CurrencyId)> { + KILLED.clone() + } + + pub fn reset() { + *KILLED = RefCell::new(vec![]); + } +} +impl OnNewAccount for TrackKilledAccounts { + fn on_new_account(who: &AccountId, currency: CurrencyId) { + KILLED.push((who, CurrencyId)); + } +} + parameter_types! { pub DustReceiver: AccountId = PalletId(*b"orml/dst").into_account(); } @@ -232,6 +269,8 @@ impl Config for Runtime { type WeightInfo = (); type ExistentialDeposits = ExistentialDeposits; type OnDust = TransferDust; + type OnNewAccount = TrackCreatedAccounts; + type OnKilledAccount = TrackKilledAccounts; type MaxLocks = ConstU32<2>; type MaxReserves = ConstU32<2>; type ReserveIdentifier = ReserveIdentifier; @@ -296,6 +335,9 @@ impl ExtBuilder { .unwrap(); } + TrackCreatedAccounts::reset(); + TrackKilledAccounts::reset(); + let mut ext = sp_io::TestExternalities::new(t); ext.execute_with(|| System::set_block_number(1)); ext diff --git a/tokens/src/tests.rs b/tokens/src/tests.rs index 0cbf6ffd3..168a4abfa 100644 --- a/tokens/src/tests.rs +++ b/tokens/src/tests.rs @@ -139,6 +139,7 @@ fn transfer_all_allow_death_should_work() { })); assert!(!Accounts::::contains_key(ALICE, DOT)); assert_eq!(Tokens::free_balance(DOT, &ALICE), 0); + assert_eq!(TrackKilledAccounts::accounts(), vec![(ALICE, DOT)]); assert_ok!(Tokens::set_lock(ID_1, DOT, &BOB, 50)); assert_eq!(Tokens::accounts(&BOB, DOT).frozen, 50); @@ -513,6 +514,7 @@ fn set_free_balance_should_work() { Tokens::set_free_balance(DOT, &ALICE, 2); assert!(Accounts::::contains_key(ALICE, DOT)); + assert_eq!(TrackCreatedAccounts::accounts(), vec![(ALICE, DOT)]); assert_eq!(Tokens::free_balance(DOT, &ALICE), 2); assert_eq!(Tokens::free_balance(DOT, &DustReceiver::get()), 1); diff --git a/traits/src/currency.rs b/traits/src/currency.rs index cd0c9d71e..bc774a861 100644 --- a/traits/src/currency.rs +++ b/traits/src/currency.rs @@ -643,6 +643,24 @@ impl OnDust for fn on_dust(_: &AccountId, _: CurrencyId, _: Balance) {} } +/// Handler for a newly created account +pub trait OnNewAccount { + fn on_new_account(who: &AccountId, currency_id: CurrencyId); +} + +impl OnDust for () { + fn on_new_account(_: &AccountId, _: CurrencyId) {} +} + +/// Handler for an account that was removed +pub trait OnKilledAccount { + fn on_killed_account(who: &AccountId, currency_id: CurrencyId); +} + +impl OnDust for () { + fn on_killed_account(_: &AccountId, _: CurrencyId) {} +} + pub trait TransferAll { fn transfer_all(source: &AccountId, dest: &AccountId) -> DispatchResult; } From fa97751606ed68527e7e4ab9b5de7681b0a6f16a Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Sat, 28 May 2022 15:06:06 +0200 Subject: [PATCH 2/9] fix compile errors --- traits/src/currency.rs | 4 ++-- traits/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/traits/src/currency.rs b/traits/src/currency.rs index bc774a861..33a90cdce 100644 --- a/traits/src/currency.rs +++ b/traits/src/currency.rs @@ -648,7 +648,7 @@ pub trait OnNewAccount { fn on_new_account(who: &AccountId, currency_id: CurrencyId); } -impl OnDust for () { +impl OnNewAccount for () { fn on_new_account(_: &AccountId, _: CurrencyId) {} } @@ -657,7 +657,7 @@ pub trait OnKilledAccount { fn on_killed_account(who: &AccountId, currency_id: CurrencyId); } -impl OnDust for () { +impl OnKilledAccount for () { fn on_killed_account(_: &AccountId, _: CurrencyId) {} } diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 818d51251..b4bb86870 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -16,7 +16,7 @@ pub use auction::{Auction, AuctionHandler, AuctionInfo, OnNewBidResult}; pub use currency::{ BalanceStatus, BasicCurrency, BasicCurrencyExtended, BasicLockableCurrency, BasicReservableCurrency, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, MultiReservableCurrency, - NamedBasicReservableCurrency, NamedMultiReservableCurrency, OnDust, + NamedBasicReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewAccount, OnKilledAccount, }; pub use data_provider::{DataFeeder, DataProvider, DataProviderExtended}; pub use get_by_key::GetByKey; From 34f50fc5c2399a9820ba69371a809147efb9b354 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Sun, 29 May 2022 17:41:01 +0200 Subject: [PATCH 3/9] remove mint param --- tokens/src/impls.rs | 9 ++++----- tokens/src/lib.rs | 5 ++--- tokens/src/tests_fungibles.rs | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/tokens/src/impls.rs b/tokens/src/impls.rs index f5a53d52e..4c1b00dd8 100644 --- a/tokens/src/impls.rs +++ b/tokens/src/impls.rs @@ -48,11 +48,11 @@ where } } - fn can_deposit(asset: Self::AssetId, who: &AccountId, amount: Self::Balance, mint: bool) -> DepositConsequence { + fn can_deposit(asset: Self::AssetId, who: &AccountId, amount: Self::Balance) -> DepositConsequence { if TestKey::contains(&asset) { - A::can_deposit(who, amount, mint) + A::can_deposit(who, amount) } else { - B::can_deposit(asset, who, amount, mint) + B::can_deposit(asset, who, amount) } } @@ -156,12 +156,11 @@ where ) } - fn can_deposit(who: &AccountId, amount: Self::Balance, mint: bool) -> DepositConsequence { + fn can_deposit(who: &AccountId, amount: Self::Balance) -> DepositConsequence { T::can_deposit( GetCurrencyId::get(), who, C::convert_balance_back(amount, GetCurrencyId::get()), - mint, ) } diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index 13782f2c9..6d731bdf3 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -1677,7 +1677,6 @@ impl fungibles::Inspect for Pallet { asset_id: Self::AssetId, who: &T::AccountId, amount: Self::Balance, - _mint: bool, ) -> DepositConsequence { Self::deposit_consequence(who, asset_id, amount, &Self::accounts(who, asset_id)) } @@ -2169,8 +2168,8 @@ where fn reducible_balance(who: &T::AccountId, keep_alive: bool) -> Self::Balance { as fungibles::Inspect<_>>::reducible_balance(GetCurrencyId::get(), who, keep_alive) } - fn can_deposit(who: &T::AccountId, amount: Self::Balance, mint: bool) -> DepositConsequence { - as fungibles::Inspect<_>>::can_deposit(GetCurrencyId::get(), who, amount, mint) + fn can_deposit(who: &T::AccountId, amount: Self::Balance) -> DepositConsequence { + as fungibles::Inspect<_>>::can_deposit(GetCurrencyId::get(), who, amount) } fn can_withdraw(who: &T::AccountId, amount: Self::Balance) -> WithdrawConsequence { as fungibles::Inspect<_>>::can_withdraw(GetCurrencyId::get(), who, amount) diff --git a/tokens/src/tests_fungibles.rs b/tokens/src/tests_fungibles.rs index 69b862165..2103ce1c6 100644 --- a/tokens/src/tests_fungibles.rs +++ b/tokens/src/tests_fungibles.rs @@ -19,7 +19,7 @@ fn fungibles_inspect_trait_should_work() { >::reducible_balance(DOT, &ALICE, true), 98 ); - assert_ok!(>::can_deposit(DOT, &ALICE, 1, false).into_result()); + assert_ok!(>::can_deposit(DOT, &ALICE, 1).into_result()); assert_ok!(>::can_withdraw(DOT, &ALICE, 1).into_result()); }); } From 89a46d97c4835fcfa8bbd1265297ed554aeac9b5 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Sun, 29 May 2022 17:41:55 +0200 Subject: [PATCH 4/9] rename new OnNewAccount trait to OnNewTokenAccount to avoid confusion --- tokens/src/lib.rs | 10 +++++----- tokens/src/mock.rs | 24 ++++++++++++------------ tokens/src/tests.rs | 23 ++++++++++++++++++++++- traits/src/currency.rs | 16 ++++++++-------- traits/src/lib.rs | 2 +- 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index 6d731bdf3..227a028ff 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -68,7 +68,7 @@ use orml_traits::{ arithmetic::{self, Signed}, currency::TransferAll, BalanceStatus, GetByKey, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, - MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewAccount, OnKilledAccount, + MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewTokenAccount, OnKilledTokenAccount, }; mod imbalances; @@ -217,10 +217,10 @@ pub mod module { type OnDust: OnDust; /// Handler for when an account was created - type OnNewAccount: OnNewAccount; + type OnNewTokenAccount: OnNewTokenAccount; /// Handler for when an account was created - type OnKilledAccount: OnKilledAccount; + type OnKilledTokenAccount: OnKilledTokenAccount; #[pallet::constant] type MaxLocks: Get; @@ -753,11 +753,11 @@ impl Pallet { // Ignore the result, because if it failed then there are remaining consumers, // and the account storage in frame_system shouldn't be reaped. let _ = frame_system::Pallet::::dec_providers(who); - T::OnKilledAccount::on_killed_account(&who, currency_id); + T::OnKilledTokenAccount::on_killed_account_for(&who, currency_id); } else if !existed && exists { // if new, increase account provider frame_system::Pallet::::inc_providers(who); - T::OnNewAccount::on_new_account(&who, currency_id); + T::OnNewTokenAccount::on_new_account_for(&who, currency_id); } if let Some(endowed) = maybe_endowed { diff --git a/tokens/src/mock.rs b/tokens/src/mock.rs index bd799b9c5..90e256249 100644 --- a/tokens/src/mock.rs +++ b/tokens/src/mock.rs @@ -228,32 +228,32 @@ thread_local! { pub struct TrackCreatedAccounts; impl TrackCreatedAccounts { pub fn accounts() -> Vec<(AccountId, CurrencyId)> { - CREATED.clone() + CREATED.with(|accounts| accounts.borrow().clone()) } pub fn reset() { - *CREATED = RefCell::new(vec![]); + CREATED.with(|accounts| { accounts.replace(vec![]); }); } } -impl OnNewAccount for TrackCreatedAccounts { - fn on_new_account(who: &AccountId, currency: CurrencyId) { - CREATED.push((who, CurrencyId)); +impl OnNewTokenAccount for TrackCreatedAccounts { + fn on_new_account_for(who: &AccountId, currency: CurrencyId) { + CREATED.with(|accounts| { accounts.borrow_mut().push((who.clone(), currency)); }); } } pub struct TrackKilledAccounts; impl TrackKilledAccounts { pub fn accounts() -> Vec<(AccountId, CurrencyId)> { - KILLED.clone() + KILLED.with(|accounts| accounts.borrow().clone()) } pub fn reset() { - *KILLED = RefCell::new(vec![]); + KILLED.with(|accounts| { accounts.replace(vec![]); }); } } -impl OnNewAccount for TrackKilledAccounts { - fn on_new_account(who: &AccountId, currency: CurrencyId) { - KILLED.push((who, CurrencyId)); +impl OnKilledTokenAccount for TrackKilledAccounts { + fn on_killed_account_for(who: &AccountId, currency: CurrencyId) { + KILLED.with(|accounts| { accounts.borrow_mut().push((who.clone(), currency)); }); } } @@ -269,8 +269,8 @@ impl Config for Runtime { type WeightInfo = (); type ExistentialDeposits = ExistentialDeposits; type OnDust = TransferDust; - type OnNewAccount = TrackCreatedAccounts; - type OnKilledAccount = TrackKilledAccounts; + type OnNewTokenAccount = TrackCreatedAccounts; + type OnKilledTokenAccount = TrackKilledAccounts; type MaxLocks = ConstU32<2>; type MaxReserves = ConstU32<2>; type ReserveIdentifier = ReserveIdentifier; diff --git a/tokens/src/tests.rs b/tokens/src/tests.rs index 168a4abfa..bafd84286 100644 --- a/tokens/src/tests.rs +++ b/tokens/src/tests.rs @@ -55,6 +55,7 @@ fn transfer_should_work() { Error::::ExistentialDeposit, ); assert_ok!(Tokens::transfer(Some(ALICE).into(), CHARLIE, DOT, 2)); + assert_eq!(TrackCreatedAccounts::accounts(), vec![(CHARLIE, DOT)]); // imply AllowDeath assert!(Accounts::::contains_key(ALICE, DOT)); @@ -131,6 +132,7 @@ fn transfer_all_allow_death_should_work() { assert!(Accounts::::contains_key(ALICE, DOT)); assert_eq!(Tokens::free_balance(DOT, &ALICE), 100); assert_ok!(Tokens::transfer_all(Some(ALICE).into(), CHARLIE, DOT, false)); + assert_eq!(TrackCreatedAccounts::accounts(), vec![(CHARLIE, DOT)]); System::assert_last_event(Event::Tokens(crate::Event::Transfer { currency_id: DOT, from: ALICE, @@ -177,6 +179,7 @@ fn force_transfer_should_work() { amount: 100, })); assert!(!Accounts::::contains_key(ALICE, DOT)); + assert_eq!(TrackKilledAccounts::accounts(), vec![(ALICE, DOT)]); assert_eq!(Tokens::free_balance(DOT, &ALICE), 0); assert_eq!(Tokens::free_balance(DOT, &BOB), 200); }); @@ -514,7 +517,6 @@ fn set_free_balance_should_work() { Tokens::set_free_balance(DOT, &ALICE, 2); assert!(Accounts::::contains_key(ALICE, DOT)); - assert_eq!(TrackCreatedAccounts::accounts(), vec![(ALICE, DOT)]); assert_eq!(Tokens::free_balance(DOT, &ALICE), 2); assert_eq!(Tokens::free_balance(DOT, &DustReceiver::get()), 1); @@ -1147,3 +1149,22 @@ fn exceeding_max_reserves_should_fail() { ); }); } + +#[test] +fn lifecycle_callbacks_are_activated() { + ExtBuilder::default() + .build() + .execute_with(|| { + assert_ok!(Tokens::set_balance(RawOrigin::Root.into(), ALICE, DOT, 200, 0)); + assert_eq!(TrackCreatedAccounts::accounts(), vec![(ALICE, DOT)]); + + assert_ok!(Tokens::set_balance(RawOrigin::Root.into(), ALICE, BTC, 200, 0)); + assert_eq!(TrackCreatedAccounts::accounts(), vec![(ALICE, DOT), (ALICE, BTC)]); + + assert_ok!(Tokens::transfer_all(Some(ALICE).into(), CHARLIE, BTC, false)); + assert_eq!(TrackCreatedAccounts::accounts(), vec![ + (ALICE, DOT), (ALICE, BTC), (CHARLIE, BTC) + ]); + assert_eq!(TrackKilledAccounts::accounts(), vec![(ALICE, BTC)]); + }) +} diff --git a/traits/src/currency.rs b/traits/src/currency.rs index 33a90cdce..6031953da 100644 --- a/traits/src/currency.rs +++ b/traits/src/currency.rs @@ -644,21 +644,21 @@ impl OnDust for } /// Handler for a newly created account -pub trait OnNewAccount { - fn on_new_account(who: &AccountId, currency_id: CurrencyId); +pub trait OnNewTokenAccount { + fn on_new_account_for(who: &AccountId, currency_id: CurrencyId); } -impl OnNewAccount for () { - fn on_new_account(_: &AccountId, _: CurrencyId) {} +impl OnNewTokenAccount for () { + fn on_new_account_for(_: &AccountId, _: CurrencyId) {} } /// Handler for an account that was removed -pub trait OnKilledAccount { - fn on_killed_account(who: &AccountId, currency_id: CurrencyId); +pub trait OnKilledTokenAccount { + fn on_killed_account_for(who: &AccountId, currency_id: CurrencyId); } -impl OnKilledAccount for () { - fn on_killed_account(_: &AccountId, _: CurrencyId) {} +impl OnKilledTokenAccount for () { + fn on_killed_account_for(_: &AccountId, _: CurrencyId) {} } pub trait TransferAll { diff --git a/traits/src/lib.rs b/traits/src/lib.rs index b4bb86870..6da422e39 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -16,7 +16,7 @@ pub use auction::{Auction, AuctionHandler, AuctionInfo, OnNewBidResult}; pub use currency::{ BalanceStatus, BasicCurrency, BasicCurrencyExtended, BasicLockableCurrency, BasicReservableCurrency, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, MultiReservableCurrency, - NamedBasicReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewAccount, OnKilledAccount, + NamedBasicReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewTokenAccount, OnKilledTokenAccount, }; pub use data_provider::{DataFeeder, DataProvider, DataProviderExtended}; pub use get_by_key::GetByKey; From 5554b66224d0a35ec239bc73417d34dff8d35ed0 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Mon, 30 May 2022 09:56:38 +0200 Subject: [PATCH 5/9] use Happened trait for lifecycle callbacks instead --- tokens/src/lib.rs | 10 +++++----- tokens/src/mock.rs | 12 ++++++------ traits/src/currency.rs | 18 ------------------ traits/src/lib.rs | 2 +- 4 files changed, 12 insertions(+), 30 deletions(-) diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index 227a028ff..19e527070 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -68,7 +68,7 @@ use orml_traits::{ arithmetic::{self, Signed}, currency::TransferAll, BalanceStatus, GetByKey, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, - MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewTokenAccount, OnKilledTokenAccount, + MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, Happened, }; mod imbalances; @@ -217,10 +217,10 @@ pub mod module { type OnDust: OnDust; /// Handler for when an account was created - type OnNewTokenAccount: OnNewTokenAccount; + type OnNewTokenAccount: Happened<(Self::AccountId, Self::CurrencyId)>; /// Handler for when an account was created - type OnKilledTokenAccount: OnKilledTokenAccount; + type OnKilledTokenAccount: Happened<(Self::AccountId, Self::CurrencyId)>; #[pallet::constant] type MaxLocks: Get; @@ -753,11 +753,11 @@ impl Pallet { // Ignore the result, because if it failed then there are remaining consumers, // and the account storage in frame_system shouldn't be reaped. let _ = frame_system::Pallet::::dec_providers(who); - T::OnKilledTokenAccount::on_killed_account_for(&who, currency_id); + T::OnKilledTokenAccount::happened(&(who.clone(), currency_id)); } else if !existed && exists { // if new, increase account provider frame_system::Pallet::::inc_providers(who); - T::OnNewTokenAccount::on_new_account_for(&who, currency_id); + T::OnNewTokenAccount::happened(&(who.clone(), currency_id)); } if let Some(endowed) = maybe_endowed { diff --git a/tokens/src/mock.rs b/tokens/src/mock.rs index 90e256249..fb98fe526 100644 --- a/tokens/src/mock.rs +++ b/tokens/src/mock.rs @@ -235,9 +235,9 @@ impl TrackCreatedAccounts { CREATED.with(|accounts| { accounts.replace(vec![]); }); } } -impl OnNewTokenAccount for TrackCreatedAccounts { - fn on_new_account_for(who: &AccountId, currency: CurrencyId) { - CREATED.with(|accounts| { accounts.borrow_mut().push((who.clone(), currency)); }); +impl Happened<(AccountId, CurrencyId)> for TrackCreatedAccounts { + fn happened((who, currency): &(AccountId, CurrencyId)) { + CREATED.with(|accounts| { accounts.borrow_mut().push((who.clone(), *currency)); }); } } @@ -251,9 +251,9 @@ impl TrackKilledAccounts { KILLED.with(|accounts| { accounts.replace(vec![]); }); } } -impl OnKilledTokenAccount for TrackKilledAccounts { - fn on_killed_account_for(who: &AccountId, currency: CurrencyId) { - KILLED.with(|accounts| { accounts.borrow_mut().push((who.clone(), currency)); }); +impl Happened<(AccountId, CurrencyId)> for TrackKilledAccounts { + fn happened((who, currency): &(AccountId, CurrencyId)) { + KILLED.with(|accounts| { accounts.borrow_mut().push((who.clone(), *currency)); }); } } diff --git a/traits/src/currency.rs b/traits/src/currency.rs index 6031953da..cd0c9d71e 100644 --- a/traits/src/currency.rs +++ b/traits/src/currency.rs @@ -643,24 +643,6 @@ impl OnDust for fn on_dust(_: &AccountId, _: CurrencyId, _: Balance) {} } -/// Handler for a newly created account -pub trait OnNewTokenAccount { - fn on_new_account_for(who: &AccountId, currency_id: CurrencyId); -} - -impl OnNewTokenAccount for () { - fn on_new_account_for(_: &AccountId, _: CurrencyId) {} -} - -/// Handler for an account that was removed -pub trait OnKilledTokenAccount { - fn on_killed_account_for(who: &AccountId, currency_id: CurrencyId); -} - -impl OnKilledTokenAccount for () { - fn on_killed_account_for(_: &AccountId, _: CurrencyId) {} -} - pub trait TransferAll { fn transfer_all(source: &AccountId, dest: &AccountId) -> DispatchResult; } diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 6da422e39..818d51251 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -16,7 +16,7 @@ pub use auction::{Auction, AuctionHandler, AuctionInfo, OnNewBidResult}; pub use currency::{ BalanceStatus, BasicCurrency, BasicCurrencyExtended, BasicLockableCurrency, BasicReservableCurrency, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, MultiReservableCurrency, - NamedBasicReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewTokenAccount, OnKilledTokenAccount, + NamedBasicReservableCurrency, NamedMultiReservableCurrency, OnDust, }; pub use data_provider::{DataFeeder, DataProvider, DataProviderExtended}; pub use get_by_key::GetByKey; From 6c47fd1ab921cc16830fac89e0561e3e20cffd25 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Mon, 30 May 2022 14:25:55 +0200 Subject: [PATCH 6/9] cargo fmt --- tokens/src/lib.rs | 10 +++------- tokens/src/mock.rs | 16 ++++++++++++---- tokens/src/tests.rs | 29 ++++++++++++++--------------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index 19e527070..e5c8cbc40 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -67,8 +67,8 @@ use sp_std::{cmp, convert::Infallible, marker, prelude::*, vec::Vec}; use orml_traits::{ arithmetic::{self, Signed}, currency::TransferAll, - BalanceStatus, GetByKey, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, - MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, Happened, + BalanceStatus, GetByKey, Happened, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, + MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, }; mod imbalances; @@ -1673,11 +1673,7 @@ impl fungibles::Inspect for Pallet { } } - fn can_deposit( - asset_id: Self::AssetId, - who: &T::AccountId, - amount: Self::Balance, - ) -> DepositConsequence { + fn can_deposit(asset_id: Self::AssetId, who: &T::AccountId, amount: Self::Balance) -> DepositConsequence { Self::deposit_consequence(who, asset_id, amount, &Self::accounts(who, asset_id)) } diff --git a/tokens/src/mock.rs b/tokens/src/mock.rs index fb98fe526..2deeaa52a 100644 --- a/tokens/src/mock.rs +++ b/tokens/src/mock.rs @@ -232,12 +232,16 @@ impl TrackCreatedAccounts { } pub fn reset() { - CREATED.with(|accounts| { accounts.replace(vec![]); }); + CREATED.with(|accounts| { + accounts.replace(vec![]); + }); } } impl Happened<(AccountId, CurrencyId)> for TrackCreatedAccounts { fn happened((who, currency): &(AccountId, CurrencyId)) { - CREATED.with(|accounts| { accounts.borrow_mut().push((who.clone(), *currency)); }); + CREATED.with(|accounts| { + accounts.borrow_mut().push((who.clone(), *currency)); + }); } } @@ -248,12 +252,16 @@ impl TrackKilledAccounts { } pub fn reset() { - KILLED.with(|accounts| { accounts.replace(vec![]); }); + KILLED.with(|accounts| { + accounts.replace(vec![]); + }); } } impl Happened<(AccountId, CurrencyId)> for TrackKilledAccounts { fn happened((who, currency): &(AccountId, CurrencyId)) { - KILLED.with(|accounts| { accounts.borrow_mut().push((who.clone(), *currency)); }); + KILLED.with(|accounts| { + accounts.borrow_mut().push((who.clone(), *currency)); + }); } } diff --git a/tokens/src/tests.rs b/tokens/src/tests.rs index bafd84286..02b923293 100644 --- a/tokens/src/tests.rs +++ b/tokens/src/tests.rs @@ -1152,19 +1152,18 @@ fn exceeding_max_reserves_should_fail() { #[test] fn lifecycle_callbacks_are_activated() { - ExtBuilder::default() - .build() - .execute_with(|| { - assert_ok!(Tokens::set_balance(RawOrigin::Root.into(), ALICE, DOT, 200, 0)); - assert_eq!(TrackCreatedAccounts::accounts(), vec![(ALICE, DOT)]); - - assert_ok!(Tokens::set_balance(RawOrigin::Root.into(), ALICE, BTC, 200, 0)); - assert_eq!(TrackCreatedAccounts::accounts(), vec![(ALICE, DOT), (ALICE, BTC)]); - - assert_ok!(Tokens::transfer_all(Some(ALICE).into(), CHARLIE, BTC, false)); - assert_eq!(TrackCreatedAccounts::accounts(), vec![ - (ALICE, DOT), (ALICE, BTC), (CHARLIE, BTC) - ]); - assert_eq!(TrackKilledAccounts::accounts(), vec![(ALICE, BTC)]); - }) + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Tokens::set_balance(RawOrigin::Root.into(), ALICE, DOT, 200, 0)); + assert_eq!(TrackCreatedAccounts::accounts(), vec![(ALICE, DOT)]); + + assert_ok!(Tokens::set_balance(RawOrigin::Root.into(), ALICE, BTC, 200, 0)); + assert_eq!(TrackCreatedAccounts::accounts(), vec![(ALICE, DOT), (ALICE, BTC)]); + + assert_ok!(Tokens::transfer_all(Some(ALICE).into(), CHARLIE, BTC, false)); + assert_eq!( + TrackCreatedAccounts::accounts(), + vec![(ALICE, DOT), (ALICE, BTC), (CHARLIE, BTC)] + ); + assert_eq!(TrackKilledAccounts::accounts(), vec![(ALICE, BTC)]); + }) } From bf3a978fcd1bb2797a3ff792d80c49523037595c Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Mon, 6 Jun 2022 11:38:07 +0200 Subject: [PATCH 7/9] Revert "remove mint param" This reverts commit 34f50fc5c2399a9820ba69371a809147efb9b354. --- tokens/src/impls.rs | 9 +++++---- tokens/src/lib.rs | 11 ++++++++--- tokens/src/tests_fungibles.rs | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/tokens/src/impls.rs b/tokens/src/impls.rs index 4c1b00dd8..f5a53d52e 100644 --- a/tokens/src/impls.rs +++ b/tokens/src/impls.rs @@ -48,11 +48,11 @@ where } } - fn can_deposit(asset: Self::AssetId, who: &AccountId, amount: Self::Balance) -> DepositConsequence { + fn can_deposit(asset: Self::AssetId, who: &AccountId, amount: Self::Balance, mint: bool) -> DepositConsequence { if TestKey::contains(&asset) { - A::can_deposit(who, amount) + A::can_deposit(who, amount, mint) } else { - B::can_deposit(asset, who, amount) + B::can_deposit(asset, who, amount, mint) } } @@ -156,11 +156,12 @@ where ) } - fn can_deposit(who: &AccountId, amount: Self::Balance) -> DepositConsequence { + fn can_deposit(who: &AccountId, amount: Self::Balance, mint: bool) -> DepositConsequence { T::can_deposit( GetCurrencyId::get(), who, C::convert_balance_back(amount, GetCurrencyId::get()), + mint, ) } diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index e5c8cbc40..06fefa10c 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -1673,7 +1673,12 @@ impl fungibles::Inspect for Pallet { } } - fn can_deposit(asset_id: Self::AssetId, who: &T::AccountId, amount: Self::Balance) -> DepositConsequence { + fn can_deposit( + asset_id: Self::AssetId, + who: &T::AccountId, + amount: Self::Balance, + _mint: bool, + ) -> DepositConsequence { Self::deposit_consequence(who, asset_id, amount, &Self::accounts(who, asset_id)) } @@ -2164,8 +2169,8 @@ where fn reducible_balance(who: &T::AccountId, keep_alive: bool) -> Self::Balance { as fungibles::Inspect<_>>::reducible_balance(GetCurrencyId::get(), who, keep_alive) } - fn can_deposit(who: &T::AccountId, amount: Self::Balance) -> DepositConsequence { - as fungibles::Inspect<_>>::can_deposit(GetCurrencyId::get(), who, amount) + fn can_deposit(who: &T::AccountId, amount: Self::Balance, mint: bool) -> DepositConsequence { + as fungibles::Inspect<_>>::can_deposit(GetCurrencyId::get(), who, amount, mint) } fn can_withdraw(who: &T::AccountId, amount: Self::Balance) -> WithdrawConsequence { as fungibles::Inspect<_>>::can_withdraw(GetCurrencyId::get(), who, amount) diff --git a/tokens/src/tests_fungibles.rs b/tokens/src/tests_fungibles.rs index 2103ce1c6..69b862165 100644 --- a/tokens/src/tests_fungibles.rs +++ b/tokens/src/tests_fungibles.rs @@ -19,7 +19,7 @@ fn fungibles_inspect_trait_should_work() { >::reducible_balance(DOT, &ALICE, true), 98 ); - assert_ok!(>::can_deposit(DOT, &ALICE, 1).into_result()); + assert_ok!(>::can_deposit(DOT, &ALICE, 1, false).into_result()); assert_ok!(>::can_withdraw(DOT, &ALICE, 1).into_result()); }); } From c6217d9b62740550a5f69c3e8a9413547b101de5 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Mon, 6 Jun 2022 14:27:18 +0200 Subject: [PATCH 8/9] add dummy impl for OnNewTokenAccount in mocks --- asset-registry/src/mock/para.rs | 2 ++ currencies/src/mock.rs | 2 ++ payments/src/mock.rs | 2 ++ xtokens/src/mock/para.rs | 2 ++ xtokens/src/mock/para_relative_view.rs | 2 ++ 5 files changed, 10 insertions(+) diff --git a/asset-registry/src/mock/para.rs b/asset-registry/src/mock/para.rs index 9a41b838d..b815fa673 100644 --- a/asset-registry/src/mock/para.rs +++ b/asset-registry/src/mock/para.rs @@ -96,6 +96,8 @@ impl orml_tokens::Config for Runtime { type MaxReserves = (); type MaxLocks = ConstU32<50>; type DustRemovalWhitelist = Nothing; + type OnNewTokenAccount = (); + type OnKilledTokenAccount = (); } #[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, Debug)] diff --git a/currencies/src/mock.rs b/currencies/src/mock.rs index 1980b388e..cae592764 100644 --- a/currencies/src/mock.rs +++ b/currencies/src/mock.rs @@ -85,6 +85,8 @@ impl orml_tokens::Config for Runtime { type MaxReserves = ConstU32<100_000>; type ReserveIdentifier = ReserveIdentifier; type DustRemovalWhitelist = Nothing; + type OnNewTokenAccount = (); + type OnKilledTokenAccount = (); } pub const NATIVE_CURRENCY_ID: CurrencyId = 1; diff --git a/payments/src/mock.rs b/payments/src/mock.rs index e26626e89..f8aecb40b 100644 --- a/payments/src/mock.rs +++ b/payments/src/mock.rs @@ -104,6 +104,8 @@ impl orml_tokens::Config for Test { type DustRemovalWhitelist = MockDustRemovalWhitelist; type MaxReserves = ConstU32<2>; type ReserveIdentifier = ReserveIdentifier; + type OnNewTokenAccount = (); + type OnKilledTokenAccount = (); } pub struct MockDisputeResolver; diff --git a/xtokens/src/mock/para.rs b/xtokens/src/mock/para.rs index b30ccb891..a75fd6611 100644 --- a/xtokens/src/mock/para.rs +++ b/xtokens/src/mock/para.rs @@ -87,6 +87,8 @@ impl orml_tokens::Config for Runtime { type MaxReserves = ConstU32<50>; type ReserveIdentifier = [u8; 8]; type DustRemovalWhitelist = Everything; + type OnNewTokenAccount = (); + type OnKilledTokenAccount = (); } parameter_types! { diff --git a/xtokens/src/mock/para_relative_view.rs b/xtokens/src/mock/para_relative_view.rs index ed4abd521..11090d52e 100644 --- a/xtokens/src/mock/para_relative_view.rs +++ b/xtokens/src/mock/para_relative_view.rs @@ -90,6 +90,8 @@ impl orml_tokens::Config for Runtime { type MaxReserves = ConstU32<50>; type ReserveIdentifier = [u8; 8]; type DustRemovalWhitelist = Everything; + type OnNewTokenAccount = (); + type OnKilledTokenAccount = (); } parameter_types! { From 451759d2645032e45829a62af2b990074ec613ab Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 7 Jun 2022 09:52:00 +1200 Subject: [PATCH 9/9] cargo fmt --- asset-registry/src/mock/para.rs | 2 +- currencies/src/mock.rs | 2 +- payments/src/mock.rs | 2 +- xtokens/src/mock/para.rs | 2 +- xtokens/src/mock/para_relative_view.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/asset-registry/src/mock/para.rs b/asset-registry/src/mock/para.rs index b815fa673..c7074fd5a 100644 --- a/asset-registry/src/mock/para.rs +++ b/asset-registry/src/mock/para.rs @@ -97,7 +97,7 @@ impl orml_tokens::Config for Runtime { type MaxLocks = ConstU32<50>; type DustRemovalWhitelist = Nothing; type OnNewTokenAccount = (); - type OnKilledTokenAccount = (); + type OnKilledTokenAccount = (); } #[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, Debug)] diff --git a/currencies/src/mock.rs b/currencies/src/mock.rs index cae592764..1ef3cf06f 100644 --- a/currencies/src/mock.rs +++ b/currencies/src/mock.rs @@ -86,7 +86,7 @@ impl orml_tokens::Config for Runtime { type ReserveIdentifier = ReserveIdentifier; type DustRemovalWhitelist = Nothing; type OnNewTokenAccount = (); - type OnKilledTokenAccount = (); + type OnKilledTokenAccount = (); } pub const NATIVE_CURRENCY_ID: CurrencyId = 1; diff --git a/payments/src/mock.rs b/payments/src/mock.rs index f8aecb40b..b31c9283e 100644 --- a/payments/src/mock.rs +++ b/payments/src/mock.rs @@ -105,7 +105,7 @@ impl orml_tokens::Config for Test { type MaxReserves = ConstU32<2>; type ReserveIdentifier = ReserveIdentifier; type OnNewTokenAccount = (); - type OnKilledTokenAccount = (); + type OnKilledTokenAccount = (); } pub struct MockDisputeResolver; diff --git a/xtokens/src/mock/para.rs b/xtokens/src/mock/para.rs index a75fd6611..2dde03460 100644 --- a/xtokens/src/mock/para.rs +++ b/xtokens/src/mock/para.rs @@ -88,7 +88,7 @@ impl orml_tokens::Config for Runtime { type ReserveIdentifier = [u8; 8]; type DustRemovalWhitelist = Everything; type OnNewTokenAccount = (); - type OnKilledTokenAccount = (); + type OnKilledTokenAccount = (); } parameter_types! { diff --git a/xtokens/src/mock/para_relative_view.rs b/xtokens/src/mock/para_relative_view.rs index 11090d52e..f6786d09f 100644 --- a/xtokens/src/mock/para_relative_view.rs +++ b/xtokens/src/mock/para_relative_view.rs @@ -91,7 +91,7 @@ impl orml_tokens::Config for Runtime { type ReserveIdentifier = [u8; 8]; type DustRemovalWhitelist = Everything; type OnNewTokenAccount = (); - type OnKilledTokenAccount = (); + type OnKilledTokenAccount = (); } parameter_types! {