trade.gno
3.65 Kb · 100 lines
1package lastcall
2
3// Spot trading against the desk. The desk quotes the oracle price and takes a
4// fee on each side; it is the counterparty to every trade. Buys pull tUSD from
5// the user and mint sStock; sells burn sStock and pay tUSD from the desk
6// reserve. Fully collateralized in tUSD held by the desk.
7
8const (
9 feeBps = 30 // 0.30% desk fee each side
10 bpsDenom = 10000 //
11 microUSD = 1000000 // 1 tUSD = 1_000000 micro; 1 share = 1_000000 units
12 minTradeUnits = 1000 // 0.001 share minimum, blocks dust
13 maxTradeUnits = 10_000_000000 // 10,000 shares max, keeps price*amount in int64
14)
15
16// notionalMicroUSD returns price * stockAmount / 1e6 — the micro-USD value of
17// stockAmount units (6 dp) of a stock quoted at `price` micro-USD per share.
18func notionalMicroUSD(price, stockAmount int64) int64 {
19 return mulDiv(price, stockAmount, microUSD)
20}
21
22func assertTradeSize(stockAmount int64) {
23 if stockAmount < minTradeUnits {
24 panic("desk: amount below minimum (0.001 share)")
25 }
26 if stockAmount > maxTradeUnits {
27 panic("desk: amount above maximum (10,000 shares)")
28 }
29}
30
31// Buy mints stockAmount units (6 dp) of sTicker to the caller and charges tUSD
32// at the desk price plus fee. Caller must already hold enough tUSD.
33func Buy(cur realm, ticker string, stockAmount int64) {
34 assertTradeSize(stockAmount)
35 s := mustStock(ticker)
36 price := freshPrice(ticker)
37 notional := notionalMicroUSD(price, stockAmount)
38 cost := mulDiv(notional, bpsDenom+feeBps, bpsDenom)
39 if cost <= 0 {
40 panic("desk: trade too small")
41 }
42 buyer := cur.Previous().Address()
43 if err := usdLedger.Transfer(buyer, deskAddr, cost); err != nil {
44 panic(err.Error())
45 }
46 if err := s.ledger.Mint(buyer, stockAmount); err != nil {
47 panic(err.Error())
48 }
49 addCost(buyer, ticker, notional) // cost basis for the UI's avg entry
50 accrueFee(cost - notional) // desk fee -> liquidity pool yield
51}
52
53// Sell burns stockAmount units of sTicker from the caller and pays tUSD at the
54// desk price minus fee, from the desk reserve.
55func Sell(cur realm, ticker string, stockAmount int64) {
56 assertTradeSize(stockAmount)
57 s := mustStock(ticker)
58 price := freshPrice(ticker)
59 notional := notionalMicroUSD(price, stockAmount)
60 proceeds := mulDiv(notional, bpsDenom-feeBps, bpsDenom)
61 seller := cur.Previous().Address()
62 heldBefore := s.token.BalanceOf(seller)
63 if err := s.ledger.Burn(seller, stockAmount); err != nil {
64 panic(err.Error())
65 }
66 if err := usdLedger.Transfer(deskAddr, seller, proceeds); err != nil {
67 panic(err.Error())
68 }
69 reduceCost(seller, ticker, stockAmount, heldBefore) // keep avg entry across sells
70 accrueFee(notional - proceeds) // desk fee -> liquidity pool yield
71}
72
73// QuoteBuy / QuoteSell are read-only helpers for the UI: the tUSD cost/proceeds
74// of trading stockAmount units at the current price.
75func QuoteBuy(ticker string, stockAmount int64) int64 {
76 assertTradeSize(stockAmount)
77 price, _ := GetPrice(ticker)
78 return mulDiv(notionalMicroUSD(price, stockAmount), bpsDenom+feeBps, bpsDenom)
79}
80
81func QuoteSell(ticker string, stockAmount int64) int64 {
82 assertTradeSize(stockAmount)
83 price, _ := GetPrice(ticker)
84 return mulDiv(notionalMicroUSD(price, stockAmount), bpsDenom-feeBps, bpsDenom)
85}
86
87// SeedUSD mints tUSD into the desk reserve so it can pay out on sells and
88// in-the-money perps. Admin only — the desk's bootstrap liquidity.
89func SeedUSD(cur realm, amount int64) {
90 assertAdmin(cur)
91 if amount <= 0 {
92 panic("desk: amount must be positive")
93 }
94 if err := usdLedger.Mint(deskAddr, amount); err != nil {
95 panic(err.Error())
96 }
97}
98
99// DeskReserveUSD is the desk's own tUSD balance.
100func DeskReserveUSD() int64 { return usd.BalanceOf(deskAddr) }