-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
168 lines (132 loc) · 4.34 KB
/
keeper.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package keepers
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/coinexchain/cet-sdk/modules/stakingx/internal/types"
dex "github.com/coinexchain/cet-sdk/types"
)
var (
NonBondableAddressesKey = []byte("0x01")
)
type Keeper struct {
key sdk.StoreKey
cdc *codec.Codec
paramSubspace params.Subspace
assetViewKeeper AssetViewKeeper
sk *staking.Keeper
dk DistributionKeeper
ak auth.AccountKeeper
bk ExpectBankxKeeper
supplyKeeper ExpectSupplyKeeper
feeCollectorName string
}
func NewKeeper(key sdk.StoreKey, cdc *codec.Codec,
paramSubspace params.Subspace, assetViewKeeper AssetViewKeeper, sk *staking.Keeper,
dk DistributionKeeper, ak auth.AccountKeeper, bk ExpectBankxKeeper,
supplyKeeper ExpectSupplyKeeper, feeCollectorName string) Keeper {
return Keeper{
key: key,
cdc: cdc,
paramSubspace: paramSubspace.WithKeyTable(types.ParamKeyTable()),
assetViewKeeper: assetViewKeeper,
sk: sk,
dk: dk,
ak: ak,
bk: bk,
supplyKeeper: supplyKeeper,
feeCollectorName: feeCollectorName,
}
}
// -----------------------------------------------------------------------------
// Params
// SetParams sets the asset module's parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSubspace.SetParamSet(ctx, ¶ms)
}
// GetParams gets the asset module's parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSubspace.GetParamSet(ctx, ¶ms)
return
}
func (k Keeper) GetMinMandatoryCommissionRate(ctx sdk.Context) (rate sdk.Dec) {
k.paramSubspace.Get(ctx, types.KeyMinMandatoryCommissionRate, &rate)
return
}
// -----------------------------------------------------------------------------
// BondPoolStatus
func (k Keeper) CalcBondPoolStatus(ctx sdk.Context) BondPool {
total := k.supplyKeeper.GetSupply(ctx).GetTotal().AmountOf(dex.CET)
var bondPool BondPool
bondPool.TotalSupply = total
bondPool.BondedTokens = k.supplyKeeper.GetModuleAccount(ctx, staking.BondedPoolName).GetCoins().AmountOf(dex.CET)
bondPool.NotBondedTokens = k.supplyKeeper.GetModuleAccount(ctx, staking.NotBondedPoolName).GetCoins().AmountOf(dex.CET)
bondPool.NonBondableTokens = calcNonBondableTokens(ctx, &k)
bondPool.BondRatio = calcBondedRatio(&bondPool)
return bondPool
}
func calcBondedRatio(p *BondPool) sdk.Dec {
if p.BondedTokens.IsNegative() || p.NonBondableTokens.IsNegative() {
return sdk.ZeroDec()
}
bondableTokens := p.TotalSupply.Sub(p.NonBondableTokens)
if !bondableTokens.IsPositive() {
return sdk.ZeroDec()
}
return p.BondedTokens.ToDec().QuoInt(bondableTokens)
}
func calcNonBondableTokens(ctx sdk.Context, k *Keeper) sdk.Int {
ret := sdk.ZeroInt()
addrs := k.getNonBondableAddresses(ctx)
for _, addr := range addrs {
if acc := k.ak.GetAccount(ctx, addr); acc != nil {
if amt := acc.GetCoins().AmountOf(dex.CET); amt.IsPositive() {
ret = ret.Add(amt)
}
}
}
communityPoolAmt := k.dk.GetFeePoolCommunityCoins(ctx).AmountOf(dex.CET)
ret = ret.Add(communityPoolAmt.TruncateInt())
return ret
}
// -----------------------------------------------------------------------------
// Non-bondable addresses
func (k Keeper) GetCetOwnerAddress(ctx sdk.Context) sdk.AccAddress {
cet := k.assetViewKeeper.GetToken(ctx, dex.CET)
if cet == nil {
return nil
}
return cet.GetOwner()
}
func (k Keeper) GetAllVestingAccountAddresses(ctx sdk.Context) []sdk.AccAddress {
addresses := make([]sdk.AccAddress, 0, 8)
k.ak.IterateAccounts(ctx, func(acc auth.Account) bool {
if vacc, ok := acc.(auth.VestingAccount); ok {
addresses = append(addresses, vacc.GetAddress())
}
return false
})
return addresses
}
func (k Keeper) SetNonBondableAddresses(ctx sdk.Context, addresses []sdk.AccAddress) {
store := ctx.KVStore(k.key)
bz, err := k.cdc.MarshalBinaryBare(addresses)
if err != nil {
panic(err)
}
store.Set(NonBondableAddressesKey, bz)
}
func (k Keeper) getNonBondableAddresses(ctx sdk.Context) (addresses []sdk.AccAddress) {
store := ctx.KVStore(k.key)
bz := store.Get(NonBondableAddressesKey)
if bz == nil {
return
}
err := k.cdc.UnmarshalBinaryBare(bz, &addresses)
if err != nil {
panic(err) // TODO
}
return
}