-
Notifications
You must be signed in to change notification settings - Fork 6
/
Fulfiller.sol
364 lines (293 loc) · 12.7 KB
/
Fulfiller.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
// Inheritances
import {IFulfiller} from "./IFulfiller.sol";
import {IFulfillerWithCallback} from "./extensions/IFulfillerWithCallback.sol";
import {Ownable2Step} from "@openzeppelin/access/Ownable2Step.sol";
import {Pausable} from "@openzeppelin/security/Pausable.sol";
import {ReentrancyGuard} from "@openzeppelin/security/ReentrancyGuard.sol";
// Libraries
import {SafeERC20} from "@openzeppelin/token/ERC20/utils/SafeERC20.sol";
import {Address} from "@openzeppelin/utils/Address.sol";
// Interfaces
import {IFloodPlain} from "../flood-plain/IFloodPlain.sol";
import {IExecutor} from "../executors/IExecutor.sol";
import {IERC20} from "@openzeppelin/token/ERC20/IERC20.sol";
contract Fulfiller is IFulfiller, IFulfillerWithCallback, Ownable2Step, Pausable, ReentrancyGuard {
using SafeERC20 for IERC20;
using Address for address payable;
address public immutable ZONE;
ExecutorInfo[] internal _executors;
mapping(address => bool) internal _books;
CallbackInfo internal _callbackInfo;
constructor(address zone) {
if (zone.code.length == 0) {
revert NotAContract();
}
ZONE = zone;
_callbackInfo.alwaysTrue = true;
}
function getExecutor(uint256 executorId) external view returns (ExecutorInfo memory /* executorInfo */ ) {
return _executors[executorId];
}
function getBookValidity(address book) external view returns (bool /* enabled */ ) {
return _books[book];
}
function pause() external onlyOwner {
_pause();
}
function unpause() external onlyOwner {
_unpause();
}
function addExecutor(address executor) external onlyOwner returns (uint256 executorId) {
ExecutorInfo memory executorInfo =
ExecutorInfo({executor: executor, hasCallback: IExecutor(executor).hasCallback(), isEnabled: true});
executorId = _executors.length;
_executors.push(executorInfo);
emit ExecutorAdded(executorId, executor);
}
function disableExecutor(uint256 executorId) external onlyOwner {
_executors[executorId].isEnabled = false;
emit ExecutorDisabled(executorId);
}
function enableExecutor(uint256 executorId) external onlyOwner {
_executors[executorId].isEnabled = true;
emit ExecutorEnabled(executorId);
}
function disableBook(address book) external onlyOwner {
_books[book] = false;
emit BookDisabled(book);
}
function enableBook(address book) external onlyOwner {
_books[book] = true;
emit BookEnabled(book);
}
function batchWithdraw(IFloodPlain.Item[] calldata items) external onlyOwner {
uint256 length = items.length;
IFloodPlain.Item calldata item;
for (uint256 i; i < length;) {
item = items[i];
if (item.token == address(0)) {
payable(msg.sender).sendValue(item.amount);
} else {
IERC20(item.token).safeTransfer(msg.sender, item.amount);
}
unchecked {
++i;
}
}
}
function pay() external payable onlyOwner {}
function sourceConsideration(
IFloodPlain.Order calldata order,
IFloodPlain.Item[] calldata requestedItems,
address, /* caller */
bytes calldata context
) external whenNotPaused nonReentrant returns (uint256[] memory) {
if (!_books[msg.sender]) {
revert InvalidBook();
}
if (order.zone != ZONE) {
revert InvalidZone();
}
uint256 itemsLength = requestedItems.length;
uint256[] memory gatheredAmounts = new uint256[](itemsLength);
// Book must have sent offer items prior to this call. The swap data is ought to use the
// received offer items. However, this fulfiller will not have any checks to ensure not
// more than the received offer items are spent. It will therefore trust `caller` to supply
// honest swap data. The caller is a trusted address and the access control is enforced
// through the zone.
// Record requested item balances before the swaps.
address token;
for (uint256 i; i < itemsLength;) {
token = requestedItems[i].token;
if (token == address(0)) {
gatheredAmounts[i] = address(this).balance;
} else {
gatheredAmounts[i] = IERC20(token).balanceOf(address(this));
}
unchecked {
++i;
}
}
// Execute swaps based on the swap instructions provided.
_executeSwaps(context);
// Get the gathered amounts, and approve book to spend them.
uint256 gatheredAmount;
for (uint256 i; i < itemsLength;) {
token = requestedItems[i].token;
if (token == address(0)) {
gatheredAmount = address(this).balance - gatheredAmounts[i];
payable(msg.sender).sendValue(gatheredAmount);
} else {
gatheredAmount = IERC20(token).balanceOf(address(this)) - gatheredAmounts[i];
IERC20(token).safeIncreaseAllowance(msg.sender, gatheredAmount);
}
gatheredAmounts[i] = gatheredAmount;
unchecked {
++i;
}
}
// Let Book pull the tokens and send to offerer. Book checks if gathered amounts cover
// requested amounts.
return gatheredAmounts;
}
fallback() external payable {
_fallback();
}
receive() external payable {
_fallback();
}
function _executeSwaps(bytes calldata swaps) internal {
// Pointer is incremented with each loop.
uint256 ptr = 0;
assembly {
ptr := swaps.offset
}
// Variables reset at each iteration, defined outside the loop to save gas.
IExecutor.Swap memory swap;
bytes memory swapData;
uint64 executorId;
ExecutorInfo memory executorInfo;
CallbackInfo storage callbackInfo = _callbackInfo;
address executor;
bool hasCallback;
// Execute a swap with each iteration.
while (msg.data.length > ptr) {
// Break the loop if only padding is left.
unchecked {
if (msg.data.length - ptr < 32) {
bool paddingRemainingOnly;
assembly {
paddingRemainingOnly := iszero(calldataload(ptr))
}
if (paddingRemainingOnly) break;
}
}
// Decode first swap instructions from ptr, ensuring executor is not disabled.
(ptr, executorId, swap) = _decodeSwap(ptr);
// Get executor details.
executorInfo = _executors[executorId];
executor = executorInfo.executor;
// Ensure executor is not disabled.
if (!executorInfo.isEnabled) {
revert DisabledExecutor();
}
// If executor requires a callback...
hasCallback = executorInfo.hasCallback;
if (hasCallback) {
// Set callback info pseudo-transient storage accordingly.
callbackInfo.expectingCallback = true;
callbackInfo.activeExecutorId = executorId;
callbackInfo.callbackSource = IExecutor(executor).getCallbackSource(swap);
}
// Construct calldata to pass to the executor.
swapData = abi.encodeWithSelector(IExecutor.swap.selector, swap);
assembly {
// Delegate swap execution to the executor.
let result := delegatecall(gas(), executor, add(swapData, 0x20), mload(swapData), 0, 0)
if iszero(result) {
// Copy the returned data.
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
}
}
// If executor had a callback...
if (hasCallback) {
// Unset callback info pseudo-transient storage after the swap is completed.
callbackInfo.expectingCallback = false;
callbackInfo.activeExecutorId = 0;
callbackInfo.callbackSource = address(0);
}
}
}
// Swaps are series of packets which follow the following scheme:
//
// * 1 byte - executor Id
// * 1 byte - bytes required for amountIn and amountOut
// 1 nibble - bytes required for amountIn, subtracted by one
// 1 nibble - bytes required for amountOut, subtracted by one
// * n bytes - amount in (this fulfiller uses max-in-exact-out slippage control scheme)
// * m bytes - amount out
// * 20 bytes - pool address to perform the swap
// * 1 byte - indices of token in & token out in the pool
// 1 nibble - index of token in
// 1 nibble - index of token out
//
// Caveat: Token indices in a pool can change when tokens are added or removed from a
// multi-token pool. This can theoretically be abused by the pool owner to steal funds
// from the fulfiller by frontrunning a swap. This is an accepted risk.
function _decodeSwap(uint256 ptr)
internal
pure
returns (uint256 endPtr, uint64 executorId, IExecutor.Swap memory swap)
{
address pool;
uint256 tokenInIndex;
uint256 tokenOutIndex;
uint256 amountIn;
uint256 amountOut;
// Decode instructions based on the above-described scheme.
assembly {
executorId := shr(248, calldataload(ptr))
ptr := add(ptr, 1)
let amountsSizes := calldataload(ptr)
ptr := add(ptr, 1)
let amountInSize := add(shr(252, amountsSizes), 1)
let amountOutSize := add(and(shr(248, amountsSizes), 0x0f), 1)
amountIn := shr(sub(256, shl(3, amountInSize)), calldataload(ptr))
ptr := add(ptr, amountInSize)
amountOut := shr(sub(256, shl(3, amountOutSize)), calldataload(ptr))
ptr := add(ptr, amountOutSize)
pool := shr(96, calldataload(ptr))
ptr := add(ptr, 20)
let tokenIndices := calldataload(ptr)
endPtr := add(ptr, 1)
tokenInIndex := shr(252, tokenIndices)
tokenOutIndex := and(shr(248, tokenIndices), 0x0f)
}
swap = IExecutor.Swap({
pool: pool,
tokenInIndex: tokenInIndex,
tokenOutIndex: tokenOutIndex,
amountIn: amountIn,
amountOut: amountOut
});
}
function _fallback() internal {
CallbackInfo memory callbackInfo = _callbackInfo;
if (!callbackInfo.expectingCallback) {
revert FallbackNotThroughExecutor();
} else {
// Ensure only the pool can callback.
if (callbackInfo.callbackSource != msg.sender) {
revert CallbackNotByPool();
}
// Get executor address corresponding to an identifier.
address executor = _executors[callbackInfo.activeExecutorId].executor;
// When a swap is going through the executor, a call made the Fulfiller is actually
// a callback to the executor. So we delegatecall to the active executor to continue
// completing the swap. Note that if a callback function has a signature clash with an
// existing function in this contract, the fallback will not be reached. Instead of
// having a custom function dispatcher to prevent this issue, we have accepted the risk
// of potentially having an incompatible pool with this fulfiller. If we encounter such
// a pool, we can then think of writing another fulfiller with a workaround. As long as
// there is no security risks currently, then it should be fine.
assembly {
// Copy msg.data. We take full control of memory in this inline assembly
// block because it will not return to Solidity code. We overwrite the
// Solidity scratch pad at memory position 0.
calldatacopy(0, 0, calldatasize())
// Call the implementation.
// out and outsize are 0 because we don't know the size yet.
let result := delegatecall(gas(), executor, 0, calldatasize(), 0, 0)
// Copy the returned data.
returndatacopy(0, 0, returndatasize())
switch result
// delegatecall returns 0 on error.
case 0 { revert(0, returndatasize()) }
default { return(0, returndatasize()) }
}
}
}
}