mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
added connector on fanto,
This commit is contained in:
parent
ad7c5f11c4
commit
68e007e26f
91
contracts/fantom/common/basic.sol
Normal file
91
contracts/fantom/common/basic.sol
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import { TokenInterface } from "./interfaces.sol";
|
||||||
|
import { Stores } from "./stores.sol";
|
||||||
|
import { DSMath } from "./math.sol";
|
||||||
|
|
||||||
|
abstract contract Basic is DSMath, Stores {
|
||||||
|
function convert18ToDec(uint256 _dec, uint256 _amt)
|
||||||
|
internal
|
||||||
|
pure
|
||||||
|
returns (uint256 amt)
|
||||||
|
{
|
||||||
|
amt = (_amt / 10**(18 - _dec));
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertTo18(uint256 _dec, uint256 _amt)
|
||||||
|
internal
|
||||||
|
pure
|
||||||
|
returns (uint256 amt)
|
||||||
|
{
|
||||||
|
amt = mul(_amt, 10**(18 - _dec));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTokenBal(TokenInterface token)
|
||||||
|
internal
|
||||||
|
view
|
||||||
|
returns (uint256 _amt)
|
||||||
|
{
|
||||||
|
_amt = address(token) == ftmAddr
|
||||||
|
? address(this).balance
|
||||||
|
: token.balanceOf(address(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTokensDec(TokenInterface buyAddr, TokenInterface sellAddr)
|
||||||
|
internal
|
||||||
|
view
|
||||||
|
returns (uint256 buyDec, uint256 sellDec)
|
||||||
|
{
|
||||||
|
buyDec = address(buyAddr) == ftmAddr ? 18 : buyAddr.decimals();
|
||||||
|
sellDec = address(sellAddr) == ftmAddr ? 18 : sellAddr.decimals();
|
||||||
|
}
|
||||||
|
|
||||||
|
function encodeEvent(string memory eventName, bytes memory eventParam)
|
||||||
|
internal
|
||||||
|
pure
|
||||||
|
returns (bytes memory)
|
||||||
|
{
|
||||||
|
return abi.encode(eventName, eventParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
function approve(
|
||||||
|
TokenInterface token,
|
||||||
|
address spender,
|
||||||
|
uint256 amount
|
||||||
|
) internal {
|
||||||
|
try token.approve(spender, amount) {} catch {
|
||||||
|
token.approve(spender, 0);
|
||||||
|
token.approve(spender, amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function changeftmAddress(address buy, address sell)
|
||||||
|
internal
|
||||||
|
pure
|
||||||
|
returns (TokenInterface _buy, TokenInterface _sell)
|
||||||
|
{
|
||||||
|
_buy = buy == ftmAddr ? TokenInterface(wftmAddr) : TokenInterface(buy);
|
||||||
|
_sell = sell == ftmAddr
|
||||||
|
? TokenInterface(wftmAddr)
|
||||||
|
: TokenInterface(sell);
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertFtmToWftm(
|
||||||
|
bool isFtm,
|
||||||
|
TokenInterface token,
|
||||||
|
uint256 amount
|
||||||
|
) internal {
|
||||||
|
if (isFtm) token.deposit{ value: amount }();
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertWftmToFtm(
|
||||||
|
bool isFtm,
|
||||||
|
TokenInterface token,
|
||||||
|
uint256 amount
|
||||||
|
) internal {
|
||||||
|
if (isFtm) {
|
||||||
|
approve(token, address(token), amount);
|
||||||
|
token.withdraw(amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
contracts/fantom/common/interfaces.sol
Normal file
35
contracts/fantom/common/interfaces.sol
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
interface TokenInterface {
|
||||||
|
function approve(address, uint256) external;
|
||||||
|
|
||||||
|
function transfer(address, uint256) external;
|
||||||
|
|
||||||
|
function transferFrom(
|
||||||
|
address,
|
||||||
|
address,
|
||||||
|
uint256
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function deposit() external payable;
|
||||||
|
|
||||||
|
function withdraw(uint256) external;
|
||||||
|
|
||||||
|
function balanceOf(address) external view returns (uint256);
|
||||||
|
|
||||||
|
function decimals() external view returns (uint256);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MemoryInterface {
|
||||||
|
function getUint(uint256 id) external returns (uint256 num);
|
||||||
|
|
||||||
|
function setUint(uint256 id, uint256 val) external;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AccountInterface {
|
||||||
|
function enable(address) external;
|
||||||
|
|
||||||
|
function disable(address) external;
|
||||||
|
|
||||||
|
function isAuth(address) external view returns (bool);
|
||||||
|
}
|
54
contracts/fantom/common/math.sol
Normal file
54
contracts/fantom/common/math.sol
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
|
||||||
|
|
||||||
|
contract DSMath {
|
||||||
|
uint256 constant WAD = 10**18;
|
||||||
|
uint256 constant RAY = 10**27;
|
||||||
|
|
||||||
|
function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
||||||
|
z = SafeMath.add(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
function sub(uint256 x, uint256 y)
|
||||||
|
internal
|
||||||
|
pure
|
||||||
|
virtual
|
||||||
|
returns (uint256 z)
|
||||||
|
{
|
||||||
|
z = SafeMath.sub(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
function mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
||||||
|
z = SafeMath.mul(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
function div(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
||||||
|
z = SafeMath.div(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
function wmul(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
||||||
|
z = SafeMath.add(SafeMath.mul(x, y), WAD / 2) / WAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
function wdiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
||||||
|
z = SafeMath.add(SafeMath.mul(x, WAD), y / 2) / y;
|
||||||
|
}
|
||||||
|
|
||||||
|
function rdiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
||||||
|
z = SafeMath.add(SafeMath.mul(x, RAY), y / 2) / y;
|
||||||
|
}
|
||||||
|
|
||||||
|
function rmul(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
||||||
|
z = SafeMath.add(SafeMath.mul(x, y), RAY / 2) / RAY;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toInt(uint256 x) internal pure returns (int256 y) {
|
||||||
|
y = int256(x);
|
||||||
|
require(y >= 0, "int-overflow");
|
||||||
|
}
|
||||||
|
|
||||||
|
function toRad(uint256 wad) internal pure returns (uint256 rad) {
|
||||||
|
rad = mul(wad, 10**27);
|
||||||
|
}
|
||||||
|
}
|
40
contracts/fantom/common/stores.sol
Normal file
40
contracts/fantom/common/stores.sol
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import { MemoryInterface } from "./interfaces.sol";
|
||||||
|
|
||||||
|
abstract contract Stores {
|
||||||
|
/**
|
||||||
|
* @dev Return FTM address
|
||||||
|
*/
|
||||||
|
address internal constant ftmAddr =
|
||||||
|
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Return Wrapped FTM address
|
||||||
|
*/
|
||||||
|
address internal constant wftmAddr =
|
||||||
|
0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Return memory variable address
|
||||||
|
*/
|
||||||
|
MemoryInterface internal constant instaMemory =
|
||||||
|
MemoryInterface(0x56439117379A53bE3CC2C55217251e2481B7a1C8);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Get Uint value from InstaMemory Contract.
|
||||||
|
*/
|
||||||
|
function getUint(uint256 getId, uint256 val)
|
||||||
|
internal
|
||||||
|
returns (uint256 returnVal)
|
||||||
|
{
|
||||||
|
returnVal = getId == 0 ? val : instaMemory.getUint(getId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Set Uint value in InstaMemory Contract.
|
||||||
|
*/
|
||||||
|
function setUint(uint256 setId, uint256 val) internal virtual {
|
||||||
|
if (setId != 0) instaMemory.setUint(setId, val);
|
||||||
|
}
|
||||||
|
}
|
14
contracts/fantom/connectors/aave/v3-import/events.sol
Normal file
14
contracts/fantom/connectors/aave/v3-import/events.sol
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogAaveV3Import(
|
||||||
|
address indexed user,
|
||||||
|
address[] ctokens,
|
||||||
|
string[] supplyIds,
|
||||||
|
string[] borrowIds,
|
||||||
|
uint256[] flashLoanFees,
|
||||||
|
uint256[] supplyAmts,
|
||||||
|
uint256[] borrowAmts
|
||||||
|
);
|
||||||
|
}
|
286
contracts/fantom/connectors/aave/v3-import/helpers.sol
Normal file
286
contracts/fantom/connectors/aave/v3-import/helpers.sol
Normal file
|
@ -0,0 +1,286 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import { DSMath } from "../../../common/math.sol";
|
||||||
|
import { Basic } from "../../../common/basic.sol";
|
||||||
|
import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol";
|
||||||
|
import { AaveInterface, AavePoolProviderInterface, AaveDataProviderInterface } from "./interface.sol";
|
||||||
|
import "./events.sol";
|
||||||
|
import "./interface.sol";
|
||||||
|
|
||||||
|
abstract contract Helper is DSMath, Basic {
|
||||||
|
/**
|
||||||
|
* @dev Aave referal code
|
||||||
|
*/
|
||||||
|
uint16 internal constant referalCode = 3228;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Aave Lending Pool Provider
|
||||||
|
*/
|
||||||
|
AavePoolProviderInterface internal constant aaveProvider =
|
||||||
|
AavePoolProviderInterface(0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Aave Protocol Data Provider
|
||||||
|
*/
|
||||||
|
AaveDataProviderInterface internal constant aaveData =
|
||||||
|
AaveDataProviderInterface(0x69FA688f1Dc47d4B5d8029D5a35FB7a548310654);
|
||||||
|
|
||||||
|
function getIsColl(address token, address user)
|
||||||
|
internal
|
||||||
|
view
|
||||||
|
returns (bool isCol)
|
||||||
|
{
|
||||||
|
(, , , , , , , , isCol) = aaveData.getUserReserveData(token, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ImportData {
|
||||||
|
address[] _supplyTokens;
|
||||||
|
address[] _borrowTokens;
|
||||||
|
ATokenInterface[] aTokens;
|
||||||
|
uint256[] supplyAmts;
|
||||||
|
uint256[] variableBorrowAmts;
|
||||||
|
uint256[] variableBorrowAmtsWithFee;
|
||||||
|
uint256[] stableBorrowAmts;
|
||||||
|
uint256[] stableBorrowAmtsWithFee;
|
||||||
|
uint256[] totalBorrowAmts;
|
||||||
|
uint256[] totalBorrowAmtsWithFee;
|
||||||
|
bool convertStable;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ImportInputData {
|
||||||
|
address[] supplyTokens;
|
||||||
|
address[] borrowTokens;
|
||||||
|
bool convertStable;
|
||||||
|
uint256[] flashLoanFees;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract AaveHelpers is Helper {
|
||||||
|
function getBorrowAmount(address _token, address userAccount)
|
||||||
|
internal
|
||||||
|
view
|
||||||
|
returns (uint256 stableBorrow, uint256 variableBorrow)
|
||||||
|
{
|
||||||
|
(
|
||||||
|
,
|
||||||
|
address stableDebtTokenAddress,
|
||||||
|
address variableDebtTokenAddress
|
||||||
|
) = aaveData.getReserveTokensAddresses(_token);
|
||||||
|
|
||||||
|
stableBorrow = ATokenInterface(stableDebtTokenAddress).balanceOf(
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
variableBorrow = ATokenInterface(variableDebtTokenAddress).balanceOf(
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBorrowAmounts(
|
||||||
|
address userAccount,
|
||||||
|
AaveInterface aave,
|
||||||
|
ImportInputData memory inputData,
|
||||||
|
ImportData memory data
|
||||||
|
) internal returns (ImportData memory) {
|
||||||
|
if (inputData.borrowTokens.length > 0) {
|
||||||
|
data._borrowTokens = new address[](inputData.borrowTokens.length);
|
||||||
|
data.variableBorrowAmts = new uint256[](
|
||||||
|
inputData.borrowTokens.length
|
||||||
|
);
|
||||||
|
data.variableBorrowAmtsWithFee = new uint256[](
|
||||||
|
inputData.borrowTokens.length
|
||||||
|
);
|
||||||
|
data.stableBorrowAmts = new uint256[](
|
||||||
|
inputData.borrowTokens.length
|
||||||
|
);
|
||||||
|
data.stableBorrowAmtsWithFee = new uint256[](
|
||||||
|
inputData.borrowTokens.length
|
||||||
|
);
|
||||||
|
data.totalBorrowAmts = new uint256[](inputData.borrowTokens.length);
|
||||||
|
data.totalBorrowAmtsWithFee = new uint256[](
|
||||||
|
inputData.borrowTokens.length
|
||||||
|
);
|
||||||
|
for (uint256 i = 0; i < inputData.borrowTokens.length; i++) {
|
||||||
|
for (uint256 j = i; j < inputData.borrowTokens.length; j++) {
|
||||||
|
if (j != i) {
|
||||||
|
require(
|
||||||
|
inputData.borrowTokens[i] !=
|
||||||
|
inputData.borrowTokens[j],
|
||||||
|
"token-repeated"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (uint256 i = 0; i < inputData.borrowTokens.length; i++) {
|
||||||
|
address _token = inputData.borrowTokens[i] == ftmAddr
|
||||||
|
? wftmAddr
|
||||||
|
: inputData.borrowTokens[i];
|
||||||
|
data._borrowTokens[i] = _token;
|
||||||
|
|
||||||
|
(
|
||||||
|
data.stableBorrowAmts[i],
|
||||||
|
data.variableBorrowAmts[i]
|
||||||
|
) = getBorrowAmount(_token, userAccount);
|
||||||
|
|
||||||
|
if (data.variableBorrowAmts[i] != 0) {
|
||||||
|
data.variableBorrowAmtsWithFee[i] = add(
|
||||||
|
data.variableBorrowAmts[i],
|
||||||
|
inputData.flashLoanFees[i]
|
||||||
|
);
|
||||||
|
data.stableBorrowAmtsWithFee[i] = data.stableBorrowAmts[i];
|
||||||
|
} else {
|
||||||
|
data.stableBorrowAmtsWithFee[i] = add(
|
||||||
|
data.stableBorrowAmts[i],
|
||||||
|
inputData.flashLoanFees[i]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
data.totalBorrowAmts[i] = add(
|
||||||
|
data.stableBorrowAmts[i],
|
||||||
|
data.variableBorrowAmts[i]
|
||||||
|
);
|
||||||
|
data.totalBorrowAmtsWithFee[i] = add(
|
||||||
|
data.stableBorrowAmtsWithFee[i],
|
||||||
|
data.variableBorrowAmtsWithFee[i]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (data.totalBorrowAmts[i] > 0) {
|
||||||
|
uint256 _amt = data.totalBorrowAmts[i];
|
||||||
|
TokenInterface(_token).approve(address(aave), _amt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSupplyAmounts(
|
||||||
|
address userAccount,
|
||||||
|
ImportInputData memory inputData,
|
||||||
|
ImportData memory data
|
||||||
|
) internal view returns (ImportData memory) {
|
||||||
|
data.supplyAmts = new uint256[](inputData.supplyTokens.length);
|
||||||
|
data._supplyTokens = new address[](inputData.supplyTokens.length);
|
||||||
|
data.aTokens = new ATokenInterface[](inputData.supplyTokens.length);
|
||||||
|
|
||||||
|
for (uint256 i = 0; i < inputData.supplyTokens.length; i++) {
|
||||||
|
for (uint256 j = i; j < inputData.supplyTokens.length; j++) {
|
||||||
|
if (j != i) {
|
||||||
|
require(
|
||||||
|
inputData.supplyTokens[i] != inputData.supplyTokens[j],
|
||||||
|
"token-repeated"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (uint256 i = 0; i < inputData.supplyTokens.length; i++) {
|
||||||
|
address _token = inputData.supplyTokens[i] == ftmAddr
|
||||||
|
? wftmAddr
|
||||||
|
: inputData.supplyTokens[i];
|
||||||
|
(address _aToken, , ) = aaveData.getReserveTokensAddresses(_token);
|
||||||
|
data._supplyTokens[i] = _token;
|
||||||
|
data.aTokens[i] = ATokenInterface(_aToken);
|
||||||
|
data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _paybackBehalfOne(
|
||||||
|
AaveInterface aave,
|
||||||
|
address token,
|
||||||
|
uint256 amt,
|
||||||
|
uint256 rateMode,
|
||||||
|
address user
|
||||||
|
) private {
|
||||||
|
aave.repay(token, amt, rateMode, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _PaybackStable(
|
||||||
|
uint256 _length,
|
||||||
|
AaveInterface aave,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address user
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_paybackBehalfOne(aave, tokens[i], amts[i], 1, user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _PaybackVariable(
|
||||||
|
uint256 _length,
|
||||||
|
AaveInterface aave,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address user
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_paybackBehalfOne(aave, tokens[i], amts[i], 2, user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _TransferAtokens(
|
||||||
|
uint256 _length,
|
||||||
|
AaveInterface aave,
|
||||||
|
ATokenInterface[] memory atokenContracts,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address[] memory tokens,
|
||||||
|
address userAccount
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
uint256 _amt = amts[i];
|
||||||
|
require(
|
||||||
|
atokenContracts[i].transferFrom(
|
||||||
|
userAccount,
|
||||||
|
address(this),
|
||||||
|
_amt
|
||||||
|
),
|
||||||
|
"allowance?"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!getIsColl(tokens[i], address(this))) {
|
||||||
|
aave.setUserUseReserveAsCollateral(tokens[i], true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _BorrowVariable(
|
||||||
|
uint256 _length,
|
||||||
|
AaveInterface aave,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_borrowOne(aave, tokens[i], amts[i], 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _BorrowStable(
|
||||||
|
uint256 _length,
|
||||||
|
AaveInterface aave,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_borrowOne(aave, tokens[i], amts[i], 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _borrowOne(
|
||||||
|
AaveInterface aave,
|
||||||
|
address token,
|
||||||
|
uint256 amt,
|
||||||
|
uint256 rateMode
|
||||||
|
) private {
|
||||||
|
aave.borrow(token, amt, rateMode, referalCode, address(this));
|
||||||
|
}
|
||||||
|
}
|
94
contracts/fantom/connectors/aave/v3-import/interface.sol
Normal file
94
contracts/fantom/connectors/aave/v3-import/interface.sol
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
interface AaveInterface {
|
||||||
|
function supply(
|
||||||
|
address asset,
|
||||||
|
uint256 amount,
|
||||||
|
address onBehalfOf,
|
||||||
|
uint16 referralCode
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function withdraw(
|
||||||
|
address _asset,
|
||||||
|
uint256 _amount,
|
||||||
|
address _to
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function borrow(
|
||||||
|
address _asset,
|
||||||
|
uint256 _amount,
|
||||||
|
uint256 _interestRateMode,
|
||||||
|
uint16 _referralCode,
|
||||||
|
address _onBehalfOf
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function repay(
|
||||||
|
address _asset,
|
||||||
|
uint256 _amount,
|
||||||
|
uint256 _rateMode,
|
||||||
|
address _onBehalfOf
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function setUserUseReserveAsCollateral(
|
||||||
|
address _asset,
|
||||||
|
bool _useAsCollateral
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function swapBorrowRateMode(address _asset, uint256 _rateMode) external;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ATokenInterface {
|
||||||
|
function scaledBalanceOf(address _user) external view returns (uint256);
|
||||||
|
|
||||||
|
function isTransferAllowed(address _user, uint256 _amount)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (bool);
|
||||||
|
|
||||||
|
function balanceOf(address _user) external view returns (uint256);
|
||||||
|
|
||||||
|
function transferFrom(
|
||||||
|
address,
|
||||||
|
address,
|
||||||
|
uint256
|
||||||
|
) external returns (bool);
|
||||||
|
|
||||||
|
function allowance(address, address) external returns (uint256);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AavePoolProviderInterface {
|
||||||
|
function getPool() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AaveDataProviderInterface {
|
||||||
|
function getReserveTokensAddresses(address _asset)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (
|
||||||
|
address aTokenAddress,
|
||||||
|
address stableDebtTokenAddress,
|
||||||
|
address variableDebtTokenAddress
|
||||||
|
);
|
||||||
|
|
||||||
|
function getUserReserveData(address _asset, address _user)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (
|
||||||
|
uint256 currentATokenBalance,
|
||||||
|
uint256 currentStableDebt,
|
||||||
|
uint256 currentVariableDebt,
|
||||||
|
uint256 principalStableDebt,
|
||||||
|
uint256 scaledVariableDebt,
|
||||||
|
uint256 stableBorrowRate,
|
||||||
|
uint256 liquidityRate,
|
||||||
|
uint40 stableRateLastUpdated,
|
||||||
|
bool usageAsCollateralEnabled
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AaveAddressProviderRegistryInterface {
|
||||||
|
function getAddressesProvidersList()
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (address[] memory);
|
||||||
|
}
|
111
contracts/fantom/connectors/aave/v3-import/main.sol
Normal file
111
contracts/fantom/connectors/aave/v3-import/main.sol
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
/**
|
||||||
|
* @title Aave v3 import connector .
|
||||||
|
* @dev Import EOA's aave V3 position to DSA's aave v3 position
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol";
|
||||||
|
import { AaveInterface, ATokenInterface } from "./interface.sol";
|
||||||
|
import "./helpers.sol";
|
||||||
|
import "./events.sol";
|
||||||
|
|
||||||
|
contract AaveV3ImportResolver is AaveHelpers {
|
||||||
|
function _importAave(address userAccount, ImportInputData memory inputData)
|
||||||
|
internal
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
require(
|
||||||
|
AccountInterface(address(this)).isAuth(userAccount),
|
||||||
|
"user-account-not-auth"
|
||||||
|
);
|
||||||
|
|
||||||
|
require(inputData.supplyTokens.length > 0, "0-length-not-allowed");
|
||||||
|
|
||||||
|
ImportData memory data;
|
||||||
|
|
||||||
|
AaveInterface aave = AaveInterface(aaveProvider.getPool());
|
||||||
|
|
||||||
|
data = getBorrowAmounts(userAccount, aave, inputData, data);
|
||||||
|
data = getSupplyAmounts(userAccount, inputData, data);
|
||||||
|
|
||||||
|
// payback borrowed amount;
|
||||||
|
_PaybackStable(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aave,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.stableBorrowAmts,
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
_PaybackVariable(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aave,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.variableBorrowAmts,
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
|
||||||
|
// transfer atokens to this address;
|
||||||
|
_TransferAtokens(
|
||||||
|
data._supplyTokens.length,
|
||||||
|
aave,
|
||||||
|
data.aTokens,
|
||||||
|
data.supplyAmts,
|
||||||
|
data._supplyTokens,
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
|
||||||
|
// borrow assets after migrating position
|
||||||
|
if (data.convertStable) {
|
||||||
|
_BorrowVariable(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aave,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.totalBorrowAmtsWithFee
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
_BorrowStable(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aave,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.stableBorrowAmtsWithFee
|
||||||
|
);
|
||||||
|
_BorrowVariable(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aave,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.variableBorrowAmtsWithFee
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
_eventName = "LogAaveV3Import(address,bool,address[],address[],uint256[],uint256[],uint256[],uint256[])";
|
||||||
|
_eventParam = abi.encode(
|
||||||
|
userAccount,
|
||||||
|
inputData.convertStable,
|
||||||
|
inputData.supplyTokens,
|
||||||
|
inputData.borrowTokens,
|
||||||
|
inputData.flashLoanFees,
|
||||||
|
data.supplyAmts,
|
||||||
|
data.stableBorrowAmts,
|
||||||
|
data.variableBorrowAmts
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Import aave V3 position .
|
||||||
|
* @notice Import EOA's aave V3 position to DSA's aave v3 position
|
||||||
|
* @param userAccount The address of the EOA from which aave position will be imported
|
||||||
|
* @param inputData The struct containing all the neccessary input data
|
||||||
|
*/
|
||||||
|
function importAave(address userAccount, ImportInputData memory inputData)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
(_eventName, _eventParam) = _importAave(userAccount, inputData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2AaveV3ImportFantom is AaveV3ImportResolver {
|
||||||
|
string public constant name = "Aave-v3-import-v1";
|
||||||
|
}
|
|
@ -36,36 +36,31 @@ const PRIVATE_KEY = process.env.PRIVATE_KEY;
|
||||||
const ETHERSCAN_API = process.env.ETHERSCAN_API_KEY;
|
const ETHERSCAN_API = process.env.ETHERSCAN_API_KEY;
|
||||||
const POLYGONSCAN_API = process.env.POLYGON_API_KEY;
|
const POLYGONSCAN_API = process.env.POLYGON_API_KEY;
|
||||||
const ARBISCAN_API = process.env.ARBISCAN_API_KEY;
|
const ARBISCAN_API = process.env.ARBISCAN_API_KEY;
|
||||||
const OPTIMISM_API = process.env.OPTIMISM_API_KEY;
|
|
||||||
const SNOWTRACE_API = process.env.SNOWTRACE_API_KEY;
|
const SNOWTRACE_API = process.env.SNOWTRACE_API_KEY;
|
||||||
const mnemonic =
|
const FANTOMSCAN_API = process.env.FANTOM_API_KEY;
|
||||||
process.env.MNEMONIC ??
|
const mnemonic = process.env.MNEMONIC ?? "test test test test test test test test test test test junk";
|
||||||
"test test test test test test test test test test test junk";
|
|
||||||
|
|
||||||
const networkGasPriceConfig: Record<string, string> = {
|
const networkGasPriceConfig: Record<string, string> = {
|
||||||
"mainnet": "160",
|
mainnet: "160",
|
||||||
"polygon": "50",
|
polygon: "50",
|
||||||
"avalanche": "50",
|
avalanche: "50",
|
||||||
"arbitrum": "2"
|
arbitrum: "2"
|
||||||
}
|
};
|
||||||
|
|
||||||
function createConfig(network: string) {
|
function createConfig(network: string) {
|
||||||
return {
|
return {
|
||||||
url: getNetworkUrl(network),
|
url: getNetworkUrl(network),
|
||||||
accounts: !!PRIVATE_KEY ? [`0x${PRIVATE_KEY}`] : { mnemonic },
|
accounts: !!PRIVATE_KEY ? [`0x${PRIVATE_KEY}`] : { mnemonic },
|
||||||
gasPrice: 1000000, // 0.0001 GWEI
|
gasPrice: 220000000000 // 0.0001 GWEI
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNetworkUrl(networkType: string) {
|
function getNetworkUrl(networkType: string) {
|
||||||
if (networkType === "avalanche")
|
if (networkType === "avalanche") return "https://api.avax.network/ext/bc/C/rpc";
|
||||||
return "https://api.avax.network/ext/bc/C/rpc";
|
else if (networkType === "polygon") return `https://polygon-mainnet.g.alchemy.com/v2/${alchemyApiKey}`;
|
||||||
else if (networkType === "polygon")
|
else if (networkType === "arbitrum") return `https://arb-mainnet.g.alchemy.com/v2/${alchemyApiKey}`;
|
||||||
return `https://polygon-mainnet.g.alchemy.com/v2/${alchemyApiKey}`;
|
else if (networkType === "optimism") return `https://opt-mainnet.g.alchemy.com/v2/${alchemyApiKey}`;
|
||||||
else if (networkType === "arbitrum")
|
else if (networkType === "fantom") return `https://rpc.ftm.tools/`;
|
||||||
return `https://arb-mainnet.g.alchemy.com/v2/${alchemyApiKey}`;
|
|
||||||
else if (networkType === "optimism")
|
|
||||||
return `https://opt-mainnet.g.alchemy.com/v2/${alchemyApiKey}`;
|
|
||||||
else return `https://eth-mainnet.alchemyapi.io/v2/${alchemyApiKey}`;
|
else return `https://eth-mainnet.alchemyapi.io/v2/${alchemyApiKey}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +68,7 @@ function getScanApiKey(networkType: string) {
|
||||||
if (networkType === "avalanche") return SNOWTRACE_API;
|
if (networkType === "avalanche") return SNOWTRACE_API;
|
||||||
else if (networkType === "polygon") return POLYGONSCAN_API;
|
else if (networkType === "polygon") return POLYGONSCAN_API;
|
||||||
else if (networkType === "arbitrum") return ARBISCAN_API;
|
else if (networkType === "arbitrum") return ARBISCAN_API;
|
||||||
else if (networkType === "optimism") return OPTIMISM_API;
|
else if (networkType === "fantom") return FANTOMSCAN_API;
|
||||||
else return ETHERSCAN_API;
|
else return ETHERSCAN_API;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,53 +83,54 @@ const config: HardhatUserConfig = {
|
||||||
settings: {
|
settings: {
|
||||||
optimizer: {
|
optimizer: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
runs: 200,
|
runs: 200
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
version: "0.6.0",
|
version: "0.6.0"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
version: "0.6.2",
|
version: "0.6.2"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
version: "0.6.5",
|
version: "0.6.5"
|
||||||
},
|
}
|
||||||
],
|
]
|
||||||
},
|
},
|
||||||
networks: {
|
networks: {
|
||||||
hardhat: {
|
hardhat: {
|
||||||
accounts: {
|
accounts: {
|
||||||
mnemonic,
|
mnemonic
|
||||||
},
|
},
|
||||||
chainId: chainIds.hardhat,
|
chainId: chainIds.hardhat,
|
||||||
forking: {
|
forking: {
|
||||||
url: String(getNetworkUrl(String(process.env.networkType))),
|
url: String(getNetworkUrl(String(process.env.networkType)))
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
mainnet: createConfig("mainnet"),
|
mainnet: createConfig("mainnet"),
|
||||||
polygon: createConfig("polygon"),
|
polygon: createConfig("polygon"),
|
||||||
avalanche: createConfig("avalanche"),
|
avalanche: createConfig("avalanche"),
|
||||||
arbitrum: createConfig("arbitrum"),
|
arbitrum: createConfig("arbitrum"),
|
||||||
optimism: createConfig("optimism"),
|
optimism: createConfig("optimism"),
|
||||||
|
fantom: createConfig("optimism")
|
||||||
},
|
},
|
||||||
paths: {
|
paths: {
|
||||||
artifacts: "./artifacts",
|
artifacts: "./artifacts",
|
||||||
cache: "./cache",
|
cache: "./cache",
|
||||||
sources: "./contracts",
|
sources: "./contracts",
|
||||||
tests: "./test",
|
tests: "./test"
|
||||||
},
|
},
|
||||||
etherscan: {
|
etherscan: {
|
||||||
apiKey: getScanApiKey(String(process.env.networkType)),
|
apiKey: getScanApiKey(String(process.env.networkType))
|
||||||
},
|
},
|
||||||
typechain: {
|
typechain: {
|
||||||
outDir: "typechain",
|
outDir: "typechain",
|
||||||
target: "ethers-v5",
|
target: "ethers-v5"
|
||||||
},
|
},
|
||||||
mocha: {
|
mocha: {
|
||||||
timeout: 10000 * 1000, // 10,000 seconds
|
timeout: 10000 * 1000 // 10,000 seconds
|
||||||
},
|
}
|
||||||
// tenderly: {
|
// tenderly: {
|
||||||
// project: process.env.TENDERLY_PROJECT,
|
// project: process.env.TENDERLY_PROJECT,
|
||||||
// username: process.env.TENDERLY_USERNAME,
|
// username: process.env.TENDERLY_USERNAME,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user