forked from crytic/properties
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERC20BasicProperties.sol
293 lines (260 loc) · 10.4 KB
/
ERC20BasicProperties.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.13;
import "../util/ERC20TestBase.sol";
abstract contract CryticERC20BasicProperties is CryticERC20Base {
constructor() {}
////////////////////////////////////////
// Properties
// Total supply should change only by means of mint or burn
function test_ERC20_constantSupply() public virtual {
require(!isMintableOrBurnable);
assertEq(initialSupply, totalSupply(), "Token supply was modified");
}
// User balance must not exceed total supply
function test_ERC20_userBalanceNotHigherThanSupply() public {
assertLte(
balanceOf(msg.sender),
totalSupply(),
"User balance higher than total supply"
);
}
// Sum of users balance must not exceed total supply
function test_ERC20_usersBalancesNotHigherThanSupply() public {
uint256 balance = balanceOf(USER1) +
balanceOf(USER2) +
balanceOf(USER3);
assertLte(
balance,
totalSupply(),
"Sum of user balances higher than total supply"
);
}
// Address zero should have zero balance
function test_ERC20_zeroAddressBalance() public {
assertEq(
balanceOf(address(0)),
0,
"Address zero balance not equal to zero"
);
}
// Transfers to zero address should not be allowed
function test_ERC20_transferToZeroAddress() public {
uint256 balance = balanceOf(address(this));
require(balance > 0);
bool r = transfer(address(0), balance);
assertWithMsg(r == false, "Successful transfer to address zero");
}
// Transfers to zero address should not be allowed
function test_ERC20_transferFromToZeroAddress(uint256 value) public {
uint256 balance_sender = balanceOf(msg.sender);
uint256 current_allowance = allowance(msg.sender, address(this));
require(balance_sender > 0 && current_allowance > 0);
uint256 maxValue = balance_sender >= current_allowance
? current_allowance
: balance_sender;
bool r = transferFrom(msg.sender, address(0), value % (maxValue + 1));
assertWithMsg(r == false, "Successful transferFrom to address zero");
}
// Self transfers should not break accounting
function test_ERC20_selfTransferFrom(uint256 value) public {
uint256 balance_sender = balanceOf(msg.sender);
uint256 current_allowance = allowance(msg.sender, address(this));
require(balance_sender > 0 && current_allowance > 0);
uint256 maxValue = balance_sender >= current_allowance
? current_allowance
: balance_sender;
bool r = transferFrom(msg.sender, msg.sender, value % (maxValue + 1));
assertWithMsg(r == true, "Failed self transferFrom");
assertEq(
balance_sender,
balanceOf(msg.sender),
"Self transferFrom breaks accounting"
);
}
// Self transfers should not break accounting
function test_ERC20_selfTransfer(uint256 value) public {
uint256 balance_sender = balanceOf(address(this));
require(balance_sender > 0);
bool r = this.transfer(address(this), value % (balance_sender + 1));
assertWithMsg(r == true, "Failed self transfer");
assertEq(
balance_sender,
balanceOf(address(this)),
"Self transfer breaks accounting"
);
}
// Transfers for more than available balance should not be allowed
function test_ERC20_transferFromMoreThanBalance(address target) public {
uint256 balance_sender = balanceOf(msg.sender);
uint256 balance_receiver = balanceOf(target);
uint256 current_allowance = allowance(msg.sender, address(this));
require(balance_sender > 0 && current_allowance > balance_sender);
bool r = transferFrom(msg.sender, target, balance_sender + 1);
assertWithMsg(
r == false,
"Successful transferFrom for more than account balance"
);
assertEq(
balanceOf(msg.sender),
balance_sender,
"TransferFrom for more than balance modified source balance"
);
assertEq(
balanceOf(target),
balance_receiver,
"TransferFrom for more than balance modified target balance"
);
}
// Transfers for more than available balance should not be allowed
function test_ERC20_transferMoreThanBalance(address target) public {
uint256 balance_sender = balanceOf(address(this));
uint256 balance_receiver = balanceOf(target);
require(balance_sender > 0);
bool r = this.transfer(target, balance_sender + 1);
assertWithMsg(
r == false,
"Successful transfer for more than account balance"
);
assertEq(
balanceOf(address(this)),
balance_sender,
"Transfer for more than balance modified source balance"
);
assertEq(
balanceOf(target),
balance_receiver,
"Transfer for more than balance modified target balance"
);
}
// Zero amount transfers should not break accounting
function test_ERC20_transferZeroAmount(address target) public {
uint256 balance_sender = balanceOf(address(this));
uint256 balance_receiver = balanceOf(target);
require(balance_sender > 0);
bool r = transfer(target, 0);
assertWithMsg(r == true, "Zero amount transfer failed");
assertEq(
balanceOf(address(this)),
balance_sender,
"Zero amount transfer modified source balance"
);
assertEq(
balanceOf(target),
balance_receiver,
"Zero amount transfer modified target balance"
);
}
// Zero amount transfers should not break accounting
function test_ERC20_transferFromZeroAmount(address target) public {
uint256 balance_sender = balanceOf(msg.sender);
uint256 balance_receiver = balanceOf(target);
uint256 current_allowance = allowance(msg.sender, address(this));
require(balance_sender > 0 && current_allowance > 0);
bool r = transferFrom(msg.sender, target, 0);
assertWithMsg(r == true, "Zero amount transferFrom failed");
assertEq(
balanceOf(msg.sender),
balance_sender,
"Zero amount transferFrom modified source balance"
);
assertEq(
balanceOf(target),
balance_receiver,
"Zero amount transferFrom modified target balance"
);
}
// Transfers should update accounting correctly
function test_ERC20_transfer(address target, uint256 amount) public {
require(target != address(this));
uint256 balance_sender = balanceOf(address(this));
uint256 balance_receiver = balanceOf(target);
require(balance_sender > 2);
uint256 transfer_value = (amount % balance_sender) + 1;
bool r = this.transfer(target, transfer_value);
assertWithMsg(r == true, "transfer failed");
assertEq(
balanceOf(address(this)),
balance_sender - transfer_value,
"Wrong source balance after transfer"
);
assertEq(
balanceOf(target),
balance_receiver + transfer_value,
"Wrong target balance after transfer"
);
}
// Transfers should update accounting correctly
function test_ERC20_transferFrom(address target, uint256 amount) public {
require(target != address(this));
require(target != msg.sender);
uint256 balance_sender = balanceOf(msg.sender);
uint256 balance_receiver = balanceOf(target);
uint256 current_allowance = allowance(msg.sender, address(this));
require(balance_sender > 2 && current_allowance > balance_sender);
uint256 transfer_value = (amount % balance_sender) + 1;
bool r = transferFrom(msg.sender, target, transfer_value);
assertWithMsg(r == true, "transferFrom failed");
assertEq(
balanceOf(msg.sender),
balance_sender - transfer_value,
"Wrong source balance after transferFrom"
);
assertEq(
balanceOf(target),
balance_receiver + transfer_value,
"Wrong target balance after transferFrom"
);
}
// Approve should set correct allowances
function test_ERC20_setAllowance(address target, uint256 amount) public {
bool r = this.approve(target, amount);
assertWithMsg(r == true, "Failed to set allowance via approve");
assertEq(
allowance(address(this), target),
amount,
"Allowance not set correctly"
);
}
// Approve should set correct allowances
function test_ERC20_setAllowanceTwice(
address target,
uint256 amount
) public {
bool r = this.approve(target, amount);
assertWithMsg(r == true, "Failed to set allowance via approve");
assertEq(
allowance(address(this), target),
amount,
"Allowance not set correctly"
);
r = this.approve(target, amount / 2);
assertWithMsg(r == true, "Failed to set allowance via approve");
assertEq(
allowance(address(this), target),
amount / 2,
"Allowance not set correctly"
);
}
// TransferFrom should decrease allowance
function test_ERC20_spendAllowanceAfterTransfer(
address target,
uint256 amount
) public {
require(target != address(this) && target != address(0));
require(target != msg.sender);
uint256 balance_sender = balanceOf(msg.sender);
uint256 current_allowance = allowance(msg.sender, address(this));
require(balance_sender > 0 && current_allowance > balance_sender);
uint256 transfer_value = (amount % balance_sender) + 1;
bool r = this.transferFrom(msg.sender, target, transfer_value);
assertWithMsg(r == true, "transferFrom failed");
// Some implementations take an allowance of 2**256-1 as infinite, and therefore don't update
if (current_allowance != type(uint256).max) {
assertEq(
allowance(msg.sender, address(this)),
current_allowance - transfer_value,
"Allowance not updated correctly"
);
}
}
}