Skip to content

Commit

Permalink
review/safer code
Browse files Browse the repository at this point in the history
  • Loading branch information
atvanguard committed Sep 30, 2023
1 parent b226baf commit 19117d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions plugin/evm/orderbook/hubbleutils/margin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
22 changes: 17 additions & 5 deletions plugin/evm/orderbook/liquidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)]
}
Expand All @@ -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)
}
}
Expand All @@ -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{
Expand All @@ -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
}

0 comments on commit 19117d0

Please sign in to comment.