Skip to content

Commit

Permalink
add vault owner shares (#1253)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 authored Mar 27, 2024
1 parent 7492e0e commit 083762c
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 31 deletions.
67 changes: 45 additions & 22 deletions protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type DepositInstance struct {
checkTxFails bool
// Whether DeliverTx fails.
deliverTxFails bool
// Expected owner shares for depositor above.
expectedOwnerShares *big.Rat
}

// DepositorSetup represents the setup of a depositor.
Expand Down Expand Up @@ -67,14 +69,16 @@ func TestMsgDepositToVault(t *testing.T) {
},
depositInstances: []DepositInstance{
{
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(123),
msgSigner: constants.Alice_Num0.Owner,
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(123),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(123, 1),
},
{
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(321),
msgSigner: constants.Alice_Num0.Owner,
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(321),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(444, 1),
},
},
totalSharesHistory: []*big.Rat{
Expand All @@ -100,14 +104,16 @@ func TestMsgDepositToVault(t *testing.T) {
},
depositInstances: []DepositInstance{
{
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(1_000),
msgSigner: constants.Alice_Num0.Owner,
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(1_000),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(1_000, 1),
},
{
depositor: constants.Bob_Num1,
depositAmount: big.NewInt(500),
msgSigner: constants.Bob_Num1.Owner,
depositor: constants.Bob_Num1,
depositAmount: big.NewInt(500),
msgSigner: constants.Bob_Num1.Owner,
expectedOwnerShares: big.NewRat(500, 1),
},
},
totalSharesHistory: []*big.Rat{
Expand All @@ -133,15 +139,17 @@ func TestMsgDepositToVault(t *testing.T) {
},
depositInstances: []DepositInstance{
{
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(1_000),
msgSigner: constants.Alice_Num0.Owner,
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(1_000),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(1_000, 1),
},
{
depositor: constants.Bob_Num1,
depositAmount: big.NewInt(501), // Greater than balance.
msgSigner: constants.Bob_Num1.Owner,
deliverTxFails: true,
depositor: constants.Bob_Num1,
depositAmount: big.NewInt(501), // Greater than balance.
msgSigner: constants.Bob_Num1.Owner,
deliverTxFails: true,
expectedOwnerShares: nil,
},
},
totalSharesHistory: []*big.Rat{
Expand Down Expand Up @@ -172,11 +180,13 @@ func TestMsgDepositToVault(t *testing.T) {
msgSigner: constants.Alice_Num0.Owner, // Incorrect signer.
checkTxFails: true,
checkTxResponseContains: "does not match signer address",
expectedOwnerShares: nil,
},
{
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(1_000),
msgSigner: constants.Alice_Num0.Owner,
depositor: constants.Alice_Num0,
depositAmount: big.NewInt(1_000),
msgSigner: constants.Alice_Num0.Owner,
expectedOwnerShares: big.NewRat(1_000, 1),
},
},
totalSharesHistory: []*big.Rat{
Expand Down Expand Up @@ -207,13 +217,15 @@ func TestMsgDepositToVault(t *testing.T) {
msgSigner: constants.Alice_Num0.Owner,
checkTxFails: true,
checkTxResponseContains: "Deposit amount is invalid",
expectedOwnerShares: nil,
},
{
depositor: constants.Bob_Num0,
depositAmount: big.NewInt(-1),
msgSigner: constants.Bob_Num0.Owner,
checkTxFails: true,
checkTxResponseContains: "Deposit amount is invalid",
expectedOwnerShares: nil,
},
},
totalSharesHistory: []*big.Rat{
Expand Down Expand Up @@ -320,6 +332,17 @@ func TestMsgDepositToVault(t *testing.T) {
vaulttypes.BigRatToNumShares(tc.totalSharesHistory[i]),
totalShares,
)
// Check that owner shares of the depositor is as expected.
ownerShares, _ := tApp.App.VaultKeeper.GetOwnerShares(
ctx,
tc.vaultId,
depositInstance.depositor.Owner,
)
require.Equal(
t,
vaulttypes.BigRatToNumShares(depositInstance.expectedOwnerShares),
ownerShares,
)
// Check that equity of the vault is as expected.
vaultEquity, err := tApp.App.VaultKeeper.GetVaultEquity(ctx, tc.vaultId)
require.NoError(t, err)
Expand Down
80 changes: 79 additions & 1 deletion protocol/x/vault/keeper/shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,54 @@ func (k Keeper) getTotalSharesIterator(ctx sdk.Context) storetypes.Iterator {
return storetypes.KVStorePrefixIterator(store, []byte{})
}

// GetOwnerShares gets owner shares for an owner in a vault.
func (k Keeper) GetOwnerShares(
ctx sdk.Context,
vaultId types.VaultId,
owner string,
) (val types.NumShares, exists bool) {
store := k.getVaultOwnerSharesStore(ctx, vaultId)

b := store.Get([]byte(owner))
if b == nil {
return val, false
}

k.cdc.MustUnmarshal(b, &val)
return val, true
}

// SetOwnerShares sets owner shares for an owner in a vault.
func (k Keeper) SetOwnerShares(
ctx sdk.Context,
vaultId types.VaultId,
owner string,
ownerShares types.NumShares,
) error {
ownerSharesRat, err := ownerShares.ToBigRat()
if err != nil {
return err
}
if ownerSharesRat.Sign() < 0 {
return types.ErrNegativeShares
}

b := k.cdc.MustMarshal(&ownerShares)
store := k.getVaultOwnerSharesStore(ctx, vaultId)
store.Set([]byte(owner), b)

return nil
}

// getVaultOwnerSharesStore returns the store for owner shares of a given vault.
func (k Keeper) getVaultOwnerSharesStore(
ctx sdk.Context,
vaultId types.VaultId,
) prefix.Store {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.OwnerSharesKeyPrefix))
return prefix.NewStore(store, vaultId.ToStateKeyPrefix())
}

// MintShares mints shares of a vault for `owner` based on `quantumsToDeposit` by:
// 1. Increasing total shares of the vault.
// 2. Increasing owner shares of the vault for given `owner`.
Expand Down Expand Up @@ -127,7 +175,37 @@ func (k Keeper) MintShares(
return err
}

// TODO (TRA-170): Increase owner shares.
// Increase owner shares in the vault.
ownerShares, exists := k.GetOwnerShares(ctx, vaultId, owner)
if !exists {
// Set owner shares to be sharesToMint.
err := k.SetOwnerShares(
ctx,
vaultId,
owner,
types.BigRatToNumShares(sharesToMint),
)
if err != nil {
return err
}
} else {
// Increase existing owner shares by sharesToMint.
existingOwnerShares, err := ownerShares.ToBigRat()
if err != nil {
return err
}
err = k.SetOwnerShares(
ctx,
vaultId,
owner,
types.BigRatToNumShares(
existingOwnerShares.Add(existingOwnerShares, sharesToMint),
),
)
if err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit 083762c

Please sign in to comment.