Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude deactivated vaults from megavault equity #2308

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export enum VaultStatus {
/** VAULT_STATUS_UNSPECIFIED - Default value, invalid and unused. */
VAULT_STATUS_UNSPECIFIED = 0,

/** VAULT_STATUS_DEACTIVATED - Don’t place orders. Does not count toward global vault balances. */
/**
* VAULT_STATUS_DEACTIVATED - Don’t place orders. Does not count toward global vault balances.
* A vault can only be set to this status if its equity is non-positive.
*/
VAULT_STATUS_DEACTIVATED = 1,

/** VAULT_STATUS_STAND_BY - Don’t place orders. Does count towards global vault balances. */
Expand All @@ -74,7 +77,10 @@ export enum VaultStatusSDKType {
/** VAULT_STATUS_UNSPECIFIED - Default value, invalid and unused. */
VAULT_STATUS_UNSPECIFIED = 0,

/** VAULT_STATUS_DEACTIVATED - Don’t place orders. Does not count toward global vault balances. */
/**
* VAULT_STATUS_DEACTIVATED - Don’t place orders. Does not count toward global vault balances.
* A vault can only be set to this status if its equity is non-positive.
*/
VAULT_STATUS_DEACTIVATED = 1,

/** VAULT_STATUS_STAND_BY - Don’t place orders. Does count towards global vault balances. */
Expand Down
1 change: 1 addition & 0 deletions proto/dydxprotocol/vault/vault.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum VaultStatus {
VAULT_STATUS_UNSPECIFIED = 0;

// Don’t place orders. Does not count toward global vault balances.
// A vault can only be set to this status if its equity is non-positive.
VAULT_STATUS_DEACTIVATED = 1;

// Don’t place orders. Does count towards global vault balances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,34 @@ func TestMsgWithdrawFromMegavault(t *testing.T) {
expectedTotalShares: 500, // unchanged
expectedOwnerShares: 47, // unchanged
},
"Success: Withdraw some unlocked shares (8% of total), one deactivated sub-vault is excluded": {
mainVaultBalance: big.NewInt(1_234),
totalShares: 500,
owner: constants.DaveAccAddress.String(),
ownerTotalShares: 47,
ownerLockedShares: 7,
vaults: []VaultSetup{
{
id: constants.Vault_Clob0,
params: vaulttypes.VaultParams{
Status: vaulttypes.VaultStatus_VAULT_STATUS_DEACTIVATED,
},
assetQuoteQuantums: big.NewInt(-400),
positionBaseQuantums: big.NewInt(0),
clobPair: constants.ClobPair_Btc,
perpetual: constants.BtcUsd_20PercentInitial_10PercentMaintenance,
marketParam: constants.TestMarketParams[0],
marketPrice: constants.TestMarketPrices[0],
postWithdrawalEquity: big.NewInt(-400), // unchanged
},
},
sharesToWithdraw: 40,
minQuoteQuantums: 50,
deliverTxFails: false,
redeemedQuoteQuantums: 98, // 1234 * 0.08 = 98.72 ~= 98 (rounded down)
expectedTotalShares: 460, // 500 - 40
expectedOwnerShares: 7, // 47 - 40
},
"Success: Withdraw some unlocked shares (0.4444% of total), 888_888 quantums in main vault, " +
"one quoting sub-vault with negative equity": {
mainVaultBalance: big.NewInt(888_888),
Expand Down
6 changes: 5 additions & 1 deletion protocol/x/vault/keeper/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// GetMegavaultEquity returns the equity of the megavault (in quote quantums), which consists of
// - equity of the megavault main subaccount
// - equity of all vaults (if positive)
// - equity of all vaults (if not-deactivated and positive)
func (k Keeper) GetMegavaultEquity(ctx sdk.Context) (*big.Int, error) {
megavaultEquity, err := k.GetSubaccountEquity(ctx, types.MegavaultMainSubaccount)
if err != nil {
Expand All @@ -32,6 +32,10 @@ func (k Keeper) GetMegavaultEquity(ctx sdk.Context) (*big.Int, error) {
var vaultParams types.VaultParams
k.cdc.MustUnmarshal(vaultParamsIterator.Value(), &vaultParams)

if vaultParams.Status == types.VaultStatus_VAULT_STATUS_DEACTIVATED {
continue
}

vaultId, err := types.GetVaultIdFromStateKey(vaultParamsIterator.Key())
if err != nil {
log.ErrorLogWithError(ctx, "Failed to get vault ID from state key", err)
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/vault/keeper/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func TestGetMegavaultEquity(t *testing.T) {
big.NewInt(345),
big.NewInt(-5),
},
expectedMegavaultEquity: big.NewInt(1_345),
expectedMegavaultEquity: big.NewInt(1_345), // deactivated vault is not counted.
},
}
for name, tc := range tests {
Expand Down
9 changes: 7 additions & 2 deletions protocol/x/vault/keeper/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ func (k Keeper) WithdrawFromMegavault(
vaultParamsIterator := k.getVaultParamsIterator(ctx)
defer vaultParamsIterator.Close()
for ; vaultParamsIterator.Valid(); vaultParamsIterator.Next() {
var vaultParams types.VaultParams
k.cdc.MustUnmarshal(vaultParamsIterator.Value(), &vaultParams)
// Skip deactivated vaults.
if vaultParams.Status == types.VaultStatus_VAULT_STATUS_DEACTIVATED {
continue
}

vaultId, err := types.GetVaultIdFromStateKey(vaultParamsIterator.Key())
if err != nil {
log.ErrorLogWithError(
Expand All @@ -174,8 +181,6 @@ func (k Keeper) WithdrawFromMegavault(
)
continue
}
var vaultParams types.VaultParams
k.cdc.MustUnmarshal(vaultParamsIterator.Value(), &vaultParams)

_, perpetual, marketParam, marketPrice, err := k.GetVaultClobPerpAndMarket(ctx, *vaultId)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions protocol/x/vault/types/vault.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading