2020-02-25 15:29:54 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-23 16:35:58 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-02-25 15:29:54 +00:00
|
|
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-04-04 23:26:15 +00:00
|
|
|
"github.com/kava-labs/kava/x/validator-vesting/types"
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-02-25 15:29:54 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier returns a new querier function
|
|
|
|
func NewQuerier(keeper Keeper) sdk.Querier {
|
2020-04-23 16:35:58 +00:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
|
2020-02-25 15:29:54 +00:00
|
|
|
switch path[0] {
|
|
|
|
case types.QueryCirculatingSupply:
|
|
|
|
return queryGetCirculatingSupply(ctx, req, keeper)
|
|
|
|
case types.QueryTotalSupply:
|
|
|
|
return queryGetTotalSupply(ctx, req, keeper)
|
|
|
|
default:
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
|
2020-02-25 15:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryGetTotalSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-02-25 15:29:54 +00:00
|
|
|
totalSupply := keeper.supplyKeeper.GetSupply(ctx).GetTotal().AmountOf("ukava")
|
2020-02-25 23:03:40 +00:00
|
|
|
supplyInt := sdk.NewDecFromInt(totalSupply).Mul(sdk.MustNewDecFromStr("0.000001")).TruncateInt64()
|
2020-04-23 16:35:58 +00:00
|
|
|
bz, err := types.ModuleCdc.MarshalJSON(supplyInt)
|
2020-02-25 15:29:54 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-02-25 15:29:54 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryGetCirculatingSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-02-25 23:03:40 +00:00
|
|
|
circulatingSupply := keeper.supplyKeeper.GetSupply(ctx).GetTotal().AmountOf("ukava")
|
2020-02-25 15:29:54 +00:00
|
|
|
keeper.ak.IterateAccounts(ctx,
|
|
|
|
func(acc authexported.Account) (stop bool) {
|
2020-02-25 23:03:40 +00:00
|
|
|
|
|
|
|
// validator vesting account
|
|
|
|
vvacc, ok := acc.(*types.ValidatorVestingAccount)
|
2020-02-25 15:29:54 +00:00
|
|
|
if ok {
|
2020-02-25 23:03:40 +00:00
|
|
|
vestedBalance := vvacc.GetVestingCoins(ctx.BlockTime()).AmountOf("ukava")
|
|
|
|
circulatingSupply = circulatingSupply.Sub(vestedBalance)
|
2020-02-25 15:29:54 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// periodic vesting account
|
2020-02-25 23:03:40 +00:00
|
|
|
pvacc, ok := acc.(*vesting.PeriodicVestingAccount)
|
2020-02-25 15:29:54 +00:00
|
|
|
if ok {
|
2020-02-25 23:03:40 +00:00
|
|
|
vestedBalance := pvacc.GetVestingCoins(ctx.BlockTime()).AmountOf("ukava")
|
|
|
|
circulatingSupply = circulatingSupply.Sub(vestedBalance)
|
2020-02-25 15:29:54 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2020-02-25 23:03:40 +00:00
|
|
|
supplyInt := sdk.NewDecFromInt(circulatingSupply).Mul(sdk.MustNewDecFromStr("0.000001")).TruncateInt64()
|
2020-04-23 16:35:58 +00:00
|
|
|
bz, err := types.ModuleCdc.MarshalJSON(supplyInt)
|
2020-02-25 15:29:54 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-02-25 15:29:54 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|