Skip to content

Commit

Permalink
Merge pull request #47 from terra-money/feat/bond-denom-supply
Browse files Browse the repository at this point in the history
feat: return correct total supply of native token
  • Loading branch information
javiersuweijie authored Nov 18, 2022
2 parents 0d95d2f + 6146a9a commit 8ea3a87
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 7 deletions.
11 changes: 7 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/capability"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
Expand Down Expand Up @@ -100,6 +99,8 @@ import (
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
dbm "github.com/tendermint/tm-db"
custombankmodule "github.com/terra-money/alliance/custom/bank"
custombankkeeper "github.com/terra-money/alliance/custom/bank/keeper"

"github.com/ignite/cli/ignite/pkg/cosmoscmd"
"github.com/ignite/cli/ignite/pkg/openapiconsole"
Expand Down Expand Up @@ -223,7 +224,7 @@ type App struct {
// keepers
AccountKeeper authkeeper.AccountKeeper
AuthzKeeper authzkeeper.Keeper
BankKeeper bankkeeper.Keeper
BankKeeper custombankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
Expand Down Expand Up @@ -339,7 +340,7 @@ func New(
app.AccountKeeper,
)

app.BankKeeper = bankkeeper.NewBaseKeeper(
app.BankKeeper = custombankkeeper.NewBaseKeeper(
appCodec,
keys[banktypes.StoreKey],
app.AccountKeeper,
Expand Down Expand Up @@ -427,6 +428,8 @@ func New(
app.DistrKeeper,
)

app.BankKeeper.RegisterKeepers(app.AllianceKeeper, &stakingKeeper)

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.StakingKeeper = *stakingKeeper.SetHooks(
Expand Down Expand Up @@ -530,7 +533,7 @@ func New(
auth.NewAppModule(appCodec, app.AccountKeeper, nil),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
custombankmodule.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
Expand Down
94 changes: 94 additions & 0 deletions custom/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package keeper

import (
"context"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
banktypes "github.com/terra-money/alliance/custom/bank/types"
alliancekeeper "github.com/terra-money/alliance/x/alliance/keeper"
alliancetypes "github.com/terra-money/alliance/x/alliance/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type Keeper struct {
bankkeeper.BaseKeeper

ak alliancekeeper.Keeper
sk banktypes.StakingKeeper
acck accountkeeper.AccountKeeper
}

var (
_ bankkeeper.Keeper = Keeper{}
)

func NewBaseKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
ak accountkeeper.AccountKeeper,
paramSpace paramtypes.Subspace,
blockedAddrs map[string]bool,
) Keeper {
keeper := Keeper{
BaseKeeper: bankkeeper.NewBaseKeeper(cdc, storeKey, ak, paramSpace, blockedAddrs),
ak: alliancekeeper.Keeper{},
sk: stakingkeeper.Keeper{},
acck: ak,
}
return keeper
}

func (k *Keeper) RegisterKeepers(ak alliancekeeper.Keeper, sk banktypes.StakingKeeper) {
k.ak = ak
k.sk = sk
}

// SupplyOf implements the Query/SupplyOf gRPC method
func (k Keeper) SupplyOf(c context.Context, req *types.QuerySupplyOfRequest) (*types.QuerySupplyOfResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if req.Denom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid denom")
}

ctx := sdk.UnwrapSDKContext(c)
supply := k.GetSupply(ctx, req.Denom)

if req.Denom == k.sk.BondDenom(ctx) {
assets := k.ak.GetAllAssets(ctx)
totalRewardWeights := sdk.ZeroDec()
for _, asset := range assets {
totalRewardWeights = totalRewardWeights.Add(asset.RewardWeight)
}
allianceBonded := k.ak.GetAllianceBondedAmount(ctx, k.acck.GetModuleAddress(alliancetypes.ModuleName))
supply.Amount = supply.Amount.Sub(allianceBonded)
}

return &types.QuerySupplyOfResponse{Amount: sdk.NewCoin(req.Denom, supply.Amount)}, nil
}

// TotalSupply implements the Query/TotalSupply gRPC method
func (k Keeper) TotalSupply(ctx context.Context, req *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
totalSupply, pageRes, err := k.GetPaginatedTotalSupply(sdkCtx, req.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

allianceBonded := k.ak.GetAllianceBondedAmount(sdkCtx, k.acck.GetModuleAddress(alliancetypes.ModuleName))
bondDenom := k.sk.BondDenom(sdkCtx)
if totalSupply.AmountOf(bondDenom).IsPositive() {
totalSupply = totalSupply.Sub(sdk.NewCoin(bondDenom, allianceBonded))
}

return &types.QueryTotalSupplyResponse{Supply: totalSupply, Pagination: pageRes}, nil
}
45 changes: 45 additions & 0 deletions custom/bank/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package bank

import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
bankmodule "github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
custombankkeeper "github.com/terra-money/alliance/custom/bank/keeper"
)

// AppModule wraps around the bank module and the bank keeper to return the right total supply ignoring bonded tokens
// that the alliance module minted to rebalance the voting power
// It modifies the TotalSupply and SupplyOf GRPC queries
type AppModule struct {
bankmodule.AppModule
keeper custombankkeeper.Keeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper custombankkeeper.Keeper, accountKeeper types.AccountKeeper) AppModule {
bankModule := bankmodule.NewAppModule(cdc, keeper, accountKeeper)
return AppModule{
AppModule: bankModule,
keeper: keeper,
}
}

// RegisterServices registers module services.
// NOTE: Overriding this method as not doing so will cause a panic
// when trying to force this custom keeper into a bankkeeper.BaseKeeper
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), bankkeeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

m := bankkeeper.NewMigrator(am.keeper.BaseKeeper)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/bank from version 1 to 2: %v", err))
}

if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
panic(fmt.Sprintf("failed to migrate x/bank from version 2 to 3: %v", err))
}
}
7 changes: 7 additions & 0 deletions custom/bank/types/keeper_interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

import sdk "github.com/cosmos/cosmos-sdk/types"

type StakingKeeper interface {
BondDenom(ctx sdk.Context) (res string)
}
1 change: 1 addition & 0 deletions x/alliance/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package alliance

import (
"fmt"
"time"

"github.com/terra-money/alliance/x/alliance/keeper"
Expand Down
2 changes: 1 addition & 1 deletion x/alliance/keeper/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (k Keeper) RebalanceHook(ctx sdk.Context, assets []*types.AllianceAsset) er
// the difference.
func (k Keeper) RebalanceBondTokenWeights(ctx sdk.Context, assets []*types.AllianceAsset) (err error) {
moduleAddr := k.accountKeeper.GetModuleAddress(types.ModuleName)
allianceBondAmount := k.getAllianceBondedAmount(ctx, moduleAddr)
allianceBondAmount := k.GetAllianceBondedAmount(ctx, moduleAddr)

nativeBondAmount := k.stakingKeeper.TotalBondedTokens(ctx).Sub(allianceBondAmount)
bondDenom := k.stakingKeeper.BondDenom(ctx)
Expand Down
4 changes: 2 additions & 2 deletions x/alliance/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,9 @@ func (k Keeper) updateValidatorShares(ctx sdk.Context, validator types.AllianceV
k.SetValidator(ctx, validator)
}

// getAllianceBondedAmount returns the total amount of bonded native tokens that are not in the
// GetAllianceBondedAmount returns the total amount of bonded native tokens that are not in the
// unbonding pool
func (k Keeper) getAllianceBondedAmount(ctx sdk.Context, delegator sdk.AccAddress) math.Int {
func (k Keeper) GetAllianceBondedAmount(ctx sdk.Context, delegator sdk.AccAddress) math.Int {
bonded := sdk.ZeroDec()
k.stakingKeeper.IterateDelegatorDelegations(ctx, delegator, func(delegation stakingtypes.Delegation) bool {
validatorAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
Expand Down

0 comments on commit 8ea3a87

Please sign in to comment.