|
| 1 | +// SPDX-License-Identifier: agpl-3.0 |
| 2 | +pragma solidity 0.6.12; |
| 3 | +pragma experimental ABIEncoderV2; |
| 4 | + |
| 5 | +import {BaseParaSwapAdapter} from './BaseParaSwapAdapter.sol'; |
| 6 | +import {PercentageMath} from '../protocol/libraries/math/PercentageMath.sol'; |
| 7 | +import {IParaSwapAugustus} from '../interfaces/IParaSwapAugustus.sol'; |
| 8 | +import {IParaSwapAugustusRegistry} from '../interfaces/IParaSwapAugustusRegistry.sol'; |
| 9 | +import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; |
| 10 | +import {IERC20Detailed} from '../dependencies/openzeppelin/contracts/IERC20Detailed.sol'; |
| 11 | + |
| 12 | +/** |
| 13 | + * @title BaseParaSwapSellAdapter |
| 14 | + * @notice Implements the logic for selling tokens on ParaSwap |
| 15 | + * @author Jason Raymond Bell |
| 16 | + */ |
| 17 | +abstract contract BaseParaSwapSellAdapter is BaseParaSwapAdapter { |
| 18 | + using PercentageMath for uint256; |
| 19 | + |
| 20 | + IParaSwapAugustusRegistry public immutable AUGUSTUS_REGISTRY; |
| 21 | + |
| 22 | + constructor( |
| 23 | + ILendingPoolAddressesProvider addressesProvider, |
| 24 | + IParaSwapAugustusRegistry augustusRegistry |
| 25 | + ) public BaseParaSwapAdapter(addressesProvider) { |
| 26 | + // Do something on Augustus registry to check the right contract was passed |
| 27 | + require(!augustusRegistry.isValidAugustus(address(0))); |
| 28 | + AUGUSTUS_REGISTRY = augustusRegistry; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @dev Swaps a token for another using ParaSwap |
| 33 | + * @param fromAmountOffset Offset of fromAmount in Augustus calldata if it should be overwritten, otherwise 0 |
| 34 | + * @param swapCalldata Calldata for ParaSwap's AugustusSwapper contract |
| 35 | + * @param augustus Address of ParaSwap's AugustusSwapper contract |
| 36 | + * @param assetToSwapFrom Address of the asset to be swapped from |
| 37 | + * @param assetToSwapTo Address of the asset to be swapped to |
| 38 | + * @param amountToSwap Amount to be swapped |
| 39 | + * @param minAmountToReceive Minimum amount to be received from the swap |
| 40 | + * @return amountReceived The amount received from the swap |
| 41 | + */ |
| 42 | + function _sellOnParaSwap( |
| 43 | + uint256 fromAmountOffset, |
| 44 | + bytes memory swapCalldata, |
| 45 | + IParaSwapAugustus augustus, |
| 46 | + IERC20Detailed assetToSwapFrom, |
| 47 | + IERC20Detailed assetToSwapTo, |
| 48 | + uint256 amountToSwap, |
| 49 | + uint256 minAmountToReceive |
| 50 | + ) internal returns (uint256 amountReceived) { |
| 51 | + require(AUGUSTUS_REGISTRY.isValidAugustus(address(augustus)), 'INVALID_AUGUSTUS'); |
| 52 | + |
| 53 | + { |
| 54 | + uint256 fromAssetDecimals = _getDecimals(assetToSwapFrom); |
| 55 | + uint256 toAssetDecimals = _getDecimals(assetToSwapTo); |
| 56 | + |
| 57 | + uint256 fromAssetPrice = _getPrice(address(assetToSwapFrom)); |
| 58 | + uint256 toAssetPrice = _getPrice(address(assetToSwapTo)); |
| 59 | + |
| 60 | + uint256 expectedMinAmountOut = |
| 61 | + amountToSwap |
| 62 | + .mul(fromAssetPrice.mul(10**toAssetDecimals)) |
| 63 | + .div(toAssetPrice.mul(10**fromAssetDecimals)) |
| 64 | + .percentMul(PercentageMath.PERCENTAGE_FACTOR - MAX_SLIPPAGE_PERCENT); |
| 65 | + |
| 66 | + require(expectedMinAmountOut <= minAmountToReceive, 'MIN_AMOUNT_EXCEEDS_MAX_SLIPPAGE'); |
| 67 | + } |
| 68 | + |
| 69 | + uint256 balanceBeforeAssetFrom = assetToSwapFrom.balanceOf(address(this)); |
| 70 | + require(balanceBeforeAssetFrom >= amountToSwap, 'INSUFFICIENT_BALANCE_BEFORE_SWAP'); |
| 71 | + uint256 balanceBeforeAssetTo = assetToSwapTo.balanceOf(address(this)); |
| 72 | + |
| 73 | + address tokenTransferProxy = augustus.getTokenTransferProxy(); |
| 74 | + assetToSwapFrom.safeApprove(tokenTransferProxy, 0); |
| 75 | + assetToSwapFrom.safeApprove(tokenTransferProxy, amountToSwap); |
| 76 | + |
| 77 | + if (fromAmountOffset != 0) { |
| 78 | + // Ensure 256 bit (32 bytes) fromAmount value is within bounds of the |
| 79 | + // calldata, not overlapping with the first 4 bytes (function selector). |
| 80 | + require(fromAmountOffset >= 4 && |
| 81 | + fromAmountOffset <= swapCalldata.length.sub(32), |
| 82 | + 'FROM_AMOUNT_OFFSET_OUT_OF_RANGE'); |
| 83 | + // Overwrite the fromAmount with the correct amount for the swap. |
| 84 | + // In memory, swapCalldata consists of a 256 bit length field, followed by |
| 85 | + // the actual bytes data, that is why 32 is added to the byte offset. |
| 86 | + assembly { |
| 87 | + mstore(add(swapCalldata, add(fromAmountOffset, 32)), amountToSwap) |
| 88 | + } |
| 89 | + } |
| 90 | + (bool success,) = address(augustus).call(swapCalldata); |
| 91 | + if (!success) { |
| 92 | + // Copy revert reason from call |
| 93 | + assembly { |
| 94 | + returndatacopy(0, 0, returndatasize()) |
| 95 | + revert(0, returndatasize()) |
| 96 | + } |
| 97 | + } |
| 98 | + require(assetToSwapFrom.balanceOf(address(this)) == balanceBeforeAssetFrom - amountToSwap, 'WRONG_BALANCE_AFTER_SWAP'); |
| 99 | + amountReceived = assetToSwapTo.balanceOf(address(this)).sub(balanceBeforeAssetTo); |
| 100 | + require(amountReceived >= minAmountToReceive, 'INSUFFICIENT_AMOUNT_RECEIVED'); |
| 101 | + |
| 102 | + emit Swapped( |
| 103 | + address(assetToSwapFrom), |
| 104 | + address(assetToSwapTo), |
| 105 | + amountToSwap, |
| 106 | + amountReceived |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
0 commit comments