forked from BitGo/eth-multisig-v4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Forwarder.sol
325 lines (288 loc) · 8.21 KB
/
Forwarder.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
// 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 './IForwarder.sol';
/**
* Contract that will forward any incoming Ether to the creator of the contract
*
*/
contract Forwarder is IERC721Receiver, ERC1155Holder, IForwarder {
// Address to which any funds sent to this contract will be forwarded
address public parentAddress;
bool public autoFlush721 = true;
bool public autoFlush1155 = true;
event ForwarderDeposited(address from, uint256 value, bytes data);
/**
* Initialize the contract, and sets the destination address to that of the creator
*/
function init(
address _parentAddress,
bool _autoFlush721,
bool _autoFlush1155
) external onlyUninitialized {
parentAddress = _parentAddress;
uint256 value = address(this).balance;
// set whether we want to automatically flush erc721/erc1155 tokens or not
autoFlush721 = _autoFlush721;
autoFlush1155 = _autoFlush1155;
if (value == 0) {
return;
}
// NOTE: 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');
}
/**
* Modifier that will execute internal code block only if the sender is the parent address
*/
modifier onlyParent() {
require(msg.sender == parentAddress, 'Only Parent');
_;
}
/**
* Modifier that will execute internal code block only if the contract has not been initialized yet
*/
modifier onlyUninitialized() {
require(parentAddress == address(0x0), 'Already initialized');
_;
}
/**
* Default function; Gets called when data is sent but does not match any other function
*/
fallback() external payable {
flush();
}
/**
* Default function; Gets called when Ether is deposited with no data, and forwards it to the parent address
*/
receive() external payable {
flush();
}
/**
* @inheritdoc IForwarder
*/
function setAutoFlush721(bool autoFlush)
external
virtual
override
onlyParent
{
autoFlush721 = autoFlush;
}
/**
* @inheritdoc IForwarder
*/
function setAutoFlush1155(bool autoFlush)
external
virtual
override
onlyParent
{
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;
}
function callFromParent(
address target,
uint256 value,
bytes calldata data
) external onlyParent returns (bytes memory) {
(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 IForwarder
*/
function flushTokens(address tokenContractAddress)
external
virtual
override
onlyParent
{
ERC20Interface instance = ERC20Interface(tokenContractAddress);
address forwarderAddress = address(this);
uint256 forwarderBalance = instance.balanceOf(forwarderAddress);
if (forwarderBalance == 0) {
return;
}
TransferHelper.safeTransfer(
tokenContractAddress,
parentAddress,
forwarderBalance
);
}
/**
* @inheritdoc IForwarder
*/
function flushERC721Token(address tokenContractAddress, uint256 tokenId)
external
virtual
override
onlyParent
{
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 IForwarder
*/
function flushERC1155Tokens(address tokenContractAddress, uint256 tokenId)
external
virtual
override
onlyParent
{
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 IForwarder
*/
function batchFlushERC1155Tokens(
address tokenContractAddress,
uint256[] calldata tokenIds
) external virtual override onlyParent {
IERC1155 instance = IERC1155(tokenContractAddress);
require(
instance.supportsInterface(type(IERC1155).interfaceId),
'The caller does not support the IERC1155 interface'
);
address forwarderAddress = address(this);
uint256[] memory amounts = new uint256[](tokenIds.length);
for (uint256 i = 0; i < tokenIds.length; i++) {
amounts[i] = instance.balanceOf(forwarderAddress, tokenIds[i]);
}
instance.safeBatchTransferFrom(
forwarderAddress,
parentAddress,
tokenIds,
amounts,
''
);
}
/**
* 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(IForwarder).interfaceId ||
super.supportsInterface(interfaceId);
}
}