Skip to content Search Apps Documentation Source Content 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.

transfer_test.gno

3.45 Kb · 61 lines
 1package abt
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestTransferFailuresPreserveState(cur realm, t *testing.T) {
10	cases := []struct {
11		name     string
12		caller   address
13		call     func(_ int, rlm realm)
14		panicMsg string
15	}{
16		{"transfer address", abtTest.Owner, func(_ int, rlm realm) { Transfer(cross(rlm), address("bad"), 1) }, "abt transfer failed: invalid address"},
17		{"transfer negative", abtTest.Owner, func(_ int, rlm realm) { Transfer(cross(rlm), abtTest.Other, -1) }, "abt transfer failed: invalid amount"},
18		{"transfer balance", abtTest.Owner, func(_ int, rlm realm) { Transfer(cross(rlm), abtTest.Other, 11) }, "abt transfer failed: insufficient balance"},
19		{"transfer self", abtTest.Owner, func(_ int, rlm realm) { Transfer(cross(rlm), abtTest.Owner, 1) }, "abt transfer failed: cannot send transfer to self"},
20		{"approve address", abtTest.Owner, func(_ int, rlm realm) { Approve(cross(rlm), address(""), 1) }, "abt approval failed: invalid address"},
21		{"approve negative", abtTest.Owner, func(_ int, rlm realm) { Approve(cross(rlm), abtTest.Other, -1) }, "abt approval failed: invalid amount"},
22		{"transferFrom owner", abtTest.Other, func(_ int, rlm realm) { TransferFrom(cross(rlm), address(""), abtTest.Admin, 1) }, "abt transfer failed: invalid address"},
23		{"transferFrom recipient", abtTest.Other, func(_ int, rlm realm) { TransferFrom(cross(rlm), abtTest.Owner, address("bad"), 1) }, "abt transfer failed: invalid address"},
24		{"transferFrom negative", abtTest.Other, func(_ int, rlm realm) { TransferFrom(cross(rlm), abtTest.Owner, abtTest.Admin, -1) }, "abt transfer failed: invalid amount"},
25		{"transferFrom allowance", abtTest.Other, func(_ int, rlm realm) { TransferFrom(cross(rlm), abtTest.Owner, abtTest.Admin, 6) }, "abt transfer failed: insufficient allowance"},
26		{"transferFrom balance", abtTest.Other, func(_ int, rlm realm) { TransferFrom(cross(rlm), abtTest.Owner, abtTest.Admin, 11) }, "abt transfer failed: insufficient balance"},
27		{"transferFrom self", abtTest.Other, func(_ int, rlm realm) { TransferFrom(cross(rlm), abtTest.Owner, abtTest.Owner, 1) }, "abt transfer failed: cannot send transfer to self"},
28	}
29	for _, tc := range cases {
30		t.Run(tc.name, func(cur realm, t *testing.T) {
31			setupABT(cross(cur), t)
32			testing.SetRealm(testing.NewUserRealm(abtTest.Admin))
33			Mint(cross(cur), abtTest.Owner, 10)
34			SetFrozen(cross(cur), false)
35			testing.SetOriginCaller(abtTest.Owner)
36			testing.SetRealm(testing.NewUserRealm(abtTest.Owner))
37			Approve(cross(cur), abtTest.Other, 5)
38			testing.SetOriginCaller(tc.caller)
39			testing.SetRealm(testing.NewUserRealm(tc.caller))
40			uassert.AbortsWithMessage(t, cur, tc.panicMsg, func() { tc.call(0, cur) })
41			uassert.Equal(t, int64(10), TotalSupply())
42			uassert.Equal(t, int64(10), BalanceOf(abtTest.Owner))
43			uassert.Equal(t, int64(0), BalanceOf(abtTest.Other))
44			uassert.Equal(t, int64(0), BalanceOf(abtTest.Admin))
45			uassert.Equal(t, int64(5), Allowance(abtTest.Owner, abtTest.Other))
46		})
47	}
48}
49
50func TestZeroTransfers(cur realm, t *testing.T) {
51	setupABT(cross(cur), t)
52	testing.SetRealm(testing.NewUserRealm(abtTest.Admin))
53	SetFrozen(cross(cur), false)
54	testing.SetOriginCaller(abtTest.Owner)
55	testing.SetRealm(testing.NewUserRealm(abtTest.Owner))
56	Transfer(cross(cur), abtTest.Other, 0)
57	TransferFrom(cross(cur), abtTest.Other, abtTest.Admin, 0)
58	uassert.Equal(t, int64(0), TotalSupply())
59	uassert.Equal(t, int64(0), BalanceOf(abtTest.Other))
60	uassert.Equal(t, int64(0), Allowance(abtTest.Other, abtTest.Owner))
61}