Skip to content

Commit

Permalink
remove owner shares from query response
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 committed Mar 28, 2024
1 parent a99616b commit 3ceb80c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 466 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export interface QueryVaultResponse {
equity: Long;
inventory: Long;
totalShares: Long;
allOwnerShares: OwnerShares[];
}
/** QueryVaultResponse is a response type for the Vault RPC method. */

Expand All @@ -49,19 +48,6 @@ export interface QueryVaultResponseSDKType {
equity: Long;
inventory: Long;
total_shares: Long;
all_owner_shares: OwnerSharesSDKType[];
}
/** OwnerShares is a message type for an owner and their shares. */

export interface OwnerShares {
owner: string;
shares: Long;
}
/** OwnerShares is a message type for an owner and their shares. */

export interface OwnerSharesSDKType {
owner: string;
shares: Long;
}

function createBaseQueryParamsRequest(): QueryParamsRequest {
Expand Down Expand Up @@ -204,8 +190,7 @@ function createBaseQueryVaultResponse(): QueryVaultResponse {
subaccountId: undefined,
equity: Long.UZERO,
inventory: Long.UZERO,
totalShares: Long.UZERO,
allOwnerShares: []
totalShares: Long.UZERO
};
}

Expand All @@ -231,10 +216,6 @@ export const QueryVaultResponse = {
writer.uint32(40).uint64(message.totalShares);
}

for (const v of message.allOwnerShares) {
OwnerShares.encode(v!, writer.uint32(50).fork()).ldelim();
}

return writer;
},

Expand Down Expand Up @@ -267,10 +248,6 @@ export const QueryVaultResponse = {
message.totalShares = (reader.uint64() as Long);
break;

case 6:
message.allOwnerShares.push(OwnerShares.decode(reader, reader.uint32()));
break;

default:
reader.skipType(tag & 7);
break;
Expand All @@ -287,62 +264,6 @@ export const QueryVaultResponse = {
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.totalShares = object.totalShares !== undefined && object.totalShares !== null ? Long.fromValue(object.totalShares) : Long.UZERO;
message.allOwnerShares = object.allOwnerShares?.map(e => OwnerShares.fromPartial(e)) || [];
return message;
}

};

function createBaseOwnerShares(): OwnerShares {
return {
owner: "",
shares: Long.UZERO
};
}

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

if (!message.shares.isZero()) {
writer.uint32(16).uint64(message.shares);
}

return writer;
},

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

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

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

case 2:
message.shares = (reader.uint64() as Long);
break;

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

return message;
},

fromPartial(object: DeepPartial<OwnerShares>): OwnerShares {
const message = createBaseOwnerShares();
message.owner = object.owner ?? "";
message.shares = object.shares !== undefined && object.shares !== null ? Long.fromValue(object.shares) : Long.UZERO;
return message;
}

Expand Down
10 changes: 2 additions & 8 deletions proto/dydxprotocol/vault/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ service Query {
}
// Queries a Vault by type and number.
rpc Vault(QueryVaultRequest) returns (QueryVaultResponse) {
option (google.api.http).get = "/dydxprotocol/v4/vault/vaults/{type}/{number}";
option (google.api.http).get =
"/dydxprotocol/v4/vault/vaults/{type}/{number}";
}
}

Expand All @@ -43,11 +44,4 @@ message QueryVaultResponse {
uint64 equity = 3;
uint64 inventory = 4;
uint64 total_shares = 5;
repeated OwnerShares all_owner_shares = 6 [ (gogoproto.nullable) = false ];
}

// OwnerShares is a message type for an owner and their shares.
message OwnerShares {
string owner = 1;
uint64 shares = 2;
}
31 changes: 5 additions & 26 deletions protocol/x/vault/keeper/grpc_query_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package keeper
import (
"context"
"fmt"
"sort"

storetypes "cosmossdk.io/store/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -34,24 +32,6 @@ func (k Keeper) Vault(
return nil, status.Error(codes.NotFound, "vault not found")
}

// Get all owner shares.
allOwnerShares := []types.OwnerShares{}
ownerSharesStore := k.getVaultOwnerSharesStore(ctx, vaultId)
ownerSharesIterator := storetypes.KVStorePrefixIterator(ownerSharesStore, []byte{})
defer ownerSharesIterator.Close()
for ; ownerSharesIterator.Valid(); ownerSharesIterator.Next() {
var shares types.NumShares
k.cdc.MustUnmarshal(ownerSharesIterator.Value(), &shares)

allOwnerShares = append(allOwnerShares, types.OwnerShares{
Owner: string(ownerSharesIterator.Key()),
Shares: shares.NumShares.BigInt().Uint64(),
})
}
sort.Slice(allOwnerShares, func(i, j int) bool {
return allOwnerShares[i].Shares > allOwnerShares[j].Shares
})

// Get vault equity.
equity, err := k.GetVaultEquity(ctx, vaultId)
if err != nil {
Expand All @@ -67,11 +47,10 @@ func (k Keeper) Vault(
inventory := k.GetVaultInventoryInPerpetual(ctx, vaultId, perpId)

return &types.QueryVaultResponse{
VaultId: vaultId,
SubaccountId: *vaultId.ToSubaccountId(),
Equity: equity.Uint64(),
Inventory: inventory.Uint64(),
TotalShares: totalShares.NumShares.BigInt().Uint64(),
AllOwnerShares: allOwnerShares,
VaultId: vaultId,
SubaccountId: *vaultId.ToSubaccountId(),
Equity: equity.Uint64(),
Inventory: inventory.Uint64(),
TotalShares: totalShares.NumShares.BigInt().Uint64(),
}, nil
}
54 changes: 11 additions & 43 deletions protocol/x/vault/keeper/grpc_query_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ func TestVault(t *testing.T) {
inventory *big.Int
// Total shares.
totalShares *big.Int
// All owner shares (in descending number of shares).
allOwnerShares []vaulttypes.OwnerShares
// Query request.
req *vaulttypes.QueryVaultRequest

Expand All @@ -41,21 +39,11 @@ func TestVault(t *testing.T) {
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),
allOwnerShares: []vaulttypes.OwnerShares{
{
Owner: constants.Alice_Num0.Owner,
Shares: 244,
},
{
Owner: constants.Bob_Num0.Owner,
Shares: 56,
},
},
vaultId: constants.Vault_Clob_0,
asset: big.NewInt(100),
perpId: 0,
inventory: big.NewInt(200),
totalShares: big.NewInt(300),
expectedEquity: 500,
},
"Error: query non-existent vault": {
Expand All @@ -68,16 +56,6 @@ func TestVault(t *testing.T) {
perpId: 0,
inventory: big.NewInt(200),
totalShares: big.NewInt(300),
allOwnerShares: []vaulttypes.OwnerShares{
{
Owner: constants.Alice_Num0.Owner,
Shares: 244,
},
{
Owner: constants.Bob_Num0.Owner,
Shares: 56,
},
},
expectedErr: "vault not found",
},
"Error: nil request": {
Expand Down Expand Up @@ -122,18 +100,9 @@ func TestVault(t *testing.T) {
ctx := tApp.InitChain()
k := tApp.App.VaultKeeper

// Set total shares and owner shares.
// Set total shares.
err := k.SetTotalShares(ctx, tc.vaultId, vaulttypes.BigIntToNumShares(tc.totalShares))
require.NoError(t, err)
for _, ownerShares := range tc.allOwnerShares {
err := k.SetOwnerShares(
ctx,
tc.vaultId,
ownerShares.Owner,
vaulttypes.BigIntToNumShares(big.NewInt(int64(ownerShares.Shares))),
)
require.NoError(t, err)
}

// Check Vault query response is as expected.
response, err := k.Vault(ctx, tc.req)
Expand All @@ -142,12 +111,11 @@ func TestVault(t *testing.T) {
} else {
require.NoError(t, err)
expectedResponse := vaulttypes.QueryVaultResponse{
VaultId: tc.vaultId,
SubaccountId: *tc.vaultId.ToSubaccountId(),
Equity: tc.expectedEquity,
Inventory: tc.inventory.Uint64(),
TotalShares: tc.totalShares.Uint64(),
AllOwnerShares: tc.allOwnerShares,
VaultId: tc.vaultId,
SubaccountId: *tc.vaultId.ToSubaccountId(),
Equity: tc.expectedEquity,
Inventory: tc.inventory.Uint64(),
TotalShares: tc.totalShares.Uint64(),
}
require.Equal(t, expectedResponse, *response)
}
Expand Down
Loading

0 comments on commit 3ceb80c

Please sign in to comment.