1package faucet
2
3import (
4 "std"
5 "testing"
6
7 "gno.land/p/demo/testutils"
8 "gno.land/r/gnoland/faucet"
9)
10
11func TestPackage(t *testing.T) {
12 var (
13 adminaddr = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
14 faucetaddr = std.DerivePkgAddr("gno.land/r/gnoland/faucet")
15 controlleraddr1 = testutils.TestAddress("controller1")
16 controlleraddr2 = testutils.TestAddress("controller2")
17 controlleraddr3 = testutils.TestAddress("controller3")
18 controlleraddr4 = testutils.TestAddress("controller4")
19 controlleraddr5 = testutils.TestAddress("controller5")
20 controlleraddr6 = testutils.TestAddress("controller6")
21 controlleraddr7 = testutils.TestAddress("controller7")
22 controlleraddr8 = testutils.TestAddress("controller8")
23 controlleraddr9 = testutils.TestAddress("controller9")
24 controlleraddr10 = testutils.TestAddress("controller10")
25 controlleraddr11 = testutils.TestAddress("controller11")
26
27 test1addr = testutils.TestAddress("test1")
28 )
29 // deposit 1000gnot to faucet contract
30 std.TestIssueCoins(faucetaddr, std.Coins{{"ugnot", 1000000000}})
31 assertBalance(t, faucetaddr, 1200000000)
32
33 // by default, balance is empty, and as a user I cannot call Transfer, or Admin commands.
34
35 assertBalance(t, test1addr, 0)
36 std.TestSetOrigCaller(test1addr)
37 assertErr(t, faucet.Transfer(test1addr, 1000000))
38
39 assertErr(t, faucet.AdminAddController(controlleraddr1))
40 std.TestSetOrigCaller(controlleraddr1)
41 assertErr(t, faucet.Transfer(test1addr, 1000000))
42
43 // as an admin, add the controller to contract and deposit more 2000gnot to contract
44 std.TestSetOrigCaller(adminaddr)
45 assertNoErr(t, faucet.AdminAddController(controlleraddr1))
46 assertBalance(t, faucetaddr, 1200000000)
47
48 // now, send some tokens as controller.
49 std.TestSetOrigCaller(controlleraddr1)
50 assertNoErr(t, faucet.Transfer(test1addr, 1000000))
51 assertBalance(t, test1addr, 1000000)
52 assertNoErr(t, faucet.Transfer(test1addr, 1000000))
53 assertBalance(t, test1addr, 2000000)
54 assertBalance(t, faucetaddr, 1198000000)
55
56 // remove controller
57 // as an admin, remove controller
58 std.TestSetOrigCaller(adminaddr)
59 assertNoErr(t, faucet.AdminRemoveController(controlleraddr1))
60 std.TestSetOrigCaller(controlleraddr1)
61 assertErr(t, faucet.Transfer(test1addr, 1000000))
62
63 // duplicate controller
64 std.TestSetOrigCaller(adminaddr)
65 assertNoErr(t, faucet.AdminAddController(controlleraddr1))
66 assertErr(t, faucet.AdminAddController(controlleraddr1))
67 // add more than more than allowed controllers
68 assertNoErr(t, faucet.AdminAddController(controlleraddr2))
69 assertNoErr(t, faucet.AdminAddController(controlleraddr3))
70 assertNoErr(t, faucet.AdminAddController(controlleraddr4))
71 assertNoErr(t, faucet.AdminAddController(controlleraddr5))
72 assertNoErr(t, faucet.AdminAddController(controlleraddr6))
73 assertNoErr(t, faucet.AdminAddController(controlleraddr7))
74 assertNoErr(t, faucet.AdminAddController(controlleraddr8))
75 assertNoErr(t, faucet.AdminAddController(controlleraddr9))
76 assertNoErr(t, faucet.AdminAddController(controlleraddr10))
77 assertErr(t, faucet.AdminAddController(controlleraddr11))
78
79 // send more than per transfer limit
80 std.TestSetOrigCaller(adminaddr)
81 faucet.AdminSetTransferLimit(300000000)
82 std.TestSetOrigCaller(controlleraddr1)
83 assertErr(t, faucet.Transfer(test1addr, 301000000))
84
85 // block transefer from the address not on the controllers list.
86 std.TestSetOrigCaller(controlleraddr11)
87 assertErr(t, faucet.Transfer(test1addr, 1000000))
88}
89
90func assertErr(t *testing.T, err string) {
91 t.Helper()
92
93 if err == "" {
94 t.Logf("info: got err: %v", err)
95 t.Errorf("expected an error, got nil.")
96 }
97}
98
99func assertNoErr(t *testing.T, err string) {
100 t.Helper()
101 if err != "" {
102 t.Errorf("got err: %v.", err)
103 }
104}
105
106func assertBalance(t *testing.T, addr std.Address, expectedBal int64) {
107 t.Helper()
108 banker := std.GetBanker(std.BankerTypeReadonly)
109 coins := banker.GetCoins(addr)
110 got := coins.AmountOf("ugnot")
111
112 if expectedBal != got {
113 t.Errorf("invalid balance: expected %d, got %d.", expectedBal, got)
114 }
115}
faucet_test.gno
3.93 Kb ยท 115 lines