-
Notifications
You must be signed in to change notification settings - Fork 288
/
fee-on-transfer.sol
382 lines (307 loc) · 11.2 KB
/
fee-on-transfer.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "forge-std/Test.sol";
/*
Name: STA token had a deflationary model with transfer fee of 1% charged from a recipient.
Description:
The actual deposited amount might be lower than the specified depositAmount of the function parameter.
VulnVault: Incompatability with deflationary / fee-on-transfer tokens
Mitigation:
Transfer the tokens first and compare pre-/after token balances to compute the actual deposited amount.
REF:
https://twitter.com/1nf0s3cpt/status/1671084918506684418
https://medium.com/1inch-network/balancer-hack-2020-a8f7131c980e
https://twitter.com/BlockSecTeam/status/1600442137811689473
*/
contract ContractTest is Test {
STA STAContract;
VulnVault VulnVaultContract;
Vault VaultContract;
function setUp() public {
STAContract = new STA();
VulnVaultContract = new VulnVault(address(STAContract));
VaultContract = new Vault(address(STAContract));
}
function testVulnFeeOnTransfer() public {
address alice = vm.addr(1);
address bob = vm.addr(2);
STAContract.balanceOf(address(this));
STAContract.transfer(alice, 1000000);
console.log("Alice's STA balance:", STAContract.balanceOf(alice)); // charge 1% fee
vm.startPrank(alice);
STAContract.approve(address(VulnVaultContract), type(uint256).max);
VulnVaultContract.deposit(10000);
//VulnVaultContract.getBalance(alice);
console.log(
"Alice deposit 10000 STA, but Alice's STA balance in VulnVaultContract:",
VulnVaultContract.getBalance(alice)
); // charge 1% fee
assertEq(
STAContract.balanceOf(address(VulnVaultContract)),
VulnVaultContract.getBalance(alice)
);
}
function testFeeOnTransfer() public {
address alice = vm.addr(1);
address bob = vm.addr(2);
STAContract.balanceOf(address(this));
STAContract.transfer(alice, 1000000);
console.log("Alice's STA balance:", STAContract.balanceOf(alice)); // charge 1% fee
vm.startPrank(alice);
STAContract.approve(address(VaultContract), type(uint256).max);
VaultContract.deposit(10000);
//VaultContract.getBalance(alice);
console.log(
"Alice deposit 10000, Alice's STA balance in VaultContract:",
VaultContract.getBalance(alice)
); // charge 1% fee
assertEq(
STAContract.balanceOf(address(VaultContract)),
VaultContract.getBalance(alice)
);
}
receive() external payable {}
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(
address owner,
address spender
) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(
address from,
address to,
uint256 value
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a / b;
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
function ceil(uint256 a, uint256 m) internal pure returns (uint256) {
uint256 c = add(a, m);
uint256 d = sub(c, 1);
return mul(div(d, m), m);
}
}
abstract contract ERC20Detailed is IERC20 {
string private _name;
string private _symbol;
uint8 private _decimals;
constructor(
string memory name,
string memory symbol,
uint8 decimals
) internal {
_name = name;
_symbol = symbol;
_decimals = decimals;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
}
contract STA is ERC20Detailed {
using SafeMath for uint256;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowed;
string constant tokenName = "Statera";
string constant tokenSymbol = "STA";
uint8 constant tokenDecimals = 18;
uint256 _totalSupply = 100000000000000000000000000;
uint256 public basePercent = 100;
constructor()
public
payable
ERC20Detailed(tokenName, tokenSymbol, tokenDecimals)
{
_issue(msg.sender, _totalSupply);
}
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
function balanceOf(address owner) public view returns (uint256) {
return _balances[owner];
}
function allowance(
address owner,
address spender
) public view returns (uint256) {
return _allowed[owner][spender];
}
function cut(uint256 value) public view returns (uint256) {
uint256 roundValue = value.ceil(basePercent);
uint256 cutValue = roundValue.mul(basePercent).div(10000);
return cutValue;
}
function transfer(address to, uint256 value) public returns (bool) {
require(value <= _balances[msg.sender]);
require(to != address(0));
uint256 tokensToBurn = cut(value);
uint256 tokensToTransfer = value.sub(tokensToBurn);
_balances[msg.sender] = _balances[msg.sender].sub(value);
_balances[to] = _balances[to].add(tokensToTransfer);
_totalSupply = _totalSupply.sub(tokensToBurn);
emit Transfer(msg.sender, to, tokensToTransfer);
emit Transfer(msg.sender, address(0), tokensToBurn);
return true;
}
function approve(address spender, uint256 value) public returns (bool) {
require(spender != address(0));
_allowed[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
function transferFrom(
address from,
address to,
uint256 value
) public returns (bool) {
require(value <= _balances[from]);
require(value <= _allowed[from][msg.sender]);
require(to != address(0));
_balances[from] = _balances[from].sub(value);
uint256 tokensToBurn = cut(value);
uint256 tokensToTransfer = value.sub(tokensToBurn);
_balances[to] = _balances[to].add(tokensToTransfer);
_totalSupply = _totalSupply.sub(tokensToBurn);
_allowed[from][msg.sender] = _allowed[from][msg.sender].sub(value);
emit Transfer(from, to, tokensToTransfer);
emit Transfer(from, address(0), tokensToBurn);
return true;
}
function upAllowance(
address spender,
uint256 addedValue
) public returns (bool) {
require(spender != address(0));
_allowed[msg.sender][spender] = (
_allowed[msg.sender][spender].add(addedValue)
);
emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
return true;
}
function downAllowance(
address spender,
uint256 subtractedValue
) public returns (bool) {
require(spender != address(0));
_allowed[msg.sender][spender] = (
_allowed[msg.sender][spender].sub(subtractedValue)
);
emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
return true;
}
function _issue(address account, uint256 amount) internal {
require(amount != 0);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
function destroy(uint256 amount) external {
_destroy(msg.sender, amount);
}
function _destroy(address account, uint256 amount) internal {
require(amount != 0);
require(amount <= _balances[account]);
_totalSupply = _totalSupply.sub(amount);
_balances[account] = _balances[account].sub(amount);
emit Transfer(account, address(0), amount);
}
function destroyFrom(address account, uint256 amount) external {
require(amount <= _allowed[account][msg.sender]);
_allowed[account][msg.sender] = _allowed[account][msg.sender].sub(
amount
);
_destroy(account, amount);
}
}
//vulnerable vault
contract VulnVault {
mapping(address => uint256) private balances;
uint256 private fee;
IERC20 private token;
event Deposit(address indexed depositor, uint256 amount);
event Withdrawal(address indexed recipient, uint256 amount);
constructor(address _tokenAddress) {
token = IERC20(_tokenAddress);
}
function deposit(uint256 amount) external {
require(amount > 0, "Deposit amount must be greater than zero");
token.transferFrom(msg.sender, address(this), amount);
balances[msg.sender] += amount;
emit Deposit(msg.sender, amount);
}
function withdraw(uint256 amount) external {
require(amount > 0, "Withdrawal amount must be greater than zero");
require(amount <= balances[msg.sender], "Insufficient balance");
balances[msg.sender] -= amount;
token.transfer(msg.sender, amount);
emit Withdrawal(msg.sender, amount);
}
function getBalance(address account) external view returns (uint256) {
return balances[account];
}
}
//Mitigated vault
contract Vault {
mapping(address => uint256) private balances;
uint256 private fee;
IERC20 private token;
event Deposit(address indexed depositor, uint256 amount);
event Withdrawal(address indexed recipient, uint256 amount);
constructor(address _tokenAddress) {
token = IERC20(_tokenAddress);
}
function deposit(uint256 amount) external {
require(amount > 0, "Deposit amount must be greater than zero");
uint256 balanceBefore = token.balanceOf(address(this));
token.transferFrom(msg.sender, address(this), amount);
uint256 balanceAfter = token.balanceOf(address(this));
uint256 actualDepositAmount = balanceAfter - balanceBefore;
balances[msg.sender] += actualDepositAmount;
emit Deposit(msg.sender, actualDepositAmount);
}
function withdraw(uint256 amount) external {
require(amount > 0, "Withdrawal amount must be greater than zero");
require(amount <= balances[msg.sender], "Insufficient balance");
balances[msg.sender] -= amount;
token.transfer(msg.sender, amount);
emit Withdrawal(msg.sender, amount);
}
function getBalance(address account) external view returns (uint256) {
return balances[account];
}
}