Last active
March 6, 2024 17:06
-
-
Save barrasso/55bc6b45a1332227ec68a5ebdcba7eb6 to your computer and use it in GitHub Desktop.
simple aave flash loan contract to pay off snx debt position on v2x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.10; | |
import "https://github.com/aave/aave-v3-core/blob/master/contracts/flashloan/base/FlashLoanSimpleReceiverBase.sol"; | |
import "https://github.com/aave/aave-v3-core/blob/master/contracts/interfaces/IPoolAddressesProvider.sol"; | |
import "https://github.com/aave/aave-v3-core/blob/master/contracts/dependencies/openzeppelin/contracts/IERC20.sol"; | |
import "https://github.com/Synthetixio/synthetix/blob/develop/contracts/interfaces/ISynthetix.sol"; | |
import "https://github.com/Uniswap/v3-periphery/blob/main/contracts/interfaces/ISwapRouter.sol"; | |
contract SnxFlashLoan is FlashLoanSimpleReceiverBase { | |
address payable owner; | |
address synthetix; | |
address account; | |
address constant tokenIn = 0x8700dAec35aF8Ff88c16BdF0418774CB3D7599B4; // SNX | |
address constant tokenOut = 0x8c6f28f2F1A3C87F0f938b96d27520d9751ec8d9; // sUSD | |
address constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1; | |
address constant WETH9 = 0x4200000000000000000000000000000000000006; | |
uint256 constant amountIn = 10000 ether; | |
uint24 constant poolFee30 = 3000; | |
uint24 constant poolFee5 = 500; | |
ISwapRouter constant router = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); | |
constructor(address _addressProvider, address _synthetix, address _account) | |
FlashLoanSimpleReceiverBase(IPoolAddressesProvider(_addressProvider)) | |
{ | |
synthetix = _synthetix; | |
account = _account; | |
} | |
function fn_RequestFlashLoan(address _token, uint256 _amount) public { | |
address receiverAddress = address(this); | |
address asset = _token; | |
uint256 amount = _amount; | |
bytes memory params = ""; | |
uint16 referralCode = 0; | |
POOL.flashLoanSimple( | |
receiverAddress, | |
asset, | |
amount, | |
params, | |
referralCode | |
); | |
} | |
//This function is called after your contract has received the flash loaned amount | |
function executeOperation( | |
address asset, | |
uint256 amount, | |
uint256 premium, | |
address initiator, | |
bytes calldata params | |
) external override returns (bool) { | |
IERC20(tokenOut).transfer(account, amount); | |
ISynthetix(synthetix).burnSynthsOnBehalf(account, amount); | |
IERC20(tokenIn).transferFrom(account, address(this), amountIn); | |
IERC20(tokenIn).approve(address(router), amountIn); | |
ISwapRouter.ExactInputParams memory swapParams = ISwapRouter.ExactInputParams({ | |
path: abi.encodePacked(tokenIn, poolFee30, WETH9, poolFee5, DAI, poolFee5, tokenOut), | |
recipient: address(this), | |
deadline: block.timestamp, | |
amountIn: amountIn, | |
amountOutMinimum: 0 | |
}); | |
uint amountOut = router.exactInput(swapParams); | |
uint256 totalAmount = amount + premium; | |
require(amountOut > totalAmount, "Not enough bro"); | |
uint256 remaining = amountOut - totalAmount; | |
IERC20(tokenOut).transfer(account, remaining); | |
IERC20(asset).approve(address(POOL), totalAmount); | |
return true; | |
} | |
receive() external payable {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment