mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
feat: calculate circulating supply with respect to vesting
This commit is contained in:
parent
7e78f37788
commit
fb7dcd7d36
@ -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)
|
||||
},
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user