Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

risk.gno

4.14 Kb · 135 lines
  1package lastcall
  2
  3// Desk risk controls for perps. The desk/pool is the counterparty, so unbounded
  4// one-sided open interest would put the liquidity pool at unbounded directional
  5// risk. Two guards, both pool-protecting and both standard on pool-backed perp
  6// desks (GMX v2, Ostium, gTrade):
  7//   - per-market open-interest caps, long and short tracked separately;
  8//   - per-asset max leverage (can be lowered below the global 10x per market).
  9// All notional is micro-USD, measured at entry price.
 10
 11import "gno.land/p/nt/ufmt/v0"
 12
 13const (
 14	defaultOICap     = 250_000_000000 // $250,000 of open notional per side, per market
 15	skewImpactMaxBps = 100            // max +/-1.00% skew price impact on entry
 16	skewScaleK       = 500_000_000000 // $500,000: net skew at which impact reaches the max
 17)
 18
 19var (
 20	oiLong   = map[string]int64{} // open long notional (micro-USD, at entry) per ticker
 21	oiShort  = map[string]int64{} // open short notional per ticker
 22	oiCapOv  = map[string]int64{} // per-ticker per-side cap override; 0 => default
 23	maxLevOv = map[string]int64{} // per-ticker max leverage override; 0 => global maxLeverage
 24)
 25
 26func oiCap(ticker string) int64 {
 27	if c := oiCapOv[ticker]; c > 0 {
 28		return c
 29	}
 30	return defaultOICap
 31}
 32
 33// maxLevOf is the leverage ceiling for a market: a per-asset override if set,
 34// otherwise the global maxLeverage. Overrides may only lower it.
 35func maxLevOf(ticker string) int64 {
 36	if l := maxLevOv[ticker]; l > 0 {
 37		return l
 38	}
 39	return maxLeverage
 40}
 41
 42func sideOI(ticker string, isLong bool) int64 {
 43	if isLong {
 44		return oiLong[ticker]
 45	}
 46	return oiShort[ticker]
 47}
 48
 49func addOI(ticker string, isLong bool, notional int64) {
 50	if isLong {
 51		oiLong[ticker] += notional
 52	} else {
 53		oiShort[ticker] += notional
 54	}
 55}
 56
 57func subOI(ticker string, isLong bool, notional int64) {
 58	if isLong {
 59		oiLong[ticker] -= notional
 60		if oiLong[ticker] < 0 {
 61			oiLong[ticker] = 0
 62		}
 63	} else {
 64		oiShort[ticker] -= notional
 65		if oiShort[ticker] < 0 {
 66			oiShort[ticker] = 0
 67		}
 68	}
 69}
 70
 71// --- admin config ---
 72
 73// SetOICap sets the per-side open-interest cap for a market (micro-USD). 0 restores
 74// the default.
 75func SetOICap(cur realm, ticker string, perSide int64) {
 76	assertAdmin(cur)
 77	if perSide < 0 {
 78		panic("desk: cap must be >= 0")
 79	}
 80	oiCapOv[ticker] = perSide
 81}
 82
 83// SetMaxLev lowers (never raises) the max leverage for a market. 0 restores the
 84// global default.
 85func SetMaxLev(cur realm, ticker string, lev int64) {
 86	assertAdmin(cur)
 87	if lev < 0 || lev > maxLeverage {
 88		panic("desk: leverage out of range")
 89	}
 90	maxLevOv[ticker] = lev
 91}
 92
 93// --- reads ---
 94
 95// OIOf returns a market's open-interest state, comma-separated:
 96// oiLong,oiShort,capPerSide,maxLev  (notional micro-USD; maxLev a plain integer).
 97func OIOf(ticker string) string {
 98	return ufmt.Sprintf("%d,%d,%d,%d", oiLong[ticker], oiShort[ticker], oiCap(ticker), maxLevOf(ticker))
 99}
100
101// --- skew-priced entry ---
102// A desk with no order book discourages one-sided books by pricing open interest:
103// a trade that deepens the long/short imbalance fills at a worse entry. Only the
104// entry price is adjusted; mark stays the oracle, so the impact is realized as the
105// position's starting PnL.
106
107// skewImpactBps is the entry price impact in bps for opening `notional` on
108// `isLong`: the opener pays when they deepen the skew and fills at the oracle
109// price when they balance it. A rebate would let an instant round trip profit.
110func skewImpactBps(ticker string, isLong bool, notional int64) int64 {
111	var before int64 // how much heavier the opener's own side already is (signed)
112	if isLong {
113		before = oiLong[ticker] - oiShort[ticker]
114	} else {
115		before = oiShort[ticker] - oiLong[ticker]
116	}
117	mid := before + notional/2 // average skew the trade pushes through, on its side
118	imp := skewImpactMaxBps * mid / skewScaleK
119	if imp > skewImpactMaxBps {
120		imp = skewImpactMaxBps
121	}
122	if imp < 0 {
123		imp = 0
124	}
125	return imp
126}
127
128// skewEntry applies a bps impact to the oracle price to get the fill/entry price:
129// a long fills higher and a short fills lower by `impBps` (worse when impBps > 0).
130func skewEntry(price, impBps int64, isLong bool) int64 {
131	if isLong {
132		return mulDiv(price, bpsDenom+impBps, bpsDenom)
133	}
134	return mulDiv(price, bpsDenom-impBps, bpsDenom)
135}