tellers_test.gno

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