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.

disperse_test.gno

2.51 Kb · 82 lines
 1package disperse
 2
 3import (
 4	"chain"
 5	"chain/banker"
 6	"strings"
 7	"testing"
 8
 9	"gno.land/p/nt/testutils/v0"
10	"gno.land/p/nym-smelt001/solrt"
11)
12
13func TestDisperseCSV(cur realm, t *testing.T) {
14	sender := testutils.TestAddress("sender_csv")
15	alice := testutils.TestAddress("alice_csv")
16	bob := testutils.TestAddress("bob_csv")
17
18	testing.SetRealm(testing.NewUserRealm(sender))
19	testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 2000)))
20	realmAddr := chain.PackageAddress("gno.land/r/nym-smelt001/disperse")
21	testing.IssueCoins(realmAddr, chain.NewCoins(chain.NewCoin("ugnot", 2000)))
22
23	DisperseCSV(cross(cur), string(alice)+", "+string(bob), 1000)
24
25	ro := banker.NewReadonlyBanker()
26	if got := ro.GetCoin(alice, "ugnot"); got != 1000 {
27		t.Fatalf("alice expected 1000 ugnot, got %d", got)
28	}
29	if got := ro.GetCoin(bob, "ugnot"); got != 1000 {
30		t.Fatalf("bob expected 1000 ugnot, got %d", got)
31	}
32
33	rendered := Render("")
34	if !strings.Contains(rendered, "Smelt BatchDisperse") || !strings.Contains(rendered, "DisperseCSV") {
35		t.Fatalf("unexpected Render output:\n%s", rendered)
36	}
37}
38
39func TestDisperseNativeU256(cur realm, t *testing.T) {
40	sender := testutils.TestAddress("sender_native")
41	alice := testutils.TestAddress("alice_native")
42	bob := testutils.TestAddress("bob_native")
43
44	testing.SetRealm(testing.NewUserRealm(sender))
45	testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 3000)))
46	realmAddr := chain.PackageAddress("gno.land/r/nym-smelt001/disperse")
47	testing.IssueCoins(realmAddr, chain.NewCoins(chain.NewCoin("ugnot", 3000)))
48
49	DisperseNative(
50		cross(cur),
51		[]address{alice, bob},
52		[]solrt.U256{solrt.U256FromU64(1200), solrt.U256FromU64(1800)},
53	)
54
55	ro := banker.NewReadonlyBanker()
56	if got := ro.GetCoin(alice, "ugnot"); got != 1200 {
57		t.Fatalf("alice expected 1200 ugnot, got %d", got)
58	}
59	if got := ro.GetCoin(bob, "ugnot"); got != 1800 {
60		t.Fatalf("bob expected 1800 ugnot, got %d", got)
61	}
62
63	batches, recipients, _ := Stats()
64	if batches == 0 || recipients < 2 {
65		t.Fatalf("expected updated stats, got batches=%d recipients=%d", batches, recipients)
66	}
67}
68
69func TestDisperseRejectsValueMismatch(cur realm, t *testing.T) {
70	sender := testutils.TestAddress("sender_bad")
71	alice := testutils.TestAddress("alice_bad")
72
73	testing.SetRealm(testing.NewUserRealm(sender))
74	testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 500)))
75
76	r := revive(func() {
77		DisperseNative(cross(cur), []address{alice}, []solrt.U256{solrt.U256FromU64(1000)})
78	})
79	if r == nil {
80		t.Fatalf("expected ValueMismatch panic when attached ugnot != sum")
81	}
82}