mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Merge pull request #100 from Instadapp/aave-flashloan-avax
[Avalanche] Added Aave flashloan connector
This commit is contained in:
commit
609deb7961
10
contracts/avalanche/connectors/instapool_v3/events.sol
Normal file
10
contracts/avalanche/connectors/instapool_v3/events.sol
Normal file
|
@ -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);
|
||||
}
|
17
contracts/avalanche/connectors/instapool_v3/interfaces.sol
Normal file
17
contracts/avalanche/connectors/instapool_v3/interfaces.sol
Normal file
|
@ -0,0 +1,17 @@
|
|||
pragma solidity >=0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
interface InstaFlashV2Interface {
|
||||
function initiateFlashLoan(address token, uint256 amt, uint route, bytes calldata data) external;
|
||||
function initiateMultiFlashLoan(
|
||||
address[] memory tokens_,
|
||||
uint256[] memory amounts_,
|
||||
uint256,
|
||||
bytes calldata data
|
||||
) external;
|
||||
}
|
||||
|
||||
interface AccountInterface {
|
||||
function enable(address) external;
|
||||
function disable(address) external;
|
||||
}
|
139
contracts/avalanche/connectors/instapool_v3/main.sol
Normal file
139
contracts/avalanche/connectors/instapool_v3/main.sol
Normal file
|
@ -0,0 +1,139 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
/**
|
||||
* @title Instapool.
|
||||
* @dev Inbuilt Flash Loan in DSA
|
||||
*/
|
||||
|
||||
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/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 { DSMath } from "../../common/math.sol";
|
||||
import { Stores } from "../../common/stores.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.
|
||||
* @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.
|
||||
*/
|
||||
function flashBorrowAndCast(
|
||||
address token,
|
||||
uint amt,
|
||||
uint route,
|
||||
bytes memory data
|
||||
) 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.initiateFlashLoan(token, amt, route, callData);
|
||||
|
||||
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);
|
||||
|
||||
if (token == avaxAddr) {
|
||||
Address.sendValue(payable(address(instaPool)), _amt);
|
||||
} else {
|
||||
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.
|
||||
*/
|
||||
function flashMultiBorrowAndCast(
|
||||
address[] memory tokens_,
|
||||
uint[] memory amts_,
|
||||
uint route,
|
||||
bytes memory data
|
||||
) 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.initiateMultiFlashLoan(tokens_, amts_, route, callData);
|
||||
|
||||
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]);
|
||||
|
||||
if (tokens_[i] == avaxAddr) {
|
||||
Address.sendValue(payable(address(instaPool)), amts_[i]);
|
||||
} else {
|
||||
IERC20(tokens_[i]).safeTransfer(address(instaPool), amts_[i]);
|
||||
}
|
||||
|
||||
setUint(setIds[i], amts_[i]);
|
||||
}
|
||||
|
||||
_eventName = "LogFlashMultiPayback(address[],uint256[])";
|
||||
_eventParam = abi.encode(tokens_, amts_);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
contract ConnectV2InstaPoolV3Avalanche is LiquidityResolver {
|
||||
string public name = "Instapool-v3";
|
||||
}
|
13
contracts/avalanche/connectors/instapool_v3/variables.sol
Normal file
13
contracts/avalanche/connectors/instapool_v3/variables.sol
Normal file
|
@ -0,0 +1,13 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import { InstaFlashV2Interface } from "./interfaces.sol";
|
||||
|
||||
contract Variables {
|
||||
|
||||
/**
|
||||
* @dev Instapool contract proxy
|
||||
*/
|
||||
InstaFlashV2Interface public constant instaPool = InstaFlashV2Interface(0x9686CE6Ad5C3f7b212CAF401b928c4bB3422E7Ba);
|
||||
|
||||
}
|
27
scripts/deployAvaxConnector.js
Normal file
27
scripts/deployAvaxConnector.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
const hre = require("hardhat");
|
||||
const { ethers, deployments, getUnnamedAccounts } = hre;
|
||||
const { deploy } = deployments;
|
||||
|
||||
|
||||
async function main() {
|
||||
|
||||
const deployer = (await getUnnamedAccounts())[0]
|
||||
|
||||
const connector = "ConnectV2InstaPoolV3Avalanche"
|
||||
|
||||
const connectorInstance = await deploy(connector, {
|
||||
from: deployer,
|
||||
})
|
||||
console.log(`${connector} deployed: `, connectorInstance.address);
|
||||
|
||||
await hre.run("sourcify", {
|
||||
address: connectorInstance.address,
|
||||
})
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => process.exit(0))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
Loading…
Reference in New Issue
Block a user