Merge pull request #379 from Kava-Labs/kd-update-circulating-supply

Calculate circulating supply with respect to vesting
This commit is contained in:
Kevin Davis 2020-02-25 18:11:32 -05:00 committed by GitHub
commit 9b49f991f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 40 deletions

View File

@ -9,7 +9,6 @@ import (
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
) )
// GetQueryCmd returns the cli query commands for this module // GetQueryCmd returns the cli query commands for this module
@ -42,7 +41,7 @@ func QueryCirculatingSupplyCmd(queryRoute string, cdc *codec.Codec) *cobra.Comma
return err return err
} }
var out sdk.Dec var out int64
cdc.MustUnmarshalJSON(res, &out) cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out) return cliCtx.PrintOutput(out)
}, },
@ -64,7 +63,7 @@ func QueryTotalSupplyCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return err return err
} }
var out sdk.Dec var out int64
cdc.MustUnmarshalJSON(res, &out) cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out) return cliCtx.PrintOutput(out)
}, },

View File

@ -1,6 +1,7 @@
package rest package rest
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -39,16 +40,23 @@ func getTotalSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
} }
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply) 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 { if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return return
} }
var totalSupply int64
cliCtx = cliCtx.WithHeight(height) err = cliCtx.Codec.UnmarshalJSON(res, &totalSupply)
// directly write output instead of putting in json if err != nil {
w.Write(res) rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
// rest.PostProcessResponse(w, cliCtx, res) 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) 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 { if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return return
} }
cliCtx = cliCtx.WithHeight(height) var circSupply int64
// directly write output instead of putting in json err = cliCtx.Codec.UnmarshalJSON(res, &circSupply)
w.Write(res) if err != nil {
// rest.PostProcessResponse(w, cliCtx, res) 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)
} }
} }

View File

@ -1,12 +1,9 @@
package keeper package keeper
import ( import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
"github.com/cosmos/cosmos-sdk/x/auth/vesting" "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" "github.com/kava-labs/kava/x/validator-vesting/internal/types"
abci "github.com/tendermint/tendermint/abci/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) { func queryGetTotalSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
totalSupply := keeper.supplyKeeper.GetSupply(ctx).GetTotal().AmountOf("ukava") totalSupply := keeper.supplyKeeper.GetSupply(ctx).GetTotal().AmountOf("ukava")
supplyDec := sdk.NewDecFromInt(totalSupply).Mul(sdk.MustNewDecFromStr("0.000001")) supplyInt := sdk.NewDecFromInt(totalSupply).Mul(sdk.MustNewDecFromStr("0.000001")).TruncateInt64()
bz, err := codec.MarshalJSONIndent(keeper.cdc, supplyDec) bz, err := keeper.cdc.MarshalJSON(supplyInt)
if err != nil { if err != nil {
return nil, sdk.ErrInternal(err.Error()) 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) { 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, keeper.ak.IterateAccounts(ctx,
func(acc authexported.Account) (stop bool) { func(acc authexported.Account) (stop bool) {
// exclude module account
_, ok := acc.(supplyexported.ModuleAccountI) // validator vesting account
vvacc, ok := acc.(*types.ValidatorVestingAccount)
if ok { if ok {
vestedBalance := vvacc.GetVestingCoins(ctx.BlockTime()).AmountOf("ukava")
circulatingSupply = circulatingSupply.Sub(vestedBalance)
return false return false
} }
// periodic vesting account // periodic vesting account
vacc, ok := acc.(vesting.PeriodicVestingAccount) pvacc, ok := acc.(*vesting.PeriodicVestingAccount)
if ok { if ok {
balance := vacc.GetCoins().AmountOf("ukava") vestedBalance := pvacc.GetVestingCoins(ctx.BlockTime()).AmountOf("ukava")
if balance.IsZero() { circulatingSupply = circulatingSupply.Sub(vestedBalance)
return false
}
spendableBalance := vacc.SpendableCoins(ctx.BlockTime()).AmountOf("ukava")
circulatingSupply = circulatingSupply.Add(sdk.MinInt(balance, spendableBalance))
return false return false
} }
// base account
bacc, ok := acc.(*auth.BaseAccount)
if ok {
// add all coins
circulatingSupply = circulatingSupply.Add(bacc.GetCoins().AmountOf("ukava"))
}
return false return false
}) })
supplyDec := sdk.NewDecFromInt(circulatingSupply).Mul(sdk.MustNewDecFromStr("0.000001")) supplyInt := sdk.NewDecFromInt(circulatingSupply).Mul(sdk.MustNewDecFromStr("0.000001")).TruncateInt64()
bz, err := codec.MarshalJSONIndent(keeper.cdc, supplyDec) bz, err := keeper.cdc.MarshalJSON(supplyInt)
if err != nil { if err != nil {
return nil, sdk.ErrInternal(err.Error()) return nil, sdk.ErrInternal(err.Error())
} }