Substrate Balances模块分析

2019-11-25  本文已影响0人  建怀

Substrate Balances模块分析

const

Storage

Extrinsics

Balacnes模块提供以下方法:

balances模块的术语:

balances实现

Balances模块提供了下面的一些实现traits,如果这些traits提供的方法是开发者需要的,那么开发者就尽量要避免跟Balances模块耦合。

初始化genesis config中设置

/// The total units issued in the system.
pub TotalIssuance get(fn total_issuance) build(|config: &GenesisConfig<T, I>| {
    config.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n)
}): T::Balance;

config.vesting.iter().filter_map(|&(ref who, begin, length, liquid)| {
    let length = <T::Balance as From<T::BlockNumber>>::from(length);

    config.balances.iter()
        .find(|&&(ref w, _)| w == who)
        .map(|&(_, balance)| {
            // Total genesis `balance` minus `liquid` equals funds locked for vesting
            let locked = balance.saturating_sub(liquid);
            // Number of units unlocked per block after `begin`
            let per_block = locked / length.max(sr_primitives::traits::One::one());

            (who.clone(), VestingSchedule {
                locked: locked,
                per_block: per_block,
                starting_block: begin
            })
        })
}).collect::<Vec<_>>()

add_extra_genesis {
    config(balances): Vec<(T::AccountId, T::Balance)>;
    config(vesting): Vec<(T::AccountId, T::BlockNumber, T::BlockNumber, T::Balance)>;
    // ^^ begin, length, amount liquid at genesis
    build(|config: &GenesisConfig<T, I>| {
        for (_, balance) in &config.balances {
            assert!(
                *balance >= <T as Trait<I>>::ExistentialDeposit::get(),
                "the balance of any account should always be more than existential deposit.",
            )
        }
    });
}
上一篇下一篇

猜你喜欢

热点阅读