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

Mid price #121

Merged
merged 7 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions plugin/evm/orderbook/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type IConfigService interface {
getMinSizeRequirement(market Market) *big.Int
GetActiveMarketsCount() int64
GetUnderlyingPrices() []*big.Int
GetMidPrices() []*big.Int
GetCollaterals() []hu.Collateral
GetLastPremiumFraction(market Market, trader *common.Address) *big.Int
GetCumulativePremiumFraction(market Market) *big.Int
Expand Down Expand Up @@ -84,6 +85,10 @@ func (cs *ConfigService) GetUnderlyingPrices() []*big.Int {
return bibliophile.GetUnderlyingPrices(cs.getStateAtCurrentBlock())
}

func (cs *ConfigService) GetMidPrices() []*big.Int {
return bibliophile.GetMidPrices(cs.getStateAtCurrentBlock())
}

func (cs *ConfigService) GetCollaterals() []hu.Collateral {
return bibliophile.GetCollaterals(cs.getStateAtCurrentBlock())
}
Expand Down
3 changes: 0 additions & 3 deletions plugin/evm/orderbook/hubbleutils/data_structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ type Market = int
type Position struct {
OpenNotional *big.Int `json:"open_notional"`
Size *big.Int `json:"size"`
// UnrealisedFunding *big.Int `json:"unrealised_funding"`
// LastPremiumFraction *big.Int `json:"last_premium_fraction"`
// LiquidationThreshold *big.Int `json:"liquidation_threshold"`
}

type Trader struct {
Expand Down
16 changes: 13 additions & 3 deletions plugin/evm/orderbook/hubbleutils/margin_math.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package hubbleutils

import (
"math"
"math/big"
)

type HubbleState struct {
Assets []Collateral
OraclePrices map[Market]*big.Int
LastPrices map[Market]*big.Int
MidPrices map[Market]*big.Int
ActiveMarkets []Market
MinAllowableMargin *big.Int
MaintenanceMargin *big.Int
}

type UserState struct {
Expand All @@ -29,6 +31,14 @@ func GetAvailableMargin_(notionalPosition, margin, reservedMargin, minAllowableM
return Sub(Sub(margin, utilisedMargin), reservedMargin)
}

func GetMarginFraction(hState *HubbleState, userState *UserState) *big.Int {
notionalPosition, margin := GetNotionalPositionAndMargin(hState, userState, Maintenance_Margin)
if notionalPosition.Sign() == 0 {
return big.NewInt(math.MaxInt64)
}
return Div(Mul1e6(margin), notionalPosition)
}

func GetNotionalPositionAndMargin(hState *HubbleState, userState *UserState, marginMode MarginMode) (*big.Int, *big.Int) {
margin := Sub(GetNormalizedMargin(hState.Assets, userState.Margins), userState.PendingFunding)
notionalPosition, unrealizedPnl := GetTotalNotionalPositionAndUnrealizedPnl(hState, userState, margin, marginMode)
Expand All @@ -53,7 +63,7 @@ func GetOptimalPnl(hState *HubbleState, position *Position, margin *big.Int, mar

// based on last price
notionalPosition, unrealizedPnl, lastPriceBasedMF := GetPositionMetadata(
atvanguard marked this conversation as resolved.
Show resolved Hide resolved
hState.LastPrices[market],
hState.MidPrices[market],
position.OpenNotional,
position.Size,
margin,
Expand Down Expand Up @@ -90,7 +100,7 @@ func GetPositionMetadata(price *big.Int, openNotional *big.Int, size *big.Int, m
}

func GetNotionalPosition(price *big.Int, size *big.Int) *big.Int {
return big.NewInt(0).Abs(Div1e18(Mul(size, price)))
return big.NewInt(0).Abs(Div1e18(Mul(price, size)))
atvanguard marked this conversation as resolved.
Show resolved Hide resolved
}

func GetNormalizedMargin(assets []Collateral, margins []*big.Int) *big.Int {
Expand Down
16 changes: 8 additions & 8 deletions plugin/evm/orderbook/liquidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ func (liq LiquidablePosition) GetUnfilledSize() *big.Int {
return big.NewInt(0).Sub(liq.Size, liq.FilledSize)
}

func calcMarginFraction(trader *Trader, pendingFunding *big.Int, assets []hu.Collateral, oraclePrices map[Market]*big.Int, lastPrices map[Market]*big.Int, markets []Market) *big.Int {
margin := new(big.Int).Sub(getNormalisedMargin(trader, assets), pendingFunding)
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(trader, margin, hu.Maintenance_Margin, oraclePrices, lastPrices, markets)
if notionalPosition.Sign() == 0 {
return big.NewInt(math.MaxInt64)
func calcMarginFraction(trader *Trader, hState *hu.HubbleState) *big.Int {
userState := &hu.UserState{
Positions: translatePositions(trader.Positions),
Margins: getMargins(trader, len(hState.Assets)),
PendingFunding: getTotalFunding(trader, hState.ActiveMarkets),
ReservedMargin: trader.Margin.Reserved,
atvanguard marked this conversation as resolved.
Show resolved Hide resolved
}
margin.Add(margin, unrealizePnL)
return new(big.Int).Div(hu.Mul1e6(margin), notionalPosition)
return hu.GetMarginFraction(hState, userState)
}

func sortLiquidableSliceByMarginFraction(positions []LiquidablePosition) []LiquidablePosition {
Expand Down Expand Up @@ -67,7 +67,7 @@ func getTotalNotionalPositionAndUnrealizedPnl(trader *Trader, margin *big.Int, m
return hu.GetTotalNotionalPositionAndUnrealizedPnl(
&hu.HubbleState{
OraclePrices: oraclePrices,
LastPrices: lastPrices,
MidPrices: lastPrices,
atvanguard marked this conversation as resolved.
Show resolved Hide resolved
ActiveMarkets: markets,
},
&hu.UserState{
Expand Down
105 changes: 72 additions & 33 deletions plugin/evm/orderbook/liquidations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ func TestGetLiquidableTraders(t *testing.T) {
assets := []hu.Collateral{{Price: big.NewInt(1e6), Weight: big.NewInt(1e6), Decimals: 6}}
t.Run("When no trader exist", func(t *testing.T) {
db := getDatabase()
oraclePrices := map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(110))}
liquidablePositions, _ := db.GetNaughtyTraders(oraclePrices, assets, []Market{market})
hState := &hu.HubbleState{
Assets: assets,
OraclePrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(110))},
ActiveMarkets: []hu.Market{market},
}
liquidablePositions, _ := db.GetNaughtyTraders(hState)
assert.Equal(t, 0, len(liquidablePositions))
})

Expand All @@ -33,9 +37,14 @@ func TestGetLiquidableTraders(t *testing.T) {
Positions: map[Market]*Position{},
},
}
db.LastPrice = map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(100))}
oraclePrices := map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(110))}
liquidablePositions, _ := db.GetNaughtyTraders(oraclePrices, assets, []Market{market})
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(),
}
liquidablePositions, _ := db.GetNaughtyTraders(hState)
assert.Equal(t, 0, len(liquidablePositions))
})

Expand All @@ -61,9 +70,16 @@ func TestGetLiquidableTraders(t *testing.T) {
db.TraderMap = map[common.Address]*Trader{
longTraderAddress: &longTrader,
}
db.LastPrice = map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(50))}
oraclePrices := map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(49))}

hState := &hu.HubbleState{
Assets: assets,
OraclePrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(49))},
MidPrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(50))},
ActiveMarkets: []hu.Market{market},
MinAllowableMargin: db.configService.getMinAllowableMargin(),
MaintenanceMargin: db.configService.getMaintenanceMargin(),
}
oraclePrices := hState.OraclePrices
// assertions begin
// for long trader
_trader := &longTrader
Expand All @@ -75,23 +91,25 @@ func TestGetLiquidableTraders(t *testing.T) {
// oracle price: notional = 49 * 10 = 490, pnl = 490-900 = -410, mf = (500-42-410)/490 = 0.097

// for hu.Min_Allowable_Margin we select the min of 2 hence orale_mf
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginLong, pendingFundingLong), hu.Min_Allowable_Margin, oraclePrices, db.GetLastPrices(), []Market{market})
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginLong, pendingFundingLong), hu.Min_Allowable_Margin, oraclePrices, hState.MidPrices, []Market{market})
assert.Equal(t, hu.Mul1e6(big.NewInt(490)), notionalPosition)
assert.Equal(t, hu.Mul1e6(big.NewInt(-410)), unrealizePnL)

availableMargin := getAvailableMargin(_trader, pendingFundingLong, assets, oraclePrices, db.GetLastPrices(), db.configService.getMinAllowableMargin(), []Market{market})
hState.MinAllowableMargin = db.configService.getMinAllowableMargin()
availableMargin := getAvailableMargin(_trader, hState)
// availableMargin = 500 - 42 (pendingFundingLong) - 410 (uPnL) - 490/5 = -50
assert.Equal(t, hu.Mul1e6(big.NewInt(-50)), availableMargin)

// for hu.Maintenance_Margin we select the max of 2 hence, last_mf
notionalPosition, unrealizePnL = getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginLong, pendingFundingLong), hu.Maintenance_Margin, oraclePrices, db.GetLastPrices(), []Market{market})
notionalPosition, unrealizePnL = getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginLong, pendingFundingLong), hu.Maintenance_Margin, oraclePrices, hState.MidPrices, []Market{market})
assert.Equal(t, hu.Mul1e6(big.NewInt(500)), notionalPosition)
assert.Equal(t, hu.Mul1e6(big.NewInt(-400)), unrealizePnL)

marginFraction := calcMarginFraction(_trader, pendingFundingLong, assets, oraclePrices, db.GetLastPrices(), []Market{market})
// marginFraction := calcMarginFraction(_trader, pendingFundingLong, assets, oraclePrices, hState.MidPrices, []Market{market})
marginFraction := calcMarginFraction(_trader, hState)
assert.Equal(t, new(big.Int).Div(hu.Mul1e6(new(big.Int).Add(new(big.Int).Sub(marginLong, pendingFundingLong), unrealizePnL)), notionalPosition), marginFraction)

liquidablePositions, _ := db.GetNaughtyTraders(oraclePrices, assets, []Market{market})
liquidablePositions, _ := db.GetNaughtyTraders(hState)
assert.Equal(t, 0, len(liquidablePositions))
})

Expand All @@ -110,8 +128,15 @@ func TestGetLiquidableTraders(t *testing.T) {
db.TraderMap = map[common.Address]*Trader{
longTraderAddress: &longTrader,
}
db.LastPrice = map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(49))}
oraclePrices := map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(50))}
hState := &hu.HubbleState{
Assets: assets,
OraclePrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(50))},
MidPrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(49))},
ActiveMarkets: []hu.Market{market},
MinAllowableMargin: db.configService.getMinAllowableMargin(),
MaintenanceMargin: db.configService.getMaintenanceMargin(),
}
oraclePrices := hState.OraclePrices

// assertions begin
// for long trader
Expand All @@ -124,23 +149,23 @@ func TestGetLiquidableTraders(t *testing.T) {
// oracle price: notional = 50 * 10 = 500, pnl = 500-900 = -400, mf = (500-42-400)/500 = 0.116

// for hu.Min_Allowable_Margin we select the min of 2 hence last_mf
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginLong, pendingFundingLong), hu.Min_Allowable_Margin, oraclePrices, db.GetLastPrices(), []Market{market})
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginLong, pendingFundingLong), hu.Min_Allowable_Margin, oraclePrices, hState.MidPrices, []Market{market})
assert.Equal(t, hu.Mul1e6(big.NewInt(490)), notionalPosition)
assert.Equal(t, hu.Mul1e6(big.NewInt(-410)), unrealizePnL)

availableMargin := getAvailableMargin(_trader, pendingFundingLong, assets, oraclePrices, db.GetLastPrices(), db.configService.getMinAllowableMargin(), []Market{market})
availableMargin := getAvailableMargin(_trader, hState)
// availableMargin = 500 - 42 (pendingFundingLong) - 410 (uPnL) - 490/5 = -50
assert.Equal(t, hu.Mul1e6(big.NewInt(-50)), availableMargin)

// for hu.Maintenance_Margin we select the max of 2 hence, oracle_mf
notionalPosition, unrealizePnL = getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginLong, pendingFundingLong), hu.Maintenance_Margin, oraclePrices, db.GetLastPrices(), []Market{market})
notionalPosition, unrealizePnL = getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginLong, pendingFundingLong), hu.Maintenance_Margin, oraclePrices, hState.MidPrices, []Market{market})
assert.Equal(t, hu.Mul1e6(big.NewInt(500)), notionalPosition)
assert.Equal(t, hu.Mul1e6(big.NewInt(-400)), unrealizePnL)

marginFraction := calcMarginFraction(_trader, pendingFundingLong, assets, oraclePrices, db.GetLastPrices(), []Market{market})
marginFraction := calcMarginFraction(_trader, hState)
assert.Equal(t, new(big.Int).Div(hu.Mul1e6(new(big.Int).Add(new(big.Int).Sub(marginLong, pendingFundingLong), unrealizePnL)), notionalPosition), marginFraction)

liquidablePositions, _ := db.GetNaughtyTraders(oraclePrices, assets, []Market{market})
liquidablePositions, _ := db.GetNaughtyTraders(hState)
assert.Equal(t, 0, len(liquidablePositions))
})
})
Expand All @@ -167,8 +192,15 @@ func TestGetLiquidableTraders(t *testing.T) {
db.TraderMap = map[common.Address]*Trader{
shortTraderAddress: &shortTrader,
}
db.LastPrice = map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(142))}
oraclePrices := map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(143))}
hState := &hu.HubbleState{
Assets: assets,
OraclePrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(143))},
MidPrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(142))},
ActiveMarkets: []hu.Market{market},
MinAllowableMargin: db.configService.getMinAllowableMargin(),
MaintenanceMargin: db.configService.getMaintenanceMargin(),
}
oraclePrices := hState.OraclePrices

// assertions begin
_trader := &shortTrader
Expand All @@ -180,23 +212,23 @@ func TestGetLiquidableTraders(t *testing.T) {
// oracle price based notional = 143 * 20 = 2860, pnl = 2100-2860 = -760, mf = (1000+37-760)/2860 = 0.096

// for hu.Min_Allowable_Margin we select the min of 2 hence, oracle_mf
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginShort, pendingFundingShort), hu.Min_Allowable_Margin, oraclePrices, db.GetLastPrices(), []Market{market})
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginShort, pendingFundingShort), hu.Min_Allowable_Margin, oraclePrices, hState.MidPrices, []Market{market})
assert.Equal(t, hu.Mul1e6(big.NewInt(2860)), notionalPosition)
assert.Equal(t, hu.Mul1e6(big.NewInt(-760)), unrealizePnL)

availableMargin := getAvailableMargin(_trader, pendingFundingShort, assets, oraclePrices, db.GetLastPrices(), db.configService.getMinAllowableMargin(), []Market{market})
availableMargin := getAvailableMargin(_trader, hState)
// availableMargin = 1000 + 37 (pendingFundingShort) -760 (uPnL) - 2860/5 = -295
assert.Equal(t, hu.Mul1e6(big.NewInt(-295)), availableMargin)

// for hu.Maintenance_Margin we select the max of 2 hence, last_mf
notionalPosition, unrealizePnL = getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginShort, pendingFundingShort), hu.Maintenance_Margin, oraclePrices, db.GetLastPrices(), []Market{market})
notionalPosition, unrealizePnL = getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginShort, pendingFundingShort), hu.Maintenance_Margin, oraclePrices, hState.MidPrices, []Market{market})
assert.Equal(t, hu.Mul1e6(big.NewInt(2840)), notionalPosition)
assert.Equal(t, hu.Mul1e6(big.NewInt(-740)), unrealizePnL)

marginFraction := calcMarginFraction(_trader, pendingFundingShort, assets, oraclePrices, db.GetLastPrices(), []Market{market})
marginFraction := calcMarginFraction(_trader, hState)
assert.Equal(t, new(big.Int).Div(hu.Mul1e6(new(big.Int).Add(new(big.Int).Sub(marginShort, pendingFundingShort), unrealizePnL)), notionalPosition), marginFraction)

liquidablePositions, _ := db.GetNaughtyTraders(oraclePrices, assets, []Market{market})
liquidablePositions, _ := db.GetNaughtyTraders(hState)
assert.Equal(t, 0, len(liquidablePositions))
})

Expand All @@ -215,8 +247,15 @@ func TestGetLiquidableTraders(t *testing.T) {
db.TraderMap = map[common.Address]*Trader{
shortTraderAddress: &shortTrader,
}
db.LastPrice = map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(143))}
oraclePrices := map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(142))}
hState := &hu.HubbleState{
Assets: assets,
OraclePrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(142))},
MidPrices: map[Market]*big.Int{market: hu.Mul1e6(big.NewInt(143))},
ActiveMarkets: []hu.Market{market},
MinAllowableMargin: db.configService.getMinAllowableMargin(),
MaintenanceMargin: db.configService.getMaintenanceMargin(),
}
oraclePrices := hState.OraclePrices

// assertions begin
_trader := &shortTrader
Expand All @@ -228,23 +267,23 @@ func TestGetLiquidableTraders(t *testing.T) {
// oracle price: notional = 142 * 20 = 2840, pnl = 2100-2840 = -740, mf = (1000+37-740)/2840 = 0.104

// for hu.Min_Allowable_Margin we select the min of 2 hence, last_mf
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginShort, pendingFundingShort), hu.Min_Allowable_Margin, oraclePrices, db.GetLastPrices(), []Market{market})
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginShort, pendingFundingShort), hu.Min_Allowable_Margin, oraclePrices, hState.MidPrices, []Market{market})
assert.Equal(t, hu.Mul1e6(big.NewInt(2860)), notionalPosition)
assert.Equal(t, hu.Mul1e6(big.NewInt(-760)), unrealizePnL)

availableMargin := getAvailableMargin(_trader, pendingFundingShort, assets, oraclePrices, db.GetLastPrices(), db.configService.getMinAllowableMargin(), []Market{market})
availableMargin := getAvailableMargin(_trader, hState)
// availableMargin = 1000 + 37 (pendingFundingShort) - 760 (uPnL) - 2860/5 = -295
assert.Equal(t, hu.Mul1e6(big.NewInt(-295)), availableMargin)

// for hu.Maintenance_Margin we select the max of 2 hence, oracle_mf
notionalPosition, unrealizePnL = getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginShort, pendingFundingShort), hu.Maintenance_Margin, oraclePrices, db.GetLastPrices(), []Market{market})
notionalPosition, unrealizePnL = getTotalNotionalPositionAndUnrealizedPnl(_trader, new(big.Int).Add(marginShort, pendingFundingShort), hu.Maintenance_Margin, oraclePrices, hState.MidPrices, []Market{market})
assert.Equal(t, hu.Mul1e6(big.NewInt(2840)), notionalPosition)
assert.Equal(t, hu.Mul1e6(big.NewInt(-740)), unrealizePnL)

marginFraction := calcMarginFraction(_trader, pendingFundingShort, assets, oraclePrices, db.GetLastPrices(), []Market{market})
marginFraction := calcMarginFraction(_trader, hState)
assert.Equal(t, new(big.Int).Div(hu.Mul1e6(new(big.Int).Add(new(big.Int).Sub(marginShort, pendingFundingShort), unrealizePnL)), notionalPosition), marginFraction)

liquidablePositions, _ := db.GetNaughtyTraders(oraclePrices, assets, []Market{market})
liquidablePositions, _ := db.GetNaughtyTraders(hState)
assert.Equal(t, 0, len(liquidablePositions))
})
})
Expand Down
Loading