Newer
Older
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! # 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 portion of a balance that is not reserved. The free balance is the only balance that matters
//! for most operations. When this balance falls below the existential deposit, most functionality of the account is
//! removed. When both it and the reserved balance are deleted, then the account is said to be dead.
//! - **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 and any related functionality will be deleted. When both it and the free balance are
//! deleted, then the account is said to be dead.
//! - **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 free balance until a specified block number. Multiple
//! locks always operate over the same funds, so they "overlay" rather than "stack".
//! - **Vesting:** Similar to a lock, this is another, but independent, liquidity restriction that reduces linearly
//! over time.
//!
//! ### 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.
//! - [`ReservableCurrency`](https://crates.parity.io/srml_support/traits/trait.ReservableCurrency.html):
//! Functions for dealing with assets that can be reserved from an account.
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! - [`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.
//! - `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.
//!
//! ## 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)?;
//!
//! Ok(())
//! }
//! }
//! }
//! ```
//!
//! ### 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>,
//! imbalance: balances::NegativeImbalance<T>,
//! ) {
//! let gas_spent = gas_meter.spent();
//! let gas_left = gas_meter.gas_left();
//!
//! // Increase total spent gas.
//! <GasSpent<T>>::mutate(|block_gas_spent| *block_gas_spent += gas_spent);
//!
//! let refund = <T::Gas as As<T::Balance>>::as_(gas_left) * gas_meter.gas_price;
//! // Refund gas using balances module
//! let refund_imbalance = <balances::Module<T>>::deposit_creating(transactor, refund);
//! // Handle imbalance
//! if let Ok(imbalance) = imbalance.offset(refund_imbalance) {
//! T::GasPayment::on_unbalanced(imbalance);
//! }
//! }
//! ```
//!
//! ## 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)]
use rstd::prelude::*;
use rstd::{cmp, result};
use parity_codec::{Codec, Encode, Decode};
use srml_support::{StorageValue, StorageMap, Parameter, decl_event, decl_storage, decl_module};
UpdateBalanceOutcome, Currency, OnFreeBalanceZero, MakePayment, OnUnbalanced,
WithdrawReason, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement,
Imbalance, SignedImbalance, ReservableCurrency
use srml_support::dispatch::Result;
Zero, SimpleArithmetic, As, StaticLookup, Member, CheckedAdd, CheckedSub,
MaybeSerializeDebug, Saturating
use system::{IsDeadAccount, OnNewAccount, ensure_signed};
mod mock;
mod tests;
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 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.
Loading full blame...