grc20factory_test.gno

2.13 Kb ยท 65 lines
 1package foo20
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/demo/testutils"
 7	"gno.land/p/demo/uassert"
 8	"gno.land/p/demo/ufmt"
 9)
10
11func TestReadOnlyPublicMethods(t *testing.T) {
12	admin := testutils.TestAddress("admin")
13	bob := testutils.TestAddress("bob")
14	carl := testutils.TestAddress("carl")
15
16	type test struct {
17		name    string
18		balance uint64
19		fn      func() uint64
20	}
21
22	checkBalances := func(step string, totSup, balAdm, balBob, allowAdmBob, balCarl uint64) {
23		tests := []test{
24			{"TotalSupply", totSup, func() uint64 { return TotalSupply("FOO") }},
25			{"BalanceOf(admin)", balAdm, func() uint64 { return BalanceOf("FOO", admin) }},
26			{"BalanceOf(bob)", balBob, func() uint64 { return BalanceOf("FOO", bob) }},
27			{"Allowance(admin, bob)", allowAdmBob, func() uint64 { return Allowance("FOO", admin, bob) }},
28			{"BalanceOf(carl)", balCarl, func() uint64 { return BalanceOf("FOO", carl) }},
29		}
30		for _, tc := range tests {
31			reason := ufmt.Sprintf("%s.%s - %s", step, tc.name, "balances do not match")
32			uassert.Equal(t, tc.balance, tc.fn(), reason)
33		}
34	}
35
36	// admin creates FOO and BAR.
37	testing.SetOriginCaller(admin)
38	NewWithAdmin("Foo", "FOO", 3, 1_111_111_000, 5_555, admin)
39	NewWithAdmin("Bar", "BAR", 3, 2_222_000, 6_666, admin)
40	checkBalances("step1", 1_111_111_000, 1_111_111_000, 0, 0, 0)
41
42	// admin mints to bob.
43	mustGetInstance("FOO").ledger.Mint(bob, 333_333_000)
44	checkBalances("step2", 1_444_444_000, 1_111_111_000, 333_333_000, 0, 0)
45
46	// carl uses the faucet.
47	testing.SetOriginCaller(carl)
48	Faucet("FOO")
49	checkBalances("step3", 1_444_449_555, 1_111_111_000, 333_333_000, 0, 5_555)
50
51	// admin gives to bob some allowance.
52	testing.SetOriginCaller(admin)
53	Approve("FOO", bob, 1_000_000)
54	checkBalances("step4", 1_444_449_555, 1_111_111_000, 333_333_000, 1_000_000, 5_555)
55
56	// bob uses a part of the allowance.
57	testing.SetOriginCaller(bob)
58	TransferFrom("FOO", admin, carl, 400_000)
59	checkBalances("step5", 1_444_449_555, 1_110_711_000, 333_333_000, 600_000, 405_555)
60
61	// bob uses a part of the allowance.
62	testing.SetOriginCaller(bob)
63	TransferFrom("FOO", admin, carl, 600_000)
64	checkBalances("step6", 1_444_449_555, 1_110_111_000, 333_333_000, 0, 1_005_555)
65}