-
Notifications
You must be signed in to change notification settings - Fork 3
/
genesis.go
98 lines (95 loc) · 3.37 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
package types
import (
"fmt"
)
// DefaultGenesis returns the default Capability genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
}
}
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (genState GenesisState) Validate() error {
if err := genState.Params.Validate(); err != nil {
return fmt.Errorf("invalid params: %w", err)
}
pairMap := map[uint64]Pair{}
for i, pair := range genState.Pairs {
if err := pair.Validate(); err != nil {
return fmt.Errorf("invalid pair at index %d: %w", i, err)
}
if pair.Id > genState.LastPairId {
return fmt.Errorf("pair at index %d has an id greater than last pair id: %d", i, pair.Id)
}
if _, ok := pairMap[pair.Id]; ok {
return fmt.Errorf("pair at index %d has a duplicate id: %d", i, pair.Id)
}
pairMap[pair.Id] = pair
}
poolMap := map[uint64]Pool{}
for i, pool := range genState.Pools {
if err := pool.Validate(); err != nil {
return fmt.Errorf("invalid pool at index %d: %w", i, err)
}
if pool.Id > genState.LastPoolId {
return fmt.Errorf("pool at index %d has an id greater than last pool id: %d", i, pool.Id)
}
if _, ok := poolMap[pool.Id]; ok {
return fmt.Errorf("pool at index %d has a duplicate pool id: %d", i, pool.Id)
}
poolMap[pool.Id] = pool
if _, ok := pairMap[pool.PairId]; !ok {
return fmt.Errorf("pool at index %d has unknown pair id: %d", i, pool.PairId)
}
}
for i, req := range genState.DepositRequests {
if err := req.Validate(); err != nil {
return fmt.Errorf("invalid deposit request at index %d: %w", i, err)
}
pool, ok := poolMap[req.PoolId]
if !ok {
return fmt.Errorf("deposit request at index %d has unknown pool id: %d", i, req.PoolId)
}
if req.MintedPoolCoin.Denom != pool.PoolCoinDenom {
return fmt.Errorf("deposit request at index %d has wrong minted pool coin: %s", i, req.MintedPoolCoin)
}
pair := pairMap[pool.PairId]
if req.DepositCoins.AmountOf(pair.BaseCoinDenom).IsZero() ||
req.DepositCoins.AmountOf(pair.QuoteCoinDenom).IsZero() {
return fmt.Errorf("deposit request at index %d has wrong deposit coins: %s", i, req.DepositCoins)
}
if !req.AcceptedCoins.IsZero() {
if req.AcceptedCoins.AmountOf(pair.BaseCoinDenom).IsZero() ||
req.AcceptedCoins.AmountOf(pair.QuoteCoinDenom).IsZero() {
return fmt.Errorf("deposit request at index %d has wrong accepted coins: %s", i, req.AcceptedCoins)
}
}
}
for i, req := range genState.WithdrawRequests {
if err := req.Validate(); err != nil {
return fmt.Errorf("invalid withdraw request at index %d: %w", i, err)
}
pool, ok := poolMap[req.PoolId]
if !ok {
return fmt.Errorf("withdraw request at index %d has unknown pool id: %d", i, req.PoolId)
}
if req.PoolCoin.Denom != pool.PoolCoinDenom {
return fmt.Errorf("withdraw request at index %d has wrong pool coin: %s", i, req.PoolCoin)
}
}
for i, req := range genState.SwapRequests {
if err := req.Validate(); err != nil {
return fmt.Errorf("invalid swap request at index %d: %w", i, err)
}
pair, ok := pairMap[req.PairId]
if !ok {
return fmt.Errorf("swap request at index %d has unknown pair id: %d", i, req.PairId)
}
if req.BatchId > pair.CurrentBatchId {
return fmt.Errorf("swap request at index %d has a batch id greater than its pair's current batch id: %d", i, req.BatchId)
}
// TODO: check denoms
}
return nil
}