0g-chain/x/validator-vesting/internal/keeper/querier.go
John Maheswaran 51fc70b5ac
[R4R] This is the implementation of the total circulating supply (#376)
* adding empty query files where we will implement the cli and rest interfaces

* adding querier file

* adding aliases, querier, expected keepers, types, module updates

* starting to work on query

* adding alias for QueryCirculatingSupply

* adding TotalCirculatingSupply type

* adding alias for TotalCirculatingSupply type

* adding QueryCirculatingSupplyCmd to clieng query file

* adding register routes function

* adding rest query handling function, register endpoint

* fix types include statement

* remove unused mux include

* another import fix

* remove unused variable

* adding rest to module

* fix missing variable names

* another missing variable fix

* remove dead code comment

* fix typo in circulating-supply

* fix import

* fix querierroute return to return the module name instead of empty string

* rename function, fix build issue

* return error instead of nil if there is an error

* import types from cosmos sdk, fix return type

* set querier route to module name in key.go

* adding query and rest updates

* aliasing new querying circulating supply function

* directly write output

* fix routes

* adding total supply functionality

* converting from microkava to kava

* Update x/validator-vesting/client/cli/query.go

typo fix

Co-Authored-By: Kevin Davis <karzak@users.noreply.github.com>

Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
2020-02-25 10:29:54 -05:00

75 lines
2.4 KiB
Go

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"
)
// NewQuerier returns a new querier function
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case types.QueryCirculatingSupply:
return queryGetCirculatingSupply(ctx, req, keeper)
case types.QueryTotalSupply:
return queryGetTotalSupply(ctx, req, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown cdp query endpoint")
}
}
}
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)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryGetCirculatingSupply(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
circulatingSupply := sdk.ZeroInt()
keeper.ak.IterateAccounts(ctx,
func(acc authexported.Account) (stop bool) {
// exclude module account
_, ok := acc.(supplyexported.ModuleAccountI)
if ok {
return false
}
// periodic vesting account
vacc, 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))
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)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}