diff --git a/protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go b/protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go index b210f534eeb..b803769a86d 100644 --- a/protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go +++ b/protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go @@ -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. @@ -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 }{ @@ -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), @@ -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), @@ -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, @@ -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), @@ -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), @@ -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), @@ -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. @@ -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. diff --git a/protocol/x/vault/keeper/orders.go b/protocol/x/vault/keeper/orders.go index 768758340b4..dae848b36a4 100644 --- a/protocol/x/vault/keeper/orders.go +++ b/protocol/x/vault/keeper/orders.go @@ -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 } diff --git a/protocol/x/vault/keeper/orders_test.go b/protocol/x/vault/keeper/orders_test.go index 327c3b67348..fbb9b036fc5 100644 --- a/protocol/x/vault/keeper/orders_test.go +++ b/protocol/x/vault/keeper/orders_test.go @@ -30,16 +30,16 @@ 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": { @@ -47,9 +47,9 @@ func TestRefreshAllVaultOrders(t *testing.T) { 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": { @@ -57,9 +57,9 @@ func TestRefreshAllVaultOrders(t *testing.T) { 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), }, }, } @@ -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) } diff --git a/protocol/x/vault/keeper/shares.go b/protocol/x/vault/keeper/shares.go index 5c2988741ac..81948611fca 100644 --- a/protocol/x/vault/keeper/shares.go +++ b/protocol/x/vault/keeper/shares.go @@ -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 } @@ -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 @@ -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 } @@ -120,28 +111,19 @@ 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) @@ -149,7 +131,7 @@ func (k Keeper) MintShares( 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)` @@ -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), ), ) @@ -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), ), ) diff --git a/protocol/x/vault/keeper/shares_test.go b/protocol/x/vault/keeper/shares_test.go index b2da4e66b2b..0728a18cb95 100644 --- a/protocol/x/vault/keeper/shares_test.go +++ b/protocol/x/vault/keeper/shares_test.go @@ -23,8 +23,8 @@ func TestGetSetTotalShares(t *testing.T) { require.Equal(t, false, exists) // Set total shares for a vault and then get. - numShares := vaulttypes.BigRatToNumShares( - big.NewRat(7, 1), + numShares := vaulttypes.BigIntToNumShares( + big.NewInt(7), ) err := k.SetTotalShares(ctx, constants.Vault_Clob_0, numShares) require.NoError(t, err) @@ -33,8 +33,8 @@ func TestGetSetTotalShares(t *testing.T) { require.Equal(t, numShares, got) // Set total shares for another vault and then get. - numShares = vaulttypes.BigRatToNumShares( - big.NewRat(456, 3), + numShares = vaulttypes.BigIntToNumShares( + big.NewInt(456), ) err = k.SetTotalShares(ctx, constants.Vault_Clob_1, numShares) require.NoError(t, err) @@ -43,8 +43,8 @@ func TestGetSetTotalShares(t *testing.T) { require.Equal(t, numShares, got) // Set total shares for second vault to 0. - numShares = vaulttypes.BigRatToNumShares( - big.NewRat(0, 1), + numShares = vaulttypes.BigIntToNumShares( + big.NewInt(0), ) err = k.SetTotalShares(ctx, constants.Vault_Clob_1, numShares) require.NoError(t, err) @@ -53,8 +53,8 @@ func TestGetSetTotalShares(t *testing.T) { require.Equal(t, numShares, got) // Set total shares for the first vault again and then get. - numShares = vaulttypes.BigRatToNumShares( - big.NewRat(73423, 59), + numShares = vaulttypes.BigIntToNumShares( + big.NewInt(7283133), ) err = k.SetTotalShares(ctx, constants.Vault_Clob_0, numShares) require.NoError(t, err) @@ -64,18 +64,16 @@ func TestGetSetTotalShares(t *testing.T) { // Set total shares for the first vault to a negative value. // Should get error and total shares should remain unchanged. - numShares = vaulttypes.BigRatToNumShares( - big.NewRat(-1, 1), + negativeShares := vaulttypes.BigIntToNumShares( + big.NewInt(-1), ) - err = k.SetTotalShares(ctx, constants.Vault_Clob_0, numShares) + err = k.SetTotalShares(ctx, constants.Vault_Clob_0, negativeShares) require.Equal(t, vaulttypes.ErrNegativeShares, err) got, exists = k.GetTotalShares(ctx, constants.Vault_Clob_0) require.Equal(t, true, exists) require.Equal( t, - vaulttypes.BigRatToNumShares( - big.NewRat(73423, 59), - ), + numShares, got, ) } @@ -93,8 +91,8 @@ func TestGetSetOwnerShares(t *testing.T) { require.Equal(t, false, exists) // Set owner shares for Alice in vault clob 0 and get. - numShares := vaulttypes.BigRatToNumShares( - big.NewRat(7, 1), + numShares := vaulttypes.BigIntToNumShares( + big.NewInt(7), ) err := k.SetOwnerShares(ctx, constants.Vault_Clob_0, alice, numShares) require.NoError(t, err) @@ -103,8 +101,8 @@ func TestGetSetOwnerShares(t *testing.T) { require.Equal(t, numShares, got) // Set owner shares for Alice in vault clob 1 and then get. - numShares = vaulttypes.BigRatToNumShares( - big.NewRat(456, 3), + numShares = vaulttypes.BigIntToNumShares( + big.NewInt(456), ) err = k.SetOwnerShares(ctx, constants.Vault_Clob_1, alice, numShares) require.NoError(t, err) @@ -113,8 +111,8 @@ func TestGetSetOwnerShares(t *testing.T) { require.Equal(t, numShares, got) // Set owner shares for Bob in vault clob 1. - numShares = vaulttypes.BigRatToNumShares( - big.NewRat(0, 1), + numShares = vaulttypes.BigIntToNumShares( + big.NewInt(0), ) err = k.SetOwnerShares(ctx, constants.Vault_Clob_1, bob, numShares) require.NoError(t, err) @@ -124,8 +122,8 @@ func TestGetSetOwnerShares(t *testing.T) { // Set owner shares for Bob in vault clob 1 to a negative value. // Should get error and total shares should remain unchanged. - numSharesInvalid := vaulttypes.BigRatToNumShares( - big.NewRat(-1, 1), + numSharesInvalid := vaulttypes.BigIntToNumShares( + big.NewInt(-1), ) err = k.SetOwnerShares(ctx, constants.Vault_Clob_1, bob, numSharesInvalid) require.ErrorIs(t, err, vaulttypes.ErrNegativeShares) @@ -142,98 +140,108 @@ func TestMintShares(t *testing.T) { // Existing vault equity. equity *big.Int // Existing vault TotalShares. - totalShares *big.Rat + totalShares *big.Int // Owner that deposits. owner string // Existing owner shares. - ownerShares *big.Rat + ownerShares *big.Int // Quote quantums to deposit. quantumsToDeposit *big.Int /* --- Expectations --- */ // Expected TotalShares after minting. - expectedTotalShares *big.Rat + expectedTotalShares *big.Int // Expected OwnerShares after minting. - expectedOwnerShares *big.Rat + expectedOwnerShares *big.Int // Expected error. expectedErr error }{ "Equity 0, TotalShares 0, OwnerShares 0, Deposit 1000": { vaultId: constants.Vault_Clob_0, equity: big.NewInt(0), - totalShares: big.NewRat(0, 1), + totalShares: big.NewInt(0), owner: constants.AliceAccAddress.String(), - ownerShares: big.NewRat(0, 1), + ownerShares: big.NewInt(0), quantumsToDeposit: big.NewInt(1_000), - // Should mint `1_000 / 1` shares. - expectedTotalShares: big.NewRat(1_000, 1), - expectedOwnerShares: big.NewRat(1_000, 1), + // Should mint `1_000` shares. + expectedTotalShares: big.NewInt(1_000), + expectedOwnerShares: big.NewInt(1_000), }, "Equity 0, TotalShares non-existent, OwnerShares non-existent, Deposit 12345654321": { vaultId: constants.Vault_Clob_0, equity: big.NewInt(0), owner: constants.AliceAccAddress.String(), quantumsToDeposit: big.NewInt(12_345_654_321), - // Should mint `12_345_654_321 / 1` shares. - expectedTotalShares: big.NewRat(12_345_654_321, 1), - expectedOwnerShares: big.NewRat(12_345_654_321, 1), + // Should mint `12_345_654_321` shares. + expectedTotalShares: big.NewInt(12_345_654_321), + expectedOwnerShares: big.NewInt(12_345_654_321), }, "Equity 1000, TotalShares non-existent, OwnerShares non-existent, Deposit 500": { vaultId: constants.Vault_Clob_0, equity: big.NewInt(1_000), owner: constants.AliceAccAddress.String(), quantumsToDeposit: big.NewInt(500), - // Should mint `500 / 1` shares. - expectedTotalShares: big.NewRat(500, 1), - expectedOwnerShares: big.NewRat(500, 1), + // Should mint `500` shares. + expectedTotalShares: big.NewInt(500), + expectedOwnerShares: big.NewInt(500), }, "Equity 4000, TotalShares 5000, OwnerShares 2500, Deposit 1000": { vaultId: constants.Vault_Clob_1, equity: big.NewInt(4_000), - totalShares: big.NewRat(5_000, 1), + totalShares: big.NewInt(5_000), owner: constants.AliceAccAddress.String(), - ownerShares: big.NewRat(2_500, 1), + ownerShares: big.NewInt(2_500), quantumsToDeposit: big.NewInt(1_000), - // Should mint `1_250 / 1` shares. - expectedTotalShares: big.NewRat(6_250, 1), - expectedOwnerShares: big.NewRat(3_750, 1), + // Should mint `1_250` shares. + expectedTotalShares: big.NewInt(6_250), + expectedOwnerShares: big.NewInt(3_750), }, - "Equity 1_000_000, TotalShares 1, OwnerShares 1/2, Deposit 1": { + "Equity 1_000_000, TotalShares 2_000, OwnerShares 1, Deposit 1_000": { vaultId: constants.Vault_Clob_1, equity: big.NewInt(1_000_000), - totalShares: big.NewRat(1, 1), + totalShares: big.NewInt(2_000), owner: constants.BobAccAddress.String(), - ownerShares: big.NewRat(1, 2), - quantumsToDeposit: big.NewInt(1), - // Should mint `1 / 1_000_000` shares. - expectedTotalShares: big.NewRat(1_000_001, 1_000_000), - expectedOwnerShares: big.NewRat(500_001, 1_000_000), + ownerShares: big.NewInt(1), + quantumsToDeposit: big.NewInt(1_000), + // Should mint `2` shares. + expectedTotalShares: big.NewInt(2_002), + expectedOwnerShares: big.NewInt(3), }, - "Equity 8000, TotalShares 4000, OwnerShares Deposit 455": { + "Equity 8000, TotalShares 4000, OwnerShares 101, Deposit 455": { vaultId: constants.Vault_Clob_1, equity: big.NewInt(8_000), - totalShares: big.NewRat(4_000, 1), + totalShares: big.NewInt(4_000), owner: constants.CarlAccAddress.String(), - ownerShares: big.NewRat(101, 7), + ownerShares: big.NewInt(101), quantumsToDeposit: big.NewInt(455), - // Should mint `455 / 2` shares. - expectedTotalShares: big.NewRat(8_455, 2), - expectedOwnerShares: big.NewRat(3_387, 14), + // Should mint `227.5` shares, round down to 227. + expectedTotalShares: big.NewInt(4_227), + expectedOwnerShares: big.NewInt(328), }, "Equity 123456, TotalShares 654321, OwnerShares 0, Deposit 123456789": { vaultId: constants.Vault_Clob_1, equity: big.NewInt(123_456), - totalShares: big.NewRat(654_321, 1), + totalShares: big.NewInt(654_321), owner: constants.DaveAccAddress.String(), quantumsToDeposit: big.NewInt(123_456_789), - // Should mint `26_926_789_878_423 / 41_152` shares. - expectedTotalShares: big.NewRat(26_953_716_496_215, 41_152), - expectedOwnerShares: big.NewRat(26_926_789_878_423, 41_152), + // Should mint `654_325_181.727` shares, round down to 654_325_181. + expectedTotalShares: big.NewInt(654_979_502), + expectedOwnerShares: big.NewInt(654_325_181), + }, + "Equity 1000000, TotalShares 1000, OwnerShares 0, Deposit 99": { + vaultId: constants.Vault_Clob_1, + equity: big.NewInt(1_000_000), + totalShares: big.NewInt(1_000), + owner: constants.DaveAccAddress.String(), + quantumsToDeposit: big.NewInt(99), + // Should mint `99 * 1_000 / 1_000_000` shares, round down to 0. + expectedTotalShares: big.NewInt(1_000), + expectedOwnerShares: big.NewInt(0), }, "Equity -1, TotalShares 10, Deposit 1": { vaultId: constants.Vault_Clob_1, equity: big.NewInt(-1), - totalShares: big.NewRat(10, 1), + totalShares: big.NewInt(10), owner: constants.AliceAccAddress.String(), quantumsToDeposit: big.NewInt(1), expectedErr: vaulttypes.ErrNonPositiveEquity, @@ -241,7 +249,7 @@ func TestMintShares(t *testing.T) { "Equity 1, TotalShares 1, Deposit 0": { vaultId: constants.Vault_Clob_1, equity: big.NewInt(1), - totalShares: big.NewRat(1, 1), + totalShares: big.NewInt(1), owner: constants.AliceAccAddress.String(), quantumsToDeposit: big.NewInt(0), expectedErr: vaulttypes.ErrInvalidDepositAmount, @@ -286,16 +294,17 @@ func TestMintShares(t *testing.T) { err := tApp.App.VaultKeeper.SetTotalShares( ctx, tc.vaultId, - vaulttypes.BigRatToNumShares(tc.totalShares), + vaulttypes.BigIntToNumShares(tc.totalShares), ) require.NoError(t, err) } + // Set vault's existing owner shares if specified. if tc.ownerShares != nil { err := tApp.App.VaultKeeper.SetOwnerShares( ctx, tc.vaultId, tc.owner, - vaulttypes.BigRatToNumShares(tc.ownerShares), + vaulttypes.BigIntToNumShares(tc.ownerShares), ) require.NoError(t, err) } @@ -314,12 +323,12 @@ func TestMintShares(t *testing.T) { totalShares, _ := tApp.App.VaultKeeper.GetTotalShares(ctx, tc.vaultId) require.Equal( t, - vaulttypes.BigRatToNumShares(tc.totalShares), + vaulttypes.BigIntToNumShares(tc.totalShares), totalShares, ) // Check that OwnerShares is unchanged. ownerShares, _ := tApp.App.VaultKeeper.GetOwnerShares(ctx, tc.vaultId, tc.owner) - require.Equal(t, vaulttypes.BigRatToNumShares(tc.ownerShares), ownerShares) + require.Equal(t, vaulttypes.BigIntToNumShares(tc.ownerShares), ownerShares) } else { require.NoError(t, err) // Check that TotalShares is as expected. @@ -327,7 +336,7 @@ func TestMintShares(t *testing.T) { require.True(t, exists) require.Equal( t, - vaulttypes.BigRatToNumShares(tc.expectedTotalShares), + vaulttypes.BigIntToNumShares(tc.expectedTotalShares), totalShares, ) // Check that OwnerShares is as expected. @@ -335,7 +344,7 @@ func TestMintShares(t *testing.T) { require.True(t, exists) require.Equal( t, - vaulttypes.BigRatToNumShares(tc.expectedOwnerShares), + vaulttypes.BigIntToNumShares(tc.expectedOwnerShares), ownerShares, ) } diff --git a/protocol/x/vault/keeper/vault.go b/protocol/x/vault/keeper/vault.go index 18892a35318..fc8236bef67 100644 --- a/protocol/x/vault/keeper/vault.go +++ b/protocol/x/vault/keeper/vault.go @@ -40,8 +40,7 @@ func (k Keeper) DecommissionVaults( 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 } diff --git a/protocol/x/vault/keeper/vault_test.go b/protocol/x/vault/keeper/vault_test.go index 161089e37b9..985c817ecfb 100644 --- a/protocol/x/vault/keeper/vault_test.go +++ b/protocol/x/vault/keeper/vault_test.go @@ -42,8 +42,8 @@ func TestDecomissionVaults(t *testing.T) { k := tApp.App.VaultKeeper // Set total shares and owner shares for both vaults. - shares := vaulttypes.BigRatToNumShares( - big.NewRat(7, 1), + shares := vaulttypes.BigIntToNumShares( + big.NewInt(7), ) err := k.SetTotalShares( ctx, @@ -98,8 +98,8 @@ func TestDecomissionVault(t *testing.T) { k.DecommissionVault(ctx, constants.Vault_Clob_0) // Set total shares and owner shares for two owners of a vault. - shares := vaulttypes.BigRatToNumShares( - big.NewRat(7, 1), + shares := vaulttypes.BigIntToNumShares( + big.NewInt(7), ) err := k.SetTotalShares( ctx, diff --git a/protocol/x/vault/types/num_shares.go b/protocol/x/vault/types/num_shares.go index 0a540bb2812..040f1b749bb 100644 --- a/protocol/x/vault/types/num_shares.go +++ b/protocol/x/vault/types/num_shares.go @@ -6,25 +6,12 @@ import ( "github.com/dydxprotocol/v4-chain/protocol/dtypes" ) -// ToBigRat converts a NumShares to a big.Rat. -// Returns an error if the denominator is zero. -func (n NumShares) ToBigRat() (*big.Rat, error) { - if n.Numerator.IsNil() || n.Denominator.IsNil() { - return nil, ErrNilFraction - } - if n.Denominator.Cmp(dtypes.NewInt(0)) == 0 { - return nil, ErrZeroDenominator - } - - return new(big.Rat).SetFrac(n.Numerator.BigInt(), n.Denominator.BigInt()), nil -} - -func BigRatToNumShares(rat *big.Rat) (n NumShares) { - if rat == nil { +// BigIntToNumShares returns a NumShares given a big.Int. +func BigIntToNumShares(num *big.Int) (n NumShares) { + if num == nil { return n } return NumShares{ - Numerator: dtypes.NewIntFromBigInt(rat.Num()), - Denominator: dtypes.NewIntFromBigInt(rat.Denom()), + NumShares: dtypes.NewIntFromBigInt(num), } } diff --git a/protocol/x/vault/types/num_shares_test.go b/protocol/x/vault/types/num_shares_test.go index 3bb15932ffd..ec1713dbbbc 100644 --- a/protocol/x/vault/types/num_shares_test.go +++ b/protocol/x/vault/types/num_shares_test.go @@ -9,103 +9,26 @@ import ( "github.com/stretchr/testify/require" ) -func TestNumShares_ToBigRat(t *testing.T) { +func TestBigIntToNumShares(t *testing.T) { tests := map[string]struct { - n types.NumShares - expectedRat *big.Rat - expectedErr error - }{ - "Success - 1/1": { - n: types.NumShares{ - Numerator: dtypes.NewInt(1), - Denominator: dtypes.NewInt(1), - }, - expectedRat: big.NewRat(1, 1), - }, - "Success - 1/77": { - n: types.NumShares{ - Numerator: dtypes.NewInt(1), - Denominator: dtypes.NewInt(77), - }, - expectedRat: big.NewRat(1, 77), - }, - "Success - 99/2": { - n: types.NumShares{ - Numerator: dtypes.NewInt(99), - Denominator: dtypes.NewInt(2), - }, - expectedRat: big.NewRat(99, 2), - }, - "Failure - numerator is nil": { - n: types.NumShares{ - Denominator: dtypes.NewInt(77), - }, - expectedErr: types.ErrNilFraction, - }, - "Failure - denominator is nil": { - n: types.NumShares{ - Numerator: dtypes.NewInt(77), - }, - expectedErr: types.ErrNilFraction, - }, - "Failure - denominator is 0": { - n: types.NumShares{ - Numerator: dtypes.NewInt(77), - Denominator: dtypes.NewInt(0), - }, - expectedErr: types.ErrZeroDenominator, - }, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - rat, err := tc.n.ToBigRat() - if tc.expectedErr == nil { - require.NoError(t, err) - require.Equal(t, tc.expectedRat, rat) - } else { - require.ErrorIs(t, err, tc.expectedErr) - require.True(t, rat == nil) - } - }) - } -} - -func TestBigRatToNumShares(t *testing.T) { - tests := map[string]struct { - rat *big.Rat + num *big.Int expectedNumShares types.NumShares }{ - "Success - 1/1": { - rat: big.NewRat(1, 1), - expectedNumShares: types.NumShares{ - Numerator: dtypes.NewInt(1), - Denominator: dtypes.NewInt(1), - }, - }, - "Success - 1/2": { - rat: big.NewRat(1, 2), - expectedNumShares: types.NumShares{ - Numerator: dtypes.NewInt(1), - Denominator: dtypes.NewInt(2), - }, - }, - "Success - 5/3": { - rat: big.NewRat(5, 3), + "Success - 1": { + num: big.NewInt(1), expectedNumShares: types.NumShares{ - Numerator: dtypes.NewInt(5), - Denominator: dtypes.NewInt(3), + NumShares: dtypes.NewInt(1), }, }, - "Success - rat is nil": { - rat: nil, + "Success - num is nil": { + num: nil, expectedNumShares: types.NumShares{}, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - n := types.BigRatToNumShares(tc.rat) + n := types.BigIntToNumShares(tc.num) require.Equal(t, tc.expectedNumShares, n) }) } diff --git a/protocol/x/vault/types/vault.pb.go b/protocol/x/vault/types/vault.pb.go index 54d9648e45f..704b30d081e 100644 --- a/protocol/x/vault/types/vault.pb.go +++ b/protocol/x/vault/types/vault.pb.go @@ -107,13 +107,10 @@ func (m *VaultId) GetNumber() uint32 { return 0 } -// 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. type NumShares struct { - // Numerator. - Numerator github_com_dydxprotocol_v4_chain_protocol_dtypes.SerializableInt `protobuf:"bytes,1,opt,name=numerator,proto3,customtype=github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt" json:"numerator"` - // Denominator. - Denominator github_com_dydxprotocol_v4_chain_protocol_dtypes.SerializableInt `protobuf:"bytes,2,opt,name=denominator,proto3,customtype=github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt" json:"denominator"` + // Number of shares. + NumShares github_com_dydxprotocol_v4_chain_protocol_dtypes.SerializableInt `protobuf:"bytes,2,opt,name=num_shares,json=numShares,proto3,customtype=github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt" json:"num_shares"` } func (m *NumShares) Reset() { *m = NumShares{} } @@ -158,7 +155,7 @@ func init() { func init() { proto.RegisterFile("dydxprotocol/vault/vault.proto", fileDescriptor_32accb5830bb2860) } var fileDescriptor_32accb5830bb2860 = []byte{ - // 320 bytes of a gzipped FileDescriptorProto + // 300 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4b, 0xa9, 0x4c, 0xa9, 0x28, 0x28, 0xca, 0x2f, 0xc9, 0x4f, 0xce, 0xcf, 0xd1, 0x2f, 0x4b, 0x2c, 0xcd, 0x29, 0x81, 0x90, 0x7a, 0x60, 0x41, 0x21, 0x21, 0x64, 0x79, 0x3d, 0xb0, 0x8c, 0x94, 0x48, 0x7a, 0x7e, 0x7a, 0x3e, @@ -166,19 +163,18 @@ var fileDescriptor_32accb5830bb2860 = []byte{ 0x19, 0x72, 0xb1, 0x94, 0x54, 0x16, 0xa4, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x19, 0xc9, 0xea, 0x61, 0x9a, 0xa1, 0x07, 0x56, 0x1a, 0x52, 0x59, 0x90, 0x1a, 0x04, 0x56, 0x2a, 0x24, 0xc6, 0xc5, 0x96, 0x57, 0x9a, 0x9b, 0x94, 0x5a, 0x24, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0x1b, 0x04, 0xe5, 0x29, - 0xdd, 0x67, 0xe4, 0xe2, 0xf4, 0x2b, 0xcd, 0x0d, 0xce, 0x48, 0x2c, 0x4a, 0x2d, 0x16, 0x4a, 0xe3, - 0xe2, 0xcc, 0x2b, 0xcd, 0x4d, 0x2d, 0x4a, 0x2c, 0xc9, 0x2f, 0x02, 0x9b, 0xce, 0xe3, 0xe4, 0x71, - 0xe2, 0x9e, 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0x0e, 0xe9, 0x99, 0x25, 0x19, 0xa5, 0x49, 0x7a, 0xc9, - 0xf9, 0xb9, 0xfa, 0xa8, 0x7e, 0x32, 0xd1, 0x4d, 0xce, 0x48, 0xcc, 0xcc, 0xd3, 0x87, 0x8b, 0xa4, - 0x80, 0x6c, 0x2c, 0xd6, 0x0b, 0x4e, 0x2d, 0xca, 0x4c, 0xcc, 0xc9, 0xac, 0x4a, 0x4c, 0xca, 0x49, - 0xf5, 0xcc, 0x2b, 0x09, 0x42, 0x18, 0x2d, 0x94, 0xc5, 0xc5, 0x9d, 0x92, 0x9a, 0x97, 0x9f, 0x9b, - 0x99, 0x07, 0xb6, 0x89, 0x89, 0xca, 0x36, 0x21, 0x1b, 0xae, 0x65, 0xc3, 0xc5, 0x09, 0x0f, 0x0c, - 0x21, 0x29, 0x2e, 0xb1, 0x30, 0xc7, 0x50, 0x9f, 0x90, 0xf8, 0x90, 0xc8, 0x00, 0xd7, 0xf8, 0x50, - 0xbf, 0xe0, 0x00, 0x57, 0x67, 0x4f, 0x37, 0x4f, 0x57, 0x17, 0x01, 0x06, 0x21, 0x61, 0x2e, 0x7e, - 0x24, 0x39, 0x67, 0x1f, 0x7f, 0x27, 0x01, 0x46, 0xa7, 0xc0, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, - 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, - 0x3c, 0x96, 0x63, 0x88, 0x32, 0x27, 0xde, 0x99, 0x15, 0xd0, 0x88, 0x07, 0xbb, 0x36, 0x89, 0x0d, - 0x2c, 0x6e, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x76, 0x09, 0x36, 0xbc, 0x1b, 0x02, 0x00, 0x00, + 0x95, 0x70, 0x71, 0xfa, 0x95, 0xe6, 0x06, 0x67, 0x24, 0x16, 0xa5, 0x16, 0x0b, 0xa5, 0x73, 0x71, + 0xe5, 0x95, 0xe6, 0xc6, 0x17, 0x83, 0x79, 0x60, 0x85, 0x3c, 0x4e, 0x1e, 0x27, 0xee, 0xc9, 0x33, + 0xdc, 0xba, 0x27, 0xef, 0x90, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x8f, + 0xea, 0x27, 0x13, 0xdd, 0xe4, 0x8c, 0xc4, 0xcc, 0x3c, 0x7d, 0xb8, 0x48, 0x0a, 0xc8, 0xc6, 0x62, + 0xbd, 0xe0, 0xd4, 0xa2, 0xcc, 0xc4, 0x9c, 0xcc, 0xaa, 0xc4, 0xa4, 0x9c, 0x54, 0xcf, 0xbc, 0x92, + 0x20, 0xce, 0x3c, 0x98, 0x45, 0x5a, 0x36, 0x5c, 0x9c, 0x70, 0x07, 0x0a, 0x49, 0x71, 0x89, 0x85, + 0x39, 0x86, 0xfa, 0x84, 0xc4, 0x87, 0x44, 0x06, 0xb8, 0xc6, 0x87, 0xfa, 0x05, 0x07, 0xb8, 0x3a, + 0x7b, 0xba, 0x79, 0xba, 0xba, 0x08, 0x30, 0x08, 0x09, 0x73, 0xf1, 0x23, 0xc9, 0x39, 0xfb, 0xf8, + 0x3b, 0x09, 0x30, 0x3a, 0x05, 0x9e, 0x78, 0x24, 0xc7, 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, + 0x72, 0x8c, 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, 0x2c, 0xc7, 0x70, 0xe3, 0xb1, 0x1c, 0x43, 0x94, + 0x39, 0xf1, 0x8e, 0xac, 0x80, 0x46, 0x06, 0xd8, 0xad, 0x49, 0x6c, 0x60, 0x71, 0x63, 0x40, 0x00, + 0x00, 0x00, 0xff, 0xff, 0xf0, 0x30, 0x49, 0x71, 0xaf, 0x01, 0x00, 0x00, } func (m *VaultId) Marshal() (dAtA []byte, err error) { @@ -235,25 +231,15 @@ func (m *NumShares) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size := m.Denominator.Size() + size := m.NumShares.Size() i -= size - if _, err := m.Denominator.MarshalTo(dAtA[i:]); err != nil { + if _, err := m.NumShares.MarshalTo(dAtA[i:]); err != nil { return 0, err } i = encodeVarintVault(dAtA, i, uint64(size)) } i-- dAtA[i] = 0x12 - { - size := m.Numerator.Size() - i -= size - if _, err := m.Numerator.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - i = encodeVarintVault(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0xa return len(dAtA) - i, nil } @@ -289,9 +275,7 @@ func (m *NumShares) Size() (n int) { } var l int _ = l - l = m.Numerator.Size() - n += 1 + l + sovVault(uint64(l)) - l = m.Denominator.Size() + l = m.NumShares.Size() n += 1 + l + sovVault(uint64(l)) return n } @@ -419,42 +403,9 @@ func (m *NumShares) Unmarshal(dAtA []byte) error { return fmt.Errorf("proto: NumShares: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Numerator", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowVault - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthVault - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthVault - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.Numerator.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Denominator", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field NumShares", wireType) } var byteLen int for shift := uint(0); ; shift += 7 { @@ -481,7 +432,7 @@ func (m *NumShares) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.Denominator.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.NumShares.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex