[][src]Crate srml_balances

Balances Module

The balances module provides functionality for handling accounts and balances. To use the balances module, you need to implement the balances Trait. Supported dispatchables are documented in the Call enum.

Overview

The balances module provides functions for:

Terminology

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.

Example of using the Currency trait from the treasury module:

This example is not tested
pub trait Trait: system::Trait {
	/// The staking balance.
	type Currency: Currency<Self::AccountId>;
}

Interface

Dispatchable Functions

The Call enum is documented here.

Public Functions

See the module 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.

This example is not tested
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):

This example is not tested
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:

The balances module depends on the system and srml_support modules as well as Substrate Core libraries and the Rust standard library.

Structs

BalanceLock
CreationFee

The fee required to create an account.

DefaultInstance

Default module instance

ExistentialDeposit

The minimum amount required to keep an account open.

FreeBalance

The 'free' balance of a given account.

GenesisConfig
Instance0

Module instance

Instance1

Module instance

Instance2

Module instance

Instance3

Module instance

Instance4

Module instance

Instance5

Module instance

Instance6

Module instance

Instance7

Module instance

Instance8

Module instance

Instance9

Module instance

Instance10

Module instance

Instance11

Module instance

Instance12

Module instance

Instance13

Module instance

Instance14

Module instance

Instance15

Module instance

Locks

Any liquidity locks on some account balances.

Module
NegativeImbalance

Opaque, move-only struct with private fields that serves as a token denoting that funds have been destroyed without any equal and opposite accounting.

PositiveImbalance

Opaque, move-only struct with private fields that serves as a token denoting that funds have been created without any equal and opposite accounting.

ReservedBalance

The amount of the balance of a given account that is externally reserved; this can still get slashed, but gets slashed last of all.

TotalIssuance

The total units issued in the system.

TransactionBaseFee

The fee to be paid for making a transaction; the base.

TransactionByteFee

The fee to be paid for making a transaction; the per-byte portion.

TransferFee

The fee required to make a transfer.

Vesting

Information regarding the vesting of a given account.

VestingSchedule

Struct to encode the vesting schedule of an individual account.

Enums

Call
RawEvent

Events for this module.

Traits

Instance

Tag a type as an instance of a module.

Subtrait
Trait

Type Definitions

Event

RawEvent specialized for the configuration Trait