Skip to content

Commit

Permalink
keep track of vault shares as integers
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 committed Mar 28, 2024
1 parent b4ec84f commit a10102f
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 364 deletions.
44 changes: 11 additions & 33 deletions indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,17 @@ export interface VaultIdSDKType {

number: number;
}
/**
* NumShares represents the number of shares in a vault in the
* format of a rational number `numerator / denominator`.
*/
/** NumShares represents the number of shares in a vault. */

export interface NumShares {
/** Numerator. */
numerator: Uint8Array;
/** Denominator. */

denominator: Uint8Array;
/** Number of shares. */
numShares: Uint8Array;
}
/**
* NumShares represents the number of shares in a vault in the
* format of a rational number `numerator / denominator`.
*/
/** NumShares represents the number of shares in a vault. */

export interface NumSharesSDKType {
/** Numerator. */
numerator: Uint8Array;
/** Denominator. */

denominator: Uint8Array;
/** Number of shares. */
num_shares: Uint8Array;
}

function createBaseVaultId(): VaultId {
Expand Down Expand Up @@ -149,19 +137,14 @@ export const VaultId = {

function createBaseNumShares(): NumShares {
return {
numerator: new Uint8Array(),
denominator: new Uint8Array()
numShares: new Uint8Array()
};
}

export const NumShares = {
encode(message: NumShares, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.numerator.length !== 0) {
writer.uint32(10).bytes(message.numerator);
}

if (message.denominator.length !== 0) {
writer.uint32(18).bytes(message.denominator);
if (message.numShares.length !== 0) {
writer.uint32(18).bytes(message.numShares);
}

return writer;
Expand All @@ -176,12 +159,8 @@ export const NumShares = {
const tag = reader.uint32();

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

case 2:
message.denominator = reader.bytes();
message.numShares = reader.bytes();
break;

default:
Expand All @@ -195,8 +174,7 @@ export const NumShares = {

fromPartial(object: DeepPartial<NumShares>): NumShares {
const message = createBaseNumShares();
message.numerator = object.numerator ?? new Uint8Array();
message.denominator = object.denominator ?? new Uint8Array();
message.numShares = object.numShares ?? new Uint8Array();
return message;
}

Expand Down
14 changes: 3 additions & 11 deletions proto/dydxprotocol/vault/vault.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,10 @@ message VaultId {
uint32 number = 2;
}

// NumShares represents the number of shares in a vault in the
// format of a rational number `numerator / denominator`.
// NumShares represents the number of shares in a vault.
message NumShares {
// Numerator.
bytes numerator = 1 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];

// Denominator.
bytes denominator = 2 [
// Number of shares.
bytes num_shares = 2 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
Expand Down
50 changes: 25 additions & 25 deletions protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type DepositInstance struct {
// Whether DeliverTx fails.
deliverTxFails bool
// Expected owner shares for depositor above.
expectedOwnerShares *big.Rat
expectedOwnerShares *big.Int
}

// DepositorSetup represents the setup of a depositor.
Expand All @@ -55,7 +55,7 @@ func TestMsgDepositToVault(t *testing.T) {

/* --- Expectations --- */
// Vault total shares after each of the above deposit instances.
totalSharesHistory []*big.Rat
totalSharesHistory []*big.Int
// Vault equity after each of the above deposit instances.
vaultEquityHistory []*big.Int
}{
Expand All @@ -72,18 +72,18 @@ func TestMsgDepositToVault(t *testing.T) {
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(123),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(123, 1),
expectedOwnerShares: big.NewInt(123),
},
{
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(321),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(444, 1),
expectedOwnerShares: big.NewInt(444),
},
},
totalSharesHistory: []*big.Rat{
big.NewRat(123, 1),
big.NewRat(444, 1),
totalSharesHistory: []*big.Int{
big.NewInt(123),
big.NewInt(444),
},
vaultEquityHistory: []*big.Int{
big.NewInt(123),
Expand All @@ -107,18 +107,18 @@ func TestMsgDepositToVault(t *testing.T) {
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(1_000),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(1_000, 1),
expectedOwnerShares: big.NewInt(1_000),
},
{
depositor: constants.Bob_Num1,
depositAmount: big.NewInt(500),
msgSigner: constants.Bob_Num1.Owner,
expectedOwnerShares: big.NewRat(500, 1),
expectedOwnerShares: big.NewInt(500),
},
},
totalSharesHistory: []*big.Rat{
big.NewRat(1_000, 1),
big.NewRat(1_500, 1),
totalSharesHistory: []*big.Int{
big.NewInt(1_000),
big.NewInt(1_500),
},
vaultEquityHistory: []*big.Int{
big.NewInt(1_000),
Expand All @@ -142,7 +142,7 @@ func TestMsgDepositToVault(t *testing.T) {
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(1_000),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(1_000, 1),
expectedOwnerShares: big.NewInt(1_000),
},
{
depositor: constants.Bob_Num1,
Expand All @@ -152,9 +152,9 @@ func TestMsgDepositToVault(t *testing.T) {
expectedOwnerShares: nil,
},
},
totalSharesHistory: []*big.Rat{
big.NewRat(1_000, 1),
big.NewRat(1_000, 1),
totalSharesHistory: []*big.Int{
big.NewInt(1_000),
big.NewInt(1_000),
},
vaultEquityHistory: []*big.Int{
big.NewInt(1_000),
Expand Down Expand Up @@ -186,12 +186,12 @@ func TestMsgDepositToVault(t *testing.T) {
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(1_000),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(1_000, 1),
expectedOwnerShares: big.NewInt(1_000),
},
},
totalSharesHistory: []*big.Rat{
big.NewRat(0, 1),
big.NewRat(1_000, 1),
totalSharesHistory: []*big.Int{
big.NewInt(0),
big.NewInt(1_000),
},
vaultEquityHistory: []*big.Int{
big.NewInt(0),
Expand Down Expand Up @@ -228,9 +228,9 @@ func TestMsgDepositToVault(t *testing.T) {
expectedOwnerShares: nil,
},
},
totalSharesHistory: []*big.Rat{
big.NewRat(0, 1),
big.NewRat(0, 1),
totalSharesHistory: []*big.Int{
big.NewInt(0),
big.NewInt(0),
},
vaultEquityHistory: []*big.Int{
big.NewInt(0),
Expand Down Expand Up @@ -329,7 +329,7 @@ func TestMsgDepositToVault(t *testing.T) {
require.True(t, exists)
require.Equal(
t,
vaulttypes.BigRatToNumShares(tc.totalSharesHistory[i]),
vaulttypes.BigIntToNumShares(tc.totalSharesHistory[i]),
totalShares,
)
// Check that owner shares of the depositor is as expected.
Expand All @@ -340,7 +340,7 @@ func TestMsgDepositToVault(t *testing.T) {
)
require.Equal(
t,
vaulttypes.BigRatToNumShares(depositInstance.expectedOwnerShares),
vaulttypes.BigIntToNumShares(depositInstance.expectedOwnerShares),
ownerShares,
)
// Check that equity of the vault is as expected.
Expand Down
3 changes: 1 addition & 2 deletions protocol/x/vault/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func (k Keeper) RefreshAllVaultOrders(ctx sdk.Context) {
k.cdc.MustUnmarshal(totalSharesIterator.Value(), &totalShares)

// Skip if TotalShares is non-positive.
totalSharesRat, err := totalShares.ToBigRat()
if err != nil || totalSharesRat.Sign() <= 0 {
if totalShares.NumShares.BigInt().Sign() <= 0 {
continue
}

Expand Down
22 changes: 11 additions & 11 deletions protocol/x/vault/keeper/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,36 @@ func TestRefreshAllVaultOrders(t *testing.T) {
// Vault IDs.
vaultIds []vaulttypes.VaultId
// Total Shares of each vault ID above.
totalShares []*big.Rat
totalShares []*big.Int
}{
"Two Vaults, Both Positive Shares": {
vaultIds: []vaulttypes.VaultId{
constants.Vault_Clob_0,
constants.Vault_Clob_1,
},
totalShares: []*big.Rat{
big.NewRat(1_000, 1),
big.NewRat(200, 1),
totalShares: []*big.Int{
big.NewInt(1_000),
big.NewInt(200),
},
},
"Two Vaults, One Positive Shares, One Zero Shares": {
vaultIds: []vaulttypes.VaultId{
constants.Vault_Clob_0,
constants.Vault_Clob_1,
},
totalShares: []*big.Rat{
big.NewRat(1_000, 1),
big.NewRat(0, 1),
totalShares: []*big.Int{
big.NewInt(1_000),
big.NewInt(0),
},
},
"Two Vaults, Both Zero Shares": {
vaultIds: []vaulttypes.VaultId{
constants.Vault_Clob_0,
constants.Vault_Clob_1,
},
totalShares: []*big.Rat{
big.NewRat(0, 1),
big.NewRat(0, 1),
totalShares: []*big.Int{
big.NewInt(0),
big.NewInt(0),
},
},
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestRefreshAllVaultOrders(t *testing.T) {
err := tApp.App.VaultKeeper.SetTotalShares(
ctx,
vaultId,
vaulttypes.BigRatToNumShares(tc.totalShares[i]),
vaulttypes.BigIntToNumShares(tc.totalShares[i]),
)
require.NoError(t, err)
}
Expand Down
Loading

0 comments on commit a10102f

Please sign in to comment.