-
Notifications
You must be signed in to change notification settings - Fork 60
/
ForwarderV4.sol
380 lines (341 loc) · 10.4 KB
/
ForwarderV4.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
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;
import '@openzeppelin/contracts/token/ERC1155/IERC1155.sol';
import '@openzeppelin/contracts/token/ERC721/IERC721.sol';
import '@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol';
import '@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol';
import './ERC20Interface.sol';
import './TransferHelper.sol';
import './IForwarderV4.sol';
/**
* @title ForwarderV4
* @notice This contract will forward any incoming Ether or token to the parent address of the contract
*/
contract ForwarderV4 is IERC721Receiver, ERC1155Holder, IForwarderV4 {
/// @notice Any funds sent to this contract will be forwarded to this address
address public parentAddress;
/// @notice Address which is allowed to call methods on this contract alongwith the parentAddress
address public feeAddress;
bool public autoFlush721 = true;
bool public autoFlush1155 = true;
/**
* @notice Event triggered when a deposit is received in the forwarder
* @param from Address from which the deposit is received
* @param value Amount of Ether received
* @param data Data sent along with the deposit
*/
event ForwarderDeposited(address from, uint256 value, bytes data);
/**
* @notice Modifier that will execute internal code block only if the sender is from the allowed addresses
*/
modifier onlyAllowedAddress() {
require(
msg.sender == parentAddress || msg.sender == feeAddress,
'Address is not allowed'
);
_;
}
/**
* @notice Modifier that will execute internal code block only if the contract has not been initialized yet
*/
modifier onlyUninitialized() {
require(parentAddress == address(0x0), 'Already initialized');
_;
}
/**
* @notice Default function; Gets called when Ether is deposited with no data, and forwards it to the parent address
*/
receive() external payable {
flush();
}
/**
* @notice Default function; Gets called when data is sent but does not match any other function
*/
fallback() external payable {
flush();
}
/**
* @notice Initialize the contract, and sets the destination address to that of the parent address
* @param _parentAddress Address to which the funds should be forwarded
* @param _feeAddress Address which is allowed to call methods on this contract alongwith the parentAddress
* @param _autoFlush721 Whether to automatically flush ERC721 tokens or not
* @param _autoFlush1155 Whether to automatically flush ERC1155 tokens or not
*/
function init(
address _parentAddress,
address _feeAddress,
bool _autoFlush721,
bool _autoFlush1155
) external onlyUninitialized {
require(_parentAddress != address(0x0), 'Invalid parent address');
parentAddress = _parentAddress;
require(_feeAddress != address(0x0), 'Invalid fee address');
feeAddress = _feeAddress;
uint256 value = address(this).balance;
/// @notice set whether we want to automatically flush erc721/erc1155 tokens or not
autoFlush721 = _autoFlush721;
autoFlush1155 = _autoFlush1155;
if (value == 0) {
return;
}
/**
* Since we are forwarding on initialization,
* we don't have the context of the original sender.
* We still emit an event about the forwarding but set
* the sender to the forwarder itself
*/
emit ForwarderDeposited(address(this), value, msg.data);
(bool success, ) = parentAddress.call{ value: value }('');
require(success, 'Flush failed');
}
/**
* @inheritdoc IForwarderV4
*/
function setAutoFlush721(bool autoFlush)
external
virtual
override
onlyAllowedAddress
{
autoFlush721 = autoFlush;
}
/**
* @inheritdoc IForwarderV4
*/
function setAutoFlush1155(bool autoFlush)
external
virtual
override
onlyAllowedAddress
{
autoFlush1155 = autoFlush;
}
/**
* ERC721 standard callback function for when a ERC721 is transfered. The forwarder will send the nft
* to the base wallet once the nft contract invokes this method after transfering the nft.
*
* @param _operator The address which called `safeTransferFrom` function
* @param _from The address of the sender
* @param _tokenId The token id of the nft
* @param data Additional data with no specified format, sent in call to `_to`
*/
function onERC721Received(
address _operator,
address _from,
uint256 _tokenId,
bytes memory data
) external virtual override returns (bytes4) {
if (autoFlush721) {
IERC721 instance = IERC721(msg.sender);
require(
instance.supportsInterface(type(IERC721).interfaceId),
'The caller does not support the ERC721 interface'
);
/// this won't work for ERC721 re-entrancy
instance.safeTransferFrom(address(this), parentAddress, _tokenId, data);
}
return this.onERC721Received.selector;
}
/**
* @notice Method to allow for calls to other contracts. This method can only be called by the parent address
* @param target The target contract address whose method needs to be called
* @param value The amount of Ether to be sent
* @param data The calldata to be sent
*/
function callFromParent(
address target,
uint256 value,
bytes calldata data
) external returns (bytes memory) {
require(msg.sender == parentAddress, 'Only Parent');
(bool success, bytes memory returnedData) = target.call{ value: value }(
data
);
require(success, 'Parent call execution failed');
return returnedData;
}
/**
* @inheritdoc ERC1155Holder
*/
function onERC1155Received(
address _operator,
address _from,
uint256 id,
uint256 value,
bytes memory data
) public virtual override returns (bytes4) {
IERC1155 instance = IERC1155(msg.sender);
require(
instance.supportsInterface(type(IERC1155).interfaceId),
'The caller does not support the IERC1155 interface'
);
if (autoFlush1155) {
instance.safeTransferFrom(address(this), parentAddress, id, value, data);
}
return this.onERC1155Received.selector;
}
/**
* @inheritdoc ERC1155Holder
*/
function onERC1155BatchReceived(
address _operator,
address _from,
uint256[] memory ids,
uint256[] memory values,
bytes memory data
) public virtual override returns (bytes4) {
IERC1155 instance = IERC1155(msg.sender);
require(
instance.supportsInterface(type(IERC1155).interfaceId),
'The caller does not support the IERC1155 interface'
);
if (autoFlush1155) {
instance.safeBatchTransferFrom(
address(this),
parentAddress,
ids,
values,
data
);
}
return this.onERC1155BatchReceived.selector;
}
/**
* @inheritdoc IForwarderV4
*/
function flushTokens(address tokenContractAddress)
external
virtual
override
onlyAllowedAddress
{
ERC20Interface instance = ERC20Interface(tokenContractAddress);
address forwarderAddress = address(this);
uint256 forwarderBalance = instance.balanceOf(forwarderAddress);
if (forwarderBalance == 0) {
return;
}
TransferHelper.safeTransfer(
tokenContractAddress,
parentAddress,
forwarderBalance
);
}
/**
* @inheritdoc IForwarderV4
*/
function flushERC721Token(address tokenContractAddress, uint256 tokenId)
external
virtual
override
onlyAllowedAddress
{
IERC721 instance = IERC721(tokenContractAddress);
require(
instance.supportsInterface(type(IERC721).interfaceId),
'The tokenContractAddress does not support the ERC721 interface'
);
address ownerAddress = instance.ownerOf(tokenId);
instance.transferFrom(ownerAddress, parentAddress, tokenId);
}
/**
* @inheritdoc IForwarderV4
*/
function flushERC1155Tokens(address tokenContractAddress, uint256 tokenId)
external
virtual
override
onlyAllowedAddress
{
IERC1155 instance = IERC1155(tokenContractAddress);
require(
instance.supportsInterface(type(IERC1155).interfaceId),
'The caller does not support the IERC1155 interface'
);
address forwarderAddress = address(this);
uint256 forwarderBalance = instance.balanceOf(forwarderAddress, tokenId);
instance.safeTransferFrom(
forwarderAddress,
parentAddress,
tokenId,
forwarderBalance,
''
);
}
/**
* @inheritdoc IForwarderV4
*/
function batchFlushERC1155Tokens(
address tokenContractAddress,
uint256[] calldata tokenIds
) external virtual override onlyAllowedAddress {
IERC1155 instance = IERC1155(tokenContractAddress);
require(
instance.supportsInterface(type(IERC1155).interfaceId),
'The caller does not support the IERC1155 interface'
);
address forwarderAddress = address(this);
uint256 length = tokenIds.length;
uint256[] memory amounts = new uint256[](tokenIds.length);
for (uint256 i; i < length; i++) {
amounts[i] = instance.balanceOf(forwarderAddress, tokenIds[i]);
}
instance.safeBatchTransferFrom(
forwarderAddress,
parentAddress,
tokenIds,
amounts,
''
);
}
/**
* @inheritdoc IForwarderV4
*/
function batchFlushERC20Tokens(address[] calldata tokenContractAddresses)
external
virtual
override
onlyAllowedAddress
{
address forwarderAddress = address(this);
uint256 length = tokenContractAddresses.length;
for (uint256 i; i < length; i++) {
ERC20Interface instance = ERC20Interface(tokenContractAddresses[i]);
uint256 forwarderBalance = instance.balanceOf(forwarderAddress);
if (forwarderBalance == 0) {
continue;
}
TransferHelper.safeTransfer(
tokenContractAddresses[i],
parentAddress,
forwarderBalance
);
}
}
/**
* @notice Flush the entire balance of the contract to the parent address.
*/
function flush() public {
uint256 value = address(this).balance;
if (value == 0) {
return;
}
emit ForwarderDeposited(msg.sender, value, msg.data);
(bool success, ) = parentAddress.call{ value: value }('');
require(success, 'Flush failed');
}
/**
* @inheritdoc IERC165
*/
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(ERC1155Holder, IERC165)
returns (bool)
{
return
interfaceId == type(IForwarderV4).interfaceId ||
super.supportsInterface(interfaceId);
}
}