-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
122 lines (106 loc) · 3.15 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package token
import (
"errors"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fastock/fastock-chain/x/common"
"github.com/fastock/fastock-chain/x/token/types"
)
// default owner of okt
const DefaultTokenOwner = "blockchain10q0rk5qnyag7wfvvt7rtphlw589m7frsku8qc9"
// all state that must be provided in genesis file
type GenesisState struct {
Params types.Params `json:"params"`
Tokens []types.Token `json:"tokens"`
LockedAssets []types.AccCoins `json:"locked_assets"`
LockedFees []types.AccCoins `json:"locked_fees"`
}
// default GenesisState used by Cosmos Hub
func defaultGenesisState() GenesisState {
return GenesisState{
Params: types.DefaultParams(),
Tokens: []types.Token{defaultGenesisStateOKT()},
LockedAssets: nil,
LockedFees: nil,
}
}
// default okt information
func defaultGenesisStateOKT() types.Token {
addr, err := sdk.AccAddressFromBech32(DefaultTokenOwner)
if err != nil {
panic(err)
}
totalSupply := sdk.NewDec(1000000000)
return types.Token{
Description: "OK Group Global Utility Token",
Symbol: common.NativeToken,
OriginalSymbol: common.NativeToken,
WholeName: "OKT",
OriginalTotalSupply: totalSupply,
Owner: addr,
Mintable: true,
}
}
func validateGenesis(data GenesisState) error {
for _, token := range data.Tokens {
msg := types.NewMsgTokenIssue(token.Description,
token.Symbol,
token.OriginalSymbol,
token.WholeName,
token.OriginalTotalSupply.String(),
token.Owner,
token.Mintable)
err := msg.ValidateBasic()
if err != nil {
return errors.New(err.Error())
}
}
return nil
}
// initGenesis initialize default parameters
// and the keeper's address to pubkey map
func initGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
// if module account dosen't exist, it will create automatically
moduleAcc := keeper.supplyKeeper.GetModuleAccount(ctx, types.ModuleName)
if moduleAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
// set params
keeper.SetParams(ctx, data.Params)
for _, token := range data.Tokens {
keeper.NewToken(ctx, token)
}
for _, lock := range data.LockedAssets {
if err := keeper.updateLockedCoins(ctx, lock.Acc, lock.Coins, true, types.LockCoinsTypeQuantity); err != nil {
panic(err)
}
}
for _, lock := range data.LockedFees {
if err := keeper.updateLockedCoins(ctx, lock.Acc, lock.Coins, true, types.LockCoinsTypeFee); err != nil {
panic(err)
}
}
}
// ExportGenesis writes the current store values
// to a genesis file, which can be imported again
// with initGenesis
func ExportGenesis(ctx sdk.Context, keeper Keeper) (data GenesisState) {
params := keeper.GetParams(ctx)
tokens := keeper.GetTokensInfo(ctx)
lockedAsset := keeper.GetAllLockedCoins(ctx)
var lockedFees []types.AccCoins
keeper.IterateLockedFees(ctx, func(acc sdk.AccAddress, coins sdk.SysCoins) bool {
lockedFees = append(lockedFees,
types.AccCoins{
Acc: acc,
Coins: coins,
})
return false
})
return GenesisState{
Params: params,
Tokens: tokens,
LockedAssets: lockedAsset,
LockedFees: lockedFees,
}
}