Skip to content

Commit

Permalink
msrginMap fix (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
atvanguard authored Dec 31, 2023
1 parent 445022b commit 024fdf5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
18 changes: 10 additions & 8 deletions plugin/evm/orderbook/liquidations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ func TestGetLiquidableTraders(t *testing.T) {
t.Run("When no trader exist", func(t *testing.T) {
db := getDatabase()
hState := &hu.HubbleState{
Assets: assets,
OraclePrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(110))},
ActiveMarkets: []hu.Market{market},
Assets: assets,
OraclePrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(110))},
ActiveMarkets: []hu.Market{market},
MinAllowableMargin: db.configService.getMinAllowableMargin(),
}
liquidablePositions, _, _ := db.GetNaughtyTraders(hState)
assert.Equal(t, 0, len(liquidablePositions))
Expand All @@ -38,11 +39,12 @@ func TestGetLiquidableTraders(t *testing.T) {
},
}
hState := &hu.HubbleState{
Assets: assets,
OraclePrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(110))},
MidPrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(100))},
ActiveMarkets: []hu.Market{market},
MaintenanceMargin: db.configService.getMaintenanceMargin(),
Assets: assets,
OraclePrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(110))},
MidPrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(100))},
ActiveMarkets: []hu.Market{market},
MaintenanceMargin: db.configService.getMaintenanceMargin(),
MinAllowableMargin: db.configService.getMinAllowableMargin(),
}
liquidablePositions, _, _ := db.GetNaughtyTraders(hState)
assert.Equal(t, 0, len(liquidablePositions))
Expand Down
11 changes: 6 additions & 5 deletions plugin/evm/orderbook/memory_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ func (db *InMemoryDatabase) GetNaughtyTraders(hState *hu.HubbleState) ([]Liquida
ReservedMargin: new(big.Int).Set(trader.Margin.Reserved),
}
marginFraction := hu.GetMarginFraction(hState, userState)
marginMap[addr] = hu.GetAvailableMargin(hState, userState)
if marginFraction.Cmp(hState.MaintenanceMargin) == -1 {
log.Info("below maintenanceMargin", "trader", addr.String(), "marginFraction", prettifyScaledBigInt(marginFraction, 6))
if len(minSizes) == 0 {
Expand All @@ -961,10 +962,10 @@ func (db *InMemoryDatabase) GetNaughtyTraders(hState *hu.HubbleState) ([]Liquida
continue
}
// has orders that might be cancellable
availableMargin := hu.GetAvailableMargin(hState, userState)
availableMargin := new(big.Int).Set(marginMap[addr])
if availableMargin.Sign() == -1 {
foundCancellableOrders := false
foundCancellableOrders, marginMap[addr] = db.determineOrdersToCancel(addr, trader, availableMargin, hState.OraclePrices, ordersToCancel, hState.MinAllowableMargin)
foundCancellableOrders = db.determineOrdersToCancel(addr, trader, availableMargin, hState.OraclePrices, ordersToCancel, hState.MinAllowableMargin)
if foundCancellableOrders {
log.Info("negative available margin", "trader", addr.String(), "availableMargin", prettifyScaledBigInt(availableMargin, 6))
} else {
Expand All @@ -981,10 +982,10 @@ func (db *InMemoryDatabase) GetNaughtyTraders(hState *hu.HubbleState) ([]Liquida
}

// assumes db.mu.RLock has been held by the caller
func (db *InMemoryDatabase) determineOrdersToCancel(addr common.Address, trader *Trader, availableMargin *big.Int, oraclePrices map[Market]*big.Int, ordersToCancel map[common.Address][]Order, minAllowableMargin *big.Int) (bool, *big.Int) {
func (db *InMemoryDatabase) determineOrdersToCancel(addr common.Address, trader *Trader, availableMargin *big.Int, oraclePrices map[Market]*big.Int, ordersToCancel map[common.Address][]Order, minAllowableMargin *big.Int) bool {
traderOrders := db.getTraderOrders(addr, Limit)
if len(traderOrders) == 0 {
return false, availableMargin
return false
}

sort.Slice(traderOrders, func(i, j int) bool {
Expand Down Expand Up @@ -1012,7 +1013,7 @@ func (db *InMemoryDatabase) determineOrdersToCancel(addr common.Address, trader
break
}
}
return foundCancellableOrders, _availableMargin
return foundCancellableOrders
}

func (db *InMemoryDatabase) getTraderOrders(trader common.Address, orderType OrderType) []Order {
Expand Down

0 comments on commit 024fdf5

Please sign in to comment.