-
Notifications
You must be signed in to change notification settings - Fork 590
/
genesis.go
60 lines (53 loc) · 1.46 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
package gamm
import (
"fmt"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/x/gamm/keeper"
"github.com/osmosis-labs/osmosis/x/gamm/types"
)
// InitGenesis initializes the capability module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState, unpacker codectypes.AnyUnpacker) {
k.SetNextPoolNumber(ctx, genState.NextPoolNumber)
liquidity := sdk.Coins{}
for _, any := range genState.Pools {
var pool types.PoolI
err := unpacker.UnpackAny(any, &pool)
if err != nil {
panic(err)
}
err = k.SetPool(ctx, pool)
if err != nil {
panic(err)
}
poolAssets := pool.GetAllPoolAssets()
for _, asset := range poolAssets {
liquidity = liquidity.Add(asset.Token)
}
}
k.SetTotalLiquidity(ctx, liquidity)
}
// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
pools, err := k.GetPools(ctx)
if err != nil {
panic(err)
}
poolAnys := []*codectypes.Any{}
for _, poolI := range pools {
pool, ok := poolI.(*types.Pool)
if !ok {
panic(fmt.Errorf("pool (%d) is not basic pool", poolI.GetId()))
}
any, err := codectypes.NewAnyWithValue(pool)
if err != nil {
panic(err)
}
poolAnys = append(poolAnys, any)
}
return &types.GenesisState{
NextPoolNumber: k.GetNextPoolNumber(ctx),
Pools: poolAnys,
}
}