Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

render_example_test.gno

2.09 Kb · 70 lines
 1package amm
 2
 3import (
 4	"gno.land/p/nt/avl/v0"
 5	"gno.land/p/nt/grc20/v0"
 6	"gno.land/p/nt/testutils/v0"
 7)
 8
 9// A fixed LP token for the examples, so the rendered symbol does not depend on
10// how many pools the other tests happened to open first.
11var (
12	exLP    *grc20.Token
13	exLedge *grc20.PrivateLedger
14)
15
16func init(cur realm) {
17	exLP, exLedge = grc20.NewToken("AMM LP AAA/BBB", "LPDEMO", 6, 9999, cur)
18	exLedge.Mint(testutils.TestAddress("lp"), 1_000_000)
19}
20
21// seedExamplePool installs one deterministic pool so the examples below do not
22// depend on which tests ran before them. The numbers are the worked example
23// from the package docs: a 1,000,000 / 4,000,000 pool after a 100,000 swap.
24func seedExamplePool() {
25	pools = avl.Tree{}
26	pools.Set(poolID(keyAAA, keyBBB), &pool{
27		keyA: keyAAA, keyB: keyBBB,
28		tokA: tokAAA, tokB: tokBBB,
29		resA: 1_100_000, resB: 3_637_356,
30		lp: exLP, lpLedger: exLedge,
31		lpKey: "gno.land/r/moul/x/amm/v0.LPDEMO",
32	})
33}
34
35// ExampleRender pins the realm's home page.
36func ExampleRender() {
37	seedExamplePool()
38	print(Render(""))
39	// Output:
40	// # Minimal AMM
41	//
42	// Constant product (`x*y=k`) with a 30 bps fee kept by the pool. Every position is a transferable GRC20 LP token. Reserves are stored, never read from balances, so sending tokens here directly does nothing and they cannot be recovered.
43	//
44	// | pool | reserves | LP token | supply | holders |
45	// |---|---|---|---|---|
46	// | [AAA/BBB](/r/moul/x/amm/v0:gno.land/r/moul/x/amm/v0.AAA~gno.land/r/moul/x/amm/v0.BBB) | 1100000 AAA / 3637356 BBB | LPDEMO | 1000000 | 1 |
47}
48
49// ExampleRender_pool pins a single pool's detail page.
50func ExampleRender_pool() {
51	seedExamplePool()
52	print(Render(poolID(keyAAA, keyBBB)))
53	// Output:
54	// # AAA / BBB
55	//
56	// - **AAA** reserve: 1100000 (`gno.land/r/moul/x/amm/v0.AAA`)
57	// - **BBB** reserve: 3637356 (`gno.land/r/moul/x/amm/v0.BBB`)
58	// - **LP token**: `gno.land/r/moul/x/amm/v0.LPDEMO` (LPDEMO)
59	// - **LP shares**: 1000000 across 1 holder(s)
60}
61
62// ExampleRender_unknown pins the miss.
63func ExampleRender_unknown() {
64	seedExamplePool()
65	print(Render("nope"))
66	// Output:
67	// # 404
68	//
69	// No pool `nope`.
70}