-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from terra-money/feat/bond-denom-supply
feat: return correct total supply of native token
- Loading branch information
Showing
7 changed files
with
157 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters