package lastcall // Limit orders for perps. On an oracle-priced desk there is no order book, so a // limit is a queued instruction: lock the margin + open fee now, and open the perp // once the oracle price reaches the limit (long: at or below; short: at or above). // Execution is permissionless (TriggerLimit) — the price bot acts as the keeper, // and anyone can trigger a ready order. Orders only execute while the market is // open, so they naturally queue over a weekend and fire on reopen. import ( "time" "gno.land/p/nt/ufmt/v0" ) type limitOrder struct { id uint64 owner address ticker string isLong bool size int64 // stock units (6 dp) margin int64 // tUSD micro locked up-front fee int64 // prepaid open fee (at the limit price) limit int64 // trigger price, micro-USD per share placedAt int64 } var ( limits = map[uint64]*limitOrder{} nextLimitID uint64 ) // PlaceLimit locks `margin` + the open fee and queues an order to open a perp when // the oracle price reaches `limit`. Returns the order id. func PlaceLimit(cur realm, ticker string, isLong bool, size, margin, limit int64) uint64 { _ = mustStock(ticker) assertPerpSize(size) assertMargin(margin) if limit <= 0 || limit > maxPrice { panic("desk: limit price out of range") } notional := notionalMicroUSD(limit, size) if notional <= 0 || notional > perpMaxNotional { panic("desk: notional out of range") } if notional > margin*maxLevOf(ticker) { panic("desk: over this market's max leverage") } fee := mulDiv(notional, perpFeeBps, bpsDenom) owner := cur.Previous().Address() if err := usdLedger.Transfer(owner, deskAddr, margin+fee); err != nil { panic(err.Error()) } nextLimitID++ id := nextLimitID limits[id] = &limitOrder{ id: id, owner: owner, ticker: ticker, isLong: isLong, size: size, margin: margin, fee: fee, limit: limit, placedAt: time.Now().Unix(), } return id } // CancelLimit refunds a pending order's locked margin + fee to its owner. func CancelLimit(cur realm, id uint64) { o, ok := limits[id] if !ok { panic("desk: unknown limit order") } if o.owner != cur.Previous().Address() { panic("desk: not your order") } if err := usdLedger.Transfer(deskAddr, o.owner, o.margin+o.fee); err != nil { panic(err.Error()) } delete(limits, id) } // limitReady reports whether an order's trigger condition holds at `mark`: // a long fills at or below its limit, a short at or above. func limitReady(o *limitOrder, mark int64) bool { if o.isLong { return mark <= o.limit } return mark >= o.limit } // fillSize shrinks an order whose notional at the fill price would exceed the // leverage its margin allows, so a price gap past the limit still fills. func fillSize(o *limitOrder, mark int64) int64 { maxNotional := o.margin * maxLevOf(o.ticker) if maxNotional > perpMaxNotional { maxNotional = perpMaxNotional } if notionalMicroUSD(mark, o.size) <= maxNotional { return o.size } return mulDiv(maxNotional, microUSD, mark) } // TriggerLimit executes a ready order at the current oracle price, using the locked // margin + prepaid fee. Permissionless (keeper / UI). Requires the market open and // the trigger reached; panics otherwise so the keeper simply retries later. func TriggerLimit(cur realm, id uint64) uint64 { o, ok := limits[id] if !ok { panic("desk: unknown limit order") } if !marketOpen { panic("desk: market closed") } mark := freshPrice(o.ticker) if !limitReady(o, mark) { panic("desk: limit not reached") } pid := openPositionCore(o.owner, o.ticker, o.isLong, fillSize(o, mark), o.margin, mark, o.fee) delete(limits, id) return pid } // --- reads --- // LimitsOf returns an owner's pending orders, ';'-joined, fields comma-separated: // id,ticker,L|S,size,margin,limit,mark (size/money micro, 6 dp). func LimitsOf(owner address) string { out := "" for id := uint64(1); id <= nextLimitID; id++ { o, ok := limits[id] if !ok || o.owner != owner { continue } mark, _ := GetPrice(o.ticker) side := "S" if o.isLong { side = "L" } if out != "" { out += ";" } out += ufmt.Sprintf("%d,%s,%s,%d,%d,%d,%d", o.id, o.ticker, side, o.size, o.margin, o.limit, mark) } return out } // AllLimits lists every pending order for the keeper to scan, ';'-joined: // id,ticker,L|S,limit,mark. The keeper triggers the ones whose condition holds. func AllLimits() string { out := "" for id := uint64(1); id <= nextLimitID; id++ { o, ok := limits[id] if !ok { continue } mark, _ := GetPrice(o.ticker) side := "S" if o.isLong { side = "L" } if out != "" { out += ";" } out += ufmt.Sprintf("%d,%s,%s,%d,%d", o.id, o.ticker, side, o.limit, mark) } return out }