-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Documentation for balances module #1943
Changes from 3 commits
74bcfcb
a544d4a
1cc5721
e4a3bf7
3e4b5e4
077cde2
dd16fd6
ceeb172
05894ab
a654735
02110b2
53680af
f1b62a4
499c239
cf750f8
2cf4009
d863ff6
5e28748
c8ec45a
36b1689
3c157e4
a0ef9b7
2d28117
df3ced9
e42f6b6
e000871
faccdbf
513b377
ce29349
fa9f215
28a1d33
aa43b56
191ecc0
665fa6b
5f391ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,16 +14,131 @@ | |
| // You should have received a copy of the GNU General Public License | ||
| // along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Balances: Handles setting and retrieval of free balance, | ||
| //! retrieving total, reserved, and unreserved balances, | ||
| //! repatriating a reserved balance to a beneficiary account that exists, | ||
| //! transfering a balance between accounts (when not reserved), | ||
| //! slashing an account balance, account removal, rewards, | ||
| //! lookup of an index to reclaim an account (when balance not reserved), | ||
| //! increasing total stake. | ||
| //! # Balances Module | ||
| //! | ||
| //! Implements functions for `Currency`, `LockableCurrency`, `TransferAsset`, | ||
| //! and `IsDeadAccount` traits. | ||
| //! ## Overview | ||
| //! | ||
| //! The balances module provides functions for: | ||
| //! | ||
| //! - Getting and setting free balance | ||
| //! - Retrieving total, reserved, and unreserved balances | ||
| //! - Repatriating a reserved balance to a beneficiary account that exists | ||
| //! - Transfering a balance between accounts (when not reserved) | ||
| //! - Slashing an account balance | ||
| //! - Account removal | ||
| //! - Lookup of an index to reclaim an account | ||
| //! - Increasing or decreasing total stake | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! - Setting and removing locks on chains that implement `LockableCurrency` | ||
| //! | ||
| //! ### Terminology | ||
| //! | ||
| //! - **Existential Deposit:** The existential deposit is the minimum balance required to create or keep an account open. This prevents "dust accounts" from filling storage. | ||
| //! - **Stake:** The total amount of tokens in existence in a system. | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! - **Reaping an account:** The act of removing an account by resetting its nonce, setting its balance to zero, and removing from storage. | ||
| //! - **Free Balance:** The free balance is the only balance that matters for most operations. When this balance falls below the existential deposit, the account is reaped. | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! - **Reserved Balance:** Reserved balance still belongs to the account holder, but is suspended. Reserved balance can still be slashed, but only after all of free balance has been slashed. If the reserved balance falls below the existential deposit then it will be deleted. | ||
| //! - **Locks:** Locks enable the runtime to lock an account's balance until a specified block number. Only runtimes that implement the `LockableCurrency` trait allow this. | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! ## Interface | ||
| //! | ||
| //! ### Types | ||
| //! | ||
| //! - Balance | ||
| //! - OnFreeBalanceZero | ||
| //! - OnNewAccount | ||
| //! - Event | ||
| //! | ||
| //! These are [associated types](https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#specifying-placeholder-types-in-trait-definitions-with-associated-types) and must be implemented in your `runtime/src/lib.rs`. For example: | ||
| //! | ||
| //! ```ignore | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! impl balances::Trait for Runtime { | ||
| //! /// The type for recording an account's balance. | ||
| //! type Balance = u128; | ||
| //! /// What to do if an account's free balance gets zeroed. | ||
| //! type OnFreeBalanceZero = (); | ||
| //! /// What to do if a new account is created. | ||
| //! type OnNewAccount = Indices; | ||
| //! /// The uniquitous event type. | ||
| //! type Event = Event; | ||
| //! } | ||
| //! ``` | ||
| //! | ||
| //! ### Dispatchable Functions | ||
| //! | ||
| //! The `Call` enum is documented here: https://crates.parity.io/srml_balances/enum.Call.html | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! <!-- TODO: Add link to rust docs (https://github.com/paritytech/substrate-developer-hub/issues/24) --> | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! - `transfer` - Transfer some liquid free balance to another staker. | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! - `set_balance` - Set the balances of a given account. Only dispatchable by a user with root privileges. | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! ### Public Functions | ||
| //! | ||
| //! <!-- TODO: Add link to rust docs (https://github.com/paritytech/substrate-developer-hub/issues/24) --> | ||
| //! | ||
| //! See the [module](https://crates.parity.io/srml_balances/struct.Module.html) for details on publicly available functions. | ||
| //! | ||
| //! **Note:** When using the publicly exposed functions, you (the runtime developer) are responsible for implementing any necessary checks (e.g. that the sender is the signer) before calling a function that will affect storage. | ||
| //! | ||
| //! ## Usage | ||
| //! | ||
| //! The following examples show how to use the balances module in your custom module. | ||
| //! | ||
| //! ### Import and Balance Transfer | ||
| //! | ||
| //! Import the `balances` module and derive your module configuration trait with the balances trait. You can now call functions from the module. | ||
| //! | ||
| //! ```ignore | ||
| //! use support::{decl_module, dispatch::Result}; | ||
| //! use system::ensure_signed; | ||
| //! | ||
| //! pub trait Trait: balances::Trait {} | ||
| //! | ||
| //! decl_module! { | ||
| //! pub struct Module<T: Trait> for enum Call where origin: T::Origin { | ||
| //! fn transfer_proxy(origin, to: T::AccountId, value: T::Balance) -> Result { | ||
| //! let sender = ensure_signed(origin)?; | ||
| //! <balances::Module<T>>::make_transfer(&sender, &to, value)?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the updated function name is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, and more generally throughout some of the early docs whose modules had a refactor. #2159 |
||
| //! | ||
| //! Ok(()) | ||
| //! } | ||
| //! } | ||
| //! } | ||
| //! ``` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kind of agree for this example but at the same time it is not specific to balance, so I'm mixed but ok
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, not highly specific to balances. I thought our idea was to have an example with everything necessary to compile and use the module. Can remove if we think the real-use example is adequate. |
||
| //! | ||
| //! ### Real Use Example | ||
| //! | ||
| //! Use the `free_balance` function (from the `Currency` trait) in the `staking` module: | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! ```ignore | ||
| //! fn bond_extra(origin, max_additional: BalanceOf<T>) { | ||
| //! let controller = ensure_signed(origin)?; | ||
| //! let mut ledger = Self::ledger(&controller).ok_or("not a controller")?; | ||
| //! let stash_balance = T::Currency::free_balance(&ledger.stash); | ||
| //! | ||
| //! if stash_balance > ledger.total { | ||
| //! let extra = (stash_balance - ledger.total).min(max_additional); | ||
| //! ledger.total += extra; | ||
| //! ledger.active += extra; | ||
| //! Self::update_ledger(&controller, ledger); | ||
| //! } | ||
| //! } | ||
| //! ``` | ||
| //! | ||
| //! ## Genesis config | ||
| //! | ||
| //! Configuration is in `<your-node-name>/src/chain_spec.rs`. The following storage items are configurable: | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! - `TotalIssuance` | ||
| //! - `ExistentialDeposit` | ||
| //! - `TransferFee` | ||
| //! - `CreationFee` | ||
| //! - `Vesting` | ||
joepetrowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| //! - `FreeBalance` | ||
| //! | ||
| //! ## Related Modules | ||
| //! | ||
| //! The balances module depends on the [`system`](https://crates.parity.io/srml_system/index.html) and [`srml_support`](https://crates.parity.io/srml_support/index.html) modules as well as Substrate Core libraries and the Rust standard library. | ||
|
|
||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
|
|
@@ -188,10 +303,13 @@ decl_module! { | |
| fn deposit_event<T>() = default; | ||
|
|
||
| /// Transfer some liquid free balance to another staker. | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// `transfer` will set the `FreeBalance` of the sender and receiver. | ||
| /// It will decrease the total stake of the system by the `TransferFee`. | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// If the sender's account is below the existential deposit as a result | ||
| /// of the transfer, the account will be reaped. | ||
| /// | ||
| /// The dispatch origin for this call must be `Signed` by the transactor. | ||
| pub fn transfer( | ||
| origin, | ||
| dest: <T::Lookup as StaticLookup>::Source, | ||
|
|
@@ -202,11 +320,14 @@ decl_module! { | |
| Self::make_transfer(&transactor, &dest, value)?; | ||
| } | ||
|
|
||
| /// Set the balances of a given account. Only dispatchable by root. | ||
| /// Set the balances of a given account. | ||
| /// | ||
| /// This will alter `FreeBalance` and `ReservedBalance` in storage. | ||
| /// If the new free or reserved balance is below the existential deposit, | ||
| /// it will also decrease the total stake of the system (`TotalIssuance`) | ||
| /// and reset the account nonce (`system::AccountNonce`). | ||
joepetrowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// | ||
| /// The dispatch origin for this call is `root`. | ||
| fn set_balance( | ||
| who: <T::Lookup as StaticLookup>::Source, | ||
| #[compact] free: T::Balance, | ||
|
|
@@ -270,7 +391,7 @@ impl<T: Trait> Module<T> { | |
| /// | ||
| /// Same as `set_free_balance`, but will create a new account. | ||
| /// | ||
| /// #NOTES | ||
| /// # NOTES | ||
| /// | ||
| /// See documentation on `FreeBalance` and `ReservedBalance` storage items for their differences | ||
| pub fn set_free_balance_creating(who: &T::AccountId, balance: T::Balance) -> UpdateBalanceOutcome { | ||
|
|
@@ -293,7 +414,7 @@ impl<T: Trait> Module<T> { | |
| /// Enforces `ExistentialDeposit` law, reaping the sender's account if its balance is | ||
| /// too low as a result of the transfer. | ||
| /// | ||
| /// #NOTES | ||
| /// # NOTES | ||
| /// | ||
| /// Will create a new account for the destination if the account does not exist. | ||
joepetrowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub fn make_transfer(transactor: &T::AccountId, dest: &T::AccountId, value: T::Balance) -> Result { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.