This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Deprecate Currency
; introduce holds and freezing into fungible
traits
#12951
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gavofyork
added
B0-silent
Changes should not be mentioned in any release notes
C1-low
PR touches the given topic and has a low impact on builders.
D9-needsaudit 👮
PR contains changes to fund-managing logic that should be properly reviewed and externally audited
labels
Dec 16, 2022
11 tasks
1 task
10 tasks
Merged
10 tasks
This was referenced Nov 28, 2023
9 tasks
Merged
10 tasks
6 tasks
4 tasks
3 tasks
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
A0-please_review
Pull request needs code review.
B1-note_worthy
Changes should be noted in the release notes
C1-low
PR touches the given topic and has a low impact on builders.
D1-audited 👍
PR contains changes to fund-managing logic that has been properly reviewed and externally audited
T1-runtime
This PR/Issue is related to the topic “runtime”.
T2-API
This PR/Issue is related to APIs.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related to paritytech/polkadot-sdk#236
Fixes #12701
Polkadot Companion: paritytech/polkadot#6780
Cumulus Companion: paritytech/cumulus#2334
Introduces Freezing and Holds: two new facilities in Balances pallet and an interface for them through
fungible
andfungibles
. Also reworks some internals of Balances pallet to make it safer. Requires Existential Deposit requirements to be made of the free balance, not the total balance.Key Points
pallet_balances::Config
trait:type MaxHolds
type MaxFreezes
type HoldIdentifier
type FreezeIdentifier
ConstU32<0>
in the case of the first two and()
in the case of the second two.pallet_balances
:upgrade_accounts
.Balances::set_balance
becomesBalances::force_set_balance
and only accepts the free balance (not the reserved balance since this no longer makes sense).Balances::transfer
becomesBalances::transfer_allow_death
fungible
/fungibles
traits making them much more type-safe.fungible::Transfer
trait intofungible::Mutate
(and same forfungibles
).fungible::Balanced
trait to be explicitly implemented (and same forfungibles
).fungible
/fungibles
traits.Currency
traits in favour offungible
traits.Currency::minimum_balance()
) in addition to any amount to be reserved. This is a BREAKING CHANGE and is likely to result in failures in tests and benchmarking.ExistentialDeposit
of zero no longer works. If you need to allow any account to have a zero balance and retain associated account data, you must introduce a pallet which allows an unpermissionedframe_system::Pallet::<Runtime>::inc_providers()
.Long Description
Some renaming:
Balances::set_balance
becomesBalances::force_set_balance
Balances::transfer
becomesBalances::transfer_allow_death
In the latter case, since ecosystem tooling largely depends on using
transfer
, a compatibility stub fortransfer
remains, however this naming is DEPRECATED and ecosystem tools should move over to usingtransfer_allow_death
within 3 months at which point thetransfer
alias will be removed.In general, it's now better to use the
fungible
trait API for Balances rather than messing with the dispatchables directly. The long-term plan is to remove the dispatchables and have all pallet-access be through XCM.Holds and Freezes
Holds are roughly analagous to
reserve
s, but are now explicitly designed to be infallibly slashed. They do not contribute to the ED but do require a provider reference, removing any possibility of account reference counting from being problematic for aslash
. They are also always named, ensuring different holds do not accidentally slash each other's balances.Freezes are essentially the same as locks, except that they overlap with Holds. Since Holds are designed to be infallibly slashed, this means that any logic using a Freeze must handle the possibility of the frozen amount being reduced, potentially to zero. A permissionless function should be provided in order to allow bookkeeping to be updated in this instance.
Both Holds and Freezes require an identifier which is configurable and is expected to be an
enum
aggregated across all pallet instances of the runtime.Balances Internals
Alter
reserve
functionality in Balances so that it does not contribute to the account's existential deposit and does use a consumer reference. Remove the concept of withdraw reasons from locks. Migration is lazy: accounts about to be mutated will be migrated prior. New accounts will automatically be mutated. A permissionless upgrade dispatchable is provided to upgrade dormant accounts.Typing
Type-safety is improved by removing all naked
bool
parameters from fungible(s) API functions. In particular,best_effort
andforce
which were both previously boolean are now typed asPrecision
andFortitude
. Lesser used arguments which have changed areon_hold
which is typed asRestriction
andminted
which is typed asProvenance
. This generally makes invocations much more readable.Privatizations
One function
mutate_account
(in Balances pallet) has been privatized. It should never have been public since it ignores some important bookkeeping. If you're using it, you probably mean to be usingmake_free_balance_be
instead.Upgrading
Because reserved/held funds do not any longer contribute to the ED, you may find that certain test and benchmark code no longer works. This is generally due to the case where exactly as many funds were placed in an account as were later reserved. To fix this breakage, simply ensure that at least the currency's
minimum_deposit
is in the account in addition to any funds which are planned to be reserved. It's usually as simple as appending+ T::Currency::minimum_balance()
to the argument ofmake_free_balance_be
.In some circumstances, errors have changed to the more general errors.
After introducing a runtime with this change in it, chains should ensure that all accounts are upgraded by repeatedly calling
update_accounts
giving it account IDs which are not yet upgraded. A script which does this shall be provided.Pallets which use the existing set of
Currency
traits should be refactored to use thefungible
traits. Aside from the actual code change, a gateway function (ensure_upgraded
) should be introduced and called prior to any mutating operations to ensure that any accounts using the oldCurrency
traits are unreserved/unlocked and said funds are instead placed on hold/frozen. A permissionless fee-free dispatchable should also be introduced (upgrade_accounts
), similar to that of the Balances pallet, which is able to do this for dormant accounts.It is not crucial to get this done in any particular timeframe, however at some point in the future Currency traits, Locks and Reserves will be deprecated and removed, so it will need to happen before then.
Important Notes
Some functions in the
Currency
traits retain their name and parameter set in thefungible
traits and have similar semantics. However, there are many functions with differences in naming, and some with differences in result types and their meaning.Currency::free_balance
may be replaced withfungible::Inspect::balance
.Currency::make_free_balance_be
may be replaced withfungible::Mutate::set_balance
.ReservableCurrency::reserved_balance
may be replaced withfungible::hold::Inspect::total_balance_on_hold
.NamedReservableCurrency::reserve
may be replaced withfungible::hold::Mutate::hold
.NamedReservableCurrency::unreserve
may be replaced withfungible::hold::Mutate::release
, however the returnedResult
must be handled and the inner ofOk
has the opposite meaning (i.e. it is the amount released, rather than the amount not released, if any).LockableCurrency::set_lock
may generally be replaced withfungible::freeze::Mutate::set_freeze
.LockableCurrency::extend_lock
may generally be replaced withfungible::freeze::Mutate::extend_freeze
.LockableCurrency::remove_lock
may be replaced withfungible::freeze::Mutate::thaw
.There are no equivalents for
ReservableCurrency::reserve
or::unreserve
: an identifier must always be used.Functions which may result in an account being deleted accept a new
Preservation
value. This has three variants:Expendable
: Equivalent toExistenceRequirement::AllowDeath
Protect
: The account must not die. If there are multiple provider refs, then this may still allow the balance to be reduced to zero.Preserve
: Not only must the account not die, but the provider reference of the Currency implementation must also be kept in place.Pallet NIS
NOTE: This updates NIS to use only the
fungible
API rather thanCurrency
. This is fine since NIS is not deployed anywhere. For other pallets already in production then a progressive migration will be needed.Pallet NIS has been altered to use
active_issuance
rather thantotal_issuance
. This means theIgnoredIssuance
is whatever should be discounted againstactive_issuance
. It should probably be replaced with()
.TODO
UnbalancedHold
.UnbalancedHold
for balances.Hold
traits forUnbalancedHold
implementations.fee_frozen
andWithdrawReasons
; just have a singlefrozen
field inAccountData
.InspectFreeze
andMutateFreeze
for Balances.MutateFreeze
ensure_upgraded
on a non-upgraded account.fungible
/fungibles
fungibles
up to date.ItemOf
work properly again.fungible
/fungibles
/nonfungibles
:suspend
/resume
toshelve
/restore
.upgrade_accounts
: https://github.com/ggwpez/substrate-scripts/blob/master/upgrade-accounts.pyMaybe
Currency
an optional feature.Next up: paritytech/polkadot-sdk#226