-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
LilJuicebox.t.sol
170 lines (121 loc) · 5.09 KB
/
LilJuicebox.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.10;
import { Vm } from 'forge-std/Vm.sol';
import { DSTest } from 'ds-test/test.sol';
import { stdError } from 'forge-std/stdlib.sol';
import { LilJuicebox, ProjectShare } from '../LilJuicebox.sol';
contract User {}
contract LilJuiceboxTest is DSTest {
User internal user;
ProjectShare internal token;
LilJuicebox internal lilJuicebox;
Vm internal hevm = Vm(HEVM_ADDRESS);
event Renounced();
event Withdrawn(uint256 amount);
event StateUpdated(LilJuicebox.State state);
event Refunded(address indexed contributor, uint256 amount);
event Contributed(address indexed contributor, uint256 amount);
function setUp() public {
user = new User();
lilJuicebox = new LilJuicebox('Test Crowdfund', 'TEST');
token = lilJuicebox.token();
}
function testCanContribute() public {
uint256 startingBalance = address(this).balance;
assertEq(token.balanceOf(address(this)), 0);
hevm.expectEmit(true, false, false, true);
emit Contributed(address(this), 1 ether);
lilJuicebox.contribute{ value: 1 ether }();
assertEq(address(lilJuicebox).balance, 1 ether);
assertEq(token.balanceOf(address(this)), 1_000_000 ether);
assertEq(address(this).balance, startingBalance - 1 ether);
}
function testCannotContributeWhenRoundIsClosed() public {
uint256 startingBalance = address(this).balance;
assertEq(token.balanceOf(address(this)), 0);
lilJuicebox.setState(LilJuicebox.State.CLOSED);
hevm.expectRevert(abi.encodeWithSignature('ContributionsClosed()'));
lilJuicebox.contribute{ value: 1 ether }();
assertEq(address(lilJuicebox).balance, 0);
assertEq(token.balanceOf(address(this)), 0);
assertEq(address(this).balance, startingBalance);
}
function testRefund() public {
lilJuicebox.contribute{ value: 10 ether }();
uint256 startingBalance = address(this).balance;
assertEq(token.balanceOf(address(this)), 10_000_000 ether);
lilJuicebox.setState(LilJuicebox.State.REFUNDING);
hevm.expectEmit(true, false, false, true);
emit Refunded(address(this), 10 ether);
lilJuicebox.refund(10_000_000 ether);
assertEq(address(lilJuicebox).balance, 0);
assertEq(token.balanceOf(address(this)), 0);
assertEq(address(this).balance, startingBalance + 10 ether);
}
function testCannotRefundsIfRefundsAreNotAvailable() public {
lilJuicebox.contribute{ value: 10 ether }();
uint256 startingBalance = address(this).balance;
assertEq(token.balanceOf(address(this)), 10_000_000 ether);
hevm.expectRevert(abi.encodeWithSignature('RefundsClosed()'));
lilJuicebox.refund(10_000_000 ether);
assertEq(address(lilJuicebox).balance, 10 ether);
assertEq(token.balanceOf(address(this)), 10_000_000 ether);
assertEq(address(this).balance, startingBalance);
}
function testCannotRefundsIfNotEnoughTokens() public {
lilJuicebox.contribute{ value: 10 ether }();
lilJuicebox.setState(LilJuicebox.State.REFUNDING);
assertEq(token.balanceOf(address(user)), 0);
hevm.prank(address(user));
hevm.expectRevert(stdError.arithmeticError); // error comes from ERC20 impl. (solmate in this test)
lilJuicebox.refund(10_000_000 ether);
assertEq(address(lilJuicebox).balance, 10 ether);
assertEq(token.balanceOf(address(user)), 0);
assertEq(address(user).balance, 0);
}
function testManagerCanWithdrawFunds() public {
hevm.deal(address(lilJuicebox), 10 ether);
uint256 initialBalance = address(this).balance;
hevm.expectEmit(false, false, false, true);
emit Withdrawn(10 ether);
lilJuicebox.withdraw();
assertEq(address(this).balance, initialBalance + 10 ether);
}
function testNonManagerCannotWithdrawFunds() public {
hevm.deal(address(lilJuicebox), 10 ether);
uint256 initialBalance = address(user).balance;
hevm.prank(address(user));
hevm.expectRevert(abi.encodeWithSignature('Unauthorized()'));
lilJuicebox.withdraw();
assertEq(address(user).balance, initialBalance);
}
function testManagerCanSetState() public {
assertEq(uint256(lilJuicebox.getState()), uint256(LilJuicebox.State.OPEN));
hevm.expectEmit(false, false, false, true);
emit StateUpdated(LilJuicebox.State.CLOSED);
lilJuicebox.setState(LilJuicebox.State.CLOSED);
assertEq(uint256(lilJuicebox.getState()), uint256(LilJuicebox.State.CLOSED));
}
function testNonManagerCannotSetState() public {
assertEq(uint256(lilJuicebox.getState()), uint256(LilJuicebox.State.OPEN));
hevm.prank(address(user));
hevm.expectRevert(abi.encodeWithSignature('Unauthorized()'));
lilJuicebox.setState(LilJuicebox.State.CLOSED);
assertEq(uint256(lilJuicebox.getState()), uint256(LilJuicebox.State.OPEN));
}
function testManagerCanRenounceOwnership() public {
assertEq(lilJuicebox.manager(), address(this));
hevm.expectEmit(false, false, false, true);
emit Renounced();
lilJuicebox.renounce();
assertEq(lilJuicebox.manager(), address(0));
}
function testNonManagerCannotRenounceOwnership() public {
assertEq(lilJuicebox.manager(), address(this));
hevm.prank(address(user));
hevm.expectRevert(abi.encodeWithSignature('Unauthorized()'));
lilJuicebox.renounce();
assertEq(lilJuicebox.manager(), address(this));
}
receive() external payable {}
}