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.

events.gno

1.54 Kb · 45 lines
 1package lastcall
 2
 3// Every trade emits a chain event, so an indexer, gnoweb or an agent can rebuild
 4// an account's history without replaying the realm's state. Amounts are
 5// integers at 6 dp, like the rest of the desk.
 6
 7import (
 8	"chain"
 9	"strconv"
10)
11
12func num(v int64) string { return strconv.FormatInt(v, 10) }
13
14func ref(v uint64) string { return strconv.FormatUint(v, 10) }
15
16func sideOf(isLong bool) string {
17	if isLong {
18		return "L"
19	}
20	return "S"
21}
22
23func emitSpot(kind string, owner address, ticker string, size, price, usd int64) {
24	chain.Emit(kind, "owner", owner.String(), "ticker", ticker, "size", num(size), "price", num(price), "usd", num(usd))
25}
26
27func emitPerpOpen(p *perp) {
28	chain.Emit("PerpOpen", "owner", p.owner.String(), "id", ref(p.id), "ticker", p.ticker, "side", sideOf(p.isLong),
29		"size", num(p.size), "price", num(p.entry), "margin", num(p.margin))
30}
31
32func emitPerpClose(p *perp, kind string, size, mark, payout int64) {
33	chain.Emit("PerpClose", "owner", p.owner.String(), "id", ref(p.id), "ticker", p.ticker, "side", sideOf(p.isLong),
34		"size", num(size), "price", num(mark), "usd", num(payout), "kind", kind)
35}
36
37func emitLimit(kind string, o *limitOrder, extra ...string) {
38	attrs := []string{"owner", o.owner.String(), "id", ref(o.id), "ticker", o.ticker, "side", sideOf(o.isLong),
39		"size", num(o.size), "price", num(o.limit), "margin", num(o.margin)}
40	chain.Emit(kind, append(attrs, extra...)...)
41}
42
43func emitPool(kind string, owner address, usd, shares int64) {
44	chain.Emit(kind, "owner", owner.String(), "usd", num(usd), "shares", num(shares))
45}