-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create a query that directly returns all module accounts withou…
…t pagination or iteration (#987) * add skeleton for query * temp commit * rename var * add accounts with balances into response * add module accounts for query * add test for query * add changelog
- Loading branch information
Showing
14 changed files
with
1,288 additions
and
2 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
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,35 @@ | ||
syntax = "proto3"; | ||
|
||
package nibiru.util.v1; | ||
|
||
import "gogoproto/gogo.proto"; | ||
import "google/api/annotations.proto"; | ||
import "cosmos/base/v1beta1/coin.proto"; | ||
import "vpool/v1/state.proto"; | ||
|
||
option go_package = "github.com/NibiruChain/nibiru/x/util/types"; | ||
|
||
// Query defines the gRPC querier service. | ||
service Query { | ||
|
||
// Queries the reserve assets in a given pool, identified by a token pair. | ||
rpc ModuleAccounts(QueryModuleAccountsRequest) returns (QueryModuleAccountsResponse) { | ||
option (google.api.http).get = "/nibiru/util/module_accounts"; | ||
} | ||
} | ||
|
||
// ---------------------------------------- | ||
|
||
message QueryModuleAccountsRequest {} | ||
|
||
message QueryModuleAccountsResponse { | ||
repeated AccountWithBalance accounts = 1 [(gogoproto.nullable) = false]; | ||
} | ||
|
||
message AccountWithBalance { | ||
string name = 1; | ||
string address = 2; | ||
|
||
repeated cosmos.base.v1beta1.Coin balance = 3 | ||
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"]; | ||
} |
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,58 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/spf13/cobra" | ||
|
||
utiltypes "github.com/NibiruChain/nibiru/x/util/types" | ||
) | ||
|
||
// GetQueryCmd returns the cli query commands for this module | ||
func GetQueryCmd() *cobra.Command { | ||
queryCmd := &cobra.Command{ | ||
Use: utiltypes.ModuleName, | ||
Short: fmt.Sprintf( | ||
"Querying commands for the %s module", utiltypes.ModuleName), | ||
DisableFlagParsing: true, | ||
SuggestionsMinimumDistance: 2, | ||
RunE: client.ValidateCmd, | ||
} | ||
|
||
for _, cmd := range []*cobra.Command{ | ||
CmdQueryModuleAccounts(), | ||
} { | ||
queryCmd.AddCommand(cmd) | ||
} | ||
|
||
return queryCmd | ||
} | ||
|
||
func CmdQueryModuleAccounts() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "module-accounts", | ||
Short: "shows all the module accounts in the blockchain", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientQueryContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
queryClient := utiltypes.NewQueryClient(clientCtx) | ||
|
||
res, err := queryClient.ModuleAccounts(cmd.Context(), &utiltypes.QueryModuleAccountsRequest{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return clientCtx.PrintProto(res) | ||
}, | ||
} | ||
|
||
flags.AddQueryFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
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,18 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
||
utiltypes "github.com/NibiruChain/nibiru/x/util/types" | ||
) | ||
|
||
// NewHandler ... | ||
func NewHandler() sdk.Handler { | ||
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { | ||
errMsg := fmt.Sprintf("unrecognized %s message type: %T", utiltypes.ModuleName, msg) | ||
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) | ||
} | ||
} |
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,41 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/types" | ||
|
||
utiltypes "github.com/NibiruChain/nibiru/x/util/types" | ||
) | ||
|
||
type queryServer struct { | ||
k utiltypes.BankKeeper | ||
} | ||
|
||
func NewQueryServer(k utiltypes.BankKeeper) utiltypes.QueryServer { | ||
return &queryServer{k: k} | ||
} | ||
|
||
func (q queryServer) ModuleAccounts( | ||
ctx context.Context, | ||
_ *utiltypes.QueryModuleAccountsRequest, | ||
) (*utiltypes.QueryModuleAccountsResponse, error) { | ||
sdkContext := sdk.UnwrapSDKContext(ctx) | ||
|
||
var moduleAccountsWithBalances []utiltypes.AccountWithBalance | ||
for _, acc := range utiltypes.ModuleAccounts { | ||
account := types.NewModuleAddress(acc) | ||
|
||
balances := q.k.GetAllBalances(sdkContext, account) | ||
|
||
accWithBalance := utiltypes.AccountWithBalance{ | ||
Name: acc, | ||
Address: account.String(), | ||
Balance: balances, | ||
} | ||
moduleAccountsWithBalances = append(moduleAccountsWithBalances, accWithBalance) | ||
} | ||
|
||
return &utiltypes.QueryModuleAccountsResponse{Accounts: moduleAccountsWithBalances}, 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,40 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdktypes "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/NibiruChain/nibiru/simapp" | ||
"github.com/NibiruChain/nibiru/x/util/keeper" | ||
"github.com/NibiruChain/nibiru/x/util/types" | ||
) | ||
|
||
func TestQueryServer_ModuleAccounts(t *testing.T) { | ||
app, ctx := simapp.NewTestNibiruAppAndContext(false) | ||
goCtx := sdktypes.WrapSDKContext(ctx) | ||
|
||
qServer := keeper.NewQueryServer(app.BankKeeper) | ||
|
||
t.Log("query accounts and check empty balance") | ||
accounts, err := qServer.ModuleAccounts(goCtx, &types.QueryModuleAccountsRequest{}) | ||
require.NoError(t, err) | ||
require.Len(t, accounts.Accounts, len(types.ModuleAccounts)) | ||
require.Equal(t, accounts.Accounts[0].Balance, sdktypes.Coins{}) | ||
|
||
t.Log("we send some money") | ||
someModuleAccount := types.ModuleAccounts[0] | ||
err = app.BankKeeper.MintCoins( | ||
ctx, | ||
someModuleAccount, | ||
sdktypes.NewCoins(sdktypes.NewInt64Coin("uniques", 1_000_000)), | ||
) | ||
require.NoError(t, err) | ||
|
||
t.Log("we check that it returns some balance") | ||
accounts, err = qServer.ModuleAccounts(goCtx, &types.QueryModuleAccountsRequest{}) | ||
require.NoError(t, err) | ||
require.Len(t, accounts.Accounts, len(types.ModuleAccounts)) | ||
require.Equal(t, accounts.Accounts[0].Balance, sdktypes.NewCoins(sdktypes.NewInt64Coin("uniques", 1_000_000))) | ||
} |
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,74 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
"github.com/gorilla/mux" | ||
"github.com/grpc-ecosystem/grpc-gateway/runtime" | ||
"github.com/spf13/cobra" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
|
||
"github.com/NibiruChain/nibiru/x/util/client/cli" | ||
"github.com/NibiruChain/nibiru/x/util/keeper" | ||
utiltypes "github.com/NibiruChain/nibiru/x/util/types" | ||
) | ||
|
||
var ( | ||
_ module.AppModule = AppModule{} | ||
) | ||
|
||
type AppModule struct { | ||
bankKeeper utiltypes.BankKeeper | ||
} | ||
|
||
func NewAppModule(bk utiltypes.BankKeeper) *AppModule { | ||
return &AppModule{ | ||
bankKeeper: bk, | ||
} | ||
} | ||
|
||
func (a AppModule) Name() string { | ||
return "util" | ||
} | ||
|
||
func (a AppModule) RegisterLegacyAminoCodec(*codec.LegacyAmino) {} | ||
func (a AppModule) RegisterInterfaces(codectypes.InterfaceRegistry) {} | ||
func (a AppModule) DefaultGenesis(codec.JSONCodec) json.RawMessage { return nil } | ||
func (a AppModule) ValidateGenesis(codec.JSONCodec, client.TxEncodingConfig, json.RawMessage) error { | ||
return nil | ||
} | ||
func (a AppModule) RegisterRESTRoutes(client.Context, *mux.Router) {} | ||
func (a AppModule) RegisterGRPCGatewayRoutes(client.Context, *runtime.ServeMux) {} | ||
func (a AppModule) GetTxCmd() *cobra.Command { return nil } | ||
func (a AppModule) GetQueryCmd() *cobra.Command { return cli.GetQueryCmd() } | ||
func (a AppModule) InitGenesis(sdk.Context, codec.JSONCodec, json.RawMessage) []abci.ValidatorUpdate { | ||
return nil | ||
} | ||
func (a AppModule) ExportGenesis(sdk.Context, codec.JSONCodec) json.RawMessage { | ||
return nil | ||
} | ||
func (a AppModule) RegisterInvariants(sdk.InvariantRegistry) {} | ||
func (a AppModule) Route() sdk.Route { | ||
return sdk.NewRoute("", NewHandler()) | ||
} | ||
func (a AppModule) QuerierRoute() string { | ||
return "" | ||
} | ||
func (a AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier { | ||
return nil | ||
} | ||
func (a AppModule) RegisterServices(cfg module.Configurator) { | ||
utiltypes.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(a.bankKeeper)) | ||
} | ||
func (a AppModule) ConsensusVersion() uint64 { | ||
return 1 | ||
} | ||
func (a AppModule) BeginBlock(sdk.Context, abci.RequestBeginBlock) {} | ||
func (a AppModule) EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate { | ||
return 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,8 @@ | ||
package types | ||
|
||
import sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
// BankKeeper defines the expected interface needed to retrieve account balances. | ||
type BankKeeper interface { | ||
GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins | ||
} |
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,6 @@ | ||
package types | ||
|
||
const ( | ||
ModuleName = "util" | ||
RouterKey = ModuleName | ||
) |
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,14 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/NibiruChain/nibiru/x/common" | ||
perptypes "github.com/NibiruChain/nibiru/x/perp/types" | ||
) | ||
|
||
var ModuleAccounts = []string{ | ||
perptypes.ModuleName, | ||
perptypes.VaultModuleAccount, | ||
perptypes.PerpEFModuleAccount, | ||
perptypes.FeePoolModuleAccount, | ||
common.TreasuryPoolModuleAccount, | ||
} |
Oops, something went wrong.