token_test.gno
3.71 Kb ยท 122 lines
1package grc20
2
3import (
4 "std"
5 "testing"
6
7 "gno.land/p/demo/testutils"
8 "gno.land/p/demo/uassert"
9 "gno.land/p/demo/ufmt"
10 "gno.land/p/demo/urequire"
11)
12
13func TestTestImpl(t *testing.T) {
14 bank, _ := NewToken("Dummy", "DUMMY", 4)
15 urequire.False(t, bank == nil, "dummy should not be nil")
16}
17
18func TestToken(t *testing.T) {
19 var (
20 alice = testutils.TestAddress("alice")
21 bob = testutils.TestAddress("bob")
22 carl = testutils.TestAddress("carl")
23 )
24
25 bank, adm := NewToken("Dummy", "DUMMY", 6)
26
27 checkBalances := func(aliceEB, bobEB, carlEB uint64) {
28 t.Helper()
29 exp := ufmt.Sprintf("alice=%d bob=%d carl=%d", aliceEB, bobEB, carlEB)
30 aliceGB := bank.BalanceOf(alice)
31 bobGB := bank.BalanceOf(bob)
32 carlGB := bank.BalanceOf(carl)
33 got := ufmt.Sprintf("alice=%d bob=%d carl=%d", aliceGB, bobGB, carlGB)
34 uassert.Equal(t, got, exp, "invalid balances")
35 }
36 checkAllowances := func(abEB, acEB, baEB, bcEB, caEB, cbEB uint64) {
37 t.Helper()
38 exp := ufmt.Sprintf("ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s", abEB, acEB, baEB, bcEB, caEB, cbEB)
39 abGB := bank.Allowance(alice, bob)
40 acGB := bank.Allowance(alice, carl)
41 baGB := bank.Allowance(bob, alice)
42 bcGB := bank.Allowance(bob, carl)
43 caGB := bank.Allowance(carl, alice)
44 cbGB := bank.Allowance(carl, bob)
45 got := ufmt.Sprintf("ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s", abGB, acGB, baGB, bcGB, caGB, cbGB)
46 uassert.Equal(t, got, exp, "invalid allowances")
47 }
48
49 checkBalances(0, 0, 0)
50 checkAllowances(0, 0, 0, 0, 0, 0)
51
52 urequire.NoError(t, adm.Mint(alice, 1000))
53 urequire.NoError(t, adm.Mint(alice, 100))
54 checkBalances(1100, 0, 0)
55 checkAllowances(0, 0, 0, 0, 0, 0)
56
57 urequire.NoError(t, adm.Approve(alice, bob, 99999999))
58 checkBalances(1100, 0, 0)
59 checkAllowances(99999999, 0, 0, 0, 0, 0)
60
61 urequire.NoError(t, adm.Approve(alice, bob, 400))
62 checkBalances(1100, 0, 0)
63 checkAllowances(400, 0, 0, 0, 0, 0)
64
65 urequire.Error(t, adm.TransferFrom(alice, bob, carl, 100000000))
66 checkBalances(1100, 0, 0)
67 checkAllowances(400, 0, 0, 0, 0, 0)
68
69 urequire.NoError(t, adm.TransferFrom(alice, bob, carl, 100))
70 checkBalances(1000, 0, 100)
71 checkAllowances(300, 0, 0, 0, 0, 0)
72
73 urequire.Error(t, adm.SpendAllowance(alice, bob, 2000000))
74 checkBalances(1000, 0, 100)
75 checkAllowances(300, 0, 0, 0, 0, 0)
76
77 urequire.NoError(t, adm.SpendAllowance(alice, bob, 100))
78 checkBalances(1000, 0, 100)
79 checkAllowances(200, 0, 0, 0, 0, 0)
80}
81
82func TestOverflow(t *testing.T) {
83 alice := testutils.TestAddress("alice")
84 bob := testutils.TestAddress("bob")
85 tok, adm := NewToken("Dummy", "DUMMY", 6)
86
87 urequire.NoError(t, adm.Mint(alice, 2<<62))
88 urequire.Equal(t, tok.BalanceOf(alice), uint64(2<<62))
89 urequire.Error(t, adm.Mint(bob, 2<<62))
90}
91
92func TestTransferFromAtomicity(t *testing.T) {
93 var (
94 owner = testutils.TestAddress("owner")
95 spender = testutils.TestAddress("spender")
96 recipient = testutils.TestAddress("recipient")
97
98 invalidRecipient = std.Address("")
99 )
100
101 token, admin := NewToken("Test", "TEST", 6)
102
103 // owner has 100 tokens, spender has 50 allowance
104 initialBalance := uint64(100)
105 initialAllowance := uint64(50)
106
107 urequire.NoError(t, admin.Mint(owner, initialBalance))
108 urequire.NoError(t, admin.Approve(owner, spender, initialAllowance))
109
110 // transfer to an invalid address to force a transfer failure
111 transferAmount := uint64(30)
112 err := admin.TransferFrom(owner, spender, invalidRecipient, transferAmount)
113 uassert.Error(t, err, "transfer should fail due to invalid address")
114
115 ownerBalance := token.BalanceOf(owner)
116 uassert.Equal(t, ownerBalance, initialBalance, "owner balance should remain unchanged")
117
118 // check if allowance was incorrectly reduced
119 remainingAllowance := token.Allowance(owner, spender)
120 uassert.Equal(t, remainingAllowance, initialAllowance,
121 "allowance should not be reduced when transfer fails")
122}