risk.gno
4.53 Kb · 146 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 closedMaxLeverage = 2
16 skewImpactMaxBps = 100 // max +/-1.00% skew price impact on entry
17 skewScaleK = 500_000_000000 // $500,000: net skew at which impact reaches the max
18)
19
20var (
21 oiLong = map[string]int64{} // open long notional (micro-USD, at entry) per ticker
22 oiShort = map[string]int64{} // open short notional per ticker
23 oiCapOv = map[string]int64{} // per-ticker per-side cap override; 0 => default
24 maxLevOv = map[string]int64{} // per-ticker max leverage override; 0 => global maxLeverage
25)
26
27func oiCap(ticker string) int64 {
28 if c := oiCapOv[ticker]; c > 0 {
29 return c
30 }
31 return defaultOICap
32}
33
34// openLevOf is the leverage ceiling for a market while it trades: a per-asset
35// override if set, otherwise the global maxLeverage. Overrides may only lower it.
36func openLevOf(ticker string) int64 {
37 if l := maxLevOv[ticker]; l > 0 {
38 return l
39 }
40 return maxLeverage
41}
42
43// maxLevOf is the ceiling a new position gets right now. While the stock market
44// is closed the quote cannot move, but the next open can gap, so new exposure is
45// held to closedMaxLeverage until then.
46func maxLevOf(ticker string) int64 {
47 if l := openLevOf(ticker); marketOpen || l <= closedMaxLeverage {
48 return l
49 }
50 return closedMaxLeverage
51}
52
53func sideOI(ticker string, isLong bool) int64 {
54 if isLong {
55 return oiLong[ticker]
56 }
57 return oiShort[ticker]
58}
59
60func addOI(ticker string, isLong bool, notional int64) {
61 if isLong {
62 oiLong[ticker] += notional
63 } else {
64 oiShort[ticker] += notional
65 }
66}
67
68func subOI(ticker string, isLong bool, notional int64) {
69 if isLong {
70 oiLong[ticker] -= notional
71 if oiLong[ticker] < 0 {
72 oiLong[ticker] = 0
73 }
74 } else {
75 oiShort[ticker] -= notional
76 if oiShort[ticker] < 0 {
77 oiShort[ticker] = 0
78 }
79 }
80}
81
82// --- admin config ---
83
84// SetOICap sets the per-side open-interest cap for a market (micro-USD). 0 restores
85// the default.
86func SetOICap(cur realm, ticker string, perSide int64) {
87 assertAdmin(cur)
88 if perSide < 0 {
89 panic("desk: cap must be >= 0")
90 }
91 oiCapOv[ticker] = perSide
92}
93
94// SetMaxLev lowers (never raises) the max leverage for a market. 0 restores the
95// global default.
96func SetMaxLev(cur realm, ticker string, lev int64) {
97 assertAdmin(cur)
98 if lev < 0 || lev > maxLeverage {
99 panic("desk: leverage out of range")
100 }
101 maxLevOv[ticker] = lev
102}
103
104// --- reads ---
105
106// OIOf returns a market's open-interest state, comma-separated:
107// oiLong,oiShort,capPerSide,maxLev (notional micro-USD; maxLev a plain integer).
108func OIOf(ticker string) string {
109 return ufmt.Sprintf("%d,%d,%d,%d", oiLong[ticker], oiShort[ticker], oiCap(ticker), maxLevOf(ticker))
110}
111
112// --- skew-priced entry ---
113// A desk with no order book discourages one-sided books by pricing open interest:
114// a trade that deepens the long/short imbalance fills at a worse entry. Only the
115// entry price is adjusted; mark stays the oracle, so the impact is realized as the
116// position's starting PnL.
117
118// skewImpactBps is the entry price impact in bps for opening `notional` on
119// `isLong`: the opener pays when they deepen the skew and fills at the oracle
120// price when they balance it. A rebate would let an instant round trip profit.
121func skewImpactBps(ticker string, isLong bool, notional int64) int64 {
122 var before int64 // how much heavier the opener's own side already is (signed)
123 if isLong {
124 before = oiLong[ticker] - oiShort[ticker]
125 } else {
126 before = oiShort[ticker] - oiLong[ticker]
127 }
128 mid := before + notional/2 // average skew the trade pushes through, on its side
129 imp := skewImpactMaxBps * mid / skewScaleK
130 if imp > skewImpactMaxBps {
131 imp = skewImpactMaxBps
132 }
133 if imp < 0 {
134 imp = 0
135 }
136 return imp
137}
138
139// skewEntry applies a bps impact to the oracle price to get the fill/entry price:
140// a long fills higher and a short fills lower by `impBps` (worse when impBps > 0).
141func skewEntry(price, impBps int64, isLong bool) int64 {
142 if isLong {
143 return mulDiv(price, bpsDenom+impBps, bpsDenom)
144 }
145 return mulDiv(price, bpsDenom-impBps, bpsDenom)
146}