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 ca477c9
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 320 deletions.
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
53 changes: 16 additions & 37 deletions protocol/x/vault/keeper/shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ func (k Keeper) SetTotalShares(
vaultId types.VaultId,
totalShares types.NumShares,
) error {
totalSharesRat, err := totalShares.ToBigRat()
if err != nil {
return err
}
if totalSharesRat.Sign() < 0 {
if totalShares.NumShares.BigInt().Sign() < 0 {
return types.ErrNegativeShares
}

Expand All @@ -46,10 +42,9 @@ func (k Keeper) SetTotalShares(
totalSharesStore.Set(vaultId.ToStateKey(), b)

// Emit metric on TotalShares.
totalSharesFloat, _ := totalSharesRat.Float32()
vaultId.SetGaugeWithLabels(
metrics.TotalShares,
totalSharesFloat,
float32(totalShares.NumShares.BigInt().Uint64()),
)

return nil
Expand Down Expand Up @@ -86,11 +81,7 @@ func (k Keeper) SetOwnerShares(
owner string,
ownerShares types.NumShares,
) error {
ownerSharesRat, err := ownerShares.ToBigRat()
if err != nil {
return err
}
if ownerSharesRat.Sign() < 0 {
if ownerShares.NumShares.BigInt().Sign() < 0 {
return types.ErrNegativeShares
}

Expand Down Expand Up @@ -120,36 +111,27 @@ func (k Keeper) MintShares(
quantumsToDeposit *big.Int,
) error {
// Quantums to deposit should be positive.
if quantumsToDeposit.Cmp(big.NewInt(0)) <= 0 {
if quantumsToDeposit.Sign() <= 0 {
return types.ErrInvalidDepositAmount
}
// Get existing TotalShares of the vault.
totalShares, exists := k.GetTotalShares(ctx, vaultId)
var existingTotalShares *big.Rat
var err error
if exists {
existingTotalShares, err = totalShares.ToBigRat()
if err != nil {
return err
}
} else {
existingTotalShares = new(big.Rat).SetInt(big.NewInt(0))
}
existingTotalShares := totalShares.NumShares.BigInt()
// Calculate shares to mint.
var sharesToMint *big.Rat
var sharesToMint *big.Int
if !exists || existingTotalShares.Sign() <= 0 {
// Mint `quoteQuantums` number of shares.
sharesToMint = new(big.Rat).SetInt(quantumsToDeposit)
sharesToMint = new(big.Int).Set(quantumsToDeposit)
// Initialize existingTotalShares as 0.
existingTotalShares = new(big.Rat).SetInt(big.NewInt(0))
existingTotalShares = big.NewInt(0)
} else {
// Get vault equity.
equity, err := k.GetVaultEquity(ctx, vaultId)
if err != nil {
return err
}
// Don't mint shares if equity is non-positive.
if equity.Cmp(big.NewInt(0)) <= 0 {
if equity.Sign() <= 0 {
return types.ErrNonPositiveEquity
}
// Mint `deposit (in quote quantums) * existing shares / vault equity (in quote quantums)`
Expand All @@ -158,16 +140,16 @@ func (k Keeper) MintShares(
// - a vault currently has 5000 shares and 4000 equity (in quote quantums)
// - each quote quantum is worth 5000 / 4000 = 1.25 shares
// - a deposit of 1000 quote quantums should thus be given 1000 * 1.25 = 1250 shares
sharesToMint = new(big.Rat).SetInt(quantumsToDeposit)
sharesToMint = new(big.Int).Set(quantumsToDeposit)
sharesToMint = sharesToMint.Mul(sharesToMint, existingTotalShares)
sharesToMint = sharesToMint.Quo(sharesToMint, new(big.Rat).SetInt(equity))
sharesToMint = sharesToMint.Quo(sharesToMint, equity)
}

// Increase TotalShares of the vault.
err = k.SetTotalShares(
err := k.SetTotalShares(
ctx,
vaultId,
types.BigRatToNumShares(
types.BigIntToNumShares(
existingTotalShares.Add(existingTotalShares, sharesToMint),
),
)
Expand All @@ -183,22 +165,19 @@ func (k Keeper) MintShares(
ctx,
vaultId,
owner,
types.BigRatToNumShares(sharesToMint),
types.BigIntToNumShares(sharesToMint),
)
if err != nil {
return err
}
} else {
// Increase existing owner shares by sharesToMint.
existingOwnerShares, err := ownerShares.ToBigRat()
if err != nil {
return err
}
existingOwnerShares := ownerShares.NumShares.BigInt()
err = k.SetOwnerShares(
ctx,
vaultId,
owner,
types.BigRatToNumShares(
types.BigIntToNumShares(
existingOwnerShares.Add(existingOwnerShares, sharesToMint),
),
)
Expand Down
Loading

0 comments on commit ca477c9

Please sign in to comment.