package lastcall // Last call before the close. A stock can gap between the close and the next // open while the desk price stands still, and with isolated margin a losing // position stops at its margin while the winning one is paid in full. An equal // long and short at 10x would then earn from any gap beyond 10% at the desk's // expense. So while the market is closed no position may stay above // closedMaxLeverage: anyone (in practice the keeper) may cut one down to it with // Delever. The cut part is settled like a close, and what it returns stays in // the position as margin. AddMargin lets an owner keep the full size instead. import ( "time" "gno.land/p/nt/ufmt/v0" ) // AddMargin adds `amount` tUSD micro from the caller to their own position, // lowering its leverage. It trades nothing, so it applies at once. func AddMargin(cur realm, id uint64, amount int64) { p := mustPerp(id) owner := cur.Previous().Address() if p.owner != owner { panic("desk: not your position") } if amount <= 0 || p.margin+amount > maxMargin { panic("desk: margin out of range") } pull(owner, amount) lock(amount) p.margin += amount emitPerp("MarginAdd", p, "usd", num(amount), "margin", num(p.margin)) } func overLeveraged(p *perp, mark int64) bool { return notionalMicroUSD(mark, p.size) > p.margin*closedMaxLeverage } // Delever cuts a position held above closedMaxLeverage while the market is // closed, at the desk price, to the size its margin carries at that leverage. // Permissionless. func Delever(cur realm, id uint64) { if marketOpen { panic("desk: the market is open") } p := mustPerp(id) mark := freshPrice(p.ticker) if !overLeveraged(p, mark) { panic("desk: within the closed-market leverage") } if payout, _, _, _ := sliceSettle(p, p.size, mark, time.Now().Unix()); payout > freeReserve()+p.margin { panic("desk: the reserve cannot pay this cut in full right now") } if cut := deleverSize(p, mark, time.Now().Unix()); cut < p.size { cutToMargin(p, cut, mark) if !overLeveraged(p, mark) { return } } settlePerp(p, mark, "delever") } func deleverSize(p *perp, mark, now int64) int64 { notional := notionalMicroUSD(mark, p.size) pnl := perpPnL(p, mark) fee := mulDiv(notional, perpFeeBps, bpsDenom) gain := pnl - fee - holdingFee(p, now) room := notional + closedMaxLeverage*gain if room <= 0 { return p.size } cut := mulDiv(notional-closedMaxLeverage*p.margin, p.size, room) + 1 if cut < minTradeUnits { cut = minTradeUnits } if cut > p.size { return p.size } return cut } func cutToMargin(p *perp, sz, mark int64) { payout, fee, hold, marginSlice := sliceSettle(p, sz, mark, time.Now().Unix()) unlock(marginSlice) if free := freeReserve(); payout > free { payout = free } lock(payout) accrueFee(collectedFee(p, sz, mark, marginSlice, fee+hold)) subOI(p.ticker, p.isLong, notionalMicroUSD(p.entry, sz)) emitPerpClose(p, "delever", sz, mark, payout) p.size -= sz p.margin = p.margin - marginSlice + payout } // Overleveraged lists, within one page of open positions, the ids Delever would // cut now, ';'-joined, for the keeper. Empty while the market is open. func Overleveraged(offset, count int) string { out := "" if marketOpen { return out } eachPage(perps, offset, count, func(item any) { p := item.(*perp) if !IsFresh(p.ticker) { return } if mark, _ := GetPrice(p.ticker); overLeveraged(p, mark) { out = joinRow(out, ufmt.Sprintf("%d", p.id)) } }) return out }