Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/go_modules/github.com/spf13/vip…
Browse files Browse the repository at this point in the history
…er-1.17.0
  • Loading branch information
Unique-Divine authored Nov 16, 2023
2 parents b3378c1 + 3b2d087 commit 72bddcf
Show file tree
Hide file tree
Showing 14 changed files with 657 additions and 81 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bump `github.com/CosmWasm/wasmd` from 0.40.2 to 0.43.0 ([#1660](https://github.com/NibiruChain/nibiru/pull/1660))
- Bump `github.com/CosmWasm/wasmd` from 0.43.0 to 0.44.0 ([#1666](https://github.com/NibiruChain/nibiru/pull/1666))
- Bump `github.com/spf13/cobra` from 1.7.0 to 1.8.0 ([#1664](https://github.com/NibiruChain/nibiru/pull/1664))
- Bump `github.com/gorilla/mux` from 1.8.0 to 1.8.1 ([#1665](https://github.com/NibiruChain/nibiru/pull/1665))

### Bug Fixes

Expand Down Expand Up @@ -135,6 +136,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1555](https://github.com/NibiruChain/nibiru/pull/1555) - feat(devgas): Convert legacy ABCI events to typed proto events
* [#1558](https://github.com/NibiruChain/nibiru/pull/1558) - feat(perp): paginated query to read the position store
* [#1554](https://github.com/NibiruChain/nibiru/pull/1554) - refactor: runs gofumpt formatter, which has nice conventions: go install mvdan.cc/gofumpt@latest
* [#1669](https://github.com/NibiruChain/nibiru/pull/1669) - feat(perp): add query to get collateral metadata

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/google/gofuzz v1.2.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0
github.com/holiman/uint256 v1.2.3
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,9 @@ github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down
39 changes: 36 additions & 3 deletions proto/nibiru/perp/v2/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ option go_package = "github.com/NibiruChain/nibiru/x/perp/v2/types";

// Query defines the gRPC querier service.
service Query {
// QueryPosition: Query one position on the given market for a user
rpc QueryPosition(QueryPositionRequest) returns (QueryPositionResponse) {
option (google.api.http).get = "/nibiru/perp/v2/position";
}

// QueryPositions: Query all positions for a user
rpc QueryPositions(QueryPositionsRequest) returns (QueryPositionsResponse) {
option (google.api.http).get = "/nibiru/perp/v2/positions";
}
Expand All @@ -26,31 +28,46 @@ service Query {
option (google.api.http).get = "/nibiru/perp/v2/position_store";
}

// Queries the reserve assets in a given pool, identified by a token pair.
// Queries the module accounts for x/perp
rpc ModuleAccounts(QueryModuleAccountsRequest)
returns (QueryModuleAccountsResponse) {
option (google.api.http).get = "/nibiru/perp/v2/module_accounts";
}

// QueryMarkets: Query all markets
rpc QueryMarkets(QueryMarketsRequest) returns (QueryMarketsResponse) {
option (google.api.http).get = "/nibiru/perp/v2/markets";
}

// QueryCollateral: Queries info about the collateral
rpc QueryCollateral(QueryCollateralRequest)
returns (QueryCollateralResponse) {
option (google.api.http).get = "/nibiru/perp/v2/collateral";
}
}

// ---------------------------------------- Positions

// QueryPositionsRequest: Request type for the
// "nibiru.perp.v2.Query/Positions" gRPC service method
message QueryPositionsRequest { string trader = 1; }

// QueryPositionsResponse: Response type for the
// "nibiru.perp.v2.Query/Positions" gRPC service method
message QueryPositionsResponse {
repeated nibiru.perp.v2.QueryPositionResponse positions = 1
[ (gogoproto.nullable) = false ];
}

// QueryPositionStoreRequest: Request type for the
// "nibiru.perp.v2.Query/PositionStore" gRPC service method
message QueryPositionStoreRequest {
// pagination defines a paginated request
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

// QueryPositionStoreResponse: Response type for the
// "nibiru.perp.v2.Query/PositionStore" gRPC service method
message QueryPositionStoreResponse {
// Position responses: collection of all stored positions (with pagination)
repeated nibiru.perp.v2.Position positions = 1
Expand All @@ -62,8 +79,8 @@ message QueryPositionStoreResponse {

// ---------------------------------------- Position

// QueryPositionRequest is the request type for the position of the x/perp
// module account.
// QueryPositionRequest: Request type for the
// "nibiru.perp.v2.Query/Position" gRPC service method
message QueryPositionRequest {
string pair = 1 [
(gogoproto.customtype) =
Expand All @@ -74,6 +91,8 @@ message QueryPositionRequest {
string trader = 2;
}

// QueryPositionResponse: Response type for the
// "nibiru.perp.v2.Query/Position" gRPC service method
message QueryPositionResponse {
// The position as it exists in the blockchain state
nibiru.perp.v2.Position position = 1 [ (gogoproto.nullable) = false ];
Expand All @@ -100,8 +119,12 @@ message QueryPositionResponse {

// ---------------------------------------- QueryModuleAccounts

// QueryModuleAccountsRequest: Request type for the
// "nibiru.perp.v2.Query/ModuleAccounts" gRPC service method
message QueryModuleAccountsRequest {}

// QueryModuleAccountsResponse: Response type for the
// "nibiru.perp.v2.Query/ModuleAccounts" gRPC service method
message QueryModuleAccountsResponse {
repeated nibiru.perp.v2.AccountWithBalance accounts = 1
[ (gogoproto.nullable) = false ];
Expand All @@ -128,3 +151,13 @@ message QueryMarketsResponse {
repeated nibiru.perp.v2.AmmMarket amm_markets = 1
[ (gogoproto.nullable) = false ];
}

// ---------------------------------------- QueryCollateral

// QueryCollateralRequest: Request type for the
// "nibiru.perp.v2.Query/Collateral" gRPC service method
message QueryCollateralRequest {}

// QueryCollateralRequest: Response type for the
// "nibiru.perp.v2.Query/Collateral" gRPC service method
message QueryCollateralResponse { string collateral_denom = 1; }
22 changes: 17 additions & 5 deletions x/perp/v2/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,16 +725,28 @@ func (s *IntegrationTestSuite) TestDonateToEcosystemFund() {
}

func (s *IntegrationTestSuite) TestQueryModuleAccount() {
s.T().Logf("donate to ecosystem fund")
resp := new(types.QueryModuleAccountsResponse)
s.NoError(
testutilcli.ExecQuery(
s.network.Validators[0].ClientCtx,
cli.CmdQueryModuleAccounts(),
[]string{},
s.network.ExecQuery(
cli.NewQueryCmd(),
[]string{"module-accounts"},
resp,
),
)
s.NotEmpty(resp.Accounts)
}

func (s *IntegrationTestSuite) TestQueryCollateralDenom() {
resp := new(types.QueryCollateralResponse)
s.NoError(
s.network.ExecQuery(
cli.NewQueryCmd(),
[]string{"collateral"},
resp,
),
)
s.Equal(types.TestingCollateralDenomNUSD, resp.CollateralDenom,
"resp: %s", resp.String())
}

func TestIntegrationTestSuite(t *testing.T) {
Expand Down
43 changes: 39 additions & 4 deletions x/perp/v2/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"

"github.com/MakeNowJust/heredoc/v2"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -14,8 +15,8 @@ import (

const FlagVersioned = "versioned"

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
// NewQueryCmd returns the cli query commands for this module
func NewQueryCmd() *cobra.Command {
// Group stablecoin queries under a subcommand
moduleQueryCmd := &cobra.Command{
Use: types.ModuleName,
Expand All @@ -31,6 +32,7 @@ func GetQueryCmd() *cobra.Command {
CmdQueryPositions(),
CmdQueryModuleAccounts(),
CmdQueryMarkets(),
CmdQueryCollateral(),
}
for _, cmd := range cmds {
moduleQueryCmd.AddCommand(cmd)
Expand Down Expand Up @@ -149,10 +151,10 @@ func CmdQueryMarkets() *cobra.Command {
cmd := &cobra.Command{
Use: "markets",
Short: "Query all market info",
Long: `
Long: heredoc.Doc(`
Query all market info. By default, only active tradable markets are shown.
If --versioned is to to true, the query will return all markets including the
inactive ones.`,
inactive ones.`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
Expand Down Expand Up @@ -184,3 +186,36 @@ inactive ones.`,

return cmd
}

// CmdQueryCollateral: Command for the "Query/Collateral" gRPC service method.
func CmdQueryCollateral() *cobra.Command {
cmd := &cobra.Command{
Use: "collateral",
Aliases: []string{"coll"},
Short: "Query the collateral denomination info.",
Long: heredoc.Doc(`
Query the metadata of the fungible collateral used by the perp module.
`,
),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.QueryCollateral(
cmd.Context(), &types.QueryCollateralRequest{},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
2 changes: 1 addition & 1 deletion x/perp/v2/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
types "github.com/NibiruChain/nibiru/x/perp/v2/types"
)

func GetTxCmd() *cobra.Command {
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Generalized automated market maker transaction subcommands",
Expand Down
30 changes: 30 additions & 0 deletions x/perp/v2/integration/action/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,33 @@ func CheckPositionStore_NumPositions(num int) QueryPositionStoreChecks {
return nil
}
}

// ---------------------------------------------------------
// QueryCollateral
// ---------------------------------------------------------

type queryCollateral struct {
wantDenom string
}

func (q queryCollateral) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
queryServer := keeper.NewQuerier(app.PerpKeeperV2)

resp, _ := queryServer.QueryCollateral(sdk.WrapSDKContext(ctx), &types.QueryCollateralRequest{})
if resp.CollateralDenom != q.wantDenom {
return ctx, fmt.Errorf(
"expected collateral denom %s, got %s",
q.wantDenom,
resp.CollateralDenom,
), false
}

return ctx, nil, false
}

// QueryCollateral: Action for the Query/Collateral gRPC query.
func QueryCollateral(expectDenom string) action.Action {
return queryCollateral{
wantDenom: expectDenom,
}
}
10 changes: 10 additions & 0 deletions x/perp/v2/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,13 @@ func (q queryServer) QueryMarkets(

return &types.QueryMarketsResponse{AmmMarkets: ammMarkets}, nil
}

func (q queryServer) QueryCollateral(
goCtx context.Context, req *types.QueryCollateralRequest,
) (*types.QueryCollateralResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
denom := q.k.Collateral.GetOr(ctx, "")
return &types.QueryCollateralResponse{
CollateralDenom: denom,
}, nil
}
33 changes: 33 additions & 0 deletions x/perp/v2/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,36 @@ func TestQueryPositionStore(t *testing.T) {

NewTestSuite(t).WithTestCases(tc...).Run()
}

func TestQueryCollateral(t *testing.T) {
tc := TestCases{
TC("state starts as expected with the mock NUSD denomination").
Given().
When().
Then(
QueryCollateral(types.TestingCollateralDenomNUSD),
),

TC("expected value returned after collateral denom changes").
Given().
When(
SetCollateral(denoms.BTC),
).
Then(
QueryCollateral(denoms.BTC),
),

TC("sanity check: multiple changes").
Given().
When(
SetCollateral(denoms.BTC),
SetCollateral(denoms.ETH),
SetCollateral(denoms.SOL),
).
Then(
QueryCollateral(denoms.SOL),
),
}

NewTestSuite(t).WithTestCases(tc...).Run()
}
4 changes: 2 additions & 2 deletions x/perp/v2/module/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ func TestNewAppModuleBasic(t *testing.T) {
appModule.EndBlock(ctx, abci.RequestEndBlock{})

cmds := appModule.GetTxCmd()
require.Len(t, cmds.Commands(), 8)
require.True(t, len(cmds.Commands()) > 0)

cmds = appModule.GetQueryCmd()
require.Len(t, cmds.Commands(), 4)
require.True(t, len(cmds.Commands()) > 0)
}
4 changes: 2 additions & 2 deletions x/perp/v2/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(

// GetTxCmd returns the capability module's root tx command.
func (a AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd()
return cli.NewTxCmd()
}

// GetQueryCmd returns the capability module's root query command.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
return cli.NewQueryCmd()
}

// ----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 72bddcf

Please sign in to comment.