Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derivatives query fix denom #691

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions proto/ununifi/derivatives/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ service Query {
}

rpc PerpetualFuturesMarket(QueryPerpetualFuturesMarketRequest) returns (QueryPerpetualFuturesMarketResponse) {
option (google.api.http).get = "/ununifi/derivatives/perpetual-futures/{base_denom}/{quote_denom}";
option (google.api.http).get = "/ununifi/derivatives/perpetual-futures/market";
}

rpc PerpetualOptions(QueryPerpetualOptionsRequest) returns (QueryPerpetualOptionsResponse) {
option (google.api.http).get = "/ununifi/derivatives/perpetual-options";
}

rpc PerpetualOptionsMarket(QueryPerpetualOptionsMarketRequest) returns (QueryPerpetualOptionsMarketResponse) {
option (google.api.http).get = "/ununifi/derivatives/perpetual-options/{base_denom}/{quote_denom}";
option (google.api.http).get = "/ununifi/derivatives/perpetual-options/market";
}

rpc AllPositions(QueryAllPositionsRequest) returns (QueryAllPositionsResponse) {
Expand Down Expand Up @@ -79,17 +79,12 @@ service Query {
}

rpc EstimateDLPTokenAmount(QueryEstimateDLPTokenAmountRequest) returns (QueryEstimateDLPTokenAmountResponse) {
option (google.api.http).get = "/ununifi/derivatives/estimate-dlp-token-amount/{mint_denom}/{amount}";
option (google.api.http).get = "/ununifi/derivatives/estimate-dlp-token-amount";
}

rpc EstimateRedeemTokenAmount(QueryEstimateRedeemTokenAmountRequest)
returns (QueryEstimateRedeemTokenAmountResponse) {
option (google.api.http).get = "/ununifi/derivatives/estimate-redeem-amount/{redeem_denom}/{lpt_amount}";
}

rpc AvailableAssetInPoolByDenom(QueryAvailableAssetInPoolByDenomRequest)
returns (QueryAvailableAssetInPoolByDenomResponse) {
option (google.api.http).get = "/ununifi/derivatives/pools/available-asset/{denom}";
option (google.api.http).get = "/ununifi/derivatives/estimate-redeem-amount";
}

rpc AvailableAssetsInPool(QueryAvailableAssetsInPoolRequest) returns (QueryAvailableAssetsInPoolResponse) {
Expand Down Expand Up @@ -346,22 +341,12 @@ message QueryEstimateRedeemTokenAmountResponse {
];
}

message QueryAvailableAssetInPoolByDenomRequest {
message QueryAvailableAssetsInPoolRequest {
string denom = 1 [
(gogoproto.moretags) = "yaml:\"denom\""
];
}

message QueryAvailableAssetInPoolByDenomResponse {
cosmos.base.v1beta1.Coin available_asset = 1 [
(gogoproto.moretags) = "yaml:\"available_asset\"",
(gogoproto.nullable) = false
];
}

message QueryAvailableAssetsInPoolRequest {
}

message QueryAvailableAssetsInPoolResponse {
repeated cosmos.base.v1beta1.Coin available_assets = 1 [
(gogoproto.moretags) = "yaml:\"available_assets\"",
Expand Down
41 changes: 10 additions & 31 deletions x/derivatives/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdQueryDLPTokenRate())
cmd.AddCommand(CmdQueryEstimateDLPTokenAmount())
cmd.AddCommand(CmdQueryEstimateRedeemTokenAmount())
cmd.AddCommand(CmdQueryAvailableAssetInPoolByDenom())
cmd.AddCommand(CmdQueryAvailableAssetsInPool())
cmd.AddCommand(CmdQueryAllPendingPaymentPositions())
cmd.AddCommand(CmdQueryPendingPaymentPosition())
Expand Down Expand Up @@ -440,46 +439,26 @@ func CmdQueryEstimateRedeemTokenAmount() *cobra.Command {
return cmd
}

func CmdQueryAvailableAssetInPoolByDenom() *cobra.Command {
func CmdQueryAvailableAssetsInPool() *cobra.Command {
cmd := &cobra.Command{
Use: "available-asset [denom]",
Short: "shows the available amount of the asset",
Args: cobra.ExactArgs(1),
Use: "available-assets [denom]",
Short: "shows the available amount of assets in the pool",
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.AvailableAssetInPoolByDenom(
context.Background(),
&types.QueryAvailableAssetInPoolByDenomRequest{
Denom: args[0],
},
)
if err != nil {
return err
var denom string
if len(args) > 1 {
denom = args[1]
} else {
denom = ""
}

return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func CmdQueryAvailableAssetsInPool() *cobra.Command {
cmd := &cobra.Command{
Use: "available-assets",
Short: "shows the available amount of all assets in the pool",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.AvailableAssetsInPool(
context.Background(),
&types.QueryAvailableAssetsInPoolRequest{},
&types.QueryAvailableAssetsInPoolRequest{Denom: denom},
)
if err != nil {
return err
Expand Down
25 changes: 8 additions & 17 deletions x/derivatives/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,34 +461,25 @@ func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types
return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
}

func (k Keeper) AvailableAssetInPoolByDenom(c context.Context, req *types.QueryAvailableAssetInPoolByDenomRequest) (*types.QueryAvailableAssetInPoolByDenomResponse, error) {
func (k Keeper) AvailableAssetsInPool(c context.Context, req *types.QueryAvailableAssetsInPoolRequest) (*types.QueryAvailableAssetsInPoolResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(c)
availableAsset, err := k.AvailableAssetInPool(ctx, req.Denom)
availableAssets, err := k.AllAvailableAssetsInPool(ctx)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

return &types.QueryAvailableAssetInPoolByDenomResponse{
AvailableAsset: availableAsset,
}, nil
}

func (k Keeper) AvailableAssetsInPool(c context.Context, req *types.QueryAvailableAssetsInPoolRequest) (*types.QueryAvailableAssetsInPoolResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
if req.Denom == "" {
return &types.QueryAvailableAssetsInPoolResponse{AvailableAssets: availableAssets}, nil
}

ctx := sdk.UnwrapSDKContext(c)
availableAssets, err := k.AllAvailableAssetsInPool(ctx)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
exist, asset := availableAssets.Find(req.Denom)
if !exist {
return &types.QueryAvailableAssetsInPoolResponse{AvailableAssets: sdk.Coins{sdk.NewInt64Coin(req.Denom, 0)}}, nil
}

return &types.QueryAvailableAssetsInPoolResponse{
AvailableAssets: availableAssets,
}, nil
return &types.QueryAvailableAssetsInPoolResponse{AvailableAssets: sdk.Coins{asset}}, nil
}
Loading