-
Notifications
You must be signed in to change notification settings - Fork 180
/
genesis.go
45 lines (39 loc) · 1.33 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
package supply
import (
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
authexported "github.com/okex/exchain/libs/cosmos-sdk/x/auth/exported"
"github.com/okex/exchain/libs/cosmos-sdk/x/supply/internal/types"
)
// InitGenesis sets supply information for genesis.
//
// CONTRACT: all types of accounts must have been already initialized/created
func InitGenesis(ctx sdk.Context, keeper Keeper, ak types.AccountKeeper, data GenesisState) {
// manually set the total supply based on accounts if not provided
if data.Supply.Empty() {
var totalSupply sdk.Coins
ak.IterateAccounts(ctx,
func(acc authexported.Account) (stop bool) {
totalSupply = totalSupply.Add(acc.GetCoins()...)
return false
},
)
data.Supply = totalSupply
}
keeper.SetSupply(ctx, types.NewSupply(data.Supply))
}
// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
var coins sdk.DecCoins
for _, coin := range keeper.GetSupply(ctx).GetTotal() {
if coin.Amount.IsZero() {
continue
}
coins = coins.Add(coin)
}
return NewGenesisState(coins)
}
// ValidateGenesis performs basic validation of supply genesis data returning an
// error for any failed validation criteria.
func ValidateGenesis(data GenesisState) error {
return types.NewSupply(data.Supply).ValidateBasic()
}