1+ // SPDX-License-Identifier: MIT
2+ pragma solidity ^ 0.8.0 ;
3+
4+ import "forge-std/Test.sol " ;
5+ import "../src/ShellToken.sol " ;
6+ import "openzeppelin-contracts/contracts/access/Ownable.sol " ;
7+
8+ contract ShellTokenTest is Test {
9+ ShellToken internal shellToken;
10+ address internal admin = makeAddr ("admin " );
11+ address internal owner = makeAddr ("owner " );
12+
13+ function setUp () public {
14+ vm.startPrank (owner);
15+ shellToken = new ShellToken ();
16+ shellToken.updateIsAdmin (admin, true );
17+ vm.stopPrank ();
18+ }
19+
20+ function test_updateIsAdmin () public {
21+ address tempAdmin = makeAddr ("tempAdmin " );
22+
23+ assertEq (shellToken.isAdmin (tempAdmin), false );
24+
25+ vm.prank (owner);
26+ shellToken.updateIsAdmin (tempAdmin, true );
27+
28+ assertEq (shellToken.isAdmin (tempAdmin), true );
29+
30+ vm.prank (owner);
31+ shellToken.updateIsAdmin (tempAdmin, false );
32+
33+ assertEq (shellToken.isAdmin (tempAdmin), false );
34+ }
35+
36+ function test_mintShells () public {
37+ address user = makeAddr ("user " );
38+
39+ vm.startPrank (admin);
40+ shellToken.mintShells (user, 100 );
41+ vm.stopPrank ();
42+
43+ assertEq (shellToken.balanceOf (user), 100 );
44+ }
45+
46+ function test_revert_mintShells_notAdmin () public {
47+ address user = makeAddr ("user " );
48+
49+ vm.startPrank (makeAddr ("notAdmin " ));
50+ vm.expectRevert ("Not an admin " );
51+ shellToken.mintShells (user, 100 );
52+ vm.stopPrank ();
53+
54+ assertEq (shellToken.balanceOf (user), 0 );
55+ }
56+
57+ function test_mintBatchShells () public {
58+ address user1 = makeAddr ("user1 " );
59+ address user2 = makeAddr ("user2 " );
60+
61+ ShellToken.Recipient[] memory recipients = new ShellToken.Recipient [](2 );
62+ recipients[0 ] = ShellToken.Recipient (user1, 100 );
63+ recipients[1 ] = ShellToken.Recipient (user2, 200 );
64+
65+ vm.startPrank (admin);
66+ shellToken.mintBatchShells (recipients);
67+ vm.stopPrank ();
68+
69+ assertEq (shellToken.balanceOf (user1), 100 );
70+ assertEq (shellToken.balanceOf (user2), 200 );
71+ }
72+
73+ function test_revert_mintBatchShells_notAdmin () public {
74+ address user = makeAddr ("user " );
75+
76+ ShellToken.Recipient[] memory recipients = new ShellToken.Recipient [](1 );
77+ recipients[0 ] = ShellToken.Recipient (user, 100 );
78+
79+ vm.startPrank (makeAddr ("notAdmin " ));
80+ vm.expectRevert ("Not an admin " );
81+ shellToken.mintBatchShells (recipients);
82+ vm.stopPrank ();
83+
84+ assertEq (shellToken.balanceOf (user), 0 );
85+ }
86+
87+ function test_deleteShells () public {
88+ address user = makeAddr ("user " );
89+
90+ vm.startPrank (admin);
91+ shellToken.mintShells (user, 100 );
92+ vm.stopPrank ();
93+
94+ assertEq (shellToken.balanceOf (user), 100 );
95+
96+ vm.startPrank (admin);
97+ shellToken.deleteShells (user, 100 );
98+ vm.stopPrank ();
99+
100+ assertEq (shellToken.balanceOf (user), 0 );
101+ }
102+
103+ function test_revert_deleteShells_notAdmin () public {
104+ address user = makeAddr ("user " );
105+
106+ vm.startPrank (admin);
107+ shellToken.mintShells (user, 100 );
108+ vm.stopPrank ();
109+
110+ assertEq (shellToken.balanceOf (user), 100 );
111+
112+ vm.startPrank (makeAddr ("notAdmin " ));
113+ vm.expectRevert ("Not an admin " );
114+ shellToken.deleteShells (user, 100 );
115+ vm.stopPrank ();
116+
117+ assertEq (shellToken.balanceOf (user), 100 );
118+ }
119+
120+ function test_revert_transfer_notAllowed () public {
121+ address user = makeAddr ("user " );
122+ address to = makeAddr ("to " );
123+
124+ vm.startPrank (admin);
125+ shellToken.mintShells (user, 100 );
126+ vm.stopPrank ();
127+
128+ assertEq (shellToken.balanceOf (user), 100 );
129+
130+ vm.startPrank (user);
131+ vm.expectRevert ("Not allowed to transfer " );
132+ shellToken.transfer (to, 100 );
133+ vm.stopPrank ();
134+
135+ assertEq (shellToken.balanceOf (user), 100 );
136+ assertEq (shellToken.balanceOf (to), 0 );
137+ }
138+
139+ function test_transfer_allowed () public {
140+ address user = makeAddr ("user " );
141+ address to = makeAddr ("to " );
142+
143+ vm.prank (admin);
144+ shellToken.mintShells (user, 100 );
145+
146+ assertEq (shellToken.balanceOf (user), 100 );
147+
148+ vm.prank (owner);
149+ shellToken.updateAllowedToTransfer (user, true );
150+
151+ vm.prank (user);
152+ shellToken.transfer (to, 100 );
153+
154+ assertEq (shellToken.balanceOf (user), 0 );
155+ assertEq (shellToken.balanceOf (to), 100 );
156+ }
157+
158+ function test_revert_transferFrom_notAllowed () public {
159+ address user = makeAddr ("user " );
160+ address to = makeAddr ("to " );
161+
162+ vm.startPrank (admin);
163+ shellToken.mintShells (user, 100 );
164+ vm.stopPrank ();
165+
166+ assertEq (shellToken.balanceOf (user), 100 );
167+
168+ vm.prank (user);
169+ shellToken.approve (address (this ), 100 );
170+
171+ vm.expectRevert ("Not allowed to transfer " );
172+ shellToken.transferFrom (user, to, 100 );
173+
174+ assertEq (shellToken.balanceOf (user), 100 );
175+ assertEq (shellToken.balanceOf (to), 0 );
176+ }
177+
178+ function test_transferFrom_allowed () public {
179+ address user = makeAddr ("user " );
180+ address to = makeAddr ("to " );
181+
182+ vm.prank (admin);
183+ shellToken.mintShells (user, 100 );
184+
185+ assertEq (shellToken.balanceOf (user), 100 );
186+
187+ vm.prank (owner);
188+ shellToken.updateAllowedToTransfer (user, true );
189+
190+ vm.prank (user);
191+ shellToken.approve (address (this ), 100 );
192+
193+ shellToken.transferFrom (user, to, 100 );
194+
195+ assertEq (shellToken.balanceOf (user), 0 );
196+ assertEq (shellToken.balanceOf (to), 100 );
197+ }
198+
199+ function test_setMultiplier () public {
200+ address activity = makeAddr ("activity " );
201+
202+ vm.startPrank (owner);
203+ shellToken.setMultiplier (activity, 100 );
204+ vm.stopPrank ();
205+ }
206+
207+ function test_revert_setMultiplier_notOwner () public {
208+ address activity = makeAddr ("activity " );
209+ address notOwner = makeAddr ("notOwner " );
210+
211+ vm.startPrank (notOwner);
212+ vm.expectRevert (abi.encodeWithSelector (Ownable.OwnableUnauthorizedAccount.selector , notOwner));
213+ shellToken.setMultiplier (activity, 100 );
214+ vm.stopPrank ();
215+
216+ vm.startPrank (admin);
217+ vm.expectRevert (abi.encodeWithSelector (Ownable.OwnableUnauthorizedAccount.selector , admin));
218+ shellToken.setMultiplier (activity, 100 );
219+ vm.stopPrank ();
220+ }
221+
222+ function test_getMultipliers () public {
223+ address [] memory contracts = new address [](3 );
224+ contracts[0 ] = makeAddr ("activity1 " );
225+ contracts[1 ] = makeAddr ("activity2 " );
226+ contracts[2 ] = makeAddr ("activity3 " );
227+
228+ vm.startPrank (owner);
229+ for (uint i; i < contracts.length ; ) {
230+ shellToken.setMultiplier (contracts[i], 100 + i);
231+ unchecked { ++ i; }
232+ }
233+ vm.stopPrank ();
234+
235+ ShellToken.Multiplier[] memory multipliers = shellToken.getMultipliers (contracts);
236+ assertEq (multipliers.length , contracts.length );
237+ for (uint i; i < multipliers.length ; ) {
238+ assertEq (multipliers[i].activity, contracts[i]);
239+ assertEq (multipliers[i].multiplier, 100 + i);
240+ unchecked { ++ i; }
241+ }
242+ }
243+ }
0 commit comments