From 508496c7b5e500d9ef02fc7619d74cc5b3d6dbdd Mon Sep 17 00:00:00 2001 From: NachoPal Date: Fri, 10 Nov 2023 15:16:33 +0100 Subject: [PATCH 1/7] add ConsiderationFromLegacy --- .../support/src/traits/tokens/fungible/mod.rs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/substrate/frame/support/src/traits/tokens/fungible/mod.rs b/substrate/frame/support/src/traits/tokens/fungible/mod.rs index 61b75fd6563c..224ff253cef6 100644 --- a/substrate/frame/support/src/traits/tokens/fungible/mod.rs +++ b/substrate/frame/support/src/traits/tokens/fungible/mod.rs @@ -73,6 +73,22 @@ use crate::{ traits::{Consideration, Footprint}, }; +trait FreezeConsiderationFromLegacy +where + A: 'static, + F: 'static + MutateFreeze, +{ + fn new_from_exact(who: &A, new: F::Balance) -> Result where Self: Sized; +} + +trait HoldConsiderationFromLegacy +where + A: 'static, + F: 'static + MutateHold, +{ + fn new_from_exact(who: &A, new: F::Balance) -> Result where Self: Sized; +} + /// Consideration method using a `fungible` balance frozen as the cost exacted for the footprint. /// /// The aggregate amount frozen under `R::get()` for any account which has multiple tickets, @@ -118,6 +134,19 @@ impl< } } +impl< + A: 'static, + F: 'static + MutateFreeze, + R: 'static + Get, + D: 'static + Convert, + > FreezeConsiderationFromLegacy for FreezeConsideration +{ + fn new_from_exact(who: &A, new: F::Balance) -> Result { + F::increase_frozen(&R::get(), who, new)?; + Ok(Self(new, PhantomData)) + } +} + /// Consideration method using a `fungible` balance frozen as the cost exacted for the footprint. #[derive( CloneNoBound, @@ -163,6 +192,19 @@ impl< } } +impl< + A: 'static, + F: 'static + MutateHold, + R: 'static + Get, + D: 'static + Convert, + > HoldConsiderationFromLegacy for HoldConsideration +{ + fn new_from_exact(who: &A, new: F::Balance) -> Result { + F::hold(&R::get(), who, new)?; + Ok(Self(new, PhantomData)) + } +} + /// Basic consideration method using a `fungible` balance frozen as the cost exacted for the /// footprint. /// @@ -202,6 +244,19 @@ impl< } } +impl< + A: 'static, + Fx: 'static + MutateFreeze, + Rx: 'static + Get, + D: 'static + Convert, + > FreezeConsiderationFromLegacy for LoneFreezeConsideration +{ + fn new_from_exact(who: &A, new: Fx::Balance) -> Result { + ensure!(Fx::balance_frozen(&Rx::get(), who).is_zero(), DispatchError::Unavailable); + Fx::set_frozen(&Rx::get(), who, new, Polite).map(|_| Self(PhantomData)) + } +} + /// Basic consideration method using a `fungible` balance placed on hold as the cost exacted for the /// footprint. /// @@ -243,3 +298,16 @@ impl< let _ = F::burn_all_held(&R::get(), who, BestEffort, Force); } } + +impl< + A: 'static, + F: 'static + MutateHold, + R: 'static + Get, + D: 'static + Convert, + > HoldConsiderationFromLegacy for LoneHoldConsideration +{ + fn new_from_exact(who: &A, new: F::Balance) -> Result { + ensure!(F::balance_on_hold(&R::get(), who).is_zero(), DispatchError::Unavailable); + F::set_on_hold(&R::get(), who, new).map(|_| Self(PhantomData)) + } +} From 5a54bc4d2c5b6bd40289cd465675c11f1b2913b1 Mon Sep 17 00:00:00 2001 From: NachoPal Date: Fri, 10 Nov 2023 16:51:43 +0100 Subject: [PATCH 2/7] make ticket option --- .../support/src/traits/tokens/fungible/mod.rs | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/substrate/frame/support/src/traits/tokens/fungible/mod.rs b/substrate/frame/support/src/traits/tokens/fungible/mod.rs index 224ff253cef6..99f10b3b4457 100644 --- a/substrate/frame/support/src/traits/tokens/fungible/mod.rs +++ b/substrate/frame/support/src/traits/tokens/fungible/mod.rs @@ -73,20 +73,36 @@ use crate::{ traits::{Consideration, Footprint}, }; -trait FreezeConsiderationFromLegacy +/// Extension for `Consideration` trait. +/// Provides the `new_from_exact` method for those types using a `fungible` balance frozen, +/// This method is useful when a new ticket needs to be created with a precise balance, instead of +/// deriving it from a footprint. +pub trait FreezeConsiderationFromLegacy: Consideration where A: 'static, F: 'static + MutateFreeze, { - fn new_from_exact(who: &A, new: F::Balance) -> Result where Self: Sized; + /// Create a ticket for a `new` balance attributable to `who`. This ticket *must* ultimately + /// be consumed through `update` or `drop` once a footprint changes or is removed. + fn new_from_exact(_who: &A, _new: F::Balance) -> Result, DispatchError> where Self: Sized { + Ok(None) + } } -trait HoldConsiderationFromLegacy +/// Extension for `Consideration` trait. +/// Provides the `new_from_exact` method for those types using a `fungible` balance placed on hold. +/// This method is useful when a new ticket needs to be created with a precise balance, instead of +/// deriving it from a footprint. +pub trait HoldConsiderationFromLegacy: Consideration where A: 'static, F: 'static + MutateHold, { - fn new_from_exact(who: &A, new: F::Balance) -> Result where Self: Sized; + /// Create a ticket for a `new` balance attributable to `who`. This ticket *must* ultimately + /// be consumed through `update` or `drop` once a footprint changes or is removed. + fn new_from_exact(_who: &A, _new: F::Balance) -> Result, DispatchError> where Self: Sized { + Ok(None) + } } /// Consideration method using a `fungible` balance frozen as the cost exacted for the footprint. @@ -141,13 +157,14 @@ impl< D: 'static + Convert, > FreezeConsiderationFromLegacy for FreezeConsideration { - fn new_from_exact(who: &A, new: F::Balance) -> Result { + fn new_from_exact(who: &A, new: F::Balance) -> Result, DispatchError> { F::increase_frozen(&R::get(), who, new)?; - Ok(Self(new, PhantomData)) + Ok(Some(Self(new, PhantomData))) } } -/// Consideration method using a `fungible` balance frozen as the cost exacted for the footprint. +/// Consideration method using a `fungible` balance placed on hold as the cost exacted for the +/// footprint. #[derive( CloneNoBound, EqNoBound, @@ -199,9 +216,9 @@ impl< D: 'static + Convert, > HoldConsiderationFromLegacy for HoldConsideration { - fn new_from_exact(who: &A, new: F::Balance) -> Result { + fn new_from_exact(who: &A, new: F::Balance) -> Result, DispatchError> { F::hold(&R::get(), who, new)?; - Ok(Self(new, PhantomData)) + Ok(Some(Self(new, PhantomData))) } } @@ -251,9 +268,9 @@ impl< D: 'static + Convert, > FreezeConsiderationFromLegacy for LoneFreezeConsideration { - fn new_from_exact(who: &A, new: Fx::Balance) -> Result { + fn new_from_exact(who: &A, new: Fx::Balance) -> Result, DispatchError> { ensure!(Fx::balance_frozen(&Rx::get(), who).is_zero(), DispatchError::Unavailable); - Fx::set_frozen(&Rx::get(), who, new, Polite).map(|_| Self(PhantomData)) + Fx::set_frozen(&Rx::get(), who, new, Polite).map(|_| Some(Self(PhantomData))) } } @@ -306,8 +323,8 @@ impl< D: 'static + Convert, > HoldConsiderationFromLegacy for LoneHoldConsideration { - fn new_from_exact(who: &A, new: F::Balance) -> Result { + fn new_from_exact(who: &A, new: F::Balance) -> Result, DispatchError> { ensure!(F::balance_on_hold(&R::get(), who).is_zero(), DispatchError::Unavailable); - F::set_on_hold(&R::get(), who, new).map(|_| Self(PhantomData)) + F::set_on_hold(&R::get(), who, new).map(|_| Some(Self(PhantomData))) } } From 8ffd37bfcc89f78f0153d4e4e3d709c8404ced4f Mon Sep 17 00:00:00 2001 From: NachoPal Date: Fri, 10 Nov 2023 16:52:33 +0100 Subject: [PATCH 3/7] fmt --- .../frame/support/src/traits/tokens/fungible/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/substrate/frame/support/src/traits/tokens/fungible/mod.rs b/substrate/frame/support/src/traits/tokens/fungible/mod.rs index 99f10b3b4457..1e7943b97fc4 100644 --- a/substrate/frame/support/src/traits/tokens/fungible/mod.rs +++ b/substrate/frame/support/src/traits/tokens/fungible/mod.rs @@ -84,7 +84,10 @@ where { /// Create a ticket for a `new` balance attributable to `who`. This ticket *must* ultimately /// be consumed through `update` or `drop` once a footprint changes or is removed. - fn new_from_exact(_who: &A, _new: F::Balance) -> Result, DispatchError> where Self: Sized { + fn new_from_exact(_who: &A, _new: F::Balance) -> Result, DispatchError> + where + Self: Sized, + { Ok(None) } } @@ -100,7 +103,10 @@ where { /// Create a ticket for a `new` balance attributable to `who`. This ticket *must* ultimately /// be consumed through `update` or `drop` once a footprint changes or is removed. - fn new_from_exact(_who: &A, _new: F::Balance) -> Result, DispatchError> where Self: Sized { + fn new_from_exact(_who: &A, _new: F::Balance) -> Result, DispatchError> + where + Self: Sized, + { Ok(None) } } From 42de057134b9261929443d9b255df05a42e4b563 Mon Sep 17 00:00:00 2001 From: NachoPal Date: Fri, 10 Nov 2023 17:29:25 +0100 Subject: [PATCH 4/7] nit --- substrate/frame/support/src/traits/tokens/fungible/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substrate/frame/support/src/traits/tokens/fungible/mod.rs b/substrate/frame/support/src/traits/tokens/fungible/mod.rs index 1e7943b97fc4..deb704ce861d 100644 --- a/substrate/frame/support/src/traits/tokens/fungible/mod.rs +++ b/substrate/frame/support/src/traits/tokens/fungible/mod.rs @@ -74,7 +74,7 @@ use crate::{ }; /// Extension for `Consideration` trait. -/// Provides the `new_from_exact` method for those types using a `fungible` balance frozen, +/// Provides a `new_from_exact` method for those types using a `fungible` balance frozen, /// This method is useful when a new ticket needs to be created with a precise balance, instead of /// deriving it from a footprint. pub trait FreezeConsiderationFromLegacy: Consideration @@ -93,7 +93,7 @@ where } /// Extension for `Consideration` trait. -/// Provides the `new_from_exact` method for those types using a `fungible` balance placed on hold. +/// Provides a `new_from_exact` method for those types using a `fungible` balance placed on hold. /// This method is useful when a new ticket needs to be created with a precise balance, instead of /// deriving it from a footprint. pub trait HoldConsiderationFromLegacy: Consideration From de0cc1c11b17383c2fb342e59e5be9ac9abdb6bd Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Thu, 7 Dec 2023 22:52:15 +1100 Subject: [PATCH 5/7] Update substrate/frame/support/src/traits/tokens/fungible/mod.rs Co-authored-by: Oliver Tale-Yazdi --- substrate/frame/support/src/traits/tokens/fungible/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/substrate/frame/support/src/traits/tokens/fungible/mod.rs b/substrate/frame/support/src/traits/tokens/fungible/mod.rs index deb704ce861d..ed65c13c2263 100644 --- a/substrate/frame/support/src/traits/tokens/fungible/mod.rs +++ b/substrate/frame/support/src/traits/tokens/fungible/mod.rs @@ -73,7 +73,8 @@ use crate::{ traits::{Consideration, Footprint}, }; -/// Extension for `Consideration` trait. +/// Extension for the `Consideration` trait to migrate legacy `Currency` deposits. +/// /// Provides a `new_from_exact` method for those types using a `fungible` balance frozen, /// This method is useful when a new ticket needs to be created with a precise balance, instead of /// deriving it from a footprint. From 490e39c63c0abbce70b96f84751378c15ea3cc41 Mon Sep 17 00:00:00 2001 From: NachoPal Date: Fri, 26 Jan 2024 11:36:50 +0100 Subject: [PATCH 6/7] replace None by Err --- .../support/src/traits/tokens/fungible/mod.rs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/substrate/frame/support/src/traits/tokens/fungible/mod.rs b/substrate/frame/support/src/traits/tokens/fungible/mod.rs index df927babf711..d9f9913e564f 100644 --- a/substrate/frame/support/src/traits/tokens/fungible/mod.rs +++ b/substrate/frame/support/src/traits/tokens/fungible/mod.rs @@ -87,11 +87,11 @@ where { /// Create a ticket for a `new` balance attributable to `who`. This ticket *must* ultimately /// be consumed through `update` or `drop` once a footprint changes or is removed. - fn new_from_exact(_who: &A, _new: F::Balance) -> Result, DispatchError> + fn new_from_exact(_who: &A, _new: F::Balance) -> Result where Self: Sized, { - Ok(None) + Err(DispatchError::Other("Unsupported")) } } @@ -106,11 +106,11 @@ where { /// Create a ticket for a `new` balance attributable to `who`. This ticket *must* ultimately /// be consumed through `update` or `drop` once a footprint changes or is removed. - fn new_from_exact(_who: &A, _new: F::Balance) -> Result, DispatchError> + fn new_from_exact(_who: &A, _new: F::Balance) -> Result where Self: Sized, { - Ok(None) + Err(DispatchError::Other("Unsupported")) } } @@ -166,9 +166,9 @@ impl< D: 'static + Convert, > FreezeConsiderationFromLegacy for FreezeConsideration { - fn new_from_exact(who: &A, new: F::Balance) -> Result, DispatchError> { + fn new_from_exact(who: &A, new: F::Balance) -> Result { F::increase_frozen(&R::get(), who, new)?; - Ok(Some(Self(new, PhantomData))) + Ok(Self(new, PhantomData)) } } @@ -225,9 +225,9 @@ impl< D: 'static + Convert, > HoldConsiderationFromLegacy for HoldConsideration { - fn new_from_exact(who: &A, new: F::Balance) -> Result, DispatchError> { + fn new_from_exact(who: &A, new: F::Balance) -> Result { F::hold(&R::get(), who, new)?; - Ok(Some(Self(new, PhantomData))) + Ok(Self(new, PhantomData)) } } @@ -277,9 +277,9 @@ impl< D: 'static + Convert, > FreezeConsiderationFromLegacy for LoneFreezeConsideration { - fn new_from_exact(who: &A, new: Fx::Balance) -> Result, DispatchError> { + fn new_from_exact(who: &A, new: Fx::Balance) -> Result { ensure!(Fx::balance_frozen(&Rx::get(), who).is_zero(), DispatchError::Unavailable); - Fx::set_frozen(&Rx::get(), who, new, Polite).map(|_| Some(Self(PhantomData))) + Fx::set_frozen(&Rx::get(), who, new, Polite).map(|_| Self(PhantomData)) } } @@ -332,8 +332,8 @@ impl< D: 'static + Convert, > HoldConsiderationFromLegacy for LoneHoldConsideration { - fn new_from_exact(who: &A, new: F::Balance) -> Result, DispatchError> { + fn new_from_exact(who: &A, new: F::Balance) -> Result { ensure!(F::balance_on_hold(&R::get(), who).is_zero(), DispatchError::Unavailable); - F::set_on_hold(&R::get(), who, new).map(|_| Some(Self(PhantomData))) + F::set_on_hold(&R::get(), who, new).map(|_| Self(PhantomData)) } } From 3277217c4c8ef49d60674b9e29af4f56eec4a797 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Fri, 26 Jan 2024 13:37:47 +0100 Subject: [PATCH 7/7] Add prdoc Signed-off-by: Oliver Tale-Yazdi --- prdoc/pr_2274.prdoc | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 prdoc/pr_2274.prdoc diff --git a/prdoc/pr_2274.prdoc b/prdoc/pr_2274.prdoc new file mode 100644 index 000000000000..8f03f140e471 --- /dev/null +++ b/prdoc/pr_2274.prdoc @@ -0,0 +1,10 @@ +title: "Add `ConsiderationFromLegacy` traits" + +doc: + - audience: Runtime Dev + description: | + Add a trait that allows to create a `Consideration` from an exact balance deposit. This allows + to migrate the legacy currency deposits to the `Consideration` API. + +crates: + - name: "frame-support"