From 61ecae25abfab7081a92ed498d51b22eb4e560a6 Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Fri, 5 Jan 2024 16:34:22 +0100 Subject: [PATCH 1/2] reduce boilerplate with macro --- .../src/traits/tokens/fungible/item_of.rs | 388 +++++++----------- 1 file changed, 151 insertions(+), 237 deletions(-) diff --git a/substrate/frame/support/src/traits/tokens/fungible/item_of.rs b/substrate/frame/support/src/traits/tokens/fungible/item_of.rs index fe252c6b0893..4aa79a36e8ab 100644 --- a/substrate/frame/support/src/traits/tokens/fungible/item_of.rs +++ b/substrate/frame/support/src/traits/tokens/fungible/item_of.rs @@ -28,6 +28,23 @@ use crate::traits::{ use sp_core::Get; use sp_runtime::{DispatchError, DispatchResult}; +// Redirects `fungible` function to the `fungibles` equivalent with the proper AssetId. +macro_rules! redirect { + ( $( + fn $fn_name:ident ( + $( + $arg_name:ident : $arg_ty:ty + ),* $(,)? + ) $(-> $fn_out:ty)?; + )+) => { + $( + fn $fn_name($($arg_name:$arg_ty),*) $(-> $fn_out)? { + F::$fn_name(A::get(), $($arg_name),*) + } + )+ + }; +} + /// Convert a `fungibles` trait implementation into a `fungible` trait implementation by identifying /// a single item. pub struct ItemOf< @@ -43,38 +60,28 @@ impl< > Inspect for ItemOf { type Balance = >::Balance; - fn total_issuance() -> Self::Balance { - >::total_issuance(A::get()) - } - fn active_issuance() -> Self::Balance { - >::active_issuance(A::get()) - } - fn minimum_balance() -> Self::Balance { - >::minimum_balance(A::get()) - } - fn balance(who: &AccountId) -> Self::Balance { - >::balance(A::get(), who) - } - fn total_balance(who: &AccountId) -> Self::Balance { - >::total_balance(A::get(), who) - } - fn reducible_balance( - who: &AccountId, - preservation: Preservation, - force: Fortitude, - ) -> Self::Balance { - >::reducible_balance(A::get(), who, preservation, force) - } - fn can_deposit( - who: &AccountId, - amount: Self::Balance, - provenance: Provenance, - ) -> DepositConsequence { - >::can_deposit(A::get(), who, amount, provenance) - } - fn can_withdraw(who: &AccountId, amount: Self::Balance) -> WithdrawConsequence { - >::can_withdraw(A::get(), who, amount) - } + + redirect!( + fn total_issuance() -> Self::Balance; + fn active_issuance() -> Self::Balance; + fn minimum_balance() -> Self::Balance; + fn balance(who: &AccountId) -> Self::Balance; + fn total_balance(who: &AccountId) -> Self::Balance; + fn reducible_balance( + who: &AccountId, + preservation: Preservation, + force: Fortitude, + ) -> Self::Balance; + fn can_deposit( + who: &AccountId, + amount: Self::Balance, + provenance: Provenance, + ) -> DepositConsequence; + fn can_withdraw( + who: &AccountId, + amount: Self::Balance, + ) -> WithdrawConsequence; + ); } impl< @@ -85,25 +92,13 @@ impl< { type Reason = F::Reason; - fn reducible_total_balance_on_hold(who: &AccountId, force: Fortitude) -> Self::Balance { - >::reducible_total_balance_on_hold( - A::get(), - who, - force, - ) - } - fn hold_available(reason: &Self::Reason, who: &AccountId) -> bool { - >::hold_available(A::get(), reason, who) - } - fn total_balance_on_hold(who: &AccountId) -> Self::Balance { - >::total_balance_on_hold(A::get(), who) - } - fn balance_on_hold(reason: &Self::Reason, who: &AccountId) -> Self::Balance { - >::balance_on_hold(A::get(), reason, who) - } - fn can_hold(reason: &Self::Reason, who: &AccountId, amount: Self::Balance) -> bool { - >::can_hold(A::get(), reason, who, amount) - } + redirect!( + fn reducible_total_balance_on_hold(who: &AccountId, force: Fortitude) -> Self::Balance; + fn hold_available(reason: &Self::Reason, who: &AccountId) -> bool; + fn total_balance_on_hold(who: &AccountId) -> Self::Balance; + fn balance_on_hold(reason: &Self::Reason, who: &AccountId) -> Self::Balance; + fn can_hold(reason: &Self::Reason, who: &AccountId, amount: Self::Balance) -> bool; + ); } impl< @@ -113,15 +108,12 @@ impl< > InspectFreeze for ItemOf { type Id = F::Id; - fn balance_frozen(id: &Self::Id, who: &AccountId) -> Self::Balance { - >::balance_frozen(A::get(), id, who) - } - fn balance_freezable(who: &AccountId) -> Self::Balance { - >::balance_freezable(A::get(), who) - } - fn can_freeze(id: &Self::Id, who: &AccountId) -> bool { - >::can_freeze(A::get(), id, who) - } + + redirect!( + fn balance_frozen(id: &Self::Id, who: &AccountId) -> Self::Balance; + fn balance_freezable(who: &AccountId) -> Self::Balance; + fn can_freeze(id: &Self::Id, who: &AccountId) -> bool; + ); } impl< @@ -136,38 +128,26 @@ impl< { >::handle_dust(fungibles::Dust(A::get(), dust.0)) } - fn write_balance( - who: &AccountId, - amount: Self::Balance, - ) -> Result, DispatchError> { - >::write_balance(A::get(), who, amount) - } - fn set_total_issuance(amount: Self::Balance) -> () { - >::set_total_issuance(A::get(), amount) - } - fn decrease_balance( - who: &AccountId, - amount: Self::Balance, - precision: Precision, - preservation: Preservation, - force: Fortitude, - ) -> Result { - >::decrease_balance( - A::get(), - who, - amount, - precision, - preservation, - force, - ) - } - fn increase_balance( - who: &AccountId, - amount: Self::Balance, - precision: Precision, - ) -> Result { - >::increase_balance(A::get(), who, amount, precision) - } + + redirect!( + fn write_balance( + who: &AccountId, + amount: Self::Balance, + ) -> Result, DispatchError>; + fn set_total_issuance(amount: Self::Balance) -> (); + fn decrease_balance( + who: &AccountId, + amount: Self::Balance, + precision: Precision, + preservation: Preservation, + force: Fortitude, + ) -> Result; + fn increase_balance( + who: &AccountId, + amount: Self::Balance, + precision: Precision, + ) -> Result; + ); } impl< @@ -176,46 +156,25 @@ impl< AccountId, > UnbalancedHold for ItemOf { - fn set_balance_on_hold( - reason: &Self::Reason, - who: &AccountId, - amount: Self::Balance, - ) -> DispatchResult { - >::set_balance_on_hold( - A::get(), - reason, - who, - amount, - ) - } - fn decrease_balance_on_hold( - reason: &Self::Reason, - who: &AccountId, - amount: Self::Balance, - precision: Precision, - ) -> Result { - >::decrease_balance_on_hold( - A::get(), - reason, - who, - amount, - precision, - ) - } - fn increase_balance_on_hold( - reason: &Self::Reason, - who: &AccountId, - amount: Self::Balance, - precision: Precision, - ) -> Result { - >::increase_balance_on_hold( - A::get(), - reason, - who, - amount, - precision, - ) - } + redirect!( + fn set_balance_on_hold( + reason: &Self::Reason, + who: &AccountId, + amount: Self::Balance, + ) -> DispatchResult; + fn decrease_balance_on_hold( + reason: &Self::Reason, + who: &AccountId, + amount: Self::Balance, + precision: Precision, + ) -> Result; + fn increase_balance_on_hold( + reason: &Self::Reason, + who: &AccountId, + amount: Self::Balance, + precision: Precision, + ) -> Result; + ); } impl< @@ -224,35 +183,24 @@ impl< AccountId: Eq, > Mutate for ItemOf { - fn mint_into(who: &AccountId, amount: Self::Balance) -> Result { - >::mint_into(A::get(), who, amount) - } - fn burn_from( - who: &AccountId, - amount: Self::Balance, - precision: Precision, - force: Fortitude, - ) -> Result { - >::burn_from(A::get(), who, amount, precision, force) - } - fn shelve(who: &AccountId, amount: Self::Balance) -> Result { - >::shelve(A::get(), who, amount) - } - fn restore(who: &AccountId, amount: Self::Balance) -> Result { - >::restore(A::get(), who, amount) - } - fn transfer( - source: &AccountId, - dest: &AccountId, - amount: Self::Balance, - preservation: Preservation, - ) -> Result { - >::transfer(A::get(), source, dest, amount, preservation) - } - - fn set_balance(who: &AccountId, amount: Self::Balance) -> Self::Balance { - >::set_balance(A::get(), who, amount) - } + redirect!( + fn mint_into(who: &AccountId, amount: Self::Balance) -> Result; + fn burn_from( + who: &AccountId, + amount: Self::Balance, + precision: Precision, + force: Fortitude, + ) -> Result; + fn shelve(who: &AccountId, amount: Self::Balance) -> Result; + fn restore(who: &AccountId, amount: Self::Balance) -> Result; + fn transfer( + source: &AccountId, + dest: &AccountId, + amount: Self::Balance, + preservation: Preservation, + ) -> Result; + fn set_balance(who: &AccountId, amount: Self::Balance) -> Self::Balance; + ); } impl< @@ -261,73 +209,41 @@ impl< AccountId, > MutateHold for ItemOf { - fn hold(reason: &Self::Reason, who: &AccountId, amount: Self::Balance) -> DispatchResult { - >::hold(A::get(), reason, who, amount) - } - fn release( - reason: &Self::Reason, - who: &AccountId, - amount: Self::Balance, - precision: Precision, - ) -> Result { - >::release(A::get(), reason, who, amount, precision) - } - fn burn_held( - reason: &Self::Reason, - who: &AccountId, - amount: Self::Balance, - precision: Precision, - force: Fortitude, - ) -> Result { - >::burn_held( - A::get(), - reason, - who, - amount, - precision, - force, - ) - } - fn transfer_on_hold( - reason: &Self::Reason, - source: &AccountId, - dest: &AccountId, - amount: Self::Balance, - precision: Precision, - mode: Restriction, - force: Fortitude, - ) -> Result { - >::transfer_on_hold( - A::get(), - reason, - source, - dest, - amount, - precision, - mode, - force, - ) - } - fn transfer_and_hold( - reason: &Self::Reason, - source: &AccountId, - dest: &AccountId, - amount: Self::Balance, - precision: Precision, - preservation: Preservation, - force: Fortitude, - ) -> Result { - >::transfer_and_hold( - A::get(), - reason, - source, - dest, - amount, - precision, - preservation, - force, - ) - } + redirect!( + fn hold(reason: &Self::Reason, who: &AccountId, amount: Self::Balance) -> DispatchResult; + fn release( + reason: &Self::Reason, + who: &AccountId, + amount: Self::Balance, + precision: Precision, + ) -> Result; + fn burn_held( + reason: &Self::Reason, + who: &AccountId, + amount: Self::Balance, + precision: Precision, + force: Fortitude, + ) -> Result; + fn transfer_on_hold( + reason: &Self::Reason, + source: &AccountId, + dest: &AccountId, + amount: Self::Balance, + precision: Precision, + mode: Restriction, + force: Fortitude, + ) -> Result; + fn transfer_and_hold( + reason: &Self::Reason, + source: &AccountId, + dest: &AccountId, + amount: Self::Balance, + precision: Precision, + preservation: Preservation, + force: Fortitude, + ) -> Result; + ); + } impl< @@ -336,15 +252,12 @@ impl< AccountId, > MutateFreeze for ItemOf { - fn set_freeze(id: &Self::Id, who: &AccountId, amount: Self::Balance) -> DispatchResult { - >::set_freeze(A::get(), id, who, amount) - } - fn extend_freeze(id: &Self::Id, who: &AccountId, amount: Self::Balance) -> DispatchResult { - >::extend_freeze(A::get(), id, who, amount) - } - fn thaw(id: &Self::Id, who: &AccountId) -> DispatchResult { - >::thaw(A::get(), id, who) - } + redirect!( + fn set_freeze(id: &Self::Id, who: &AccountId, amount: Self::Balance) -> DispatchResult; + fn extend_freeze(id: &Self::Id, who: &AccountId, amount: Self::Balance) -> DispatchResult; + fn thaw(id: &Self::Id, who: &AccountId) -> DispatchResult; + ); + } pub struct ConvertImbalanceDropHandler( @@ -377,6 +290,7 @@ impl< ConvertImbalanceDropHandler; type OnDropCredit = ConvertImbalanceDropHandler; + fn deposit( who: &AccountId, value: Self::Balance, From daef713c4820b7a1e7c738aef8c288276bbecef9 Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Fri, 5 Jan 2024 16:44:44 +0100 Subject: [PATCH 2/2] Add missing redirects to overridable provided methods --- .../src/traits/tokens/fungible/item_of.rs | 68 ++++++++++++++++++- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/substrate/frame/support/src/traits/tokens/fungible/item_of.rs b/substrate/frame/support/src/traits/tokens/fungible/item_of.rs index 4aa79a36e8ab..38a69f4c6f12 100644 --- a/substrate/frame/support/src/traits/tokens/fungible/item_of.rs +++ b/substrate/frame/support/src/traits/tokens/fungible/item_of.rs @@ -98,6 +98,11 @@ impl< fn total_balance_on_hold(who: &AccountId) -> Self::Balance; fn balance_on_hold(reason: &Self::Reason, who: &AccountId) -> Self::Balance; fn can_hold(reason: &Self::Reason, who: &AccountId, amount: Self::Balance) -> bool; + fn ensure_can_hold( + reason: &Self::Reason, + who: &AccountId, + amount: Self::Balance, + ) -> DispatchResult; ); } @@ -147,6 +152,9 @@ impl< amount: Self::Balance, precision: Precision, ) -> Result; + fn handle_raw_dust(amount: Self::Balance); + fn deactivate(amount: Self::Balance); + fn reactivate(amount: Self::Balance); ); } @@ -184,7 +192,10 @@ impl< > Mutate for ItemOf { redirect!( - fn mint_into(who: &AccountId, amount: Self::Balance) -> Result; + fn mint_into( + who: &AccountId, + amount: Self::Balance, + ) -> Result; fn burn_from( who: &AccountId, amount: Self::Balance, @@ -200,6 +211,11 @@ impl< preservation: Preservation, ) -> Result; fn set_balance(who: &AccountId, amount: Self::Balance) -> Self::Balance; + fn done_mint_into(who: &AccountId, amount: Self::Balance); + fn done_burn_from(who: &AccountId, amount: Self::Balance); + fn done_shelve(who: &AccountId, amount: Self::Balance); + fn done_restore(who: &AccountId, amount: Self::Balance); + fn done_transfer(source: &AccountId, dest: &AccountId, amount: Self::Balance); ); } @@ -242,8 +258,28 @@ impl< preservation: Preservation, force: Fortitude, ) -> Result; + fn burn_all_held( + reason: &Self::Reason, + who: &AccountId, + precision: Precision, + force: Fortitude, + ) -> Result; + fn done_hold(reason: &Self::Reason, who: &AccountId, amount: Self::Balance); + fn done_release(reason: &Self::Reason, who: &AccountId, amount: Self::Balance); + fn done_burn_held(reason: &Self::Reason, who: &AccountId, amount: Self::Balance); + fn done_transfer_on_hold( + reason: &Self::Reason, + source: &AccountId, + dest: &AccountId, + amount: Self::Balance, + ); + fn done_transfer_and_hold( + reason: &Self::Reason, + source: &AccountId, + dest: &AccountId, + transferred: Self::Balance, + ); ); - } impl< @@ -256,8 +292,23 @@ impl< fn set_freeze(id: &Self::Id, who: &AccountId, amount: Self::Balance) -> DispatchResult; fn extend_freeze(id: &Self::Id, who: &AccountId, amount: Self::Balance) -> DispatchResult; fn thaw(id: &Self::Id, who: &AccountId) -> DispatchResult; + fn set_frozen( + id: &Self::Id, + who: &AccountId, + amount: Self::Balance, + fortitude: Fortitude, + ) -> DispatchResult; + fn ensure_frozen( + id: &Self::Id, + who: &AccountId, + amount: Self::Balance, + fortitude: Fortitude, + ) -> DispatchResult; + fn decrease_frozen(id: &Self::Id, who: &AccountId, amount: Self::Balance) + -> DispatchResult; + fn increase_frozen(id: &Self::Id, who: &AccountId, amount: Self::Balance) + -> DispatchResult; ); - } pub struct ConvertImbalanceDropHandler( @@ -347,6 +398,13 @@ impl< ) .map(imbalance::from_fungibles) } + + redirect!( + fn done_rescind(amount: Self::Balance); + fn done_issue(amount: Self::Balance); + fn done_deposit(who: &AccountId, amount: Self::Balance); + fn done_withdraw(who: &AccountId, amount: Self::Balance); + ); } impl< @@ -364,6 +422,10 @@ impl< >::slash(A::get(), reason, who, amount); (imbalance::from_fungibles(credit), amount) } + + redirect!( + fn done_slash(reason: &Self::Reason, who: &AccountId, amount: Self::Balance); + ); } #[test]