-
Notifications
You must be signed in to change notification settings - Fork 48
/
keeper.go
84 lines (68 loc) · 2.93 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
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/tendermint/tendermint/libs/log"
"github.com/desmos-labs/desmos/v3/x/supply/types"
)
type Keeper struct {
cdc codec.BinaryCodec
ak authkeeper.AccountKeeper
bk bankkeeper.Keeper
dk distributionkeeper.Keeper
}
// NewKeeper creates new instances of the supply keeper
func NewKeeper(cdc codec.BinaryCodec,
ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, dk distributionkeeper.Keeper) Keeper {
return Keeper{
cdc: cdc,
ak: ak,
bk: bk,
dk: dk,
}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
// GetTotalSupply returns the total supply computed using the following formula:
// total_supply = total_supply / divider
func (k Keeper) GetTotalSupply(ctx sdk.Context, coinDenom string, divider sdk.Int) sdk.Int {
totalSupply := k.bk.GetSupply(ctx, coinDenom)
return totalSupply.Amount.Quo(divider)
}
// GetCirculatingSupply returns the circulating supply computed using the following formula:
// circulating_supply = (total_supply - community_pool - vested_amount) / divider
func (k Keeper) GetCirculatingSupply(ctx sdk.Context, coinDenom string, divider sdk.Int) sdk.Int {
var circulatingSupply sdk.Int
// Get total supply
totalSupply := k.bk.GetSupply(ctx, coinDenom)
// Get the community pool denom amount
communityPoolDenomAmount := k.dk.GetFeePoolCommunityCoins(ctx).AmountOf(coinDenom)
// Subtract community pool amount from the total supply
circulatingSupply = totalSupply.Amount.Sub(communityPoolDenomAmount.RoundInt())
// Subtract all vesting account locked tokens amount from the circulating supply
k.ak.IterateAccounts(ctx, func(account authtypes.AccountI) (stop bool) {
if vestingAcc, ok := account.(exported.VestingAccount); ok {
circulatingSupply = subtractVestingAccountDenomAmounts(circulatingSupply, vestingAcc, coinDenom)
}
return false
})
// Convert the circulating supply with the divider factor
convertedCirculatingSupply := circulatingSupply.Quo(divider)
return convertedCirculatingSupply
}
// subtractVestingAccountDenomAmounts subtract the given vesting account denom amount from the
// circulating supply
func subtractVestingAccountDenomAmounts(circulatingSupply sdk.Int,
vestingAccount exported.VestingAccount, denom string) sdk.Int {
originalVesting := vestingAccount.GetOriginalVesting()
delegatedFree := vestingAccount.GetDelegatedFree()
originalVestingAmount := originalVesting.AmountOf(denom)
delegatedFreeAmount := delegatedFree.AmountOf(denom)
return circulatingSupply.Sub(originalVestingAmount).Sub(delegatedFreeAmount)
}