Skip to content

Commit

Permalink
[TRA-380] use serializable int for equity and inventory in vault query (
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 authored Jun 13, 2024
1 parent 15f9719 commit 43c96ea
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 111 deletions.
28 changes: 14 additions & 14 deletions indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ export interface QueryVaultRequestSDKType {
export interface QueryVaultResponse {
vaultId?: VaultId;
subaccountId?: SubaccountId;
equity: Long;
inventory: Long;
equity: Uint8Array;
inventory: Uint8Array;
totalShares: Long;
}
/** QueryVaultResponse is a response type for the Vault RPC method. */

export interface QueryVaultResponseSDKType {
vault_id?: VaultIdSDKType;
subaccount_id?: SubaccountIdSDKType;
equity: Long;
inventory: Long;
equity: Uint8Array;
inventory: Uint8Array;
total_shares: Long;
}
/** QueryAllVaultsRequest is a request type for the AllVaults RPC method. */
Expand Down Expand Up @@ -249,8 +249,8 @@ function createBaseQueryVaultResponse(): QueryVaultResponse {
return {
vaultId: undefined,
subaccountId: undefined,
equity: Long.UZERO,
inventory: Long.UZERO,
equity: new Uint8Array(),
inventory: new Uint8Array(),
totalShares: Long.UZERO
};
}
Expand All @@ -265,12 +265,12 @@ export const QueryVaultResponse = {
SubaccountId.encode(message.subaccountId, writer.uint32(18).fork()).ldelim();
}

if (!message.equity.isZero()) {
writer.uint32(24).uint64(message.equity);
if (message.equity.length !== 0) {
writer.uint32(26).bytes(message.equity);
}

if (!message.inventory.isZero()) {
writer.uint32(32).uint64(message.inventory);
if (message.inventory.length !== 0) {
writer.uint32(34).bytes(message.inventory);
}

if (!message.totalShares.isZero()) {
Expand Down Expand Up @@ -298,11 +298,11 @@ export const QueryVaultResponse = {
break;

case 3:
message.equity = (reader.uint64() as Long);
message.equity = reader.bytes();
break;

case 4:
message.inventory = (reader.uint64() as Long);
message.inventory = reader.bytes();
break;

case 5:
Expand All @@ -322,8 +322,8 @@ export const QueryVaultResponse = {
const message = createBaseQueryVaultResponse();
message.vaultId = object.vaultId !== undefined && object.vaultId !== null ? VaultId.fromPartial(object.vaultId) : undefined;
message.subaccountId = object.subaccountId !== undefined && object.subaccountId !== null ? SubaccountId.fromPartial(object.subaccountId) : undefined;
message.equity = object.equity !== undefined && object.equity !== null ? Long.fromValue(object.equity) : Long.UZERO;
message.inventory = object.inventory !== undefined && object.inventory !== null ? Long.fromValue(object.inventory) : Long.UZERO;
message.equity = object.equity ?? new Uint8Array();
message.inventory = object.inventory ?? new Uint8Array();
message.totalShares = object.totalShares !== undefined && object.totalShares !== null ? Long.fromValue(object.totalShares) : Long.UZERO;
return message;
}
Expand Down
12 changes: 10 additions & 2 deletions proto/dydxprotocol/vault/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ message QueryVaultResponse {
VaultId vault_id = 1 [ (gogoproto.nullable) = false ];
dydxprotocol.subaccounts.SubaccountId subaccount_id = 2
[ (gogoproto.nullable) = false ];
uint64 equity = 3;
uint64 inventory = 4;
bytes equity = 3 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];
bytes inventory = 4 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];
uint64 total_shares = 5;
}

Expand Down
5 changes: 3 additions & 2 deletions protocol/x/vault/keeper/grpc_query_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/types/query"
"github.com/dydxprotocol/v4-chain/protocol/dtypes"
"github.com/dydxprotocol/v4-chain/protocol/lib"
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
Expand Down Expand Up @@ -51,8 +52,8 @@ func (k Keeper) Vault(
return &types.QueryVaultResponse{
VaultId: vaultId,
SubaccountId: *vaultId.ToSubaccountId(),
Equity: equity.Uint64(),
Inventory: inventory.Uint64(),
Equity: dtypes.NewIntFromBigInt(equity),
Inventory: dtypes.NewIntFromBigInt(inventory),
TotalShares: totalShares.NumShares.BigInt().Uint64(),
}, nil
}
Expand Down
20 changes: 16 additions & 4 deletions protocol/x/vault/keeper/grpc_query_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestVault(t *testing.T) {
req *vaulttypes.QueryVaultRequest

/* --- Expectations --- */
expectedEquity uint64
expectedEquity *big.Int
expectedErr string
}{
"Success": {
Expand All @@ -44,7 +44,19 @@ func TestVault(t *testing.T) {
perpId: 0,
inventory: big.NewInt(200),
totalShares: big.NewInt(300),
expectedEquity: 500,
expectedEquity: big.NewInt(500),
},
"Success: negative inventory and equity": {
req: &vaulttypes.QueryVaultRequest{
Type: vaulttypes.VaultType_VAULT_TYPE_CLOB,
Number: 0,
},
vaultId: constants.Vault_Clob_0,
asset: big.NewInt(100),
perpId: 0,
inventory: big.NewInt(-200),
totalShares: big.NewInt(300),
expectedEquity: big.NewInt(-300),
},
"Error: query non-existent vault": {
req: &vaulttypes.QueryVaultRequest{
Expand Down Expand Up @@ -113,8 +125,8 @@ func TestVault(t *testing.T) {
expectedResponse := vaulttypes.QueryVaultResponse{
VaultId: tc.vaultId,
SubaccountId: *tc.vaultId.ToSubaccountId(),
Equity: tc.expectedEquity,
Inventory: tc.inventory.Uint64(),
Equity: dtypes.NewIntFromBigInt(tc.expectedEquity),
Inventory: dtypes.NewIntFromBigInt(tc.inventory),
TotalShares: tc.totalShares.Uint64(),
}
require.Equal(t, expectedResponse, *response)
Expand Down
Loading

0 comments on commit 43c96ea

Please sign in to comment.