tellers_test.gno

4.05 Kb ยท 129 lines
  1package grc20
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/demo/testutils"
  7	"gno.land/p/demo/uassert"
  8	"gno.land/p/demo/ufmt"
  9	"gno.land/p/demo/urequire"
 10)
 11
 12func TestCallerTellerImpl(t *testing.T) {
 13	tok, _ := NewToken("Dummy", "DUMMY", 4)
 14	teller := tok.CallerTeller()
 15	urequire.False(t, tok == nil)
 16	var _ Teller = teller
 17}
 18
 19func TestTeller(t *testing.T) {
 20	var (
 21		alice = testutils.TestAddress("alice")
 22		bob   = testutils.TestAddress("bob")
 23		carl  = testutils.TestAddress("carl")
 24	)
 25
 26	token, ledger := NewToken("Dummy", "DUMMY", 6)
 27
 28	checkBalances := func(aliceEB, bobEB, carlEB uint64) {
 29		t.Helper()
 30		exp := ufmt.Sprintf("alice=%d bob=%d carl=%d", aliceEB, bobEB, carlEB)
 31		aliceGB := token.BalanceOf(alice)
 32		bobGB := token.BalanceOf(bob)
 33		carlGB := token.BalanceOf(carl)
 34		got := ufmt.Sprintf("alice=%d bob=%d carl=%d", aliceGB, bobGB, carlGB)
 35		uassert.Equal(t, got, exp, "invalid balances")
 36	}
 37	checkAllowances := func(abEB, acEB, baEB, bcEB, caEB, cbEB uint64) {
 38		t.Helper()
 39		exp := ufmt.Sprintf("ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s", abEB, acEB, baEB, bcEB, caEB, cbEB)
 40		abGB := token.Allowance(alice, bob)
 41		acGB := token.Allowance(alice, carl)
 42		baGB := token.Allowance(bob, alice)
 43		bcGB := token.Allowance(bob, carl)
 44		caGB := token.Allowance(carl, alice)
 45		cbGB := token.Allowance(carl, bob)
 46		got := ufmt.Sprintf("ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s", abGB, acGB, baGB, bcGB, caGB, cbGB)
 47		uassert.Equal(t, got, exp, "invalid allowances")
 48	}
 49
 50	checkBalances(0, 0, 0)
 51	checkAllowances(0, 0, 0, 0, 0, 0)
 52
 53	urequire.NoError(t, ledger.Mint(alice, 1000))
 54	urequire.NoError(t, ledger.Mint(alice, 100))
 55	checkBalances(1100, 0, 0)
 56	checkAllowances(0, 0, 0, 0, 0, 0)
 57
 58	urequire.NoError(t, ledger.Approve(alice, bob, 99999999))
 59	checkBalances(1100, 0, 0)
 60	checkAllowances(99999999, 0, 0, 0, 0, 0)
 61
 62	urequire.NoError(t, ledger.Approve(alice, bob, 400))
 63	checkBalances(1100, 0, 0)
 64	checkAllowances(400, 0, 0, 0, 0, 0)
 65
 66	urequire.Error(t, ledger.TransferFrom(alice, bob, carl, 100000000))
 67	checkBalances(1100, 0, 0)
 68	checkAllowances(400, 0, 0, 0, 0, 0)
 69
 70	urequire.NoError(t, ledger.TransferFrom(alice, bob, carl, 100))
 71	checkBalances(1000, 0, 100)
 72	checkAllowances(300, 0, 0, 0, 0, 0)
 73
 74	urequire.Error(t, ledger.SpendAllowance(alice, bob, 2000000))
 75	checkBalances(1000, 0, 100)
 76	checkAllowances(300, 0, 0, 0, 0, 0)
 77
 78	urequire.NoError(t, ledger.SpendAllowance(alice, bob, 100))
 79	checkBalances(1000, 0, 100)
 80	checkAllowances(200, 0, 0, 0, 0, 0)
 81}
 82
 83func TestCallerTeller(t *testing.T) {
 84	alice := testutils.TestAddress("alice")
 85	bob := testutils.TestAddress("bob")
 86	carl := testutils.TestAddress("carl")
 87
 88	token, ledger := NewToken("Dummy", "DUMMY", 6)
 89	teller := token.CallerTeller()
 90
 91	checkBalances := func(aliceEB, bobEB, carlEB uint64) {
 92		t.Helper()
 93		exp := ufmt.Sprintf("alice=%d bob=%d carl=%d", aliceEB, bobEB, carlEB)
 94		aliceGB := token.BalanceOf(alice)
 95		bobGB := token.BalanceOf(bob)
 96		carlGB := token.BalanceOf(carl)
 97		got := ufmt.Sprintf("alice=%d bob=%d carl=%d", aliceGB, bobGB, carlGB)
 98		uassert.Equal(t, got, exp, "invalid balances")
 99	}
100	checkAllowances := func(abEB, acEB, baEB, bcEB, caEB, cbEB uint64) {
101		t.Helper()
102		exp := ufmt.Sprintf("ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s", abEB, acEB, baEB, bcEB, caEB, cbEB)
103		abGB := token.Allowance(alice, bob)
104		acGB := token.Allowance(alice, carl)
105		baGB := token.Allowance(bob, alice)
106		bcGB := token.Allowance(bob, carl)
107		caGB := token.Allowance(carl, alice)
108		cbGB := token.Allowance(carl, bob)
109		got := ufmt.Sprintf("ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s", abGB, acGB, baGB, bcGB, caGB, cbGB)
110		uassert.Equal(t, got, exp, "invalid allowances")
111	}
112
113	urequire.NoError(t, ledger.Mint(alice, 1000))
114	checkBalances(1000, 0, 0)
115	checkAllowances(0, 0, 0, 0, 0, 0)
116
117	testing.SetOriginCaller(alice)
118	urequire.NoError(t, teller.Approve(bob, 600))
119	checkBalances(1000, 0, 0)
120	checkAllowances(600, 0, 0, 0, 0, 0)
121
122	testing.SetOriginCaller(bob)
123	urequire.Error(t, teller.TransferFrom(alice, carl, 700))
124	checkBalances(1000, 0, 0)
125	checkAllowances(600, 0, 0, 0, 0, 0)
126	urequire.NoError(t, teller.TransferFrom(alice, carl, 400))
127	checkBalances(600, 0, 400)
128	checkAllowances(200, 0, 0, 0, 0, 0)
129}