From ed2aceb09e51beb64432308a3d6b7540e9645189 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Fri, 5 Jul 2024 17:41:51 +0530 Subject: [PATCH] feat: add instapool v5 for all chains --- .../connectors/instapool_v5/events.sol | 10 ++ .../connectors/instapool_v5/interfaces.sol | 11 ++ .../connectors/instapool_v5/main.sol | 138 ++++++++++++++++++ .../connectors/instapool_v5/variables.sol | 14 ++ .../base/connectors/instapool_v5/events.sol | 10 ++ .../connectors/instapool_v5/interfaces.sol | 11 ++ .../base/connectors/instapool_v5/main.sol | 136 +++++++++++++++++ .../connectors/instapool_v5/variables.sol | 13 ++ .../connectors/instapool_v5/events.sol | 10 ++ .../connectors/instapool_v5/interfaces.sol | 11 ++ .../optimism/connectors/instapool_v5/main.sol | 136 +++++++++++++++++ .../connectors/instapool_v5/variables.sol | 13 ++ .../connectors/instapool_v5/events.sol | 10 ++ .../connectors/instapool_v5/interfaces.sol | 11 ++ .../polygon/connectors/instapool_v5/main.sol | 138 ++++++++++++++++++ .../connectors/instapool_v5/variables.sol | 13 ++ 16 files changed, 685 insertions(+) create mode 100644 contracts/avalanche/connectors/instapool_v5/events.sol create mode 100644 contracts/avalanche/connectors/instapool_v5/interfaces.sol create mode 100644 contracts/avalanche/connectors/instapool_v5/main.sol create mode 100644 contracts/avalanche/connectors/instapool_v5/variables.sol create mode 100644 contracts/base/connectors/instapool_v5/events.sol create mode 100644 contracts/base/connectors/instapool_v5/interfaces.sol create mode 100644 contracts/base/connectors/instapool_v5/main.sol create mode 100644 contracts/base/connectors/instapool_v5/variables.sol create mode 100644 contracts/optimism/connectors/instapool_v5/events.sol create mode 100644 contracts/optimism/connectors/instapool_v5/interfaces.sol create mode 100644 contracts/optimism/connectors/instapool_v5/main.sol create mode 100644 contracts/optimism/connectors/instapool_v5/variables.sol create mode 100644 contracts/polygon/connectors/instapool_v5/events.sol create mode 100644 contracts/polygon/connectors/instapool_v5/interfaces.sol create mode 100644 contracts/polygon/connectors/instapool_v5/main.sol create mode 100644 contracts/polygon/connectors/instapool_v5/variables.sol diff --git a/contracts/avalanche/connectors/instapool_v5/events.sol b/contracts/avalanche/connectors/instapool_v5/events.sol new file mode 100644 index 0000000..a6cb012 --- /dev/null +++ b/contracts/avalanche/connectors/instapool_v5/events.sol @@ -0,0 +1,10 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +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/avalanche/connectors/instapool_v5/interfaces.sol b/contracts/avalanche/connectors/instapool_v5/interfaces.sol new file mode 100644 index 0000000..a7dff8e --- /dev/null +++ b/contracts/avalanche/connectors/instapool_v5/interfaces.sol @@ -0,0 +1,11 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +interface InstaFlashV5Interface { + function flashLoan(address[] memory tokens, uint256[] memory amts, uint route, bytes memory data, bytes memory extraData) external; +} + +interface AccountInterface { + function enable(address) external; + function disable(address) external; +} diff --git a/contracts/avalanche/connectors/instapool_v5/main.sol b/contracts/avalanche/connectors/instapool_v5/main.sol new file mode 100644 index 0000000..d69e9ea --- /dev/null +++ b/contracts/avalanche/connectors/instapool_v5/main.sol @@ -0,0 +1,138 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +/** + * @title Instapool. + * @dev Inbuilt Flash Loan in DSA + */ + +import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; +import { Address } from "@openzeppelin/contracts/utils/Address.sol"; +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import { TokenInterface } from "../../common/interfaces.sol"; +import { AccountInterface } from "./interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { Variables } from "./variables.sol"; +import { Events } from "./events.sol"; + +contract LiquidityConnector is Stores, Variables, Events { + using SafeERC20 for IERC20; + + /** + * @dev Borrow Flashloan and Cast spells. + * @notice Borrow Flashloan and Cast spells. + * @param token Token Address. + * @param amt Token Amount. + * @param route Flashloan source route. + * @param data targets & data for cast. + * @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes + */ + function flashBorrowAndCast( + address token, + uint amt, + uint route, + bytes memory data, + bytes memory extraData + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AccountInterface(address(this)).enable(address(instaPool)); + (string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[])); + + bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool)); + + address[] memory tokens_ = new address[](1); + tokens_[0] = token; + uint[] memory amts_ = new uint[](1); + amts_[0] = amt; + instaPool.flashLoan(tokens_, amts_, route, callData_, extraData); + + AccountInterface(address(this)).disable(address(instaPool)); + + _eventName = "LogFlashBorrow(address,uint256)"; + _eventParam = abi.encode(token, amt); + } + + /** + * @dev Return token to InstaPool. + * @notice 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 multi-tokens Flashloan and Cast spells. + * @notice Borrow multi-tokens Flashloan and Cast spells. + * @param tokens_ Array of Token Addresses. + * @param amts_ Array of Token Amounts. + * @param route Flashloan source route. + * @param data targets & data for cast. + * @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes + */ + function flashMultiBorrowAndCast( + address[] memory tokens_, + uint[] memory amts_, + uint route, + bytes memory data, + bytes memory extraData + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AccountInterface(address(this)).enable(address(instaPool)); + (string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[])); + + bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool)); + + instaPool.flashLoan(tokens_, amts_, route, callData_, extraData); + + AccountInterface(address(this)).disable(address(instaPool)); + _eventName = "LogFlashMultiBorrow(address[],uint256[])"; + _eventParam = abi.encode(tokens_, amts_); + } + + /** + * @dev Return multi-tokens to InstaPool. + * @notice Return multi-tokens to InstaPool. + * @param tokens_ Array of Token Addresses. + * @param amts_ Array of Token Amounts. + * @param getIds Array of getId token amounts. + * @param setIds Array of setId token amounts. + */ + function flashMultiPayback( + address[] memory tokens_, + uint[] memory amts_, + uint[] memory getIds, + uint[] memory setIds + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + for (uint i = 0; i < tokens_.length; i++) { + amts_[i] = getUint(getIds[i], amts_[i]); + + IERC20(tokens_[i]).safeTransfer(address(instaPool), amts_[i]); + + setUint(setIds[i], amts_[i]); + } + + _eventName = "LogFlashMultiPayback(address[],uint256[])"; + _eventParam = abi.encode(tokens_, amts_); + } + +} + +contract ConnectV2InstaPoolV5 is LiquidityConnector { + string public name = "Instapool-v5"; +} diff --git a/contracts/avalanche/connectors/instapool_v5/variables.sol b/contracts/avalanche/connectors/instapool_v5/variables.sol new file mode 100644 index 0000000..3c098ff --- /dev/null +++ b/contracts/avalanche/connectors/instapool_v5/variables.sol @@ -0,0 +1,14 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import { InstaFlashV5Interface } from "./interfaces.sol"; + +contract Variables { + + /** + * @dev Instapool contract proxy + */ + InstaFlashV5Interface public constant instaPool = + InstaFlashV5Interface(0x352423e2fA5D5c99343d371C9e3bC56C87723Cc7); + +} \ No newline at end of file diff --git a/contracts/base/connectors/instapool_v5/events.sol b/contracts/base/connectors/instapool_v5/events.sol new file mode 100644 index 0000000..a6cb012 --- /dev/null +++ b/contracts/base/connectors/instapool_v5/events.sol @@ -0,0 +1,10 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +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/base/connectors/instapool_v5/interfaces.sol b/contracts/base/connectors/instapool_v5/interfaces.sol new file mode 100644 index 0000000..a7dff8e --- /dev/null +++ b/contracts/base/connectors/instapool_v5/interfaces.sol @@ -0,0 +1,11 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +interface InstaFlashV5Interface { + function flashLoan(address[] memory tokens, uint256[] memory amts, uint route, bytes memory data, bytes memory extraData) external; +} + +interface AccountInterface { + function enable(address) external; + function disable(address) external; +} diff --git a/contracts/base/connectors/instapool_v5/main.sol b/contracts/base/connectors/instapool_v5/main.sol new file mode 100644 index 0000000..b5352b6 --- /dev/null +++ b/contracts/base/connectors/instapool_v5/main.sol @@ -0,0 +1,136 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +/** + * @title Instapool. + * @dev Inbuilt Flash Loan in DSA + */ + +import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import { AccountInterface } from "./interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { Variables } from "./variables.sol"; +import { Events } from "./events.sol"; + +contract LiquidityConnector is Stores, Variables, Events { + using SafeERC20 for IERC20; + + /** + * @dev Borrow Flashloan and Cast spells. + * @notice Borrow Flashloan and Cast spells. + * @param token Token Address. + * @param amt Token Amount. + * @param route Flashloan source route. + * @param data targets & data for cast. + * @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes + */ + function flashBorrowAndCast( + address token, + uint amt, + uint route, + bytes memory data, + bytes memory extraData + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AccountInterface(address(this)).enable(address(instaPool)); + (string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[])); + + bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool)); + + address[] memory tokens_ = new address[](1); + tokens_[0] = token; + uint[] memory amts_ = new uint[](1); + amts_[0] = amt; + instaPool.flashLoan(tokens_, amts_, route, callData_, extraData); + + AccountInterface(address(this)).disable(address(instaPool)); + + _eventName = "LogFlashBorrow(address,uint256)"; + _eventParam = abi.encode(token, amt); + } + + /** + * @dev Return token to InstaPool. + * @notice 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 multi-tokens Flashloan and Cast spells. + * @notice Borrow multi-tokens Flashloan and Cast spells. + * @param tokens_ Array of Token Addresses. + * @param amts_ Array of Token Amounts. + * @param route Flashloan source route. + * @param data targets & data for cast. + * @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes + */ + function flashMultiBorrowAndCast( + address[] memory tokens_, + uint[] memory amts_, + uint route, + bytes memory data, + bytes memory extraData + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AccountInterface(address(this)).enable(address(instaPool)); + (string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[])); + + bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool)); + + instaPool.flashLoan(tokens_, amts_, route, callData_, extraData); + + AccountInterface(address(this)).disable(address(instaPool)); + _eventName = "LogFlashMultiBorrow(address[],uint256[])"; + _eventParam = abi.encode(tokens_, amts_); + } + + /** + * @dev Return multi-tokens to InstaPool. + * @notice Return multi-tokens to InstaPool. + * @param tokens_ Array of Token Addresses. + * @param amts_ Array of Token Amounts. + * @param getIds Array of getId token amounts. + * @param setIds Array of setId token amounts. + */ + function flashMultiPayback( + address[] memory tokens_, + uint[] memory amts_, + uint[] memory getIds, + uint[] memory setIds + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + for (uint i = 0; i < tokens_.length; i++) { + amts_[i] = getUint(getIds[i], amts_[i]); + + IERC20(tokens_[i]).safeTransfer(address(instaPool), amts_[i]); + + setUint(setIds[i], amts_[i]); + } + + _eventName = "LogFlashMultiPayback(address[],uint256[])"; + _eventParam = abi.encode(tokens_, amts_); + } + +} + +contract ConnectV2InstaPoolV5 is LiquidityConnector { + string public name = "Instapool-v5"; +} diff --git a/contracts/base/connectors/instapool_v5/variables.sol b/contracts/base/connectors/instapool_v5/variables.sol new file mode 100644 index 0000000..723faba --- /dev/null +++ b/contracts/base/connectors/instapool_v5/variables.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import { InstaFlashV5Interface } from "./interfaces.sol"; + +contract Variables { + + /** + * @dev Instapool contract proxy + */ + InstaFlashV5Interface public constant instaPool = + InstaFlashV5Interface(0x352423e2fA5D5c99343d371C9e3bC56C87723Cc7); +} \ No newline at end of file diff --git a/contracts/optimism/connectors/instapool_v5/events.sol b/contracts/optimism/connectors/instapool_v5/events.sol new file mode 100644 index 0000000..a6cb012 --- /dev/null +++ b/contracts/optimism/connectors/instapool_v5/events.sol @@ -0,0 +1,10 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +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/optimism/connectors/instapool_v5/interfaces.sol b/contracts/optimism/connectors/instapool_v5/interfaces.sol new file mode 100644 index 0000000..a7dff8e --- /dev/null +++ b/contracts/optimism/connectors/instapool_v5/interfaces.sol @@ -0,0 +1,11 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +interface InstaFlashV5Interface { + function flashLoan(address[] memory tokens, uint256[] memory amts, uint route, bytes memory data, bytes memory extraData) external; +} + +interface AccountInterface { + function enable(address) external; + function disable(address) external; +} diff --git a/contracts/optimism/connectors/instapool_v5/main.sol b/contracts/optimism/connectors/instapool_v5/main.sol new file mode 100644 index 0000000..b5352b6 --- /dev/null +++ b/contracts/optimism/connectors/instapool_v5/main.sol @@ -0,0 +1,136 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +/** + * @title Instapool. + * @dev Inbuilt Flash Loan in DSA + */ + +import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import { AccountInterface } from "./interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { Variables } from "./variables.sol"; +import { Events } from "./events.sol"; + +contract LiquidityConnector is Stores, Variables, Events { + using SafeERC20 for IERC20; + + /** + * @dev Borrow Flashloan and Cast spells. + * @notice Borrow Flashloan and Cast spells. + * @param token Token Address. + * @param amt Token Amount. + * @param route Flashloan source route. + * @param data targets & data for cast. + * @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes + */ + function flashBorrowAndCast( + address token, + uint amt, + uint route, + bytes memory data, + bytes memory extraData + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AccountInterface(address(this)).enable(address(instaPool)); + (string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[])); + + bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool)); + + address[] memory tokens_ = new address[](1); + tokens_[0] = token; + uint[] memory amts_ = new uint[](1); + amts_[0] = amt; + instaPool.flashLoan(tokens_, amts_, route, callData_, extraData); + + AccountInterface(address(this)).disable(address(instaPool)); + + _eventName = "LogFlashBorrow(address,uint256)"; + _eventParam = abi.encode(token, amt); + } + + /** + * @dev Return token to InstaPool. + * @notice 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 multi-tokens Flashloan and Cast spells. + * @notice Borrow multi-tokens Flashloan and Cast spells. + * @param tokens_ Array of Token Addresses. + * @param amts_ Array of Token Amounts. + * @param route Flashloan source route. + * @param data targets & data for cast. + * @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes + */ + function flashMultiBorrowAndCast( + address[] memory tokens_, + uint[] memory amts_, + uint route, + bytes memory data, + bytes memory extraData + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AccountInterface(address(this)).enable(address(instaPool)); + (string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[])); + + bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool)); + + instaPool.flashLoan(tokens_, amts_, route, callData_, extraData); + + AccountInterface(address(this)).disable(address(instaPool)); + _eventName = "LogFlashMultiBorrow(address[],uint256[])"; + _eventParam = abi.encode(tokens_, amts_); + } + + /** + * @dev Return multi-tokens to InstaPool. + * @notice Return multi-tokens to InstaPool. + * @param tokens_ Array of Token Addresses. + * @param amts_ Array of Token Amounts. + * @param getIds Array of getId token amounts. + * @param setIds Array of setId token amounts. + */ + function flashMultiPayback( + address[] memory tokens_, + uint[] memory amts_, + uint[] memory getIds, + uint[] memory setIds + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + for (uint i = 0; i < tokens_.length; i++) { + amts_[i] = getUint(getIds[i], amts_[i]); + + IERC20(tokens_[i]).safeTransfer(address(instaPool), amts_[i]); + + setUint(setIds[i], amts_[i]); + } + + _eventName = "LogFlashMultiPayback(address[],uint256[])"; + _eventParam = abi.encode(tokens_, amts_); + } + +} + +contract ConnectV2InstaPoolV5 is LiquidityConnector { + string public name = "Instapool-v5"; +} diff --git a/contracts/optimism/connectors/instapool_v5/variables.sol b/contracts/optimism/connectors/instapool_v5/variables.sol new file mode 100644 index 0000000..723faba --- /dev/null +++ b/contracts/optimism/connectors/instapool_v5/variables.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import { InstaFlashV5Interface } from "./interfaces.sol"; + +contract Variables { + + /** + * @dev Instapool contract proxy + */ + InstaFlashV5Interface public constant instaPool = + InstaFlashV5Interface(0x352423e2fA5D5c99343d371C9e3bC56C87723Cc7); +} \ No newline at end of file diff --git a/contracts/polygon/connectors/instapool_v5/events.sol b/contracts/polygon/connectors/instapool_v5/events.sol new file mode 100644 index 0000000..a6cb012 --- /dev/null +++ b/contracts/polygon/connectors/instapool_v5/events.sol @@ -0,0 +1,10 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +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/polygon/connectors/instapool_v5/interfaces.sol b/contracts/polygon/connectors/instapool_v5/interfaces.sol new file mode 100644 index 0000000..a7dff8e --- /dev/null +++ b/contracts/polygon/connectors/instapool_v5/interfaces.sol @@ -0,0 +1,11 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +interface InstaFlashV5Interface { + function flashLoan(address[] memory tokens, uint256[] memory amts, uint route, bytes memory data, bytes memory extraData) external; +} + +interface AccountInterface { + function enable(address) external; + function disable(address) external; +} diff --git a/contracts/polygon/connectors/instapool_v5/main.sol b/contracts/polygon/connectors/instapool_v5/main.sol new file mode 100644 index 0000000..89de418 --- /dev/null +++ b/contracts/polygon/connectors/instapool_v5/main.sol @@ -0,0 +1,138 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +/** + * @title Instapool. + * @dev Inbuilt Flash Loan in DSA + */ + +import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; +import { Address } from "@openzeppelin/contracts/utils/Address.sol"; +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import { TokenInterface } from "../../common/interfaces.sol"; +import { AccountInterface } from "./interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { Variables } from "./variables.sol"; +import { Events } from "./events.sol"; + +contract LiquidityConnectors is Stores, Variables, Events { + using SafeERC20 for IERC20; + + /** + * @dev Borrow Flashloan and Cast spells. + * @notice Borrow Flashloan and Cast spells. + * @param token Token Address. + * @param amt Token Amount. + * @param route Flashloan source route. + * @param data targets & data for cast. + * @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes + */ + function flashBorrowAndCast( + address token, + uint amt, + uint route, + bytes memory data, + bytes memory extraData + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AccountInterface(address(this)).enable(address(instaPool)); + (string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[])); + + bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool)); + + address[] memory tokens_ = new address[](1); + tokens_[0] = token; + uint[] memory amts_ = new uint[](1); + amts_[0] = amt; + instaPool.flashLoan(tokens_, amts_, route, callData_, extraData); + + AccountInterface(address(this)).disable(address(instaPool)); + + _eventName = "LogFlashBorrow(address,uint256)"; + _eventParam = abi.encode(token, amt); + } + + /** + * @dev Return token to InstaPool. + * @notice 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 multi-tokens Flashloan and Cast spells. + * @notice Borrow multi-tokens Flashloan and Cast spells. + * @param tokens_ Array of Token Addresses. + * @param amts_ Array of Token Amounts. + * @param route Flashloan source route. + * @param data targets & data for cast. + * @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes + */ + function flashMultiBorrowAndCast( + address[] memory tokens_, + uint[] memory amts_, + uint route, + bytes memory data, + bytes memory extraData + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AccountInterface(address(this)).enable(address(instaPool)); + (string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[])); + + bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool)); + + instaPool.flashLoan(tokens_, amts_, route, callData_, extraData); + + AccountInterface(address(this)).disable(address(instaPool)); + _eventName = "LogFlashMultiBorrow(address[],uint256[])"; + _eventParam = abi.encode(tokens_, amts_); + } + + /** + * @dev Return multi-tokens to InstaPool. + * @notice Return multi-tokens to InstaPool. + * @param tokens_ Array of Token Addresses. + * @param amts_ Array of Token Amounts. + * @param getIds Array of getId token amounts. + * @param setIds Array of setId token amounts. + */ + function flashMultiPayback( + address[] memory tokens_, + uint[] memory amts_, + uint[] memory getIds, + uint[] memory setIds + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + for (uint i = 0; i < tokens_.length; i++) { + amts_[i] = getUint(getIds[i], amts_[i]); + + IERC20(tokens_[i]).safeTransfer(address(instaPool), amts_[i]); + + setUint(setIds[i], amts_[i]); + } + + _eventName = "LogFlashMultiPayback(address[],uint256[])"; + _eventParam = abi.encode(tokens_, amts_); + } + +} + +contract ConnectV2InstaPoolV5 is LiquidityConnectors { + string public name = "Instapool-v5"; +} diff --git a/contracts/polygon/connectors/instapool_v5/variables.sol b/contracts/polygon/connectors/instapool_v5/variables.sol new file mode 100644 index 0000000..723faba --- /dev/null +++ b/contracts/polygon/connectors/instapool_v5/variables.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import { InstaFlashV5Interface } from "./interfaces.sol"; + +contract Variables { + + /** + * @dev Instapool contract proxy + */ + InstaFlashV5Interface public constant instaPool = + InstaFlashV5Interface(0x352423e2fA5D5c99343d371C9e3bC56C87723Cc7); +} \ No newline at end of file