Skip to content

Commit

Permalink
New Page About Balances & Locks (#5859)
Browse files Browse the repository at this point in the history
* added new page

* added content

* minor edits

* added clarification ED

* rm duplicated content in Build

* added info OpenGov

- new figures locks
- reorg content
- rm duplicates

* final edits

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-account-balances.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-accounts.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-accounts.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-polkadot-opengov.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Update docs/learn/learn-polkadot-opengov.md

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>

* Radha's feedback

---------

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>
  • Loading branch information
filippoweb3 and DrW3RK authored May 7, 2024
1 parent 13fa395 commit defbcce
Show file tree
Hide file tree
Showing 22 changed files with 407 additions and 290 deletions.
Binary file added docs/assets/balance-example-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/balance-example-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/balance-example-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/balance-example-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/delegation-locks-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/delegation-locks-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/locks-example-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/locks-example-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/voting-locks-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/voting-locks-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 0 additions & 81 deletions docs/build/build-protocol-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,87 +81,6 @@ signing algorithms:
Note that the address for a secp256k1 key is the SS58 encoding of the _hash of the public key_ in
order to reduce the public key from 33 bytes to 32 bytes.

## Existential Deposit

Polkadot, and most Substrate-based chains, use an _existential deposit_ (ED) to prevent dust
accounts from bloating chain state. If an account drops below the ED, it will be _reaped,_ i.e.
completely removed from storage and the nonce reset. Polkadot's ED is
{{ polkadot: <RPC network="polkadot" path="consts.balances.existentialDeposit" defaultValue={10000000000} filter="humanReadable"/>, :polkadot }}
{{ kusama: <RPC network="polkadot" path="consts.balances.existentialDeposit" defaultValue={10000000000} filter="humanReadable"/>, :kusama }}
while Kusama's is
{{ polkadot: <RPC network="kusama" path="consts.balances.existentialDeposit" defaultValue={333333333} filter="humanReadable"/>. :polkadot }}
{{ kusama: <RPC network="kusama" path="consts.balances.existentialDeposit" defaultValue={333333333} filter="humanReadable"/>. :kusama }}
You can always verify the existential deposit by checking the
[chain state](https://polkadot.js.org/apps/#/chainstate) for the constant
`balances.existentialDeposit`.

If an account drops below the _existential deposit_ ED, the account is reaped ("deactivated"), and
any remaining funds are burned. The address can be reactivated anytime by transferring funds greater
than the existential deposit. This will not restore the burned funds when the account was reaped.

:::info

For more information about the existential deposit visit the
[dedicated section](../learn/learn-accounts.md#existential-deposit-and-reaping) in the Accounts page
or Support Page for
[What is the Existential Deposit?](https://support.polkadot.network/support/solutions/articles/65000168651-what-is-the-existential-deposit-)

:::

Likewise, if you send a transfer with value below the ED to a new account, it will fail. Custodial
wallets should set a minimum withdrawal amount that is greater than the ED to guarantee successful
withdrawals.

Wallets and custodians who track account nonces for auditing purposes should take care not to have
accounts reaped, as users could refund the address and try making transactions from it. The Balances
pallet provides a `transfer_keep_alive` function that will return an error and abort rather than
make the transfer if doing so would result in reaping the sender's account.

:::info The existential deposit is a property of the Relay Chain

Your account on the Relay Chain has no direct impact on parachains as you have separate accounts on
each parachain. Still, parachains are able to define an existential deposit of their own, but this
is separate to that of the Relay Chain ED.

:::

:::note Existential deposit for the Asset Hub

The Asset Hub parachain has a lower existential deposit (0.1 DOT) than the Relay Chain (1 DOT) as
well as lower transaction fees. It is highly recommended to handle balance transfers on the Asset
Hub. Asset Hub integration is discussed in the next page of the guide.

:::

## Free vs. Reserved vs. Locked vs. Vesting Balance

Account balance information is stored in
[`AccountData`](https://paritytech.github.io/substrate/master/pallet_balances/struct.AccountData.html).
Polkadot primarily deals with two types of balances: free and reserved.

For most operations, free balance is what you are interested in. It is the "power" of an account in
staking and governance, for example. Reserved balance represents funds that have been set aside by
some operation and still belong to the account holder, but cannot be used.

Locks are an abstraction over free balance that prevent spending for certain purposes. Several locks
can operate on the same account, but they overlap rather than add. Locks are automatically added
onto accounts when tasks are done on the network (e.g. leasing a parachain slot or voting), these
are not customizable. For example, an account could have a free balance of 200 DOT with two locks on
it: 150 DOT for `Transfer` purposes and 100 DOT for `Reserve` purposes. The account could not make a
transfer that brings its free balance below 150 DOT, but an operation could result in reserving DOT
such that the free balance is below 150, but above 100 DOT.

Bonding tokens for staking and voting in governance referenda both utilize locks.

Vesting is another abstraction that uses locks on free balance. Vesting sets a lock that decreases
over time until all the funds are transferable.

More info:

- [Lockable Currency](https://paritytech.github.io/substrate/master/frame_support/traits/trait.LockableCurrency.html)
- [Lock Withdraw Reasons](https://paritytech.github.io/substrate/master/frame_support/traits/struct.WithdrawReasons.html)
- [Vesting Info](https://paritytech.github.io/substrate/master/pallet_vesting/struct.VestingInfo.html)

## Extrinsics and Events

<!-- todo: link to dev hub once up, not sure if this should be here ? -->
Expand Down
2 changes: 1 addition & 1 deletion docs/general/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Explore Polkadot with a secure and user-friendly wallets listed on the
the minimum balance required to have an active account on Polkadot Network. If your account
balance drops below the minimum, your account will be reaped. Learn more about
[Accounts](../learn/learn-accounts.md) and the
[Existential Deposit](../build/build-protocol-info.md#existential-deposit) requirement.
[Existential Deposit](../learn/learn-accounts.md#existential-deposit-and-reaping) requirement.

- {{ polkadot: __<RPC network="polkadot" path="query.nominationPools.minJoinBond" defaultValue={10000000000} filter="humanReadable"/>:__ :polkadot }}
the minimum contribution required to join a [nomination pool](../learn/learn-nomination-pools.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/general/kusama/kusama-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ governance, acquisition of a parachain slot and for enabling several key functio
the minimum balance required to have an active account on Kusama Network. If your account balance
drops below the minimum, your account will be reaped. Learn more about
[Accounts](../../learn/learn-accounts.md) and the
[Existential Deposit](../../build/build-protocol-info.md#existential-deposit) requirement.
[Existential Deposit](../../learn/learn-accounts.md#existential-deposit-and-reaping) requirement.

- {{ kusama: __<RPC network="kusama" path="query.nominationPools.minJoinBond" defaultValue={1666666650} filter="humanReadable"/>:__ :kusama }}
the minimum contribution required to join a
Expand Down
3 changes: 2 additions & 1 deletion docs/general/polkadotjs-ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ In this section of the UI, you can see accounts injected from a browser extensio
[**Polkadot-JS Extension**](./polkadotjs.md#polkadot-js-extension) or
[**other in-browser wallets**](./wallets-and-extensions.md#browser-extensions). It is also possible
to expand balance details and see different
[**account balance types**](../learn/learn-accounts.md#account-balance-types). You can also:
[**account balance types**](../learn/learn-account-balances.md#balance-types-on-polkadot-js). You
can also:

- Add an account (this option must be enabled under [Settings](#settings)). Note that if you clear
the cache of your browser, you will lose it, and you will need to recover it through seed phrase
Expand Down
224 changes: 224 additions & 0 deletions docs/learn/learn-account-balances.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
---
id: learn-account-balances
title: Account Balances
sidebar_label: Account Balances
description: Polkadot and Kusama Network Account Balance Types.
keywords: [Polkadot, Kusama, locks, balance, account]
slug: ../learn-account-balances
---

In {{ polkadot: Polkadot :polkadot }}{{ kusama: Kusama :kusama }}, there are different types of
balances depending on the account activity. Different balance types dictate whether your balance can
be used for transfers, to pay fees, or must remain frozen and unused due to an on-chain requirement.

:::info A more efficient distribution of account balance types

Soon, Polkadot SDK pallets will implement the _fungible_ trait (see
[Reference docs](https://paritytech.github.io/polkadot-sdk/master/frame_support/traits/tokens/fungible/index.html)
for more info about traits). This new logic will allow for more efficient use of your account
balance. Specifically, the
[fungible trait](https://github.com/paritytech/polkadot-sdk/issues/1833#issuecomment-1805764506)
will allow using the `free` balance for on-chain activity like setting proxies and identities.

:::

There are 5 types of account balances:

- **Free** is the balance that can be used for on-chain activity like staking, participating in
governance etc. but is not necessarily spendable (or transferrable)
- **Frozen** is the free balance locked for [staking](./learn-staking.md),
[governance](./learn-polkadot-opengov.md), and [vesting](./learn-transactions.md#vested-transfers)
(also called locked balance)
- **On hold** is used for [identities](./learn-identity.md), [proxies](./learn-proxies.md),
[OpenGov preimages and deposits](./learn-guides-polkadot-opengov.md#claiming-opengov-deposits),
and it is no longer free (also called reserved balance)
- **Spendable** is the free balance that can be spent
- **Untouchable** is the portion of the free balance that cannot be moved (i.e., not spendable) but
can still be used for on-chain activity

The spendable balance is calculated as follows:

```
spendable = free - max(frozen - on_hold, ED)
```

where `free`, `frozen` and `on_hold` are defined above. The `ED` is the the
[existential deposit](./learn-accounts.md#existential-deposit-and-reaping).

**Wallet providers might show you the spendable, locked, and reserved balance.**

## Example of Account Balance Types

Below is an in-depth example of how an account balance composition changes depending on user actions
once
[the _fungible_ trait](https://paritytech.github.io/polkadot-sdk/master/frame_support/traits/tokens/fungible/index.html)
is used for account balances. Let’s take, for example, an account with 100 DOT.

```
Free: 100 DOT
Frozen: 0 DOT
On hold: 0 DOT
Spendable: 99 DOT
Untouchable: 1 DOT (ED)
```

![balance-example-1](../assets/balance-example-1.png)

In this case, the existential deposit of 1 DOT is untouchable (meaning you can’t touch it if the
account can’t or shouldn’t get reaped). If 80 DOT from the account is staked, we get the following
balance structure:

```
Free: 100 DOT
Frozen : 80 DOT
Onhold: 0 DOT
Spendable: 20 DOT
Untouchable: 80 DOT
```

![balance-example-2](../assets/balance-example-2.png)

The spendable balance would be 20 DOT (which would also include fees for future transactions from
this account).

Note how the account cannot be reaped from the state while it has a frozen balance, or in general
any [consumer and provider reference](./learn-guides-accounts.md#query-account-data-in-polkadot-js).
Those references determine if an account can be reaped, usually because other accounts depend on the
existence of such an account). For example, the existential deposit adds a provider reference simply
because the account exists, while a proxy account adds a consumer reference (the proxy existence
depends on the proxied account; the proxy is the consumer). **Because the existential deposit is
part of the untouchable balance, the user can use all the spendable balance (there is no need to
keep 1 DOT as spendable).**

:::info

The use of the _free_ balance as shown in the following figures will be possible once the _fungible_
trait is implemented for account balances.

:::

If the account creates a proxy, it will use the `free` balance as shown below.

```
Free: 80 DOT
Frozen : 80 DOT
Onhold: 20 DOT
Spendable: 20 DOT
Untouchable: 60 DOT
```

![balance-example-3](../assets/balance-example-3.png)

**Note how, through the fungible trait, the system uses the `balance` that is frozen instead of the
`free` balance that is spendable (present configuration on-chain).** In other words, holds are
subtracted from free balance but overlap with the frozen balance. The free portion shrinks from 100
to 80 DOT, and the `on_hold` portion increases from 0 to 20 DOT. The creation of an identity will
grow the `on_hold` portion to 40 DOT, and shrink further the `free` from 80 to 60 DOT. Note how the
spendable balance stays the same in the process.

```
Free: 60 DOT
Frozen: 80 DOT
Onhold: 40 DOT
Spendable: 20 DOT
Untouchable: 40 DOT
```

![balance-example-4](../assets/balance-example-4.png)

This update using the fungible trait allows the use of the frozen balance for on-chain activity like
setting up proxies and identities. Note that
[holds are slashable](https://github.com/paritytech/substrate/pull/12951), and the pallet
[migrations](https://github.com/paritytech/polkadot-sdk/issues/226) need to take that into account.
This means that freezes should account for hold being slashed (for example, your stash account
balance getting reduced because your governance deposit for a proposal was slashed).

## Locks

Locks are abstractions over an account's free balance, preventing it from being spent. Several locks
can overlap on the same account balance instead of being stacked on top of one another. Locks are
automatically added onto accounts when the account participates in activities on-chain (staking,
voting, etc.), but these are not customizable.

Locks are accounted for within the `frozen` balance of the account. This is the balance that can be
`free` but not transferrable, and locked in [staking](./learn-staking.md),
[governance](./learn-polkadot-opengov.md) and [vesting](./learn-transactions.md#vested-transfers).

Locks overlap (in both amount and duration), and the general rule is that:

- If you have multiple locks of different amounts of tokens, the biggest lock decides the total
amount of locked tokens
- If you have multiple locks of the same amount of tokens, the lock with the longest duration
decides when those tokens can be unlocked

Let's take, for example, 80 DOT as a `frozen` balance. These 80 DOT are currently used in staking
and governance as follows:

- 80 DOT Staking (just unbonded) -> lock 28 days
- 24 DOT OpenGov 1x conviction (referendum just ended, winning side) -> lock 7 days
- 4 DOT OpenGov 6x conviction (referendum just ended, winning side) -> lock 224 days

![locks-example-1](../assets/locks-example-1.png)

The 1 DOT ED is the existential deposit. The locked amount is 80 DOT (not 108 DOT). But those 80 DOT
will be available for unlock at different times. You will first need to remove the governance lock
on the 24 DOT after 7 days, then remove the staking lock for the 80 DOT after 28 days, and finally,
after 224 days, you will be able to remove the second governance lock.

![locks-example-2](../assets/locks-example-2.png)

After 224 days, all 80 DOT (- ED) will be free and transferrable.

### Edge Case for Locks

The longest period and the largest amount are considered if you use different convictions while you
have ongoing locks.

Following the previous example, if you:

- undelegate a 1x conviction delegation of 24 DOT, you will get a 7-day lock on 24 DOT
- delegate 4 DOT with 6x conviction
- undelegate again before the 1x conviction lock is removed

You will get a 6x conviction for 24 DOT! See
[here](https://substrate.stackexchange.com/questions/5067/delegating-and-undelegating-during-the-lock-period-extends-it-for-the-initial-am)
for more information.

## Balance Types on Polkadot-JS

Below is an example that displays different balance types on the
[Polkadot-JS UI (wallet)](../general/polkadotjs-ui.md) of a Kusama account (note that the balance
types are the same for a Polkadot account).

![account_balance_types](../assets/account-balance-types.png)

- The **total** balance indicates the total number of tokens in the account. Note that this number
does not necessarily correspond to the tokens you can transfer. In the example, the total number
of tokens is 0.6274 KSM. The **transferrable** balance indicates the number of free tokens to be
transferred. This is calculated by subtracting the number of _locked_ and _reserved_ tokens from
the total number of tokens. Locked funds correspond to tokens used in staking, governance, and
vested transfers (see below). In the example, the transferrable balance is 0.0106 KSM.
- The **vested** balance indicates tokens sent to the account and released with a specific time
schedule. The account owns the tokens, but they are _locked_ and become available for transfer
after a specific number of blocks. In the example, the vested balance is 0.25 KSM.
- The **bonded** balance indicates the number of tokens that are _locked_ for on-chain participation
to staking. In the example, the bonded balance is 0.4 KSM.
- The **democracy** balance indicates the number of tokens that are _locked_ for on-chain
participation in democracy (i.e., voting for referenda and council). In the example, the democracy
balance is 0.4 KSM.
- The **redeemable** balance indicates the number of tokens ready to be unlocked to become
transferrable again. Those tokens already went through the unbonding period. In this case, the
redeemable balance is 0.1 KSM.
- The **locked** balance indicates the number of frozen tokens for on-chain participation to staking
and democracy or for vested transfers. **Locks do not stack**, which means that if you have
different locks, the total locked balance is not the addition of the individual locks. Instead,
**the biggest lock decides the total locked balance**. In the example, the locked balance is 0.55
KSM because the biggest lock is on democracy (0.55 KSM).
- The **reserved** balance indicates the number of frozen tokens for on-chain activity other than
staking, governance, and vested transfers. Such activity can be setting an identity or a proxy.
Reserved funds are held due to on-chain requirements and can usually be freed by taking some
on-chain action. For example, the "Identity" pallet reserves funds while an on-chain identity is
registered, but by clearing the identity, you can unreserve the funds and make them free again.
The same applies to proxies. The idea is that those actions require some network memory usage that
is not given for free. In the example, we created a governance proxy, and the reserved funds for
this are 0.0668 KSM.
Loading

0 comments on commit defbcce

Please sign in to comment.