diff --git a/contracts/receivers/flashloan-connector/events.sol b/contracts/receivers/flashloan-connector/events.sol new file mode 100644 index 0000000..9fc1cd4 --- /dev/null +++ b/contracts/receivers/flashloan-connector/events.sol @@ -0,0 +1,10 @@ +pragma solidity >=0.7.0; +pragma experimental ABIEncoderV2; + +contract Events { + event LogFlashBorrow(address token, uint256 tokenAmt); + event LogFlashPayback(address token, uint256 tokenAmt); + + event LogFlashMultiBorrow(address[] token, uint256[] tokenAmts); + event LogFlashMultiPayback(address[] token, uint256[] tokenAmts); +} \ No newline at end of file diff --git a/contracts/receivers/flashloan-connector/interfaces.sol b/contracts/receivers/flashloan-connector/interfaces.sol new file mode 100644 index 0000000..c191135 --- /dev/null +++ b/contracts/receivers/flashloan-connector/interfaces.sol @@ -0,0 +1,6 @@ +pragma solidity >=0.7.0; +pragma experimental ABIEncoderV2; + +interface InstaFlashV2Interface { + function initiateFlashLoan(address[] calldata tokens, uint256[] calldata amts, uint route, bytes calldata data) external; +} \ No newline at end of file diff --git a/contracts/receivers/flashloan-connector/main.sol b/contracts/receivers/flashloan-connector/main.sol new file mode 100644 index 0000000..571909d --- /dev/null +++ b/contracts/receivers/flashloan-connector/main.sol @@ -0,0 +1,118 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import { TokenInterface } from "../../common/interfaces.sol"; +import { DSMath } from "../../common/math.sol"; +import { Stores } from "../../common/stores-polygon.sol"; +import { Variables } from "./variables.sol"; +import { Events } from "./events.sol"; + +contract LiquidityResolver is DSMath, Stores, Variables, Events { + using SafeERC20 for IERC20; + + /** + * @dev Borrow Flashloan and Cast spells. + * @param token Token Address. + * @param amt Token Amount. + * @param data targets & data for cast. + */ + function flashBorrowAndCast( + address token, + uint amt, + uint route, + bytes memory data + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + address[] memory tokens = new address[](1); + uint[] memory amts = new uint[](1); + tokens[0] = token; + amts[0] = amt; + + instaPool.initiateFlashLoan(tokens, amts, route, data); + + _eventName = "LogFlashBorrow(address,uint256)"; + _eventParam = abi.encode(token, amt); + } + + /** + * @dev Return token to InstaPool. + * @param token Token Address. + * @param amt Token Amount. + * @param getId Get token amount at this ID from `InstaMemory` Contract. + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function flashPayback( + address token, + uint amt, + uint getId, + uint setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + IERC20 tokenContract = IERC20(token); + + tokenContract.safeTransfer(address(instaPool), _amt); + + setUint(setId, _amt); + + _eventName = "LogFlashPayback(address,uint256)"; + _eventParam = abi.encode(token, _amt); + } + + /** + * @dev Borrow Flashloan and Cast spells. + * @param tokens Array of token Addresses. + * @param amts Array of token Amounts. + * @param route Route to borrow. + * @param data targets & data for cast. + */ + function flashMultiBorrowAndCast( + address[] calldata tokens, + uint[] calldata amts, + uint route, + bytes calldata data + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + instaPool.initiateFlashLoan(tokens, amts, route, data); + + _eventName = "LogFlashMultiBorrow(address[],uint256[])"; + _eventParam = abi.encode(tokens, amts); + } + + /** + * @dev Return Multiple token liquidity to InstaPool. + * @param tokens Array of token addresses. + * @param amts Array of token amounts. + * @param getId get token amounts at this IDs from `InstaMemory` Contract. + * @param setId set token amounts at this IDs in `InstaMemory` Contract. + */ + function flashMultiPayback( + address[] calldata tokens, + uint[] calldata amts, + uint[] calldata getId, + uint[] calldata setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _length = tokens.length; + + uint[] memory _amts = new uint[](_length); + + for (uint i = 0; i < _length; i++) { + uint _amt = getUint(getId[i], amts[i]); + + _amts[i] = _amt; + + IERC20 tokenContract = IERC20(tokens[i]); + + tokenContract.safeTransfer(address(instaPool), _amt); + + setUint(setId[i], _amt); + } + + _eventName = "LogFlashMultiPayback(address[],uint256[])"; + _eventParam = abi.encode(tokens, _amts); + } +} + +contract ConnectInstaPool is LiquidityResolver { + string public name = "Instapool-v1"; +} \ No newline at end of file diff --git a/contracts/receivers/flashloan-connector/variables.sol b/contracts/receivers/flashloan-connector/variables.sol new file mode 100644 index 0000000..956f260 --- /dev/null +++ b/contracts/receivers/flashloan-connector/variables.sol @@ -0,0 +1,12 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +import { InstaFlashV2Interface } from "./interfaces.sol"; + +contract Variables { + + /** + * @dev Instapool / Receiver contract proxy + */ + InstaFlashV2Interface public constant instaPool = InstaFlashV2Interface(0x4A090897f47993C2504144419751D6A91D79AbF4); +} \ No newline at end of file