/
genesis.go
78 lines (68 loc) · 1.93 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
package types
import (
"fmt"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type poolShares struct {
totalShares sdkmath.Int
totalSharesOwned sdkmath.Int
}
var (
// DefaultPoolRecords is used to set default records in default genesis state
DefaultPoolRecords = PoolRecords{}
// DefaultShareRecords is used to set default records in default genesis state
DefaultShareRecords = ShareRecords{}
)
// NewGenesisState creates a new genesis state.
func NewGenesisState(params Params, poolRecords PoolRecords, shareRecords ShareRecords) GenesisState {
return GenesisState{
Params: params,
PoolRecords: poolRecords,
ShareRecords: shareRecords,
}
}
// Validate validates the module's genesis state
func (gs GenesisState) Validate() error {
if err := gs.Params.Validate(); err != nil {
return err
}
if err := gs.PoolRecords.Validate(); err != nil {
return err
}
if err := gs.ShareRecords.Validate(); err != nil {
return err
}
totalShares := make(map[string]poolShares)
for _, pr := range gs.PoolRecords {
totalShares[pr.PoolID] = poolShares{
totalShares: pr.TotalShares,
totalSharesOwned: sdk.ZeroInt(),
}
}
for _, sr := range gs.ShareRecords {
if shares, found := totalShares[sr.PoolID]; found {
shares.totalSharesOwned = shares.totalSharesOwned.Add(sr.SharesOwned)
totalShares[sr.PoolID] = shares
} else {
totalShares[sr.PoolID] = poolShares{
totalShares: sdk.ZeroInt(),
totalSharesOwned: sr.SharesOwned,
}
}
}
for poolID, ps := range totalShares {
if !ps.totalShares.Equal(ps.totalSharesOwned) {
return fmt.Errorf("total depositor shares %s not equal to pool '%s' total shares %s", ps.totalSharesOwned.String(), poolID, ps.totalShares.String())
}
}
return nil
}
// DefaultGenesisState returns a default genesis state
func DefaultGenesisState() GenesisState {
return NewGenesisState(
DefaultParams(),
DefaultPoolRecords,
DefaultShareRecords,
)
}