Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConsiderationFromLegacy traits #2274

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions prdoc/pr_2274.prdoc
Original file line number Diff line number Diff line change
@@ -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"
94 changes: 93 additions & 1 deletion substrate/frame/support/src/traits/tokens/fungible/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,45 @@ use crate::{
traits::{Consideration, Footprint},
};

/// 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.
pub trait FreezeConsiderationFromLegacy<A, F>: Consideration<A>
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
where
A: 'static,
F: 'static + MutateFreeze<A>,
{
/// 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<Self, DispatchError>
where
Self: Sized,
{
Err(DispatchError::Other("Unsupported"))
}
}

/// Extension for `Consideration` trait.
/// 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<A, F>: Consideration<A>
where
A: 'static,
F: 'static + MutateHold<A>,
{
/// 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<Self, DispatchError>
where
Self: Sized,
{
Err(DispatchError::Other("Unsupported"))
}
}

/// 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,
Expand Down Expand Up @@ -120,7 +159,21 @@ impl<
}
}

/// Consideration method using a `fungible` balance frozen as the cost exacted for the footprint.
impl<
A: 'static,
F: 'static + MutateFreeze<A>,
R: 'static + Get<F::Id>,
D: 'static + Convert<Footprint, F::Balance>,
> FreezeConsiderationFromLegacy<A, F> for FreezeConsideration<A, F, R, D>
{
fn new_from_exact(who: &A, new: F::Balance) -> Result<Self, DispatchError> {
F::increase_frozen(&R::get(), who, new)?;
Ok(Self(new, PhantomData))
}
}

/// Consideration method using a `fungible` balance placed on hold as the cost exacted for the
/// footprint.
#[derive(
CloneNoBound,
EqNoBound,
Expand Down Expand Up @@ -165,6 +218,19 @@ impl<
}
}

impl<
A: 'static,
F: 'static + MutateHold<A>,
R: 'static + Get<F::Reason>,
D: 'static + Convert<Footprint, F::Balance>,
> HoldConsiderationFromLegacy<A, F> for HoldConsideration<A, F, R, D>
{
fn new_from_exact(who: &A, new: F::Balance) -> Result<Self, DispatchError> {
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.
///
Expand Down Expand Up @@ -204,6 +270,19 @@ impl<
}
}

impl<
A: 'static,
Fx: 'static + MutateFreeze<A>,
Rx: 'static + Get<Fx::Id>,
D: 'static + Convert<Footprint, Fx::Balance>,
> FreezeConsiderationFromLegacy<A, Fx> for LoneFreezeConsideration<A, Fx, Rx, D>
{
fn new_from_exact(who: &A, new: Fx::Balance) -> Result<Self, DispatchError> {
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.
///
Expand Down Expand Up @@ -245,3 +324,16 @@ impl<
let _ = F::burn_all_held(&R::get(), who, BestEffort, Force);
}
}

impl<
A: 'static,
F: 'static + MutateHold<A>,
R: 'static + Get<F::Reason>,
D: 'static + Convert<Footprint, F::Balance>,
> HoldConsiderationFromLegacy<A, F> for LoneHoldConsideration<A, F, R, D>
{
fn new_from_exact(who: &A, new: F::Balance) -> Result<Self, DispatchError> {
ensure!(F::balance_on_hold(&R::get(), who).is_zero(), DispatchError::Unavailable);
F::set_on_hold(&R::get(), who, new).map(|_| Self(PhantomData))
}
}
Loading