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

[TRA-181] Add query function for collateral pool address. #1256

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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}";
Copy link
Contributor

@shrenujb shrenujb Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be part of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I noticed I didn't update the URL in a previous PR so rolled it up into this one. Going to update the description.

}

// 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
}
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
Loading