-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTokenBuyer.sol
470 lines (385 loc) · 19.4 KB
/
TokenBuyer.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// SPDX-License-Identifier: GPL-3.0
/*********************************
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░█████████░░█████████░░░ *
* ░░░░░░██░░░████░░██░░░████░░░ *
* ░░██████░░░████████░░░████░░░ *
* ░░██░░██░░░████░░██░░░████░░░ *
* ░░██░░██░░░████░░██░░░████░░░ *
* ░░░░░░█████████░░█████████░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
*********************************/
pragma solidity ^0.8.17;
import { Ownable } from 'openzeppelin-contracts/contracts/access/Ownable.sol';
import { Pausable } from 'openzeppelin-contracts/contracts/security/Pausable.sol';
import { IERC20Metadata } from 'openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol';
import { SafeERC20 } from 'openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol';
import { ReentrancyGuard } from 'openzeppelin-contracts/contracts/security/ReentrancyGuard.sol';
import { Math } from 'openzeppelin-contracts/contracts/utils/math/Math.sol';
import { IPriceFeed } from './IPriceFeed.sol';
import { IBuyETHCallback } from './IBuyETHCallback.sol';
import { IPayer } from './IPayer.sol';
/// @title TokenBuyer
/// @notice Buys ERC20 tokens for ETH at oracle prices
/// It limits the amount of tokens it wants to buy using 2 factors:
/// 1. The amount of debt registered in a `Payer` contract
/// 2. A minimal "buffer" amount of tokens it wants to maintain
/// @dev Inspired by https://github.com/banteg/yfi-buyer
contract TokenBuyer is Ownable, Pausable, ReentrancyGuard {
using SafeERC20 for IERC20Metadata;
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
ERRORS
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
error FailedSendingETH(bytes data);
error FailedWithdrawingETH(bytes data);
error ReceivedInsufficientTokens(uint256 expected, uint256 actual);
error OnlyAdminOrOwner();
error InvalidBotDiscountBPs();
error InvalidBaselinePaymentTokenAmount();
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
EVENTS
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
event SoldETH(address indexed to, uint256 ethOut, uint256 tokenIn);
event BotDiscountBPsSet(uint16 oldBPs, uint16 newBPs);
event BaselinePaymentTokenAmountSet(uint256 oldAmount, uint256 newAmount);
event ETHWithdrawn(address indexed to, uint256 amount);
event MinAdminBotDiscountBPsSet(uint16 oldBPs, uint16 newBPs);
event MaxAdminBotDiscountBPsSet(uint16 oldBPs, uint16 newBPs);
event MinAdminBaselinePaymentTokenAmountSet(uint256 oldAmount, uint256 newAmount);
event MaxAdminBaselinePaymentTokenAmountSet(uint256 oldAmount, uint256 newAmount);
event PriceFeedSet(address oldFeed, address newFeed);
event PayerSet(address oldPayer, address newPayer);
event AdminSet(address oldAdmin, address newAdmin);
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
IMMUTABLES
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
uint256 public constant MAX_BPS = 10_000;
/// @notice The ERC20 token the owner of this contract wants to exchange for ETH
IERC20Metadata public immutable paymentToken;
/// @notice 10**paymentTokenDecimals, for the calculation for ETH price
uint256 public immutable paymentTokenDecimalsDigits;
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
STORAGE VARIABLES
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// @notice a `Payer` contract to which `TokenBuyer` sends the ERC20 tokens. Also used for checking how much debt there is
IPayer public payer;
/// @notice The contract used to fetch the price of ETH in `paymentToken`
IPriceFeed public priceFeed;
/// @notice The minimum `paymentToken` balance the `payer` contract should have
uint256 public baselinePaymentTokenAmount;
/// @notice The minimum allowed value for `baselinePaymentTokenAmount`
uint256 public minAdminBaselinePaymentTokenAmount;
/// @notice The maximum allowed value for `baselinePaymentTokenAmount`
uint256 public maxAdminBaselinePaymentTokenAmount;
/// @notice the amount of basis points to decrease the price by, to increase the incentive to transact with this contract
uint16 public botDiscountBPs;
/// @notice The minimum discount allowed in bps
uint16 public minAdminBotDiscountBPs;
/// @notice The maximum discount allowed in bps
uint16 public maxAdminBotDiscountBPs;
/// @notice Contract admin, allowed to do certain lower risk operations
address public admin;
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
MODIFIERS
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
modifier onlyAdminOrOwner() {
if (admin != msg.sender && owner() != msg.sender) {
revert OnlyAdminOrOwner();
}
_;
}
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
CONSTRUCTOR
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
constructor(
IPriceFeed _priceFeed,
uint256 _baselinePaymentTokenAmount,
uint256 _minAdminBaselinePaymentTokenAmount,
uint256 _maxAdminBaselinePaymentTokenAmount,
uint16 _botDiscountBPs,
uint16 _minAdminBotDiscountBPs,
uint16 _maxAdminBotDiscountBPs,
address _owner,
address _admin,
address _payer
) {
payer = IPayer(_payer);
address _paymentToken = address(payer.paymentToken());
paymentToken = IERC20Metadata(_paymentToken);
paymentTokenDecimalsDigits = 10**IERC20Metadata(_paymentToken).decimals();
priceFeed = _priceFeed;
baselinePaymentTokenAmount = _baselinePaymentTokenAmount;
minAdminBaselinePaymentTokenAmount = _minAdminBaselinePaymentTokenAmount;
maxAdminBaselinePaymentTokenAmount = _maxAdminBaselinePaymentTokenAmount;
if (
(_botDiscountBPs > MAX_BPS) ||
(_maxAdminBotDiscountBPs > MAX_BPS) ||
(_minAdminBotDiscountBPs > _maxAdminBotDiscountBPs)
) {
revert InvalidBotDiscountBPs();
}
botDiscountBPs = _botDiscountBPs;
minAdminBotDiscountBPs = _minAdminBotDiscountBPs;
maxAdminBotDiscountBPs = _maxAdminBotDiscountBPs;
_transferOwnership(_owner);
admin = _admin;
}
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
EXTERNAL TRANSACTIONS
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// @notice Allow ETH top ups
receive() external payable {}
/// @notice Buy ETH from this contract in exchange for `paymentToken` tokens.
/// The price is determined using `priceFeed` plus `botDiscountBPs`
/// Immediately invokes `payer` to pay back outstanding debt
/// @dev Caps `tokenAmount` by the amount of tokens the contract needs
/// @param tokenAmount the amount of ERC20 tokens msg.sender wishes to sell to this contract in exchange for ETH
function buyETH(uint256 tokenAmount) external nonReentrant whenNotPaused {
uint256 amount = Math.min(tokenAmount, tokenAmountNeeded());
// Cache payer
IPayer _payer = payer;
// Transfer tokens from msg.sender to `payer`
paymentToken.safeTransferFrom(msg.sender, address(_payer), amount);
// Invoke `payer` to pay back outstanding debt
_payer.payBackDebt(amount);
// Send msg.sender ETH
uint256 ethAmount = ethAmountPerTokenAmount(amount);
safeSendETH(msg.sender, ethAmount, '');
emit SoldETH(msg.sender, ethAmount, amount);
}
/// @notice Buy ETH from this contract in exchange for `paymentToken` tokens.
/// The price is determined using `priceFeed` plus `botDiscountBPs`
/// Immediately invokes `payer` to pay back outstanding debt
/// @dev First sends ETH by calling a callback, and then checks it received tokens.
/// This allowed the caller to swap the ETH for tokens instead of holding tokens in advance
/// @param tokenAmount the amount of ERC20 tokens msg.sender wishes to sell to this contract in exchange for ETH
/// @param to the address to send ETH to by calling the callback function on it
/// @param data arbitrary data passed through by the caller, usually used for callback verification
function buyETH(
uint256 tokenAmount,
address to,
bytes calldata data
) external nonReentrant whenNotPaused {
uint256 amount = Math.min(tokenAmount, tokenAmountNeeded());
IPayer _payer = payer;
// Starting balance of `payer`
uint256 balanceBefore = paymentToken.balanceOf(address(_payer));
// Send ETH to `to`
uint256 ethAmount = ethAmountPerTokenAmount(amount);
IBuyETHCallback(to).buyETHCallback{ value: ethAmount }(msg.sender, amount, data);
// Check that `payers` balance increased by the expected amount
uint256 tokensReceived = paymentToken.balanceOf(address(_payer)) - balanceBefore;
if (tokensReceived < amount) {
revert ReceivedInsufficientTokens(amount, tokensReceived);
}
// Invoke `payer` to pay back outstanding debt
_payer.payBackDebt(tokensReceived);
emit SoldETH(to, ethAmount, tokensReceived);
}
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
VIEW FUNCTIONS
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// @notice Get how much ETH this contract needs in order to fund its current obligations plus `additionalTokens`, with
/// a safety buffer `bufferBPs` basis points.
/// @param additionalTokens an additional amount of `paymentToken` liability to use in this ETH requirement calculation, in payment token decimals.
/// @param bufferBPs the number of basis points to add on top of the token liability price in ETH as a safety buffer, e.g.
/// if `bufferBPs` is 10K, the function will return twice the amount it needs according to price alone.
/// @return the amount of ETH needed
function ethNeeded(uint256 additionalTokens, uint256 bufferBPs) public view returns (uint256) {
uint256 tokenAmount = tokenAmountNeeded() + additionalTokens;
uint256 ethCostOfTokens = ethAmountPerTokenAmount(tokenAmount);
uint256 ethCostWithBuffer = (ethCostOfTokens * (bufferBPs + 10_000)) / 10_000;
if (address(this).balance > ethCostWithBuffer) {
return 0;
} else {
return ethCostWithBuffer - address(this).balance;
}
}
/// @notice Returns the amount of tokens this contract is willing to exchange of ETH
/// @return amount of tokens
function tokenAmountNeeded() public view returns (uint256) {
IPayer _payer = payer;
uint256 _tokensAvailable = paymentToken.balanceOf(address(_payer));
uint256 totalDebt = _payer.totalDebt();
unchecked {
uint256 neededTokens = baselinePaymentTokenAmount + totalDebt;
if (_tokensAvailable > neededTokens) {
return 0;
}
return neededTokens - _tokensAvailable;
}
}
/// @notice Returns the ETH/`paymentToken` price this contract is willing to exchange ETH at, including the discount
/// @return The price, in 18 decimal format
function price() public view returns (uint256) {
unchecked {
return (priceFeed.price() * (10_000 - botDiscountBPs)) / 10_000;
}
}
/// @notice Returns the amount of ETH this contract will send in exchange for `tokenAmount` tokens
/// @param tokenAmount the amount of tokens
/// @return amount of ETH the contract will sell for `tokenAmount` of tokens
function ethAmountPerTokenAmount(uint256 tokenAmount) public view returns (uint256) {
unchecked {
// Example:
// if tokenAmount == 3400000000 (3400 USDC) (6 decimals)
// and price() == 1745910000000000000000 (1745.91) (18 decimals)
// ((3400000000 * 1e36) / 1745910000000000000000) / 1e6 = 1.947408515e18 (3400/1745.91)
return ((tokenAmount * 1e36) / price()) / paymentTokenDecimalsDigits;
}
}
/// @notice Returns the amount of tokens the contract can buy and the amount of ETH it will pay for it
/// This takes into account the current ETH balance this contract has
/// @return tokenAmount amount of tokens the contract can buy
/// @return ethAmount amount of ETH it will pay for the tokens
function tokenAmountNeededAndETHPayout() public view returns (uint256, uint256) {
uint256 tokenAmount = tokenAmountNeeded();
uint256 ethAmount = ethAmountPerTokenAmount(tokenAmount);
uint256 ethAvailable = address(this).balance;
if (ethAvailable >= ethAmount) {
return (tokenAmount, ethAmount);
} else {
// Tokens amount will be rounded down to avoid trying to buy more eth than available
tokenAmount = tokenAmountPerEthAmount(ethAvailable);
// Recalculate eth amount because tokens amount are rounded down
ethAmount = ethAmountPerTokenAmount(tokenAmount);
return (tokenAmount, ethAmount);
}
}
/// @notice Returns the amount of tokens the contract expects in return for eth
/// @param ethAmount amount of ETH contract to be swapped
/// @return amount of tokens the contract will sell the ETH for
/// @dev result is rounded down
function tokenAmountPerEthAmount(uint256 ethAmount) public view returns (uint256) {
return (ethAmount * price() * paymentTokenDecimalsDigits) / 1e36;
}
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
ADMIN or OWNER TRANSACTIONS
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// @notice Update `botDiscountBPs`
function setBotDiscountBPs(uint16 newBotDiscountBPs) external onlyAdminOrOwner {
// Admin is limited to min-max range, owner is not
if (
admin == msg.sender &&
(newBotDiscountBPs < minAdminBotDiscountBPs || newBotDiscountBPs > maxAdminBotDiscountBPs)
) {
revert InvalidBotDiscountBPs();
}
emit BotDiscountBPsSet(botDiscountBPs, newBotDiscountBPs);
botDiscountBPs = newBotDiscountBPs;
}
/// @notice Update `baselinePaymentTokenAmount`
/// @param newBaselinePaymentTokenAmount the new `baselinePaymentTokenAmount` in token decimals.
function setBaselinePaymentTokenAmount(uint256 newBaselinePaymentTokenAmount) external onlyAdminOrOwner {
// Admin is limited to min-max range, owner is not
if (
admin == msg.sender &&
(newBaselinePaymentTokenAmount < minAdminBaselinePaymentTokenAmount ||
newBaselinePaymentTokenAmount > maxAdminBaselinePaymentTokenAmount)
) {
revert InvalidBaselinePaymentTokenAmount();
}
emit BaselinePaymentTokenAmountSet(baselinePaymentTokenAmount, newBaselinePaymentTokenAmount);
baselinePaymentTokenAmount = newBaselinePaymentTokenAmount;
}
/// @notice pause ETH buying
function pause() external onlyAdminOrOwner {
_pause();
}
/// @notice unpause ETH buying
function unpause() external onlyAdminOrOwner {
_unpause();
}
/// @notice Withdraw all ETH to the contract owner
function withdrawETH() external onlyAdminOrOwner {
uint256 amount = address(this).balance;
address to = owner();
(bool sent, bytes memory data) = to.call{ value: amount }('');
if (!sent) {
revert FailedWithdrawingETH(data);
}
emit ETHWithdrawn(to, amount);
}
/// @notice set a new Admin
function setAdmin(address newAdmin) external onlyAdminOrOwner {
emit AdminSet(admin, newAdmin);
admin = newAdmin;
}
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
OWNER TRANSACTIONS
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// @notice Update minAdminBotDiscountBPs
function setMinAdminBotDiscountBPs(uint16 newMinAdminBotDiscountBPs) external onlyOwner {
emit MinAdminBotDiscountBPsSet(minAdminBotDiscountBPs, newMinAdminBotDiscountBPs);
minAdminBotDiscountBPs = newMinAdminBotDiscountBPs;
}
/// @notice Update maxAdminBotDiscountBPs
function setMaxAdminBotDiscountBPs(uint16 newMaxAdminBotDiscountBPs) external onlyOwner {
emit MaxAdminBotDiscountBPsSet(maxAdminBotDiscountBPs, newMaxAdminBotDiscountBPs);
maxAdminBotDiscountBPs = newMaxAdminBotDiscountBPs;
}
/// @notice Update minAdminBaselinePaymentTokenAmount
function setMinAdminBaselinePaymentTokenAmount(uint256 newMinAdminBaselinePaymentTokenAmount) external onlyOwner {
emit MinAdminBaselinePaymentTokenAmountSet(
minAdminBaselinePaymentTokenAmount,
newMinAdminBaselinePaymentTokenAmount
);
minAdminBaselinePaymentTokenAmount = newMinAdminBaselinePaymentTokenAmount;
}
/// @notice Update maxAdminBaselinePaymentTokenAmount
function setMaxAdminBaselinePaymentTokenAmount(uint256 newMaxAdminBaselinePaymentTokenAmount) external onlyOwner {
emit MaxAdminBaselinePaymentTokenAmountSet(
maxAdminBaselinePaymentTokenAmount,
newMaxAdminBaselinePaymentTokenAmount
);
maxAdminBaselinePaymentTokenAmount = newMaxAdminBaselinePaymentTokenAmount;
}
/// @notice Update priceFeed
function setPriceFeed(IPriceFeed newPriceFeed) external onlyOwner {
emit PriceFeedSet(address(priceFeed), address(newPriceFeed));
priceFeed = newPriceFeed;
}
/// @notice Update `payer`
function setPayer(address newPayer) external onlyOwner {
emit PayerSet(address(payer), newPayer);
payer = IPayer(newPayer);
}
/**
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
INTERNAL FUNCTIONS
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
function safeSendETH(
address to,
uint256 ethAmount,
bytes memory data
) internal {
// If contract balance is insufficient it reverts
(bool sent, bytes memory returnData) = to.call{ value: ethAmount }(data);
if (!sent) {
revert FailedSendingETH(returnData);
}
}
}