-
Notifications
You must be signed in to change notification settings - Fork 4
/
genesis.go
66 lines (57 loc) · 2.1 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
package ammswap
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
tokentypes "github.com/zenchainprotocol/zenchain-node/x/token/types"
"github.com/zenchainprotocol/zenchain-node/x/ammswap/types"
)
// GenesisState stores genesis data, all slashing state that must be provided at genesis
type GenesisState struct {
Params Params `json:"params"`
SwapTokenPairRecords []SwapTokenPair `json:"swap_token_pair_records"`
}
// nolint
func NewGenesisState(swapTokenPairRecords []SwapTokenPair) GenesisState {
return GenesisState{SwapTokenPairRecords: nil}
}
// ValidateGenesis validates the format of the specified genesisState
func ValidateGenesis(data GenesisState) error {
for _, record := range data.SwapTokenPairRecords {
if !record.QuotePooledCoin.IsValid() {
return fmt.Errorf("invalid SwapTokenPairRecord: QuotePooledCoin: %s", record.QuotePooledCoin.String())
}
if !record.BasePooledCoin.IsValid() {
return fmt.Errorf("invalid SwapTokenPairRecord: BasePooledCoin: %s", record.BasePooledCoin)
}
if !tokentypes.NotAllowedOriginSymbol(record.PoolTokenName) {
return fmt.Errorf("invalid SwapTokenPairRecord: PoolToken: %s. Error: invalid PoolToken", record.PoolTokenName)
}
}
return nil
}
// nolint
func DefaultGenesisState() GenesisState {
return GenesisState{
Params: types.DefaultParams(),
SwapTokenPairRecords: nil,
}
}
// InitGenesis init genesis data to keeper
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
keeper.SetParams(ctx, data.Params)
for _, record := range data.SwapTokenPairRecords {
keeper.SetSwapTokenPair(ctx, record.TokenPairName(), record)
}
}
// ExportGenesis exports genesis from keeper
func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
var records []SwapTokenPair
iterator := k.GetSwapTokenPairsIterator(ctx)
for ; iterator.Valid(); iterator.Next() {
tokenPair := SwapTokenPair{}
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &tokenPair)
records = append(records, tokenPair)
}
params := k.GetParams(ctx)
return GenesisState{SwapTokenPairRecords: records, Params: params}
}