Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Documentation for balances module #1943

Merged
merged 35 commits into from
Mar 26, 2019
Merged
Changes from 1 commit
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
139 changes: 109 additions & 30 deletions srml/balances/README.adoc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Balances Module
= Balances Module

The balances module provides the functionality for handling balances.

## Overview
== Overview

The balances module provides functions for:

Expand All @@ -18,32 +18,99 @@ The balances module provides functions for:

The dispatchable function `transfer` ensures that the sender has signed the transaction. When using the publicly exposed functions in the implementation, you will need to do these checks in your runtime, as many functions will affect storage without ensuring, for example, that the sender is the signer.

## Public Interface
== Public Interface

### Types
=== Types

- Balance
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved
- AccountId
- OnFreeBalanceZero
- OnNewAccount
- Event

### Dispatchable Functions
These are link:https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#specifying-placeholder-types-in-trait-definitions-with-associated-types[associated types] and must be implemented in your `runtime/src/lib.rs`. For example:

```rust
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

// TODO: Add link to rust docs (https://github.com/paritytech/substrate-developer-hub/issues/24)
- `transfer` - Transfer some liquid free balance to another staker.
- `set_balance` - Set the balances of a given account. Only dispatchable by a user with root priviledges.

## Usage
== Usage

The following example shows how to use the balances module in your custom module.

1. Import the `balances` module and derive your module configuration trait with the balances trait.
=== Importing into your runtime

Import the `balances` module and derive your module configuration trait with the balances trait.

Include in your `runtime/Cargo.toml`

```rust
use balances;
[dependencies.balances]
default_features = false
git = 'https://github.com/paritytech/substrate.git'
package = 'srml-balances'
rev = '82744fbb6f4d677f2edfe9d88737c237622c97a4'
```
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved

Include in your `runtime/src/lib.rs`

```rust
// Import
pub use balances::Call as BalancesCall;

// Implement
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 ubiquitous event type.
type Event = Event;
}

// Include in your `construct_runtime`
construct_runtime!(
pub enum Runtime with Log(InternalLog: DigestItem<Hash, Ed25519AuthorityId>) where
Block = Block,
NodeBlock = opaque::Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
// snip
Indices: indices,
Balances: balances,
Sudo: sudo,
// snip
}
);
```

Derive your module configuration trait in your `runtime/src/<your-node-name>.rs`

pub trait Trait: balances::Trait { }
```rust
pub trait Trait: balances::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
```

2. In your module, call the balance module:
==== Example of getters

You now have access to the functions in `balances`. In your module, call the getters:

```rust
// Get existential deposit
Expand All @@ -53,32 +120,44 @@ let ed = Balances::existential_deposit();
let tf = Balances::transfer_fee();
```

3. Include a balance transfer in a block (example from the `executor` module tests):
==== Real Use Example

Use a balance transfer to buy a link:https://github.com/shawntabrizi/substrate-collectables-workshop/blob/master/3/assets/3.5-finished-code.rs#L105[SubstrateKitty]:

```rust
let mut t = new_test_ext(COMPACT_CODE, false);
let block1 = construct_block(
&mut t,
1,
GENESIS_HASH.into(),
vec![
CheckedExtrinsic {
signed: None,
function: Call::Timestamp(timestamp::Call::set(42)),
},
CheckedExtrinsic {
signed: Some((alice(), 0)),
function: Call::Balances(balances::Call::transfer(bob().into(), 69)),
},
]
);
fn buy_kitty(origin, kitty_id: T::Hash, max_price: T::Balance) -> Result {
let sender = ensure_signed(origin)?;

ensure!(<Kitties<T>>::exists(kitty_id), "This kitty does not exist");

let owner = Self::owner_of(kitty_id).ok_or("No owner for this kitty")?;
ensure!(owner != sender, "You can't buy your own kitty");

let mut kitty = Self::kitty(kitty_id);

let kitty_price = kitty.price;
ensure!(!kitty_price.is_zero(), "The kitty you want to buy is not for sale");
ensure!(kitty_price <= max_price, "The kitty you want to buy costs more than your max price");

// Make a balance transfer
<balances::Module<T>>::make_transfer(&sender, &owner, kitty_price)?;

Self::_transfer_from(owner.clone(), sender.clone(), kitty_id)?;
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved

kitty.price = <T::Balance as As<u64>>::sa(0);
<Kitties<T>>::insert(kitty_id, kitty);

Self::deposit_event(RawEvent::Bought(sender, owner, kitty_id, kitty_price));

Ok(())
}
```

## Dependencies
== Dependencies

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

### Genesis config
=== Genesis config

Configuration is in `<your-node-name>/src/chain_spec.rs`. The following storage items are configurable:

Expand Down