Skip to content

Commit

Permalink
[TRA-509] skip vault order calculation if market price is 0 (backport #…
Browse files Browse the repository at this point in the history
…1952) (#1953)

Co-authored-by: Tian <[email protected]>
  • Loading branch information
mergify[bot] and tqin7 authored Jul 22, 2024
1 parent 8dca05e commit 67639e9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions protocol/x/vault/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ func (k Keeper) GetVaultClobOrders(
err,
fmt.Sprintf("VaultId: %v", vaultId),
)
} else if marketPrice.Price == 0 {
// Market price can be zero upon market initialization or due to invalid exchange config.
return orders, errorsmod.Wrap(
types.ErrZeroMarketPrice,
fmt.Sprintf("VaultId: %v", vaultId),
)
}

// Calculate leverage = open notional / equity.
Expand Down Expand Up @@ -215,6 +221,8 @@ func (k Keeper) GetVaultClobOrders(
orderSize.Quo(orderSize, lib.BigIntOneMillion())

// Round (towards-zero) order size to the nearest multiple of step size.
// Note: below division by StepBaseQuantums is safe as x/clob disallows
// a clob pair's StepBaseQuantums to be zero.
stepSize := lib.BigU(clobPair.StepBaseQuantums)
orderSize.Quo(orderSize, stepSize).Mul(orderSize, stepSize)

Expand Down Expand Up @@ -315,6 +323,8 @@ func (k Keeper) GetVaultClobOrders(
}

// Bound subticks between the minimum and maximum subticks.
// Note: below division by SubticksPerTick is safe as x/clob disallows
// a clob pair's SubticksPerTick to be zero.
subticksPerTick := lib.BigU(clobPair.SubticksPerTick)
subticks = lib.BigIntRoundToMultiple(
subticks,
Expand Down
15 changes: 15 additions & 0 deletions protocol/x/vault/keeper/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,21 @@ func TestGetVaultClobOrders(t *testing.T) {
perpetual: constants.BtcUsd_0DefaultFunding_10AtomicResolution,
expectedErr: vaulttypes.ErrNonPositiveEquity,
},
"Error - Market price is zero": {
vaultParams: vaulttypes.DefaultParams(),
vaultId: constants.Vault_Clob_0,
vaultAssetQuoteQuantums: big.NewInt(1_000_000_000), // 1,000 USDC
vaultInventoryBaseQuantums: big.NewInt(0),
clobPair: constants.ClobPair_Btc,
marketParam: constants.TestMarketParams[0],
marketPrice: pricestypes.MarketPrice{
Id: 0,
Exponent: -5,
Price: 0,
},
perpetual: constants.BtcUsd_0DefaultFunding_10AtomicResolution,
expectedErr: vaulttypes.ErrZeroMarketPrice,
},
}

for name, tc := range tests {
Expand Down
5 changes: 5 additions & 0 deletions protocol/x/vault/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@ var (
16,
"TotalShares does not match sum of OwnerShares",
)
ErrZeroMarketPrice = errorsmod.Register(
ModuleName,
17,
"MarketPrice is zero",
)
)

0 comments on commit 67639e9

Please sign in to comment.