From fb7dcd7d3657a89cbb7f6c5a58ee4461e894b774 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Tue, 25 Feb 2020 18:03:40 -0500 Subject: [PATCH] feat: calculate circulating supply with respect to vesting --- x/validator-vesting/client/cli/query.go | 5 +-- x/validator-vesting/client/rest/query.go | 39 +++++++++++++------ .../internal/keeper/querier.go | 38 +++++++----------- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/x/validator-vesting/client/cli/query.go b/x/validator-vesting/client/cli/query.go index 1bf19c4a..5b13bff7 100644 --- a/x/validator-vesting/client/cli/query.go +++ b/x/validator-vesting/client/cli/query.go @@ -9,7 +9,6 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" ) // GetQueryCmd returns the cli query commands for this module @@ -42,7 +41,7 @@ func QueryCirculatingSupplyCmd(queryRoute string, cdc *codec.Codec) *cobra.Comma return err } - var out sdk.Dec + var out int64 cdc.MustUnmarshalJSON(res, &out) return cliCtx.PrintOutput(out) }, @@ -64,7 +63,7 @@ func QueryTotalSupplyCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { return err } - var out sdk.Dec + var out int64 cdc.MustUnmarshalJSON(res, &out) return cliCtx.PrintOutput(out) }, diff --git a/x/validator-vesting/client/rest/query.go b/x/validator-vesting/client/rest/query.go index 3dd853c0..3dbf8a3e 100644 --- a/x/validator-vesting/client/rest/query.go +++ b/x/validator-vesting/client/rest/query.go @@ -1,6 +1,7 @@ package rest import ( + "encoding/json" "fmt" "net/http" @@ -39,16 +40,23 @@ func getTotalSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply) - res, height, err := cliCtx.QueryWithData(route, bz) + res, _, err := cliCtx.QueryWithData(route, bz) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } - - cliCtx = cliCtx.WithHeight(height) - // directly write output instead of putting in json - w.Write(res) - // rest.PostProcessResponse(w, cliCtx, res) + var totalSupply int64 + err = cliCtx.Codec.UnmarshalJSON(res, &totalSupply) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + resBytes, err := json.Marshal(totalSupply) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + w.Write(resBytes) } } @@ -73,16 +81,23 @@ func getCirculatingSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryCirculatingSupply) - res, height, err := cliCtx.QueryWithData(route, bz) + res, _, err := cliCtx.QueryWithData(route, bz) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } - cliCtx = cliCtx.WithHeight(height) - // directly write output instead of putting in json - w.Write(res) - // rest.PostProcessResponse(w, cliCtx, res) + var circSupply int64 + err = cliCtx.Codec.UnmarshalJSON(res, &circSupply) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + resBytes, err := json.Marshal(circSupply) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + w.Write(resBytes) } - } diff --git a/x/validator-vesting/internal/keeper/querier.go b/x/validator-vesting/internal/keeper/querier.go index f9319008..dafc5f5b 100644 --- a/x/validator-vesting/internal/keeper/querier.go +++ b/x/validator-vesting/internal/keeper/querier.go @@ -1,12 +1,9 @@ package keeper import ( - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/auth/vesting" - supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported" "github.com/kava-labs/kava/x/validator-vesting/internal/types" abci "github.com/tendermint/tendermint/abci/types" ) @@ -27,8 +24,8 @@ func NewQuerier(keeper Keeper) sdk.Querier { func queryGetTotalSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) { totalSupply := keeper.supplyKeeper.GetSupply(ctx).GetTotal().AmountOf("ukava") - supplyDec := sdk.NewDecFromInt(totalSupply).Mul(sdk.MustNewDecFromStr("0.000001")) - bz, err := codec.MarshalJSONIndent(keeper.cdc, supplyDec) + supplyInt := sdk.NewDecFromInt(totalSupply).Mul(sdk.MustNewDecFromStr("0.000001")).TruncateInt64() + bz, err := keeper.cdc.MarshalJSON(supplyInt) if err != nil { return nil, sdk.ErrInternal(err.Error()) } @@ -36,37 +33,28 @@ func queryGetTotalSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) } func queryGetCirculatingSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) { - circulatingSupply := sdk.ZeroInt() + circulatingSupply := keeper.supplyKeeper.GetSupply(ctx).GetTotal().AmountOf("ukava") keeper.ak.IterateAccounts(ctx, func(acc authexported.Account) (stop bool) { - // exclude module account - _, ok := acc.(supplyexported.ModuleAccountI) + + // validator vesting account + vvacc, ok := acc.(*types.ValidatorVestingAccount) if ok { + vestedBalance := vvacc.GetVestingCoins(ctx.BlockTime()).AmountOf("ukava") + circulatingSupply = circulatingSupply.Sub(vestedBalance) return false } - // periodic vesting account - vacc, ok := acc.(vesting.PeriodicVestingAccount) + pvacc, ok := acc.(*vesting.PeriodicVestingAccount) if ok { - balance := vacc.GetCoins().AmountOf("ukava") - if balance.IsZero() { - return false - } - spendableBalance := vacc.SpendableCoins(ctx.BlockTime()).AmountOf("ukava") - circulatingSupply = circulatingSupply.Add(sdk.MinInt(balance, spendableBalance)) + vestedBalance := pvacc.GetVestingCoins(ctx.BlockTime()).AmountOf("ukava") + circulatingSupply = circulatingSupply.Sub(vestedBalance) return false } - - // base account - bacc, ok := acc.(*auth.BaseAccount) - if ok { - // add all coins - circulatingSupply = circulatingSupply.Add(bacc.GetCoins().AmountOf("ukava")) - } return false }) - supplyDec := sdk.NewDecFromInt(circulatingSupply).Mul(sdk.MustNewDecFromStr("0.000001")) - bz, err := codec.MarshalJSONIndent(keeper.cdc, supplyDec) + supplyInt := sdk.NewDecFromInt(circulatingSupply).Mul(sdk.MustNewDecFromStr("0.000001")).TruncateInt64() + bz, err := keeper.cdc.MarshalJSON(supplyInt) if err != nil { return nil, sdk.ErrInternal(err.Error()) }