-
Notifications
You must be signed in to change notification settings - Fork 8
/
Foreign20.sol
278 lines (249 loc) · 7.73 KB
/
Foreign20.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IBosonAccountHandler } from "../interfaces/handlers/IBosonAccountHandler.sol";
import { IBosonMetaTransactionsHandler } from "../interfaces/handlers/IBosonMetaTransactionsHandler.sol";
import { BosonTypes } from "../domain/BosonTypes.sol";
import { MockNativeMetaTransaction } from "./MockNativeMetaTransaction.sol";
/**
* @title Foreign20
*
* @notice Mock ERC-(20) NFT for Unit Testing
*/
contract Foreign20 is ERC20Pausable, MockNativeMetaTransaction {
string public constant TOKEN_NAME = "Foreign20";
string public constant TOKEN_SYMBOL = "20Test";
string public constant ERC712_VERSION = "1";
constructor() ERC20(TOKEN_NAME, TOKEN_SYMBOL) {
_initializeEIP712(TOKEN_NAME, ERC712_VERSION);
}
// This is to support Native meta transactions
// never use msg.sender directly, use _msgSender() instead
function _msgSender() internal view override returns (address sender) {
if (msg.sender == address(this)) {
bytes memory array = msg.data;
uint256 index = msg.data.length;
assembly {
// Load the 32 bytes word from memory with the address on the lower 20 bytes, and mask those.
sender := and(mload(add(array, index)), 0xffffffffffffffffffffffffffffffffffffffff)
}
} else {
sender = msg.sender;
}
return sender;
}
/**
* Mints some tokens
* @param _account - address that gets the tokens
* @param _amount - amount to mint
*/
function mint(address _account, uint256 _amount) public {
_mint(_account, _amount);
}
/**
* Pauses the token transfers
*/
function pause() public {
_pause();
}
/**
* Deletes the contract code
*/
function destruct() public {
selfdestruct(payable(msg.sender));
}
}
/**
* @title Foreign20 that fails when name() is called
*
* We need other ERC20 methods such as approve, transferFrom etc, so it's easier to just override the function that we don't want to succeed
*
* @notice Mock ERC-(20) NFT for Unit Testing
*/
contract Foreign20NoName is Foreign20 {
function name() public pure override returns (string memory) {
// simulate the contract without "name" implementation.
revert();
}
}
/**
* @title Foreign20 that reenters into protocol
*
*
* @notice Mock ERC-(20) NFT for Unit Testing
*/
contract Foreign20Malicious is Foreign20 {
address private protocolAddress;
address private owner;
constructor() {
owner = msg.sender;
}
function setProtocolAddress(address _newProtocolAddress) external {
protocolAddress = _newProtocolAddress;
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override {
super._beforeTokenTransfer(from, to, amount);
// When funds are transferred from protocol, reenter
if (from == protocolAddress) {
// this is for demonstration purposes only, therefore id "3" is hardcoded
IBosonAccountHandler(msg.sender).updateBuyer(BosonTypes.Buyer(3, payable(owner), true));
}
}
}
/**
* @title Foreign20 that reenters into protocol
*
*
* @notice Mock ERC-(20) for Unit Testing
*/
contract Foreign20Malicious2 is Foreign20 {
address private protocolAddress;
address private owner;
bytes private metaTxBytes;
bytes32 private sigR;
bytes32 private sigS;
uint8 private sigV;
address private attacker;
constructor() {
owner = msg.sender;
}
function setProtocolAddress(address _newProtocolAddress) external {
protocolAddress = _newProtocolAddress;
}
function setMetaTxBytes(
address _attacker,
bytes calldata _metaTxBytes,
bytes32 _sigR,
bytes32 _sigS,
uint8 _sigV
) external {
metaTxBytes = _metaTxBytes;
sigR = _sigR;
sigS = _sigS;
sigV = _sigV;
attacker = _attacker;
}
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual override {
super._beforeTokenTransfer(from, to, amount);
// When funds are transferred from protocol, reenter
if (to == protocolAddress) {
// this is for demonstration purposes only,
IBosonMetaTransactionsHandler(msg.sender).executeMetaTransaction(
attacker,
"getNextExchangeId()",
metaTxBytes,
0,
sigR,
sigS,
sigV
);
}
}
}
/**
* @title Foreign20 that consumes all gas when name called
*
*
* @notice Mock ERC-(20) for Unit Testing
*/
contract Foreign20MaliciousName is Foreign20 {
function name() public pure override returns (string memory) {
// name consumes all gas
unchecked {
uint256 i = 0;
while (true) {
i++;
}
}
return "nothing";
}
}
/**
* @title Foreign20 that takes a fee during the transfer
*
*
* @notice Mock ERC-(20) for Unit Testing
*/
contract Foreign20WithFee is Foreign20 {
uint256 private fee = 3;
/**
* @dev See {ERC20-_beforeTokenTransfer}.
*
* Burn part of the transferred value
*
*/
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual override {
if (to != address(0) && from != address(0)) {
uint256 _fee = (amount * fee) / 100;
_burn(to, _fee);
}
super._afterTokenTransfer(from, to, amount);
}
function setFee(uint256 _newFee) external {
fee = _newFee;
}
}
/**
* @title Foreign20 that return false when transfer is called
*
*
* @notice Mock ERC-(20) for Unit Testing
*/
contract Foreign20TransferReturnFalse is Foreign20 {
function transfer(address, uint256) public virtual override returns (bool) {
return false;
}
}
/**
* @title Foreign20 that return false when transferFrom is called
*
*
* @notice Mock ERC-(20) for Unit Testing
*/
contract Foreign20TransferFromReturnFalse is Foreign20 {
function transferFrom(address, address, uint256) public virtual override returns (bool) {
return false;
}
}
/*
* @title Foreign20 that consumes all gas when transfer is called
*
* @notice Mock ERC-(20) for Unit Testing
*/
contract Foreign20GasTheft is Foreign20 {
function transferFrom(address, address, uint256) public virtual override returns (bool) {
while (true) {
// consume all gas
}
return false;
}
}
/*
* @title Foreign20 that returns an absurdly long return data
*
* @notice Mock ERC-(20) for Unit Testing
*/
contract Foreign20ReturnBomb is Foreign20 {
function transferFrom(address, address, uint256) public virtual override returns (bool) {
assembly {
revert(0, 3000000)
// This is carefully chosen. If it's too low, not enough gas is consumed and contract that call it does not run out of gas.
// If it's too high, then this contract runs out of gas before the return data is returned.
}
}
}
/*
* @title Foreign20 that returns true, but the data cannot be decoded into a boolean
*
* @notice Mock ERC-(20) for Unit Testing
*/
contract Foreign20MalformedReturn is Foreign20 {
function transferFrom(address, address, uint256) public virtual override returns (bool) {
assembly {
return(0, 31) // return too short data
// return(0, 33) // return too long data
// return(0x40, 32) // return a value that is not 0 or 1
}
}
}