-
Notifications
You must be signed in to change notification settings - Fork 1
/
genesis.go
84 lines (68 loc) · 2.53 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
package dex
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fibonacci-chain/core/x/dex/keeper"
"github.com/fibonacci-chain/core/x/dex/types"
)
// InitGenesis initializes the dex module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
k.CreateModuleAccount(ctx)
// Set all the longBook
for _, contractState := range genState.ContractState {
err := k.SetContract(ctx, &contractState.ContractInfo)
if err != nil {
panic(err)
}
for _, elem := range contractState.PairList {
k.AddRegisteredPair(ctx, contractState.ContractInfo.ContractAddr, elem)
}
for _, elem := range contractState.LongBookList {
k.SetLongBook(ctx, contractState.ContractInfo.ContractAddr, elem)
}
for _, elem := range contractState.ShortBookList {
k.SetShortBook(ctx, contractState.ContractInfo.ContractAddr, elem)
}
for _, elem := range contractState.PriceList {
for _, priceElem := range elem.Prices {
k.SetPriceState(ctx, *priceElem, contractState.ContractInfo.ContractAddr)
}
}
k.SetNextOrderID(ctx, contractState.ContractInfo.ContractAddr, contractState.NextOrderId)
}
// this line is used by starport scaffolding # genesis/module/init
k.SetParams(ctx, genState.Params)
k.SetEpoch(ctx, genState.LastEpoch)
}
// ExportGenesis returns the dex module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)
allContractInfo := k.GetAllContractInfo(ctx)
contractStates := make([]types.ContractState, len(allContractInfo))
for i, contractInfo := range allContractInfo {
contractAddr := contractInfo.ContractAddr
registeredPairs := k.GetAllRegisteredPairs(ctx, contractAddr)
// Save all price info for contract, for all its pairs
contractPrices := []types.ContractPairPrices{}
for _, elem := range registeredPairs {
pairPrices := k.GetAllPrices(ctx, contractAddr, elem)
contractPrices = append(contractPrices, types.ContractPairPrices{
PricePair: elem,
Prices: pairPrices,
})
}
contractStates[i] = types.ContractState{
ContractInfo: contractInfo,
LongBookList: k.GetAllLongBook(ctx, contractAddr),
ShortBookList: k.GetAllShortBook(ctx, contractAddr),
PairList: registeredPairs,
PriceList: contractPrices,
NextOrderId: k.GetNextOrderID(ctx, contractAddr),
}
}
genesis.ContractState = contractStates
_, currentEpoch := k.IsNewEpoch(ctx)
genesis.LastEpoch = currentEpoch
return genesis
}