Skip to content

Commit

Permalink
[TRA-181] Add query function for collateral pool address.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau committed Mar 27, 2024
1 parent da39a93 commit ccc0ef4
Show file tree
Hide file tree
Showing 5 changed files with 684 additions and 58 deletions.
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" ];
}
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
}
68 changes: 68 additions & 0 deletions protocol/x/subaccounts/keeper/grpc_query_collateral_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package keeper_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)

func TestQueryCollateralPoolAddress(t *testing.T) {
for testName, tc := range map[string]struct {
// Parameters
request *types.QueryCollateralPoolAddressRequest

// Expectations
response *types.QueryCollateralPoolAddressResponse
err error
}{
"Nil request results in error": {
err: status.Error(codes.InvalidArgument, "invalid request"),
},
"Cross perpetual": {
request: &types.QueryCollateralPoolAddressRequest{
PerpetualId: constants.BtcUsd_NoMarginRequirement.Params.Id,
},
response: &types.QueryCollateralPoolAddressResponse{
CollateralPoolAddress: types.ModuleAddress.String(),
},
},
"Isolated perpetual": {
request: &types.QueryCollateralPoolAddressRequest{
PerpetualId: constants.IsoUsd_IsolatedMarket.Params.Id,
},
response: &types.QueryCollateralPoolAddressResponse{
CollateralPoolAddress: constants.IsoCollateralPoolAddress.String(),
},
},
"Perpetual not found": {
request: &types.QueryCollateralPoolAddressRequest{
PerpetualId: uint32(1000),
},
err: status.Error(codes.NotFound, fmt.Sprintf(
"Perpetual id %+v not found.",
uint32(1000),
)),
},
} {
t.Run(testName, func(t *testing.T) {
ctx, keeper, pricesKeeper, perpetualsKeeper, _, _, _, _, _ := keepertest.SubaccountsKeepers(t, true)
keepertest.CreateTestMarkets(t, ctx, pricesKeeper)
keepertest.CreateTestLiquidityTiers(t, ctx, perpetualsKeeper)
keepertest.CreateTestPerpetuals(t, ctx, perpetualsKeeper)
response, err := keeper.CollateralPoolAddress(ctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
require.NoError(t, err)
require.Equal(t, tc.response, response)
}
})
}
}
Loading

0 comments on commit ccc0ef4

Please sign in to comment.