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.

stocks.gno

1.26 Kb · 54 lines
 1package lastcall
 2
 3import "gno.land/p/nt/grc20/v0"
 4
 5// A stock is one synthetic-equity market: a GRC20 token (6 decimals) whose
 6// ledger the desk owns, so the desk can mint on Buy and burn on Sell.
 7type stock struct {
 8	token  *grc20.Token
 9	ledger *grc20.PrivateLedger
10}
11
12var (
13	stocks  = map[string]*stock{}
14	tickers = []string{} // insertion order, for Render
15)
16
17var defaultMarkets = []struct{ ticker, name string }{
18	{"NVDA", "Synthetic NVIDIA"},
19	{"AAPL", "Synthetic Apple"},
20	{"TSLA", "Synthetic Tesla"},
21	{"MSFT", "Synthetic Microsoft"},
22	{"AMZN", "Synthetic Amazon"},
23}
24
25func initStocks(cur realm) {
26	for _, m := range defaultMarkets {
27		listStock(cur, m.ticker, m.name)
28	}
29}
30
31func listStock(cur realm, ticker, name string) {
32	if _, ok := stocks[ticker]; ok {
33		panic("desk: market exists: " + ticker)
34	}
35	t, l := grc20.NewToken(name, "s"+ticker, 6, nextTokID.Next(), cur)
36	stocks[ticker] = &stock{token: t, ledger: l}
37	tickers = append(tickers, ticker)
38}
39
40func mustStock(ticker string) *stock {
41	s, ok := stocks[ticker]
42	if !ok {
43		panic("desk: unknown ticker " + ticker)
44	}
45	return s
46}
47
48func StockBalance(ticker string, owner address) int64 {
49	return mustStock(ticker).token.BalanceOf(owner)
50}
51
52func StockSupply(ticker string) int64 {
53	return mustStock(ticker).token.TotalSupply()
54}