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.

desk.gno

3.91 Kb · 125 lines
  1// Package lastcall is a synthetic stocks desk on gno.land.
  2//
  3// It is self-contained: it runs its own price feed (off-chain updaters push
  4// quotes, trusted by caller address, see feed.gno), issues a test-USD
  5// collateral token and synthetic stock tokens, and lets users trade them spot
  6// against the desk. There is no external oracle and no external liquidity —
  7// the desk is always the counterparty at its own quoted price.
  8//
  9// All amounts are integers. USD values are micro-USD (6 decimals):
 10// $190.42 -> 190420000. Token amounts use 6 decimals too, so one whole share
 11// is 1_000000 units and lines up 1:1 with the price scale.
 12package lastcall
 13
 14import (
 15	"gno.land/p/nt/seqid/v0"
 16	"gno.land/p/nt/ufmt/v0"
 17)
 18
 19var (
 20	admin     address  // deployer; may rotate admin, manage updaters, seed the reserve
 21	deskAddr  address  // this realm's own address — the escrow / reserve account
 22	nextTokID seqid.ID // unique id source for every grc20 token this realm mints
 23)
 24
 25func init(cur realm) {
 26	admin = cur.Previous().Address()
 27	updaters = []address{admin}
 28	deskAddr = cur.Address()
 29	initUSD(cur)    // create the tUSD collateral token
 30	initStocks(cur) // create the default synthetic stock markets
 31}
 32
 33// --- admin ---
 34
 35func SetAdmin(cur realm, a address) {
 36	assertAdmin(cur)
 37	if !a.IsValid() {
 38		panic("desk: invalid admin address")
 39	}
 40	admin = a
 41}
 42
 43func Admin() address       { return admin }
 44func DeskAddress() address { return deskAddr }
 45
 46func assertAdmin(cur realm) {
 47	if cur.Previous().Address() != admin {
 48		panic("desk: admin only")
 49	}
 50}
 51
 52// --- render ---
 53
 54func Render(path string) string {
 55	out := "# LastCall\n\n"
 56	out += "Synthetic stock desk: spot, perps, limit orders and a fee pool. Test funds only.\n\n"
 57	out += "## Markets\n\n"
 58	out += "| Ticker | Price (USD) | Quote | Supply | Long OI | Short OI |\n|---|---|---|---|---|---|\n"
 59	for _, t := range tickers {
 60		ps, quote := "-", "none"
 61		if p, ok := prices[t]; ok {
 62			ps = usdFmt(p.Value)
 63			quote = "last call passed"
 64			if IsFresh(t) {
 65				quote = "live"
 66			}
 67		}
 68		out += ufmt.Sprintf("| s%s | %s | %s | %s | %s | %s |\n", t, ps, quote, sharesFmt(stocks[t].token.TotalSupply()), usdFmt(oiLong[t]), usdFmt(oiShort[t]))
 69	}
 70	out += "\n## Desk\n\n"
 71	out += ufmt.Sprintf("* tUSD total supply: %s\n", usdFmt(usd.TotalSupply()))
 72	out += ufmt.Sprintf("* desk tUSD reserve: %s\n", usdFmt(usd.BalanceOf(deskAddr)))
 73	out += ufmt.Sprintf("* locked behind positions and orders: %s\n", usdFmt(lockedUSD))
 74	out += ufmt.Sprintf("* fee pool: %s, cumulative fees %s\n", usdFmt(assetsNow()), usdFmt(poolFeesAll))
 75	out += ufmt.Sprintf("* open positions: %d, pending orders: %d\n", perps.Size(), orders.Size())
 76	out += ufmt.Sprintf("* a price older than %d seconds stops every trade that moves value\n", maxPriceAge)
 77	out += "* every trade fills on the first price observed after it was placed\n"
 78	if marketOpen {
 79		out += ufmt.Sprintf("* stock market open: new perps up to %dx\n", maxLeverage)
 80	} else {
 81		out += ufmt.Sprintf("* stock market closed: new perps up to %dx until the open, positions above it are cut to it\n", closedMaxLeverage)
 82	}
 83	out += "\n## Keys\n\n"
 84	out += ufmt.Sprintf("* admin: %s\n* desk reserve account: %s\n", admin.String(), deskAddr.String())
 85	out += ufmt.Sprintf("* price updaters, %d must agree: %s\n", quorum, Updaters())
 86	return out
 87}
 88
 89// usdFmt formats micro-USD as a dollar string with two decimals.
 90func usdFmt(micro int64) string {
 91	neg := micro < 0
 92	if neg {
 93		micro = -micro
 94	}
 95	d := micro / 1000000
 96	c := (micro % 1000000) / 10000
 97	cs := ufmt.Sprintf("%d", c)
 98	if len(cs) == 1 {
 99		cs = "0" + cs
100	}
101	s := ufmt.Sprintf("$%d.%s", d, cs)
102	if neg {
103		s = "-" + s
104	}
105	return s
106}
107
108// sharesFmt formats a 6-decimal token amount as a share count.
109func sharesFmt(units int64) string {
110	neg := units < 0
111	if neg {
112		units = -units
113	}
114	d := units / 1000000
115	f := units % 1000000
116	fs := ufmt.Sprintf("%d", f)
117	for len(fs) < 6 {
118		fs = "0" + fs
119	}
120	s := ufmt.Sprintf("%d.%s", d, fs)
121	if neg {
122		s = "-" + s
123	}
124	return s
125}