A simple, secure module for dealing with fungible assets.
The Assets module provides functionality for asset management of fungible asset classes with a fixed supply, including:
To use it in your runtime, you need to implement the assets Trait
.
The supported dispatchable functions are documented in the Call
enum.
The assets system in Substrate is designed to make the following possible:
issue
- Issues the total supply of a new fungible asset to the account of the caller of the function.transfer
- Transfers an amount
of units of fungible asset id
from the balance of
the function caller's account (origin
) to a target
account.destroy
- Destroys the entire holding of a fungible asset id
associated with the account
that called the function.Please refer to the Call
enum and its associated variants for documentation on each function.
balance
- Get the asset id
balance of who
.total_supply
- Get the total supply of an asset id
.Please refer to the Module
struct for details on publicly available functions.
The following example shows how to use the Assets module in your runtime by exposing public functions to:
Import the Assets module and types and derive your runtime's configuration traits from the Assets module trait.
```rust use palletassets as assets; use framesupport::{declmodule, dispatch, ensure}; use framesystem::ensure_signed;
pub trait Trait: assets::Trait { }
declmodule! {
pub struct Module
const ACCOUNT_ALICE: u64 = 1;
const ACCOUNT_BOB: u64 = 2;
const COUNT_AIRDROP_RECIPIENTS: u64 = 2;
const TOKENS_FIXED_SUPPLY: u64 = 100;
ensure!(!COUNT_AIRDROP_RECIPIENTS.is_zero(), "Divide by zero error.");
let asset_id = Self::next_asset_id();
<NextAssetId<T>>::mutate(|asset_id| *asset_id += 1);
<Balances<T>>::insert((asset_id, &ACCOUNT_ALICE), TOKENS_FIXED_SUPPLY / COUNT_AIRDROP_RECIPIENTS);
<Balances<T>>::insert((asset_id, &ACCOUNT_BOB), TOKENS_FIXED_SUPPLY / COUNT_AIRDROP_RECIPIENTS);
<TotalSupply<T>>::insert(asset_id, TOKENS_FIXED_SUPPLY);
Self::deposit_event(RawEvent::Issued(asset_id, sender, TOKENS_FIXED_SUPPLY));
Ok(())
}
}
} ```
Below are assumptions that must be held when using this module. If any of them are violated, the behavior of this module is undefined.
Trait::AssetId::max_value()
.License: Apache-2.0