From 19117d0e03506cfda92ecc9089cba0b3805c9275 Mon Sep 17 00:00:00 2001 From: atvanguard <3612498+atvanguard@users.noreply.github.com> Date: Sat, 30 Sep 2023 07:53:59 +0100 Subject: [PATCH] review/safer code --- .../evm/orderbook/hubbleutils/margin_math.go | 6 ++--- plugin/evm/orderbook/liquidations.go | 22 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/plugin/evm/orderbook/hubbleutils/margin_math.go b/plugin/evm/orderbook/hubbleutils/margin_math.go index b7d346388a..7179eac899 100644 --- a/plugin/evm/orderbook/hubbleutils/margin_math.go +++ b/plugin/evm/orderbook/hubbleutils/margin_math.go @@ -62,7 +62,7 @@ func GetOptimalPnl(hState *HubbleState, position *Position, margin *big.Int, mar } // based on last price - notionalPosition, unrealizedPnl, lastPriceBasedMF := GetPositionMetadata( + notionalPosition, unrealizedPnl, midPriceBasedMF := GetPositionMetadata( hState.MidPrices[market], position.OpenNotional, position.Size, @@ -77,8 +77,8 @@ func GetOptimalPnl(hState *HubbleState, position *Position, margin *big.Int, mar margin, ) - if (marginMode == Maintenance_Margin && oracleBasedMF.Cmp(lastPriceBasedMF) == 1) || // for liquidations - (marginMode == Min_Allowable_Margin && oracleBasedMF.Cmp(lastPriceBasedMF) == -1) { // for increasing leverage + if (marginMode == Maintenance_Margin && oracleBasedMF.Cmp(midPriceBasedMF) == 1) || // for liquidations + (marginMode == Min_Allowable_Margin && oracleBasedMF.Cmp(midPriceBasedMF) == -1) { // for increasing leverage return oracleBasedNotional, oracleBasedUnrealizedPnl } return notionalPosition, unrealizedPnl diff --git a/plugin/evm/orderbook/liquidations.go b/plugin/evm/orderbook/liquidations.go index 548a705628..c4d3c2c480 100644 --- a/plugin/evm/orderbook/liquidations.go +++ b/plugin/evm/orderbook/liquidations.go @@ -19,7 +19,7 @@ type LiquidablePosition struct { } func (liq LiquidablePosition) GetUnfilledSize() *big.Int { - return big.NewInt(0).Sub(liq.Size, liq.FilledSize) + return new(big.Int).Sub(liq.Size, liq.FilledSize) } func calcMarginFraction(trader *Trader, hState *hu.HubbleState) *big.Int { @@ -45,6 +45,13 @@ func getNormalisedMargin(trader *Trader, assets []hu.Collateral) *big.Int { func getMargins(trader *Trader, numAssets int) []*big.Int { margin := make([]*big.Int, numAssets) + if trader.Margin.Deposited == nil { + return margin + } + numAssets_ := len(trader.Margin.Deposited) + if numAssets_ < numAssets { + numAssets = numAssets_ + } for i := 0; i < numAssets; i++ { margin[i] = trader.Margin.Deposited[Collateral(i)] } @@ -54,7 +61,7 @@ func getMargins(trader *Trader, numAssets int) []*big.Int { func getTotalFunding(trader *Trader, markets []Market) *big.Int { totalPendingFunding := big.NewInt(0) for _, market := range markets { - if trader.Positions[market] != nil { + if trader.Positions[market] != nil && trader.Positions[market].UnrealisedFunding != nil && trader.Positions[market].UnrealisedFunding.Sign() != 0 { totalPendingFunding.Add(totalPendingFunding, trader.Positions[market].UnrealisedFunding) } } @@ -63,11 +70,11 @@ func getTotalFunding(trader *Trader, markets []Market) *big.Int { type MarginMode = hu.MarginMode -func getTotalNotionalPositionAndUnrealizedPnl(trader *Trader, margin *big.Int, marginMode MarginMode, oraclePrices map[Market]*big.Int, lastPrices map[Market]*big.Int, markets []Market) (*big.Int, *big.Int) { +func getTotalNotionalPositionAndUnrealizedPnl(trader *Trader, margin *big.Int, marginMode MarginMode, oraclePrices map[Market]*big.Int, midPrices map[Market]*big.Int, markets []Market) (*big.Int, *big.Int) { return hu.GetTotalNotionalPositionAndUnrealizedPnl( &hu.HubbleState{ OraclePrices: oraclePrices, - MidPrices: lastPrices, + MidPrices: midPrices, ActiveMarkets: markets, }, &hu.UserState{ @@ -89,7 +96,12 @@ func prettifyScaledBigInt(number *big.Int, precision int8) string { func translatePositions(positions map[int]*Position) map[int]*hu.Position { huPositions := make(map[int]*hu.Position) for key, value := range positions { - huPositions[key] = &value.Position + if value != nil { + huPositions[key] = &hu.Position{ + Size: new(big.Int).Set(value.Size), + OpenNotional: new(big.Int).Set(value.OpenNotional), + } + } } return huPositions }