-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TRA-181] Add query function for collateral pool address.
- Loading branch information
1 parent
da39a93
commit ccc0ef4
Showing
5 changed files
with
684 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
protocol/x/subaccounts/keeper/grpc_query_collateral_pool.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
protocol/x/subaccounts/keeper/grpc_query_collateral_pool_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.