forked from sherlock-audit/2023-04-splits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwapperImpl.sol
282 lines (225 loc) · 10.1 KB
/
SwapperImpl.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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.17;
import {ERC20} from "solmate/tokens/ERC20.sol";
import {OracleImpl} from "splits-oracle/OracleImpl.sol";
import {PausableImpl} from "splits-utils/PausableImpl.sol";
import {SafeCastLib} from "solady/utils/SafeCastLib.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {TokenUtils} from "splits-utils/TokenUtils.sol";
import {WalletImpl} from "splits-utils/WalletImpl.sol";
import {ISwapperFlashCallback} from "./interfaces/ISwapperFlashCallback.sol";
/// @title Swapper Implementation
/// @author 0xSplits
/// @notice A contract to trustlessly & automatically convert multi-token
/// onchain revenue into a single token
/// Please be aware, owner has _FULL CONTROL_ of the deployment.
/// @dev This contract uses a modular oracle. Be very careful to use a secure
/// oracle with sensible defaults & overrides for desired behavior. Otherwise
/// may result in catastrophic loss of funds.
/// This contract uses token = address(0) to refer to ETH.
contract SwapperImpl is WalletImpl, PausableImpl {
/// -----------------------------------------------------------------------
/// libraries
/// -----------------------------------------------------------------------
using SafeTransferLib for address;
using SafeCastLib for uint256;
using TokenUtils for address;
/// -----------------------------------------------------------------------
/// errors
/// -----------------------------------------------------------------------
error Invalid_AmountsToBeneficiary();
error Invalid_QuoteToken();
error InsufficientFunds_InContract();
error InsufficientFunds_FromTrader();
/// -----------------------------------------------------------------------
/// structs
/// -----------------------------------------------------------------------
struct InitParams {
address owner;
bool paused;
address beneficiary;
address tokenToBeneficiary;
OracleImpl oracle;
}
/// -----------------------------------------------------------------------
/// events
/// -----------------------------------------------------------------------
event SetBeneficiary(address beneficiary);
event SetTokenToBeneficiary(address tokenToBeneficiary);
event SetOracle(OracleImpl oracle);
event ReceiveETH(uint256 amount);
event Payback(address indexed payer, uint256 amount);
event Flash(
address indexed trader,
OracleImpl.QuoteParams[] quoteParams,
address tokenToBeneficiary,
uint256[] amountsToBeneficiary,
uint256 excessToBeneficiary
);
/// -----------------------------------------------------------------------
/// storage
/// -----------------------------------------------------------------------
/// -----------------------------------------------------------------------
/// storage - constants & immutables
/// -----------------------------------------------------------------------
address public immutable swapperFactory;
/// -----------------------------------------------------------------------
/// storage - mutables
/// -----------------------------------------------------------------------
/// slot 0 - 11 bytes free
/// OwnableImpl storage
/// address internal $owner;
/// 20 bytes
/// PausableImpl storage
/// bool internal $paused;
/// 1 byte
/// slot 1 - 0 bytes free
/// address to receive post-swap tokens
address internal $beneficiary;
/// 20 bytes
/// used to track eth payback in flash
uint96 internal $_payback;
/// 12 bytes
/// slot 2 - 12 bytes free
/// token type to send beneficiary
/// @dev 0x0 used for ETH
address internal $tokenToBeneficiary;
/// 20 bytes
/// slot 3 - 12 bytes free
/// price oracle for flash
OracleImpl internal $oracle;
/// 20 bytes
/// -----------------------------------------------------------------------
/// constructor & initializer
/// -----------------------------------------------------------------------
constructor() {
swapperFactory = msg.sender;
}
function initializer(InitParams calldata params_) external {
// only swapperFactory may call `initializer`
if (msg.sender != swapperFactory) revert Unauthorized();
// don't need to init wallet separately
__initPausable({owner_: params_.owner, paused_: params_.paused});
$beneficiary = params_.beneficiary;
$tokenToBeneficiary = params_.tokenToBeneficiary;
$oracle = params_.oracle;
}
/// -----------------------------------------------------------------------
/// functions
/// -----------------------------------------------------------------------
/// -----------------------------------------------------------------------
/// functions - public & external
/// -----------------------------------------------------------------------
/// -----------------------------------------------------------------------
/// functions - public & external - onlyOwner
/// -----------------------------------------------------------------------
/// set beneficiary
function setBeneficiary(address beneficiary_) external onlyOwner {
$beneficiary = beneficiary_;
emit SetBeneficiary(beneficiary_);
}
/// set tokenToBeneficiary
function setTokenToBeneficiary(address tokenToBeneficiary_) external onlyOwner {
$tokenToBeneficiary = tokenToBeneficiary_;
emit SetTokenToBeneficiary(tokenToBeneficiary_);
}
/// set oracle
function setOracle(OracleImpl oracle_) external onlyOwner {
$oracle = oracle_;
emit SetOracle(oracle_);
}
/// -----------------------------------------------------------------------
/// functions - public & external - view
/// -----------------------------------------------------------------------
function beneficiary() external view returns (address) {
return $beneficiary;
}
function tokenToBeneficiary() external view returns (address) {
return $tokenToBeneficiary;
}
function oracle() external view returns (OracleImpl) {
return $oracle;
}
/// -----------------------------------------------------------------------
/// functions - public & external - permissionless
/// -----------------------------------------------------------------------
/// emit event when receiving ETH
/// @dev implemented w/i clone bytecode
/* receive() external payable { */
/* emit ReceiveETH(msg.value); */
/* } */
/// allows flash to track eth payback to beneficiary
/// @dev if used outside swapperFlashCallback, msg.sender may lose funds
/// accumulates until next flash call
function payback() external payable {
$_payback += msg.value.toUint96();
emit Payback(msg.sender, msg.value);
}
/// allow third parties to withdraw tokens in return for sending tokenToBeneficiary to beneficiary
function flash(OracleImpl.QuoteParams[] calldata quoteParams_, bytes calldata callbackData_)
external
payable
pausable
{
address _tokenToBeneficiary = $tokenToBeneficiary;
(uint256 amountToBeneficiary, uint256[] memory amountsToBeneficiary) =
_transferToTrader(_tokenToBeneficiary, quoteParams_);
ISwapperFlashCallback(msg.sender).swapperFlashCallback({
tokenToBeneficiary: _tokenToBeneficiary,
amountToBeneficiary: amountToBeneficiary,
data: callbackData_
});
uint256 excessToBeneficiary = _transferToBeneficiary(_tokenToBeneficiary, amountToBeneficiary);
emit Flash(msg.sender, quoteParams_, _tokenToBeneficiary, amountsToBeneficiary, excessToBeneficiary);
}
/// -----------------------------------------------------------------------
/// functions - private & internal
/// -----------------------------------------------------------------------
function _transferToTrader(address tokenToBeneficiary_, OracleImpl.QuoteParams[] calldata quoteParams_)
internal
returns (uint256 amountToBeneficiary, uint256[] memory amountsToBeneficiary)
{
amountsToBeneficiary = $oracle.getQuoteAmounts(quoteParams_);
uint256 length = quoteParams_.length;
if (amountsToBeneficiary.length != length) revert Invalid_AmountsToBeneficiary();
uint128 amountToTrader;
address tokenToTrader;
for (uint256 i; i < length;) {
OracleImpl.QuoteParams calldata qp = quoteParams_[i];
if (tokenToBeneficiary_ != qp.quotePair.quote) revert Invalid_QuoteToken();
tokenToTrader = qp.quotePair.base;
amountToTrader = qp.baseAmount;
if (amountToTrader > tokenToTrader._balanceOf(address(this))) {
revert InsufficientFunds_InContract();
}
amountToBeneficiary += amountsToBeneficiary[i];
tokenToTrader._safeTransfer(msg.sender, amountToTrader);
unchecked {
++i;
}
}
}
function _transferToBeneficiary(address tokenToBeneficiary_, uint256 amountToBeneficiary_)
internal
returns (uint256 excessToBeneficiary)
{
address _beneficiary = $beneficiary;
if (tokenToBeneficiary_._isETH()) {
if ($_payback < amountToBeneficiary_) {
revert InsufficientFunds_FromTrader();
}
$_payback = 0;
// send eth to beneficiary
uint256 ethBalance = address(this).balance;
excessToBeneficiary = ethBalance - amountToBeneficiary_;
_beneficiary.safeTransferETH(ethBalance);
} else {
tokenToBeneficiary_.safeTransferFrom(msg.sender, _beneficiary, amountToBeneficiary_);
// flush excess tokenToBeneficiary to beneficiary
excessToBeneficiary = ERC20(tokenToBeneficiary_).balanceOf(address(this));
if (excessToBeneficiary > 0) {
tokenToBeneficiary_.safeTransfer(_beneficiary, excessToBeneficiary);
}
}
}
}