Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 committed Mar 28, 2024
1 parent b4ec84f commit 12e85cc
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 110 deletions.
2 changes: 1 addition & 1 deletion protocol/x/vault/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func BeginBlocker(
ctx sdk.Context,
keeper *keeper.Keeper,
) {
keeper.DecommissionVaults(ctx)
keeper.DecommissionNonPositiveEquityVaults(ctx)
}

func EndBlocker(
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/vault/keeper/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (k Keeper) GetVaultEquity(
}

// DecommissionVaults decommissions all vaults with positive shares and non-positive equity.
func (k Keeper) DecommissionVaults(
func (k Keeper) DecommissionNonPositiveEquityVaults(
ctx sdk.Context,
) {
// Iterate through all vaults.
Expand Down Expand Up @@ -64,7 +64,7 @@ func (k Keeper) DecommissionVaults(
}
}

// DecommissionVault decommissions a vault by deleting its tota shares and owner shares.
// DecommissionVault decommissions a vault by deleting its total shares and owner shares.
func (k Keeper) DecommissionVault(
ctx sdk.Context,
vaultId types.VaultId,
Expand Down
303 changes: 196 additions & 107 deletions protocol/x/vault/keeper/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,122 +14,211 @@ import (
"github.com/stretchr/testify/require"
)

func TestDecomissionVaults(t *testing.T) {
vault0 := constants.Vault_Clob_0
vault1 := constants.Vault_Clob_1
// Initialize vault 0 with positive equity.
tApp := testapp.NewTestAppBuilder(t).WithGenesisDocFn(func() (genesis types.GenesisDoc) {
genesis = testapp.DefaultGenesis()
testapp.UpdateGenesisDocWithAppStateForModule(
&genesis,
func(genesisState *satypes.GenesisState) {
genesisState.Subaccounts = []satypes.Subaccount{
{
Id: vault0.ToSubaccountId(),
AssetPositions: []*satypes.AssetPosition{
{
AssetId: assettypes.AssetUsdc.Id,
Quantums: dtypes.NewInt(1),
},
},
},
}
func TestDecommissionNonPositiveEquityVaults(t *testing.T) {
tests := map[string]struct {
/* --- Setup --- */
// Vault IDs.
vaultIds []vaulttypes.VaultId
// Total shares of above vaults.
totalShares []*big.Rat
// Equities of above vaults.
equities []*big.Int

/* --- Expectations --- */
// Whether the vaults are decommissioned.
decommissioned []bool
}{
"Decomission no vault": {
vaultIds: []vaulttypes.VaultId{
constants.Vault_Clob_0,
constants.Vault_Clob_1,
},
totalShares: []*big.Rat{
big.NewRat(7, 1),
big.NewRat(7, 1),
},
equities: []*big.Int{
big.NewInt(1),
big.NewInt(1),
},
decommissioned: []bool{
false,
false,
},
},
"Decomission one vault": {
vaultIds: []vaulttypes.VaultId{
constants.Vault_Clob_0,
constants.Vault_Clob_1,
},
totalShares: []*big.Rat{
big.NewRat(7, 1),
big.NewRat(7, 1),
},
equities: []*big.Int{
big.NewInt(1),
big.NewInt(0),
},
)
return genesis
}).Build()
ctx := tApp.InitChain()
k := tApp.App.VaultKeeper
decommissioned: []bool{
false,
true, // this vault should be decommissioned.
},
},
"Decomission two vaults": {
vaultIds: []vaulttypes.VaultId{
constants.Vault_Clob_0,
constants.Vault_Clob_1,
},
totalShares: []*big.Rat{
big.NewRat(7, 1),
big.NewRat(7, 1),
},
equities: []*big.Int{
big.NewInt(0),
big.NewInt(-1),
},
decommissioned: []bool{
true,
true,
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Initialize vaults with their equities.
tApp := testapp.NewTestAppBuilder(t).WithGenesisDocFn(func() (genesis types.GenesisDoc) {
genesis = testapp.DefaultGenesis()
testapp.UpdateGenesisDocWithAppStateForModule(
&genesis,
func(genesisState *satypes.GenesisState) {
subaccounts := []satypes.Subaccount{}
for i, vaultId := range tc.vaultIds {
if tc.equities[i].Sign() != 0 {
subaccounts = append(
subaccounts,
satypes.Subaccount{
Id: vaultId.ToSubaccountId(),
AssetPositions: []*satypes.AssetPosition{
{
AssetId: assettypes.AssetUsdc.Id,
Quantums: dtypes.NewIntFromBigInt(tc.equities[i]),
},
},
},
)
}
}
genesisState.Subaccounts = subaccounts
},
)
return genesis
}).Build()
ctx := tApp.InitChain()
k := tApp.App.VaultKeeper

// Set total shares and owner shares for both vaults.
shares := vaulttypes.BigRatToNumShares(
big.NewRat(7, 1),
)
err := k.SetTotalShares(
ctx,
vault0,
shares,
)
require.NoError(t, err)
err = k.SetOwnerShares(
ctx,
vault0,
constants.Alice_Num0.Owner,
shares,
)
require.NoError(t, err)
err = k.SetTotalShares(
ctx,
vault1,
shares,
)
require.NoError(t, err)
err = k.SetOwnerShares(
ctx,
vault1,
constants.Bob_Num0.Owner,
shares,
)
require.NoError(t, err)
// Set total shares and owner shares for all vaults.
testOwner := constants.Alice_Num0.Owner
for i, vaultId := range tc.vaultIds {
err := k.SetTotalShares(
ctx,
vaultId,
vaulttypes.BigRatToNumShares(tc.totalShares[i]),
)
require.NoError(t, err)
err = k.SetOwnerShares(
ctx,
vaultId,
testOwner,
vaulttypes.BigRatToNumShares(big.NewRat(7, 1)),
)
require.NoError(t, err)
}

// Decomission all vaults.
k.DecommissionVaults(ctx)
// Decomission all vaults.
k.DecommissionNonPositiveEquityVaults(ctx)

// Check that total shares and owner shares are deleted for decomissioned
// vaults and not deleted for non-decomissioned vaults.
for i, decomissioned := range tc.decommissioned {
_, exists := k.GetTotalShares(ctx, tc.vaultIds[i])
require.Equal(t, !decomissioned, exists)
_, exists = k.GetOwnerShares(ctx, tc.vaultIds[i], testOwner)
require.Equal(t, !decomissioned, exists)
}
})
}

// Check that total shares and owner shares are not deleted for vault 0.
got, exists := k.GetTotalShares(ctx, vault0)
require.Equal(t, true, exists)
require.Equal(t, shares, got)
got, exists = k.GetOwnerShares(ctx, vault0, constants.Alice_Num0.Owner)
require.Equal(t, true, exists)
require.Equal(t, shares, got)
// Check that total shares and owner shares are deleted for vault 1.
_, exists = k.GetTotalShares(ctx, vault1)
require.Equal(t, false, exists)
_, exists = k.GetOwnerShares(ctx, vault1, constants.Bob_Num0.Owner)
require.Equal(t, false, exists)
}

func TestDecomissionVault(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.VaultKeeper
tests := map[string]struct {
/* --- Setup --- */
// Vault ID.
vaultId vaulttypes.VaultId
// Whether total shares exists.
totalSharesExists bool
// Owners.
owners []string
}{
"Total shares doesn't exist, no owners": {
vaultId: constants.Vault_Clob_0,
},
"Total shares exists, no owners": {
vaultId: constants.Vault_Clob_0,
totalSharesExists: true,
},
"Total shares exists, one owner": {
vaultId: constants.Vault_Clob_1,
totalSharesExists: true,
owners: []string{constants.Alice_Num0.Owner},
},
"Total shares exists, two owners": {
vaultId: constants.Vault_Clob_1,
totalSharesExists: true,
owners: []string{
constants.Alice_Num0.Owner,
constants.Bob_Num0.Owner,
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.VaultKeeper

// Decomission a non-existent vault.
k.DecommissionVault(ctx, constants.Vault_Clob_0)
shares := vaulttypes.BigRatToNumShares(
big.NewRat(7, 1),
)

// Set total shares and owner shares for two owners of a vault.
shares := vaulttypes.BigRatToNumShares(
big.NewRat(7, 1),
)
err := k.SetTotalShares(
ctx,
constants.Vault_Clob_0,
shares,
)
require.NoError(t, err)
err = k.SetOwnerShares(
ctx,
constants.Vault_Clob_0,
constants.Alice_Num0.Owner,
shares,
)
require.NoError(t, err)
err = k.SetOwnerShares(
ctx,
constants.Vault_Clob_0,
constants.Bob_Num0.Owner,
shares,
)
require.NoError(t, err)
if tc.totalSharesExists {
err := k.SetTotalShares(
ctx,
tc.vaultId,
shares,
)
require.NoError(t, err)
}
for _, owner := range tc.owners {
err := k.SetOwnerShares(
ctx,
tc.vaultId,
owner,
shares,
)
require.NoError(t, err)
}

// Decomission above vault.
k.DecommissionVault(ctx, constants.Vault_Clob_0)
// Decomission vault.
k.DecommissionVault(ctx, tc.vaultId)

// Check that total shares and owner shares are deleted.
_, exists := k.GetTotalShares(ctx, constants.Vault_Clob_0)
require.Equal(t, false, exists)
_, exists = k.GetOwnerShares(ctx, constants.Vault_Clob_0, constants.Alice_Num0.Owner)
require.Equal(t, false, exists)
_, exists = k.GetOwnerShares(ctx, constants.Vault_Clob_0, constants.Bob_Num0.Owner)
require.Equal(t, false, exists)
// Check that total shares and owner shares are deleted.
_, exists := k.GetTotalShares(ctx, tc.vaultId)
require.Equal(t, false, exists)
for _, owner := range tc.owners {
_, exists = k.GetOwnerShares(ctx, tc.vaultId, owner)
require.Equal(t, false, exists)
}
})
}
}

0 comments on commit 12e85cc

Please sign in to comment.