generated from bgd-labs/bgd-forge-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
BaseParaSwapBuyAdapter.sol
112 lines (97 loc) · 4.81 KB
/
BaseParaSwapBuyAdapter.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import {PercentageMath} from '@aave/core-v3/contracts/protocol/libraries/math/PercentageMath.sol';
import {IPoolAddressesProvider} from '@aave/core-v3/contracts/interfaces/IPoolAddressesProvider.sol';
import {IERC20Detailed} from '@aave/core-v3/contracts/dependencies/openzeppelin/contracts/IERC20Detailed.sol';
import {SafeERC20} from '@aave/core-v3/contracts/dependencies/openzeppelin/contracts/SafeERC20.sol';
import {IParaSwapAugustus} from '../interfaces/IParaSwapAugustus.sol';
import {IParaSwapAugustusRegistry} from '../interfaces/IParaSwapAugustusRegistry.sol';
import {BaseParaSwapAdapter} from './BaseParaSwapAdapter.sol';
/**
* @title BaseParaSwapBuyAdapter
* @notice Implements the logic for buying tokens on ParaSwap
*/
abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
using SafeERC20 for IERC20Detailed;
using PercentageMath for uint256;
IParaSwapAugustusRegistry public immutable AUGUSTUS_REGISTRY;
constructor(
IPoolAddressesProvider addressesProvider,
address pool,
IParaSwapAugustusRegistry augustusRegistry
) BaseParaSwapAdapter(addressesProvider, pool) {
// Do something on Augustus registry to check the right contract was passed
require(!augustusRegistry.isValidAugustus(address(0)), 'Not a valid Augustus address');
AUGUSTUS_REGISTRY = augustusRegistry;
}
/**
* @dev Swaps a token for another using ParaSwap
* @param toAmountOffset Offset of toAmount in Augustus calldata if it should be overwritten, otherwise 0
* @param paraswapData Data for Paraswap Adapter
* @param assetToSwapFrom Address of the asset to be swapped from
* @param assetToSwapTo Address of the asset to be swapped to
* @param maxAmountToSwap Max amount to be swapped
* @param amountToReceive Amount to be received from the swap
* @return amountSold The amount sold during the swap
* @return amountBought The amount bought during the swap
*/
function _buyOnParaSwap(
uint256 toAmountOffset,
bytes memory paraswapData,
IERC20Detailed assetToSwapFrom,
IERC20Detailed assetToSwapTo,
uint256 maxAmountToSwap,
uint256 amountToReceive
) internal returns (uint256 amountSold, uint256 amountBought) {
(bytes memory buyCalldata, IParaSwapAugustus augustus) = abi.decode(
paraswapData,
(bytes, IParaSwapAugustus)
);
require(AUGUSTUS_REGISTRY.isValidAugustus(address(augustus)), 'INVALID_AUGUSTUS');
{
uint256 fromAssetDecimals = _getDecimals(assetToSwapFrom);
uint256 toAssetDecimals = _getDecimals(assetToSwapTo);
uint256 fromAssetPrice = _getPrice(address(assetToSwapFrom));
uint256 toAssetPrice = _getPrice(address(assetToSwapTo));
uint256 expectedMaxAmountToSwap = ((amountToReceive *
(toAssetPrice * (10 ** fromAssetDecimals))) / (fromAssetPrice * (10 ** toAssetDecimals)))
.percentMul(PercentageMath.PERCENTAGE_FACTOR + MAX_SLIPPAGE_PERCENT);
require(maxAmountToSwap <= expectedMaxAmountToSwap, 'maxAmountToSwap exceed max slippage');
}
uint256 balanceBeforeAssetFrom = assetToSwapFrom.balanceOf(address(this));
require(balanceBeforeAssetFrom >= maxAmountToSwap, 'INSUFFICIENT_BALANCE_BEFORE_SWAP');
uint256 balanceBeforeAssetTo = assetToSwapTo.balanceOf(address(this));
address tokenTransferProxy = augustus.getTokenTransferProxy();
assetToSwapFrom.safeApprove(tokenTransferProxy, maxAmountToSwap);
if (toAmountOffset != 0) {
// Ensure 256 bit (32 bytes) toAmountOffset value is within bounds of the
// calldata, not overlapping with the first 4 bytes (function selector).
require(
toAmountOffset >= 4 && toAmountOffset <= buyCalldata.length - 32,
'TO_AMOUNT_OFFSET_OUT_OF_RANGE'
);
// Overwrite the toAmount with the correct amount for the buy.
// In memory, buyCalldata consists of a 256 bit length field, followed by
// the actual bytes data, that is why 32 is added to the byte offset.
assembly {
mstore(add(buyCalldata, add(toAmountOffset, 32)), amountToReceive)
}
}
(bool success, ) = address(augustus).call(buyCalldata);
if (!success) {
// Copy revert reason from call
assembly {
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
}
}
// Reset allowance
assetToSwapFrom.safeApprove(tokenTransferProxy, 0);
uint256 balanceAfterAssetFrom = assetToSwapFrom.balanceOf(address(this));
amountSold = balanceBeforeAssetFrom - balanceAfterAssetFrom;
require(amountSold <= maxAmountToSwap, 'WRONG_BALANCE_AFTER_SWAP');
amountBought = assetToSwapTo.balanceOf(address(this)) - balanceBeforeAssetTo;
require(amountBought >= amountToReceive, 'INSUFFICIENT_AMOUNT_RECEIVED');
emit Bought(address(assetToSwapFrom), address(assetToSwapTo), amountSold, amountBought);
}
}