Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
74bcfcb
comment updates
joepetrowski Mar 6, 2019
a544d4a
Merge branch 'master' into joe-balances-docs
joepetrowski Mar 6, 2019
1cc5721
added rustdoc and readme
joepetrowski Mar 6, 2019
e4a3bf7
clarified LockableCurrency trait
joepetrowski Mar 6, 2019
3e4b5e4
Currency trait rustdocs
joepetrowski Mar 7, 2019
077cde2
fixed typo
joepetrowski Mar 7, 2019
dd16fd6
fixed suggestions round 1
joepetrowski Mar 8, 2019
ceeb172
UpdateBalanceOutcome docs (open for discussion)
joepetrowski Mar 8, 2019
05894ab
rm description of enum, consolidation, rm ReclaimRebate
joepetrowski Mar 9, 2019
a654735
type clarification, examples overhaul, adoc formatting
joepetrowski Mar 10, 2019
02110b2
adoc to md
joepetrowski Mar 11, 2019
53680af
format change for rustdoc
joepetrowski Mar 12, 2019
f1b62a4
update links and fix typos
joepetrowski Mar 12, 2019
499c239
typos and links
joepetrowski Mar 13, 2019
cf750f8
Merge remote-tracking branch 'origin/master' into joe-balances-docs
joepetrowski Mar 13, 2019
2cf4009
updates according to comments
joepetrowski Mar 13, 2019
d863ff6
new example
joepetrowski Mar 14, 2019
5e28748
small clarifications
joepetrowski Mar 14, 2019
c8ec45a
trait implementation section
joepetrowski Mar 14, 2019
36b1689
missing ```
joepetrowski Mar 14, 2019
3c157e4
small changes, ready for review
joepetrowski Mar 14, 2019
a0ef9b7
line width update
joepetrowski Mar 15, 2019
2d28117
Merge branch 'master' into joe-balances-docs
joepetrowski Mar 18, 2019
df3ced9
Merge branch 'master' into joe-balances-docs
joepetrowski Mar 21, 2019
e42f6b6
small tweaks
joepetrowski Mar 25, 2019
e000871
Update srml/balances/src/lib.rs
gui1117 Mar 25, 2019
faccdbf
Update srml/balances/src/lib.rs
gui1117 Mar 25, 2019
513b377
Update srml/balances/src/lib.rs
gui1117 Mar 25, 2019
ce29349
Update srml/balances/src/lib.rs
gui1117 Mar 25, 2019
fa9f215
Update lib.rs
joepetrowski Mar 25, 2019
28a1d33
address review by thiolliere
joepetrowski Mar 25, 2019
aa43b56
remove common warning
joepetrowski Mar 26, 2019
191ecc0
Merge remote-tracking branch 'origin/master' into joe-balances-docs
gavofyork Mar 26, 2019
665fa6b
Update docs
gavofyork Mar 26, 2019
5f391ae
updated srml example
joepetrowski Mar 26, 2019
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
208 changes: 177 additions & 31 deletions srml/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,150 @@
// 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 balance, reserve and unreserve balance,
//! 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 not balance not reserved),
//! increasing total stake.
//! # Balances Module
//!
//! The balances module provides functionality for handling accounts and balances. To use the balances module, you need
//! to implement the [balances Trait](https://crates.parity.io/srml_balances/trait.Trait.html). Supported dispatchables
//! are documented in the [`Call` enum](https://crates.parity.io/srml_balances/enum.Call.html).
//!
//! ## Overview
//!
//! The balances module provides functions for:
//!
//! - Getting and setting free balances
//! - Retrieving total, reserved, and unreserved balances
//! - Repatriating a reserved balance to a beneficiary account that exists
//! - Transferring a balance between accounts (when not reserved)
//! - Slashing an account balance
//! - Account creation and removal
//! - Lookup of an index to reclaim an account
//! - Managing total issuance
//! - Setting and managing locks
//!
//! ### Terminology
//!
//! - **Existential Deposit:** The minimum balance required to create or keep an account open. This prevents
//! "dust accounts" from filling storage.
//! - **Total Issuance:** The total amount of units in existence in a system.
//! - **Reaping an account:** The act of removing an account by resetting its nonce. Happens after its balance is set
//! to zero.
//! - **Free Balance:** The liquid or spendable balance. The free balance is the only balance that matters for most
//! operations. When this balance falls below the existential deposit, the account is removed.
//! - **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.
//! - **Imbalance:** A condition when some funds were created or deducted without equal and opposite accounting.
//! Functions that result in an imbalance will return an object of the `Imbalance` trait that must be handled.
//! - **Lock:** A freeze on a specified amount of an account's balance until a specified block number.
//!
//! ### Implementations
//!
//! The balances module provides implementations for the following traits. If these traits provide the functionality
//! that you need, then you can avoid coupling with the balances module.
//!
//! - [`Currency`](https://crates.parity.io/srml_support/traits/trait.Currency.html): Functions for dealing with a
//! fungible assets system.
//! - [`LockableCurrency`](https://crates.parity.io/srml_support/traits/trait.LockableCurrency.html): Functions for
//! dealing with accounts that allow liquidity restrictions.
//! - [`Imbalance`](https://crates.parity.io/srml_support/traits/trait.Imbalance.html): Functions for handling
//! imbalances between total issuance in the system and account balances. Must be used when a function
//! creates new funds (e.g. a reward) or destroys some funds (e.g. a system fee).
//! - [`MakePayent`](https://crates.parity.io/srml_support/traits/trait.MakePayment.html): Simple trait designed
//! for hooking into a transaction payment.
//! - [`IsDeadAccount`](https://crates.parity.io/srml_system/trait.IsDeadAccount.html): Determiner to say whether a
//! given account is unused.
//!
//! Example of using the `Currency` trait from the treasury module:
//!
//! ```rust,ignore
//! pub trait Trait: system::Trait {
//! /// The staking balance.
//! type Currency: Currency<Self::AccountId>;
//! }
//! ```
//!
//! ## Interface
//!
//! ### Dispatchable Functions
//!
//! The `Call` enum is documented [here](https://crates.parity.io/srml_balances/enum.Call.html).
//!
//! - `transfer` - Transfer some liquid free balance to another account.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the unified quite full explaining top documentation but at the same time I feel like it may result in outdated documentation or unnecessary duplication, here I wonder if it not better to change to the same kind of redirection as module below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's why I kept the definitions really short here, so the detail is in the function documentation. I don't have a strong opinion about including them here or not, which is why I put the link to the Call documentation before the list.

//! - `set_balance` - Set the balances of a given account. The origin of this call must be root.
//!
//! ### Public Functions
//!
//! 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.
//!
//! ```rust,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)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the updated function name is transfer() instead of make_transfer

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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(())
//! }
//! }
//! }
//! ```
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 in the `contract` module (gas.rs):
//!
//! ```rust,ignore
//! pub fn refund_unused_gas<T: Trait>(transactor: &T::AccountId, gas_meter: GasMeter<T>) {
//! // Increase total spent gas.
//! // This cannot overflow, since `gas_spent` is never greater than `block_gas_limit`, which
//! // also has T::Gas type.
//! let gas_spent = <Module<T>>::gas_spent() + gas_meter.spent();
//! <GasSpent<T>>::put(gas_spent);
//!
//! // Refund gas left by the price it was bought.
//! let b = <balances::Module<T>>::free_balance(transactor);
//! let refund = <T::Gas as As<T::Balance>>::as_(gas_meter.gas_left) * gas_meter.gas_price;
//! <balances::Module<T>>::set_free_balance(transactor, b + refund);
//! <balances::Module<T>>::increase_total_stake_by(refund);
//! }
//! ```
//!
//! ## Genesis config
//!
//! The following storage items depend on the genesis config:
//!
//! - `TotalIssuance`
//! - `ExistentialDeposit`
//! - `TransferFee`
//! - `CreationFee`
//! - `Vesting`
//! - `FreeBalance`
//! - `TransactionBaseFee`
//! - `TransactionByteFee`
//!
//! ## 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)]

Expand All @@ -47,7 +184,7 @@ pub trait Subtrait<I: Instance = DefaultInstance>: system::Trait {
/// The balance of an account.
type Balance: Parameter + Member + SimpleArithmetic + Codec + Default + Copy + As<usize> + As<u64> + MaybeSerializeDebug;

/// A function which is invoked when the free-balance has fallen below the existential deposit and
/// A function that is invoked when the free-balance has fallen below the existential deposit and
/// has been reduced to zero.
///
/// Gives a chance to clean up resources associated with the given account.
Expand All @@ -61,7 +198,7 @@ pub trait Trait<I: Instance = DefaultInstance>: system::Trait {
/// The balance of an account.
type Balance: Parameter + Member + SimpleArithmetic + Codec + Default + Copy + As<usize> + As<u64> + MaybeSerializeDebug;

/// A function which is invoked when the free-balance has fallen below the existential deposit and
/// A function that is invoked when the free-balance has fallen below the existential deposit and
/// has been reduced to zero.
///
/// Gives a chance to clean up resources associated with the given account.
Expand Down Expand Up @@ -136,15 +273,15 @@ pub struct BalanceLock<Balance, BlockNumber> {

decl_storage! {
trait Store for Module<T: Trait<I>, I: Instance=DefaultInstance> as Balances {
/// The total amount of stake on the system.
/// The total units issued in the system.
pub TotalIssuance get(total_issuance) build(|config: &GenesisConfig<T, I>| {
config.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n)
}): T::Balance;
/// The minimum amount allowed to keep an account open.
/// The minimum amount required to keep an account open.
pub ExistentialDeposit get(existential_deposit) config(): T::Balance;
/// The fee required to make a transfer.
pub TransferFee get(transfer_fee) config(): T::Balance;
/// The fee required to create an account. At least as big as ReclaimRebate.
/// The fee required to create an account.
pub CreationFee get(creation_fee) config(): T::Balance;
/// The fee to be paid for making a transaction; the base.
pub TransactionBaseFee get(transaction_base_fee) config(): T::Balance;
Expand Down Expand Up @@ -175,11 +312,11 @@ decl_storage! {

/// The 'free' balance of a given account.
///
/// This is the only balance that matters in terms of most operations on tokens. It is
/// alone used to determine the balance when in the contract execution environment. When this
/// This is the only balance that matters in terms of most operations on tokens. It
/// alone is used to determine the balance when in the contract execution environment. When this
/// balance falls below the value of `ExistentialDeposit`, then the 'current account' is
/// deleted: specifically `FreeBalance`. Furthermore, `OnFreeBalanceZero` callback
/// is invoked, giving a chance to external modules to cleanup data associated with
/// deleted: specifically `FreeBalance`. Further, the `OnFreeBalanceZero` callback
/// is invoked, giving a chance to external modules to clean up data associated with
/// the deleted account.
///
/// `system::AccountNonce` is also deleted if `ReservedBalance` is also zero (it also gets
Expand All @@ -190,14 +327,13 @@ decl_storage! {
/// slashed, but gets slashed last of all.
///
/// This balance is a 'reserve' balance that other subsystems use in order to set aside tokens
/// that are still 'owned' by the account holder, but which are suspendable. (This is different
/// and wholly unrelated to the `Bondage` system used in the staking module.)
/// that are still 'owned' by the account holder, but which are suspendable.
///
/// When this balance falls below the value of `ExistentialDeposit`, then this 'reserve account'
/// is deleted: specifically, `ReservedBalance`.
///
/// `system::AccountNonce` is also deleted if `FreeBalance` is also zero (it also gets
/// collapsed to zero if it ever becomes less than `ExistentialDeposit`.
/// collapsed to zero if it ever becomes less than `ExistentialDeposit`.)
pub ReservedBalance get(reserved_balance): map T::AccountId => T::Balance;

/// Any liquidity locks on some account balances.
Expand All @@ -214,7 +350,14 @@ decl_module! {
pub struct Module<T: Trait<I>, I: Instance = DefaultInstance> for enum Call where origin: T::Origin {
fn deposit_event<T, I>() = default;

/// Transfer some liquid free balance to another staker.
/// Transfer some liquid free balance to another account.
///
/// `transfer` will set the `FreeBalance` of the sender and receiver.
/// It will decrease the total issuance of the system by the `TransferFee`.
/// 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,
Expand All @@ -226,6 +369,13 @@ decl_module! {
}

/// 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 issuance of the system (`TotalIssuance`)
/// and reset the account nonce (`system::AccountNonce`).
///
/// The dispatch origin for this call is `root`.
fn set_balance(
who: <T::Lookup as StaticLookup>::Source,
#[compact] free: T::Balance,
Expand All @@ -238,7 +388,6 @@ decl_module! {
}
}

// For funding methods, see Currency trait
impl<T: Trait<I>, I: Instance> Module<T, I> {

// PUBLIC IMMUTABLES
Expand All @@ -254,10 +403,11 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {

// PRIVATE MUTABLES

/// Set the free balance of an account to some new value.
/// Set the reserved balance of an account to some new value. Will enforce `ExistentialDeposit`
/// law, annulling the account as needed.
///
/// Will enforce ExistentialDeposit law, annulling the account as needed.
/// In that case it will return `AccountKilled`.
/// Doesn't do any preparatory work for creating a new account, so should only be used when it
/// is known that the account already exists.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to have a standardized preconditions documentation section, many method here have preconditions that have to be meet in order to ensure consistency.

///
/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
/// the caller will do this.
Expand All @@ -272,18 +422,16 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
}
}

/// Set the free balance of an account to some new value. Will enforce ExistentialDeposit
/// law annulling the account as needed.
/// Set the free balance of an account to some new value. Will enforce `ExistentialDeposit`
/// law, annulling the account as needed.
///
/// Doesn't do any preparatory work for creating a new account, so should only be used when it
/// is known that the account already exists.
///
/// Returns if the account was successfully updated or update has led to killing of the account.
///
/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
/// the caller will do this.
fn set_free_balance(who: &T::AccountId, balance: T::Balance) -> UpdateBalanceOutcome {
// Commented out for no - but consider it instructive.
// Commented out for now - but consider it instructive.
// assert!(!Self::total_balance(who).is_zero());
if balance < Self::existential_deposit() {
<FreeBalance<T, I>>::insert(who, balance);
Expand Down Expand Up @@ -670,8 +818,6 @@ where
// Free balance can never be less than ED. If that happens, it gets reduced to zero
// and the account information relevant to this subsystem is deleted (i.e. the
// account is reaped).
// NOTE: This is orthogonal to the `Bondage` value that an account has, a high
// value of which makes even the `free_balance` unspendable.
let outcome = if balance < <Module<T, I>>::existential_deposit() {
Self::set_free_balance(who, balance);
UpdateBalanceOutcome::AccountKilled
Expand Down
2 changes: 1 addition & 1 deletion srml/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ decl_event! {

decl_storage! {
trait Store for Module<T: Trait> as Contract {
/// The fee required to create a contract. At least as big as staking's ReclaimRebate.
/// The fee required to create a contract.
ContractFee get(contract_fee) config(): T::Balance = T::Balance::sa(21);
/// The fee charged for a call into a contract.
CallBaseFee get(call_base_fee) config(): T::Gas = T::Gas::sa(135);
Expand Down
Loading