Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

trade.gno

2.11 Kb · 58 lines
 1package lastcall
 2
 3// Spot trading against the desk. The desk quotes its own price and takes a fee on
 4// each side; it is the counterparty to every trade. Buy and Sell are orders that
 5// fill on the next price (orders.gno): a buy mints sStock, a sell pays tUSD from
 6// the desk reserve, never from tUSD locked behind margin or orders.
 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// QuoteBuy / QuoteSell are read-only helpers for the UI: the tUSD cost/proceeds
32// of trading stockAmount units at the current price.
33func QuoteBuy(ticker string, stockAmount int64) int64 {
34	assertTradeSize(stockAmount)
35	price, _ := GetPrice(ticker)
36	return mulDiv(notionalMicroUSD(price, stockAmount), bpsDenom+feeBps, bpsDenom)
37}
38
39func QuoteSell(ticker string, stockAmount int64) int64 {
40	assertTradeSize(stockAmount)
41	price, _ := GetPrice(ticker)
42	return mulDiv(notionalMicroUSD(price, stockAmount), bpsDenom-feeBps, bpsDenom)
43}
44
45// SeedUSD mints tUSD into the desk reserve so it can pay out on sells and
46// in-the-money perps. Admin only — the desk's bootstrap liquidity.
47func SeedUSD(cur realm, amount int64) {
48	assertAdmin(cur)
49	if amount <= 0 {
50		panic("desk: amount must be positive")
51	}
52	if err := usdLedger.Mint(deskAddr, amount); err != nil {
53		panic(err.Error())
54	}
55}
56
57// DeskReserveUSD is the desk's own tUSD balance.
58func DeskReserveUSD() int64 { return usd.BalanceOf(deskAddr) }