/
TestTokenFlow.sol
231 lines (193 loc) · 7.04 KB
/
TestTokenFlow.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: MIT
pragma solidity 0.8.6;
import './helpers/TestBaseWorkflow.sol';
/// @notice This file tests JBToken related flows
contract TestTokenFlow is TestBaseWorkflow {
JBController private _controller;
JBTokenStore private _tokenStore;
JBProjectMetadata private _projectMetadata;
JBFundingCycleData private _data;
JBFundingCycleMetadata private _metadata;
JBGroupedSplits[] private _groupedSplits; // Default empty
JBFundAccessConstraints[] private _fundAccessConstraints; // Default empty
IJBPaymentTerminal[] private _terminals; // Default empty
uint256 private _projectId;
address private _projectOwner;
uint256 private _reservedRate = 5000;
function setUp() public override {
super.setUp();
_controller = jbController();
_tokenStore = jbTokenStore();
_projectMetadata = JBProjectMetadata({content: 'myIPFSHash', domain: 1});
_data = JBFundingCycleData({
duration: 14,
weight: 1000 * 10**18,
discountRate: 450000000,
ballot: IJBFundingCycleBallot(address(0))
});
_metadata = JBFundingCycleMetadata({
global: JBGlobalFundingCycleMetadata({allowSetTerminals: false, allowSetController: false}),
reservedRate: _reservedRate,
redemptionRate: 5000, //50%
ballotRedemptionRate: 0,
pausePay: false,
pauseDistributions: false,
pauseRedeem: false,
pauseBurn: false,
allowMinting: true,
allowChangeToken: true,
allowTerminalMigration: false,
allowControllerMigration: false,
holdFees: false,
useTotalOverflowForRedemptions: false,
useDataSourceForPay: false,
useDataSourceForRedeem: false,
dataSource: address(0)
});
_projectOwner = multisig();
_projectId = _controller.launchProjectFor(
_projectOwner,
_projectMetadata,
_data,
_metadata,
block.timestamp,
_groupedSplits,
_fundAccessConstraints,
_terminals,
''
);
}
/**
* @notice tests the following flow with fuzzed values:
* launch project → issue token → change token → mint token → burn token
*/
function testFuzzTokenFlow(
uint256 mintAmount,
uint256 burnAmount,
bool mintPreferClaimed,
bool burnPreferClaimed
) public {
// Might overflow in processed token tracker if burn amount >= max int256 (ie (2**256)/2 -1 )
evm.assume(burnAmount < (2**256)/2);
// calls will originate from projectOwner addr
evm.startPrank(_projectOwner);
// issue an ERC-20 token for project
_controller.issueTokenFor(_projectId, 'TestName', 'TestSymbol');
// create a new IJBToken and change it's owner to the tokenStore
IJBToken _newToken = new JBToken('NewTestName', 'NewTestSymbol');
_newToken.transferOwnership(_projectId, address(_tokenStore));
// change the projects token to _newToken
_controller.changeTokenOf(_projectId, _newToken, address(0));
// confirm the project's new JBToken
assertEq(address(_tokenStore.tokenOf(_projectId)), address(_newToken));
address _beneficiary = address(1234);
uint256 _expectedTokenBalance = 0;
uint256 _beneficiaryTokenAmount = mintAmount / 2; // 50% reserved rate results in half the mintAmount
if (mintAmount == 0) evm.expectRevert(abi.encodeWithSignature('ZERO_TOKENS_TO_MINT()'));
else _expectedTokenBalance = _beneficiaryTokenAmount;
// mint tokens to beneficiary addr
_controller.mintTokensOf(
_projectId,
mintAmount,
_beneficiary,
'Mint memo',
mintPreferClaimed,
true /*use reserved rate*/
);
// total token balance should be half of token count due to 50% reserved rate
assertEq(_tokenStore.balanceOf(_beneficiary, _projectId), _expectedTokenBalance);
if (burnAmount == 0) evm.expectRevert(abi.encodeWithSignature('NO_BURNABLE_TOKENS()'));
else if (burnAmount > _expectedTokenBalance)
evm.expectRevert(abi.encodeWithSignature('INSUFFICIENT_FUNDS()'));
else _expectedTokenBalance = _expectedTokenBalance - burnAmount;
// burn tokens from beneficiary addr
// next call will originate from holder addr
evm.stopPrank();
evm.prank(_beneficiary);
_controller.burnTokensOf(
_beneficiary,
_projectId,
/* _tokenCount */
burnAmount,
'Burn memo',
burnPreferClaimed
);
// total balance of tokens should be updated
assertEq(_tokenStore.balanceOf(_beneficiary, _projectId), _expectedTokenBalance);
}
/**
* @notice tests the following corner case:
* launch project → issue token → mint max claimed tokens → mint max unclaimed tokens → try to claim unclaimed tokens
*/
function testLargeTokenClaimFlow() public {
// calls will originate from projectOwner addr
evm.startPrank(_projectOwner);
// issue an ERC-20 token for project
_controller.issueTokenFor(_projectId, 'TestName', 'TestSymbol');
address _beneficiary = address(1234);
// mint claimed tokens to beneficiary addr
_controller.mintTokensOf(
_projectId,
type(uint224).max,
_beneficiary,
'Mint memo',
true,
false /*use reserved rate*/
);
// mint unclaimed tokens to beneficiary addr
_controller.mintTokensOf(
_projectId,
type(uint256).max,
_beneficiary,
'Mint memo',
false,
false
);
// try to claim the unclaimed tokens
evm.stopPrank();
evm.prank(_beneficiary);
_tokenStore.claimFor(
_beneficiary,
_projectId,
/* _amount */
1
);
}
/**
* @notice tests the following corner case:
* launch project → issue token → mint unclaimed tokens → switch to new token → claim unclaimed tokens of the new token
*/
function testTokenChangeFlow() public {
// calls will originate from projectOwner addr
evm.startPrank(_projectOwner);
// issue an ERC-20 token for project
_controller.issueTokenFor(_projectId, 'TestName', 'TestSymbol');
address _beneficiary = address(1234);
// mint unclaimed tokens to beneficiary addr
_controller.mintTokensOf(
_projectId,
type(uint256).max,
_beneficiary,
'Mint memo',
false,
false
);
// create a new IJBToken and change it's owner to the tokenStore
IJBToken _newToken = new JBToken('NewTestName', 'NewTestSymbol');
_newToken.transferOwnership(_projectId, address(_tokenStore));
// change the projects token to _newToken
_controller.changeTokenOf(_projectId, _newToken, address(0));
// claim and mint the max possible amount of unclaimed tokens
evm.stopPrank();
evm.prank(_beneficiary);
_tokenStore.claimFor(_beneficiary, _projectId, type(uint224).max);
// total token balanced should be updated
assertEq(_newToken.balanceOf(_beneficiary, _projectId), type(uint224).max);
assertEq(
_tokenStore.unclaimedBalanceOf(_beneficiary, _projectId),
type(uint256).max - type(uint224).max
);
assertEq(_tokenStore.unclaimedTotalSupplyOf(_projectId), type(uint256).max - type(uint224).max);
assertEq(_tokenStore.balanceOf(_beneficiary, _projectId), type(uint256).max);
}
}