package lastcall // Spot trading against the desk. The desk quotes the oracle price and takes a // fee on each side; it is the counterparty to every trade. Buys pull tUSD from // the user and mint sStock; sells burn sStock and pay tUSD from the desk // reserve. Fully collateralized in tUSD held by the desk. const ( feeBps = 30 // 0.30% desk fee each side bpsDenom = 10000 // microUSD = 1000000 // 1 tUSD = 1_000000 micro; 1 share = 1_000000 units minTradeUnits = 1000 // 0.001 share minimum, blocks dust maxTradeUnits = 10_000_000000 // 10,000 shares max, keeps price*amount in int64 ) // notionalMicroUSD returns price * stockAmount / 1e6 — the micro-USD value of // stockAmount units (6 dp) of a stock quoted at `price` micro-USD per share. func notionalMicroUSD(price, stockAmount int64) int64 { return mulDiv(price, stockAmount, microUSD) } func assertTradeSize(stockAmount int64) { if stockAmount < minTradeUnits { panic("desk: amount below minimum (0.001 share)") } if stockAmount > maxTradeUnits { panic("desk: amount above maximum (10,000 shares)") } } // Buy mints stockAmount units (6 dp) of sTicker to the caller and charges tUSD // at the desk price plus fee. Caller must already hold enough tUSD. func Buy(cur realm, ticker string, stockAmount int64) { assertTradeSize(stockAmount) s := mustStock(ticker) price := freshPrice(ticker) notional := notionalMicroUSD(price, stockAmount) cost := mulDiv(notional, bpsDenom+feeBps, bpsDenom) if cost <= 0 { panic("desk: trade too small") } buyer := cur.Previous().Address() if err := usdLedger.Transfer(buyer, deskAddr, cost); err != nil { panic(err.Error()) } if err := s.ledger.Mint(buyer, stockAmount); err != nil { panic(err.Error()) } addCost(buyer, ticker, notional) // cost basis for the UI's avg entry accrueFee(cost - notional) // desk fee -> liquidity pool yield } // Sell burns stockAmount units of sTicker from the caller and pays tUSD at the // desk price minus fee, from the desk reserve. func Sell(cur realm, ticker string, stockAmount int64) { assertTradeSize(stockAmount) s := mustStock(ticker) price := freshPrice(ticker) notional := notionalMicroUSD(price, stockAmount) proceeds := mulDiv(notional, bpsDenom-feeBps, bpsDenom) seller := cur.Previous().Address() heldBefore := s.token.BalanceOf(seller) if err := s.ledger.Burn(seller, stockAmount); err != nil { panic(err.Error()) } if err := usdLedger.Transfer(deskAddr, seller, proceeds); err != nil { panic(err.Error()) } reduceCost(seller, ticker, stockAmount, heldBefore) // keep avg entry across sells accrueFee(notional - proceeds) // desk fee -> liquidity pool yield } // QuoteBuy / QuoteSell are read-only helpers for the UI: the tUSD cost/proceeds // of trading stockAmount units at the current price. func QuoteBuy(ticker string, stockAmount int64) int64 { assertTradeSize(stockAmount) price, _ := GetPrice(ticker) return mulDiv(notionalMicroUSD(price, stockAmount), bpsDenom+feeBps, bpsDenom) } func QuoteSell(ticker string, stockAmount int64) int64 { assertTradeSize(stockAmount) price, _ := GetPrice(ticker) return mulDiv(notionalMicroUSD(price, stockAmount), bpsDenom-feeBps, bpsDenom) } // SeedUSD mints tUSD into the desk reserve so it can pay out on sells and // in-the-money perps. Admin only — the desk's bootstrap liquidity. func SeedUSD(cur realm, amount int64) { assertAdmin(cur) if amount <= 0 { panic("desk: amount must be positive") } if err := usdLedger.Mint(deskAddr, amount); err != nil { panic(err.Error()) } } // DeskReserveUSD is the desk's own tUSD balance. func DeskReserveUSD() int64 { return usd.BalanceOf(deskAddr) }