mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 23:35:19 +00:00
[R4R] Restore supply endpoints (#586)
* feat: restore supply endpoints * use complete circulating supply schedule * standardize query path
This commit is contained in:
parent
037700388b
commit
db2b237e1d
81
x/validator-vesting/client/cli/query.go
Normal file
81
x/validator-vesting/client/cli/query.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/client/context"
|
||||||
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/x/validator-vesting/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetQueryCmd returns the cli query commands for the kavadist module
|
||||||
|
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||||
|
valVestingQueryCmd := &cobra.Command{
|
||||||
|
Use: types.QueryPath,
|
||||||
|
Short: "Querying commands for the validator vesting module",
|
||||||
|
}
|
||||||
|
|
||||||
|
valVestingQueryCmd.AddCommand(flags.GetCommands(
|
||||||
|
queryCirculatingSupply(queryRoute, cdc),
|
||||||
|
queryTotalSupply(queryRoute, cdc),
|
||||||
|
)...)
|
||||||
|
|
||||||
|
return valVestingQueryCmd
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func queryCirculatingSupply(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||||
|
return &cobra.Command{
|
||||||
|
Use: "circulating-supply",
|
||||||
|
Short: "Get circulating supply",
|
||||||
|
Long: "Get the current circulating supply of kava tokens",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||||
|
|
||||||
|
// Query
|
||||||
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryCirculatingSupply), nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cliCtx = cliCtx.WithHeight(height)
|
||||||
|
|
||||||
|
// Decode and print results
|
||||||
|
var out int64
|
||||||
|
if err := cdc.UnmarshalJSON(res, &out); err != nil {
|
||||||
|
return fmt.Errorf("failed to unmarshal supply: %w", err)
|
||||||
|
}
|
||||||
|
return cliCtx.PrintOutput(out)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func queryTotalSupply(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||||
|
return &cobra.Command{
|
||||||
|
Use: "total-supply",
|
||||||
|
Short: "Get total supply",
|
||||||
|
Long: "Get the current total supply of kava tokens",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||||
|
|
||||||
|
// Query
|
||||||
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryTotalSupply), nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cliCtx = cliCtx.WithHeight(height)
|
||||||
|
|
||||||
|
// Decode and print results
|
||||||
|
var out int64
|
||||||
|
if err := cdc.UnmarshalJSON(res, &out); err != nil {
|
||||||
|
return fmt.Errorf("failed to unmarshal supply: %w", err)
|
||||||
|
}
|
||||||
|
return cliCtx.PrintOutput(out)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
102
x/validator-vesting/client/rest/query.go
Normal file
102
x/validator-vesting/client/rest/query.go
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
package rest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/client/context"
|
||||||
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/x/validator-vesting/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
||||||
|
r.HandleFunc(fmt.Sprintf("/%s/circulatingsupply", types.QueryPath), getCirculatingSupplyHandlerFn(cliCtx)).Methods("GET")
|
||||||
|
r.HandleFunc(fmt.Sprintf("/%s/totalsupply", types.QueryPath), getTotalSupplyHandlerFn(cliCtx)).Methods("GET")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTotalSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
params := types.NewBaseQueryParams(page, limit)
|
||||||
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply)
|
||||||
|
res, _, err := cliCtx.QueryWithData(route, bz)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCirculatingSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
params := types.NewBaseQueryParams(page, limit)
|
||||||
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryCirculatingSupply)
|
||||||
|
res, _, err := cliCtx.QueryWithData(route, bz)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
12
x/validator-vesting/client/rest/rest.go
Normal file
12
x/validator-vesting/client/rest/rest.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package rest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/client/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegisterRoutes registers kavadist-related REST handlers to a router
|
||||||
|
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
||||||
|
registerQueryRoutes(cliCtx, r)
|
||||||
|
}
|
@ -1,10 +1,10 @@
|
|||||||
package keeper
|
package keeper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
|
|
||||||
|
|
||||||
"github.com/kava-labs/kava/x/validator-vesting/types"
|
"github.com/kava-labs/kava/x/validator-vesting/types"
|
||||||
|
|
||||||
@ -36,30 +36,51 @@ func queryGetTotalSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func queryGetCirculatingSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
func queryGetCirculatingSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
||||||
circulatingSupply := keeper.supplyKeeper.GetSupply(ctx).GetTotal().AmountOf("ukava")
|
supplyInt := getCirculatingSupply(ctx.BlockTime())
|
||||||
keeper.ak.IterateAccounts(ctx,
|
bz, err := keeper.cdc.MarshalJSON(supplyInt)
|
||||||
func(acc authexported.Account) (stop bool) {
|
|
||||||
|
|
||||||
// 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
|
|
||||||
pvacc, ok := acc.(*vesting.PeriodicVestingAccount)
|
|
||||||
if ok {
|
|
||||||
vestedBalance := pvacc.GetVestingCoins(ctx.BlockTime()).AmountOf("ukava")
|
|
||||||
circulatingSupply = circulatingSupply.Sub(vestedBalance)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
supplyInt := sdk.NewDecFromInt(circulatingSupply).Mul(sdk.MustNewDecFromStr("0.000001")).TruncateInt64()
|
|
||||||
bz, err := types.ModuleCdc.MarshalJSON(supplyInt)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
||||||
}
|
}
|
||||||
return bz, nil
|
return bz, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCirculatingSupply(blockTime time.Time) sdk.Int {
|
||||||
|
vestingDates := []time.Time{
|
||||||
|
time.Date(2020, 9, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
time.Date(2020, 11, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
time.Date(2021, 2, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
time.Date(2021, 5, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
time.Date(2021, 8, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
time.Date(2021, 11, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
time.Date(2022, 2, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
time.Date(2022, 5, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
time.Date(2022, 8, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
time.Date(2022, 11, 5, 14, 0, 0, 0, time.UTC),
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case blockTime.Before(vestingDates[0]):
|
||||||
|
return sdk.NewInt(27190672)
|
||||||
|
case blockTime.After(vestingDates[0]) && blockTime.Before(vestingDates[1]) || blockTime.Equal(vestingDates[0]):
|
||||||
|
return sdk.NewInt(29442227)
|
||||||
|
case blockTime.After(vestingDates[1]) && blockTime.Before(vestingDates[2]) || blockTime.Equal(vestingDates[1]):
|
||||||
|
return sdk.NewInt(46876230)
|
||||||
|
case blockTime.After(vestingDates[2]) && blockTime.Before(vestingDates[3]) || blockTime.Equal(vestingDates[2]):
|
||||||
|
return sdk.NewInt(58524186)
|
||||||
|
case blockTime.After(vestingDates[3]) && blockTime.Before(vestingDates[4]) || blockTime.Equal(vestingDates[3]):
|
||||||
|
return sdk.NewInt(70172142)
|
||||||
|
case blockTime.After(vestingDates[4]) && blockTime.Before(vestingDates[5]) || blockTime.Equal(vestingDates[4]):
|
||||||
|
return sdk.NewInt(81443180)
|
||||||
|
case blockTime.After(vestingDates[5]) && blockTime.Before(vestingDates[6]) || blockTime.Equal(vestingDates[5]):
|
||||||
|
return sdk.NewInt(90625000)
|
||||||
|
case blockTime.After(vestingDates[6]) && blockTime.Before(vestingDates[7]) || blockTime.Equal(vestingDates[6]):
|
||||||
|
return sdk.NewInt(92968750)
|
||||||
|
case blockTime.After(vestingDates[7]) && blockTime.Before(vestingDates[8]) || blockTime.Equal(vestingDates[7]):
|
||||||
|
return sdk.NewInt(95312500)
|
||||||
|
case blockTime.After(vestingDates[8]) && blockTime.Before(vestingDates[9]) || blockTime.Equal(vestingDates[8]):
|
||||||
|
return sdk.NewInt(97656250)
|
||||||
|
default:
|
||||||
|
return sdk.NewInt(100000000)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -15,6 +15,8 @@ import (
|
|||||||
"github.com/cosmos/cosmos-sdk/types/module"
|
"github.com/cosmos/cosmos-sdk/types/module"
|
||||||
sim "github.com/cosmos/cosmos-sdk/x/simulation"
|
sim "github.com/cosmos/cosmos-sdk/x/simulation"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/x/validator-vesting/client/cli"
|
||||||
|
"github.com/kava-labs/kava/x/validator-vesting/client/rest"
|
||||||
"github.com/kava-labs/kava/x/validator-vesting/simulation"
|
"github.com/kava-labs/kava/x/validator-vesting/simulation"
|
||||||
"github.com/kava-labs/kava/x/validator-vesting/types"
|
"github.com/kava-labs/kava/x/validator-vesting/types"
|
||||||
)
|
)
|
||||||
@ -54,14 +56,16 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RegisterRESTRoutes registers no REST routes for the crisis module.
|
// RegisterRESTRoutes registers no REST routes for the crisis module.
|
||||||
func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {}
|
func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
|
||||||
|
rest.RegisterRoutes(ctx, rtr)
|
||||||
|
}
|
||||||
|
|
||||||
// GetTxCmd returns no root tx command for the validator-vesting module.
|
// GetTxCmd returns no root tx command for the validator-vesting module.
|
||||||
func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil }
|
func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil }
|
||||||
|
|
||||||
// GetQueryCmd returns no root query command for the validator-vesting module.
|
// GetQueryCmd returns no root query command for the validator-vesting module.
|
||||||
func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
|
func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
|
||||||
return nil
|
return cli.GetQueryCmd(types.StoreKey, cdc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppModule implements an application module for the validator-vesting module.
|
// AppModule implements an application module for the validator-vesting module.
|
||||||
|
@ -13,6 +13,9 @@ const (
|
|||||||
|
|
||||||
// QuerierRoute should be set to module name
|
// QuerierRoute should be set to module name
|
||||||
QuerierRoute = ModuleName
|
QuerierRoute = ModuleName
|
||||||
|
|
||||||
|
// QueryPath shortened name for public API (cli and REST)
|
||||||
|
QueryPath = "vesting"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
Loading…
Reference in New Issue
Block a user