Skip to content

Commit

Permalink
[TRA-181] Add query function for collateral pool address. (#1256)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau committed Mar 27, 2024
1 parent 2f6fffc commit ddd1715
Show file tree
Hide file tree
Showing 9 changed files with 883 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setPaginationParams } from "../../helpers";
import { LCDClient } from "@osmonauts/lcd";
import { QueryGetSubaccountRequest, QuerySubaccountResponseSDKType, QueryAllSubaccountRequest, QuerySubaccountAllResponseSDKType, QueryGetWithdrawalAndTransfersBlockedInfoRequest, QueryGetWithdrawalAndTransfersBlockedInfoResponseSDKType } from "./query";
import { QueryGetSubaccountRequest, QuerySubaccountResponseSDKType, QueryAllSubaccountRequest, QuerySubaccountAllResponseSDKType, QueryGetWithdrawalAndTransfersBlockedInfoRequest, QueryGetWithdrawalAndTransfersBlockedInfoResponseSDKType, QueryCollateralPoolAddressRequest, QueryCollateralPoolAddressResponseSDKType } from "./query";
export class LCDQueryClient {
req: LCDClient;

Expand All @@ -13,6 +13,7 @@ export class LCDQueryClient {
this.subaccount = this.subaccount.bind(this);
this.subaccountAll = this.subaccountAll.bind(this);
this.getWithdrawalAndTransfersBlockedInfo = this.getWithdrawalAndTransfersBlockedInfo.bind(this);
this.collateralPoolAddress = this.collateralPoolAddress.bind(this);
}
/* Queries a Subaccount by id */

Expand Down Expand Up @@ -43,16 +44,15 @@ export class LCDQueryClient {


async getWithdrawalAndTransfersBlockedInfo(params: QueryGetWithdrawalAndTransfersBlockedInfoRequest): Promise<QueryGetWithdrawalAndTransfersBlockedInfoResponseSDKType> {
const options: any = {
params: {}
};
const endpoint = `dydxprotocol/subaccounts/withdrawals_and_transfers_blocked_info/${params.perpetualId}`;
return await this.req.get<QueryGetWithdrawalAndTransfersBlockedInfoResponseSDKType>(endpoint);
}
/* Queries the collateral pool account address for a perpetual id. */

if (typeof params?.perpetualId !== "undefined") {
options.params.perpetual_id = params.perpetualId;
}

const endpoint = `dydxprotocol/subaccounts/withdrawals_and_transfers_blocked_info`;
return await this.req.get<QueryGetWithdrawalAndTransfersBlockedInfoResponseSDKType>(endpoint, options);
async collateralPoolAddress(params: QueryCollateralPoolAddressRequest): Promise<QueryCollateralPoolAddressResponseSDKType> {
const endpoint = `dydxprotocol/subaccounts/collateral_pool_address/${params.perpetualId}`;
return await this.req.get<QueryCollateralPoolAddressResponseSDKType>(endpoint);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Rpc } from "../../helpers";
import * as _m0 from "protobufjs/minimal";
import { QueryClient, createProtobufRpcClient } from "@cosmjs/stargate";
import { QueryGetSubaccountRequest, QuerySubaccountResponse, QueryAllSubaccountRequest, QuerySubaccountAllResponse, QueryGetWithdrawalAndTransfersBlockedInfoRequest, QueryGetWithdrawalAndTransfersBlockedInfoResponse } from "./query";
import { QueryGetSubaccountRequest, QuerySubaccountResponse, QueryAllSubaccountRequest, QuerySubaccountAllResponse, QueryGetWithdrawalAndTransfersBlockedInfoRequest, QueryGetWithdrawalAndTransfersBlockedInfoResponse, QueryCollateralPoolAddressRequest, QueryCollateralPoolAddressResponse } from "./query";
/** Query defines the gRPC querier service. */

export interface Query {
Expand All @@ -16,6 +16,9 @@ export interface Query {
*/

getWithdrawalAndTransfersBlockedInfo(request: QueryGetWithdrawalAndTransfersBlockedInfoRequest): Promise<QueryGetWithdrawalAndTransfersBlockedInfoResponse>;
/** Queries the collateral pool account address for a perpetual id. */

collateralPoolAddress(request: QueryCollateralPoolAddressRequest): Promise<QueryCollateralPoolAddressResponse>;
}
export class QueryClientImpl implements Query {
private readonly rpc: Rpc;
Expand All @@ -25,6 +28,7 @@ export class QueryClientImpl implements Query {
this.subaccount = this.subaccount.bind(this);
this.subaccountAll = this.subaccountAll.bind(this);
this.getWithdrawalAndTransfersBlockedInfo = this.getWithdrawalAndTransfersBlockedInfo.bind(this);
this.collateralPoolAddress = this.collateralPoolAddress.bind(this);
}

subaccount(request: QueryGetSubaccountRequest): Promise<QuerySubaccountResponse> {
Expand All @@ -47,6 +51,12 @@ export class QueryClientImpl implements Query {
return promise.then(data => QueryGetWithdrawalAndTransfersBlockedInfoResponse.decode(new _m0.Reader(data)));
}

collateralPoolAddress(request: QueryCollateralPoolAddressRequest): Promise<QueryCollateralPoolAddressResponse> {
const data = QueryCollateralPoolAddressRequest.encode(request).finish();
const promise = this.rpc.request("dydxprotocol.subaccounts.Query", "CollateralPoolAddress", data);
return promise.then(data => QueryCollateralPoolAddressResponse.decode(new _m0.Reader(data)));
}

}
export const createRpcQueryExtension = (base: QueryClient) => {
const rpc = createProtobufRpcClient(base);
Expand All @@ -62,6 +72,10 @@ export const createRpcQueryExtension = (base: QueryClient) => {

getWithdrawalAndTransfersBlockedInfo(request: QueryGetWithdrawalAndTransfersBlockedInfoRequest): Promise<QueryGetWithdrawalAndTransfersBlockedInfoResponse> {
return queryService.getWithdrawalAndTransfersBlockedInfo(request);
},

collateralPoolAddress(request: QueryCollateralPoolAddressRequest): Promise<QueryCollateralPoolAddressResponse> {
return queryService.collateralPoolAddress(request);
}

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,52 @@ export interface QueryGetWithdrawalAndTransfersBlockedInfoResponseSDKType {
chain_outage_seen_at_block: number;
withdrawals_and_transfers_unblocked_at_block: number;
}
/**
* QueryCollateralPoolAddressRequest is the request type for fetching the
* account address of the collateral pool associated with the passed in
* perpetual id.
*/

export interface QueryCollateralPoolAddressRequest {
/**
* QueryCollateralPoolAddressRequest is the request type for fetching the
* account address of the collateral pool associated with the passed in
* perpetual id.
*/
perpetualId: number;
}
/**
* QueryCollateralPoolAddressRequest is the request type for fetching the
* account address of the collateral pool associated with the passed in
* perpetual id.
*/

export interface QueryCollateralPoolAddressRequestSDKType {
/**
* QueryCollateralPoolAddressRequest is the request type for fetching the
* account address of the collateral pool associated with the passed in
* perpetual id.
*/
perpetual_id: number;
}
/**
* QueryCollateralPoolAddressResponse is a response type for fetching the
* account address of the collateral pool associated with the passed in
* perpetual id.
*/

export interface QueryCollateralPoolAddressResponse {
collateralPoolAddress: string;
}
/**
* QueryCollateralPoolAddressResponse is a response type for fetching the
* account address of the collateral pool associated with the passed in
* perpetual id.
*/

export interface QueryCollateralPoolAddressResponseSDKType {
collateral_pool_address: string;
}

function createBaseQueryGetSubaccountRequest(): QueryGetSubaccountRequest {
return {
Expand Down Expand Up @@ -393,4 +439,94 @@ export const QueryGetWithdrawalAndTransfersBlockedInfoResponse = {
return message;
}

};

function createBaseQueryCollateralPoolAddressRequest(): QueryCollateralPoolAddressRequest {
return {
perpetualId: 0
};
}

export const QueryCollateralPoolAddressRequest = {
encode(message: QueryCollateralPoolAddressRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.perpetualId !== 0) {
writer.uint32(8).uint32(message.perpetualId);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): QueryCollateralPoolAddressRequest {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseQueryCollateralPoolAddressRequest();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.perpetualId = reader.uint32();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<QueryCollateralPoolAddressRequest>): QueryCollateralPoolAddressRequest {
const message = createBaseQueryCollateralPoolAddressRequest();
message.perpetualId = object.perpetualId ?? 0;
return message;
}

};

function createBaseQueryCollateralPoolAddressResponse(): QueryCollateralPoolAddressResponse {
return {
collateralPoolAddress: ""
};
}

export const QueryCollateralPoolAddressResponse = {
encode(message: QueryCollateralPoolAddressResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.collateralPoolAddress !== "") {
writer.uint32(10).string(message.collateralPoolAddress);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): QueryCollateralPoolAddressResponse {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseQueryCollateralPoolAddressResponse();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.collateralPoolAddress = reader.string();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<QueryCollateralPoolAddressResponse>): QueryCollateralPoolAddressResponse {
const message = createBaseQueryCollateralPoolAddressResponse();
message.collateralPoolAddress = object.collateralPoolAddress ?? "";
return message;
}

};
23 changes: 22 additions & 1 deletion proto/dydxprotocol/subaccounts/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ service Query {
QueryGetWithdrawalAndTransfersBlockedInfoRequest)
returns (QueryGetWithdrawalAndTransfersBlockedInfoResponse) {
option (google.api.http).get =
"/dydxprotocol/subaccounts/withdrawals_and_transfers_blocked_info";
"/dydxprotocol/subaccounts/withdrawals_and_transfers_blocked_info/"
"{perpetual_id}";
}

// Queries the collateral pool account address for a perpetual id.
rpc CollateralPoolAddress(QueryCollateralPoolAddressRequest)
returns (QueryCollateralPoolAddressResponse) {
option (google.api.http).get =
"/dydxprotocol/subaccounts/collateral_pool_address/{perpetual_id}";
}
}

Expand Down Expand Up @@ -69,3 +77,16 @@ message QueryGetWithdrawalAndTransfersBlockedInfoResponse {
uint32 chain_outage_seen_at_block = 2;
uint32 withdrawals_and_transfers_unblocked_at_block = 3;
}

// QueryCollateralPoolAddressRequest is the request type for fetching the
// account address of the collateral pool associated with the passed in
// perpetual id.
message QueryCollateralPoolAddressRequest { uint32 perpetual_id = 1; }

// QueryCollateralPoolAddressResponse is a response type for fetching the
// account address of the collateral pool associated with the passed in
// perpetual id.
message QueryCollateralPoolAddressResponse {
string collateral_pool_address = 1
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}
37 changes: 37 additions & 0 deletions protocol/mocks/QueryClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions protocol/x/subaccounts/keeper/grpc_query_collateral_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package keeper

import (
"context"
"errors"
"fmt"

"github.com/dydxprotocol/v4-chain/protocol/lib"
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
"github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) CollateralPoolAddress(
c context.Context,
req *types.QueryCollateralPoolAddressRequest,
) (*types.QueryCollateralPoolAddressResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := lib.UnwrapSDKContext(c, types.ModuleName)

collateralPool, err := k.GetCollateralPoolFromPerpetualId(
ctx,
req.PerpetualId,
)
if err != nil {
if errors.Is(err, perptypes.ErrPerpetualDoesNotExist) {
return nil,
status.Error(
codes.NotFound,
fmt.Sprintf(
"Perpetual id %+v not found.",
req.PerpetualId,
),
)
}

return nil, status.Error(codes.Internal, "internal error")
}

return &types.QueryCollateralPoolAddressResponse{
CollateralPoolAddress: collateralPool.String(),
}, nil
}
Loading

0 comments on commit ddd1715

Please sign in to comment.