package lastcall // Desk risk controls for perps. The desk/pool is the counterparty, so unbounded // one-sided open interest would put the liquidity pool at unbounded directional // risk. Two guards, both pool-protecting and both standard on pool-backed perp // desks (GMX v2, Ostium, gTrade): // - per-market open-interest caps, long and short tracked separately; // - per-asset max leverage (can be lowered below the global 10x per market). // All notional is micro-USD, measured at entry price. import "gno.land/p/nt/ufmt/v0" const ( defaultOICap = 250_000_000000 // $250,000 of open notional per side, per market closedMaxLeverage = 2 skewImpactMaxBps = 100 // max +/-1.00% skew price impact on entry skewScaleK = 500_000_000000 // $500,000: net skew at which impact reaches the max ) var ( oiLong = map[string]int64{} // open long notional (micro-USD, at entry) per ticker oiShort = map[string]int64{} // open short notional per ticker oiCapOv = map[string]int64{} // per-ticker per-side cap override; 0 => default maxLevOv = map[string]int64{} // per-ticker max leverage override; 0 => global maxLeverage ) func oiCap(ticker string) int64 { if c := oiCapOv[ticker]; c > 0 { return c } return defaultOICap } // openLevOf is the leverage ceiling for a market while it trades: a per-asset // override if set, otherwise the global maxLeverage. Overrides may only lower it. func openLevOf(ticker string) int64 { if l := maxLevOv[ticker]; l > 0 { return l } return maxLeverage } // maxLevOf is the ceiling a new position gets right now. While the stock market // is closed the quote cannot move, but the next open can gap, so new exposure is // held to closedMaxLeverage until then. func maxLevOf(ticker string) int64 { if l := openLevOf(ticker); marketOpen || l <= closedMaxLeverage { return l } return closedMaxLeverage } func sideOI(ticker string, isLong bool) int64 { if isLong { return oiLong[ticker] } return oiShort[ticker] } func addOI(ticker string, isLong bool, notional int64) { if isLong { oiLong[ticker] += notional } else { oiShort[ticker] += notional } } func subOI(ticker string, isLong bool, notional int64) { if isLong { oiLong[ticker] -= notional if oiLong[ticker] < 0 { oiLong[ticker] = 0 } } else { oiShort[ticker] -= notional if oiShort[ticker] < 0 { oiShort[ticker] = 0 } } } // --- admin config --- // SetOICap sets the per-side open-interest cap for a market (micro-USD). 0 restores // the default. func SetOICap(cur realm, ticker string, perSide int64) { assertAdmin(cur) if perSide < 0 { panic("desk: cap must be >= 0") } oiCapOv[ticker] = perSide } // SetMaxLev lowers (never raises) the max leverage for a market. 0 restores the // global default. func SetMaxLev(cur realm, ticker string, lev int64) { assertAdmin(cur) if lev < 0 || lev > maxLeverage { panic("desk: leverage out of range") } maxLevOv[ticker] = lev } // --- reads --- // OIOf returns a market's open-interest state, comma-separated: // oiLong,oiShort,capPerSide,maxLev (notional micro-USD; maxLev a plain integer). func OIOf(ticker string) string { return ufmt.Sprintf("%d,%d,%d,%d", oiLong[ticker], oiShort[ticker], oiCap(ticker), maxLevOf(ticker)) } // --- skew-priced entry --- // A desk with no order book discourages one-sided books by pricing open interest: // a trade that deepens the long/short imbalance fills at a worse entry. Only the // entry price is adjusted; mark stays the oracle, so the impact is realized as the // position's starting PnL. // skewImpactBps is the entry price impact in bps for opening `notional` on // `isLong`: the opener pays when they deepen the skew and fills at the oracle // price when they balance it. A rebate would let an instant round trip profit. func skewImpactBps(ticker string, isLong bool, notional int64) int64 { var before int64 // how much heavier the opener's own side already is (signed) if isLong { before = oiLong[ticker] - oiShort[ticker] } else { before = oiShort[ticker] - oiLong[ticker] } mid := before + notional/2 // average skew the trade pushes through, on its side imp := skewImpactMaxBps * mid / skewScaleK if imp > skewImpactMaxBps { imp = skewImpactMaxBps } if imp < 0 { imp = 0 } return imp } // skewEntry applies a bps impact to the oracle price to get the fill/entry price: // a long fills higher and a short fills lower by `impBps` (worse when impBps > 0). func skewEntry(price, impBps int64, isLong bool) int64 { if isLong { return mulDiv(price, bpsDenom+impBps, bpsDenom) } return mulDiv(price, bpsDenom-impBps, bpsDenom) }