mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Merge pull request #185 from Instadapp/aave-v2-to-v3-import
Aave v2 to v3 import
This commit is contained in:
commit
25db24f8bd
|
|
@ -0,0 +1,15 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogAaveImportV2ToV3(
|
||||||
|
address indexed user,
|
||||||
|
bool doImport,
|
||||||
|
bool convertStable,
|
||||||
|
address[] supplyTokens,
|
||||||
|
address[] borrowTokens,
|
||||||
|
uint256[] flashLoanFees,
|
||||||
|
uint256[] supplyAmts,
|
||||||
|
uint256[] stableBorrowAmts,
|
||||||
|
uint256[] variableBorrowAmts
|
||||||
|
);
|
||||||
|
}
|
||||||
328
contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol
Normal file
328
contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol
Normal file
|
|
@ -0,0 +1,328 @@
|
||||||
|
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 "./events.sol";
|
||||||
|
import "./interfaces.sol";
|
||||||
|
|
||||||
|
abstract contract Helper is DSMath, Basic {
|
||||||
|
/**
|
||||||
|
* @dev Aave referal code
|
||||||
|
*/
|
||||||
|
uint16 internal constant referalCode = 3228;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev AaveV2 Lending Pool Provider
|
||||||
|
*/
|
||||||
|
AaveV2LendingPoolProviderInterface internal constant aaveV2Provider =
|
||||||
|
AaveV2LendingPoolProviderInterface(
|
||||||
|
0xb6A86025F0FE1862B372cb0ca18CE3EDe02A318f // v2 address: LendingPoolAddressProvider avax
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev AaveV3 Lending Pool Provider
|
||||||
|
*/
|
||||||
|
AaveV3PoolProviderInterface internal constant aaveV3Provider =
|
||||||
|
AaveV3PoolProviderInterface(
|
||||||
|
0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb // v3 - PoolAddressesProvider Avalanche
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Aave Protocol Data Provider
|
||||||
|
*/
|
||||||
|
AaveV2DataProviderInterface internal constant aaveV2Data =
|
||||||
|
AaveV2DataProviderInterface(0x65285E9dfab318f57051ab2b139ccCf232945451); // aave v2 - avax
|
||||||
|
|
||||||
|
function getIsColl(address token, address user)
|
||||||
|
internal
|
||||||
|
view
|
||||||
|
returns (bool isCol)
|
||||||
|
{
|
||||||
|
(, , , , , , , , isCol) = aaveV2Data.getUserReserveData(token, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ImportData {
|
||||||
|
address[] _supplyTokens;
|
||||||
|
address[] _borrowTokens;
|
||||||
|
ATokenV2Interface[] 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 _AaveHelper is Helper {
|
||||||
|
function getBorrowAmountV2(address _token, address userAccount)
|
||||||
|
internal
|
||||||
|
view
|
||||||
|
returns (uint256 stableBorrow, uint256 variableBorrow)
|
||||||
|
{
|
||||||
|
(
|
||||||
|
,
|
||||||
|
address stableDebtTokenAddress,
|
||||||
|
address variableDebtTokenAddress
|
||||||
|
) = aaveV2Data.getReserveTokensAddresses(_token);
|
||||||
|
|
||||||
|
stableBorrow = ATokenV2Interface(stableDebtTokenAddress).balanceOf(
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
variableBorrow = ATokenV2Interface(variableDebtTokenAddress).balanceOf(
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBorrowAmountsV2(
|
||||||
|
address userAccount,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
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.stableBorrowAmts = new uint256[](
|
||||||
|
inputData.borrowTokens.length
|
||||||
|
);
|
||||||
|
data.totalBorrowAmts = 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] == avaxAddr
|
||||||
|
? wavaxAddr
|
||||||
|
: inputData.borrowTokens[i];
|
||||||
|
data._borrowTokens[i] = _token;
|
||||||
|
|
||||||
|
(
|
||||||
|
data.stableBorrowAmts[i],
|
||||||
|
data.variableBorrowAmts[i]
|
||||||
|
) = getBorrowAmountV2(_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(aaveV2), _amt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSupplyAmountsV2(
|
||||||
|
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 ATokenV2Interface[](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] == avaxAddr
|
||||||
|
? wavaxAddr
|
||||||
|
: inputData.supplyTokens[i];
|
||||||
|
(address _aToken, , ) = aaveV2Data.getReserveTokensAddresses(
|
||||||
|
_token
|
||||||
|
);
|
||||||
|
data._supplyTokens[i] = _token;
|
||||||
|
data.aTokens[i] = ATokenV2Interface(_aToken);
|
||||||
|
data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _paybackBehalfOneV2(
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
address token,
|
||||||
|
uint256 amt,
|
||||||
|
uint256 rateMode,
|
||||||
|
address user
|
||||||
|
) private {
|
||||||
|
aaveV2.repay(token, amt, rateMode, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _PaybackStableV2(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address user
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_paybackBehalfOneV2(aaveV2, tokens[i], amts[i], 1, user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _PaybackVariableV2(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address user
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_paybackBehalfOneV2(aaveV2, tokens[i], amts[i], 2, user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _TransferAtokensV2(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
ATokenV2Interface[] 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))) {
|
||||||
|
aaveV2.setUserUseReserveAsCollateral(tokens[i], true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _WithdrawTokensFromV2(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address[] memory tokens
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
uint256 _amt = amts[i];
|
||||||
|
address _token = tokens[i];
|
||||||
|
aaveV2.withdraw(_token, _amt, address(this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _depositTokensV3(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV3Interface aaveV3,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address[] memory tokens
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
uint256 _amt = amts[i];
|
||||||
|
address _token = tokens[i];
|
||||||
|
TokenInterface tokenContract = TokenInterface(_token);
|
||||||
|
require(
|
||||||
|
tokenContract.balanceOf(address(this)) >= _amt,
|
||||||
|
"Insufficient funds to deposit in v3"
|
||||||
|
);
|
||||||
|
approve(tokenContract, address(aaveV3), _amt);
|
||||||
|
aaveV3.supply(_token, _amt, address(this), referalCode);
|
||||||
|
|
||||||
|
if (!getIsColl(_token, address(this))) {
|
||||||
|
aaveV3.setUserUseReserveAsCollateral(_token, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _BorrowVariableV3(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV3Interface aaveV3,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_borrowOneV3(aaveV3, tokens[i], amts[i], 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _BorrowStableV3(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV3Interface aaveV3,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_borrowOneV3(aaveV3, tokens[i], amts[i], 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _borrowOneV3(
|
||||||
|
AaveV3Interface aaveV3,
|
||||||
|
address token,
|
||||||
|
uint256 amt,
|
||||||
|
uint256 rateMode
|
||||||
|
) private {
|
||||||
|
aaveV3.borrow(token, amt, rateMode, referalCode, address(this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,150 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
// aave v2
|
||||||
|
interface AaveV2Interface {
|
||||||
|
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 getUserAccountData(address user)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (
|
||||||
|
uint256 totalCollateralETH,
|
||||||
|
uint256 totalDebtETH,
|
||||||
|
uint256 availableBorrowsETH,
|
||||||
|
uint256 currentLiquidationThreshold,
|
||||||
|
uint256 ltv,
|
||||||
|
uint256 healthFactor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AaveV2LendingPoolProviderInterface {
|
||||||
|
function getLendingPool() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aave Protocol Data Provider
|
||||||
|
interface AaveV2DataProviderInterface {
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
// function getReserveConfigurationData(address asset)
|
||||||
|
// external
|
||||||
|
// view
|
||||||
|
// returns (
|
||||||
|
// uint256 decimals,
|
||||||
|
// uint256 ltv,
|
||||||
|
// uint256 liquidationThreshold,
|
||||||
|
// uint256 liquidationBonus,
|
||||||
|
// uint256 reserveFactor,
|
||||||
|
// bool usageAsCollateralEnabled,
|
||||||
|
// bool borrowingEnabled,
|
||||||
|
// bool stableBorrowRateEnabled,
|
||||||
|
// bool isActive,
|
||||||
|
// bool isFrozen
|
||||||
|
// );
|
||||||
|
|
||||||
|
function getReserveTokensAddresses(address asset)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (
|
||||||
|
address aTokenAddress,
|
||||||
|
address stableDebtTokenAddress,
|
||||||
|
address variableDebtTokenAddress
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ATokenV2Interface {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// aave v3
|
||||||
|
interface AaveV3Interface {
|
||||||
|
function supply(
|
||||||
|
address asset,
|
||||||
|
uint256 amount,
|
||||||
|
address onBehalfOf,
|
||||||
|
uint16 referralCode
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function withdraw(
|
||||||
|
address asset,
|
||||||
|
uint256 amount,
|
||||||
|
address to
|
||||||
|
) external returns (uint256);
|
||||||
|
|
||||||
|
function borrow(
|
||||||
|
address asset,
|
||||||
|
uint256 amount,
|
||||||
|
uint256 interestRateMode,
|
||||||
|
uint16 referralCode,
|
||||||
|
address onBehalfOf
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function repay(
|
||||||
|
address asset,
|
||||||
|
uint256 amount,
|
||||||
|
uint256 interestRateMode,
|
||||||
|
address onBehalfOf
|
||||||
|
) external returns (uint256);
|
||||||
|
|
||||||
|
function setUserUseReserveAsCollateral(address asset, bool useAsCollateral)
|
||||||
|
external;
|
||||||
|
|
||||||
|
function swapBorrowRateMode(address asset, uint256 interestRateMode)
|
||||||
|
external;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AaveV3PoolProviderInterface {
|
||||||
|
function getPool() external view returns (address);
|
||||||
|
}
|
||||||
150
contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol
Normal file
150
contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol
Normal file
|
|
@ -0,0 +1,150 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol";
|
||||||
|
import "./interfaces.sol";
|
||||||
|
import "./helpers.sol";
|
||||||
|
import "./events.sol";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title Aave v2 to v3 import connector .
|
||||||
|
* @dev migrate aave V2 position to aave v3 position
|
||||||
|
*/
|
||||||
|
contract _AaveV2ToV3MigrationResolver is _AaveHelper {
|
||||||
|
function _importAave(
|
||||||
|
address userAccount,
|
||||||
|
ImportInputData memory inputData,
|
||||||
|
bool doImport
|
||||||
|
) internal returns (string memory _eventName, bytes memory _eventParam) {
|
||||||
|
if (doImport) {
|
||||||
|
// check only when we are importing from user's address
|
||||||
|
require(
|
||||||
|
AccountInterface(address(this)).isAuth(userAccount),
|
||||||
|
"user-account-not-auth"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
require(inputData.supplyTokens.length > 0, "0-length-not-allowed");
|
||||||
|
|
||||||
|
ImportData memory data;
|
||||||
|
|
||||||
|
AaveV2Interface aaveV2 = AaveV2Interface(
|
||||||
|
aaveV2Provider.getLendingPool()
|
||||||
|
);
|
||||||
|
AaveV3Interface aaveV3 = AaveV3Interface(aaveV3Provider.getPool());
|
||||||
|
|
||||||
|
data = getBorrowAmountsV2(userAccount, aaveV2, inputData, data);
|
||||||
|
data = getSupplyAmountsV2(userAccount, inputData, data);
|
||||||
|
|
||||||
|
// payback borrowed amount;
|
||||||
|
_PaybackStableV2(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV2,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.stableBorrowAmts,
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
_PaybackVariableV2(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV2,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.variableBorrowAmts,
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
|
||||||
|
if (doImport) {
|
||||||
|
// transfer atokens to this address;
|
||||||
|
_TransferAtokensV2(
|
||||||
|
data._supplyTokens.length,
|
||||||
|
aaveV2,
|
||||||
|
data.aTokens,
|
||||||
|
data.supplyAmts,
|
||||||
|
data._supplyTokens,
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// withdraw v2 supplied tokens
|
||||||
|
_WithdrawTokensFromV2(
|
||||||
|
data._supplyTokens.length,
|
||||||
|
aaveV2,
|
||||||
|
data.supplyAmts,
|
||||||
|
data._supplyTokens
|
||||||
|
);
|
||||||
|
// deposit tokens in v3
|
||||||
|
_depositTokensV3(
|
||||||
|
data._supplyTokens.length,
|
||||||
|
aaveV3,
|
||||||
|
data.supplyAmts,
|
||||||
|
data._supplyTokens
|
||||||
|
);
|
||||||
|
|
||||||
|
// borrow assets in aave v3 after migrating position
|
||||||
|
if (data.convertStable) {
|
||||||
|
_BorrowVariableV3(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV3,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.totalBorrowAmtsWithFee
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
_BorrowStableV3(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV3,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.stableBorrowAmtsWithFee
|
||||||
|
);
|
||||||
|
_BorrowVariableV3(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV3,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.variableBorrowAmtsWithFee
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
_eventName = "LogAaveImportV2ToV3(address,bool,bool,address[],address[],uint256[],uint256[],uint256[],uint256[])";
|
||||||
|
_eventParam = abi.encode(
|
||||||
|
userAccount,
|
||||||
|
doImport,
|
||||||
|
inputData.convertStable,
|
||||||
|
inputData.supplyTokens,
|
||||||
|
inputData.borrowTokens,
|
||||||
|
inputData.flashLoanFees,
|
||||||
|
data.supplyAmts,
|
||||||
|
data.stableBorrowAmts,
|
||||||
|
data.variableBorrowAmts
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Import aave position .
|
||||||
|
* @notice Import EOA's aave V2 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 importAaveV2ToV3(
|
||||||
|
address userAccount,
|
||||||
|
ImportInputData memory inputData
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
(_eventName, _eventParam) = _importAave(userAccount, inputData, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Migrate aave position .
|
||||||
|
* @notice Migrate DSA's aave V2 position to DSA's aave v3 position
|
||||||
|
* @param inputData The struct containing all the neccessary input data
|
||||||
|
*/
|
||||||
|
function migrateAaveV2ToV3(ImportInputData memory inputData)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
(_eventName, _eventParam) = _importAave(msg.sender, inputData, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2AaveV2ToV3MigrationAvalanche is _AaveV2ToV3MigrationResolver {
|
||||||
|
string public constant name = "Aave-Import-v2-to-v3";
|
||||||
|
}
|
||||||
15
contracts/polygon/connectors/aave/v2-to-v3-import/events.sol
Normal file
15
contracts/polygon/connectors/aave/v2-to-v3-import/events.sol
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogAaveImportV2ToV3(
|
||||||
|
address indexed user,
|
||||||
|
bool doImport,
|
||||||
|
bool convertStable,
|
||||||
|
address[] supplyTokens,
|
||||||
|
address[] borrowTokens,
|
||||||
|
uint256[] flashLoanFees,
|
||||||
|
uint256[] supplyAmts,
|
||||||
|
uint256[] stableBorrowAmts,
|
||||||
|
uint256[] variableBorrowAmts
|
||||||
|
);
|
||||||
|
}
|
||||||
328
contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol
Normal file
328
contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol
Normal file
|
|
@ -0,0 +1,328 @@
|
||||||
|
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 "./events.sol";
|
||||||
|
import "./interfaces.sol";
|
||||||
|
|
||||||
|
abstract contract Helper is DSMath, Basic {
|
||||||
|
/**
|
||||||
|
* @dev Aave referal code
|
||||||
|
*/
|
||||||
|
uint16 internal constant referalCode = 3228;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev AaveV2 Lending Pool Provider
|
||||||
|
*/
|
||||||
|
AaveV2LendingPoolProviderInterface internal constant aaveV2Provider =
|
||||||
|
AaveV2LendingPoolProviderInterface(
|
||||||
|
0xd05e3E715d945B59290df0ae8eF85c1BdB684744 // v2 address
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev AaveV3 Lending Pool Provider
|
||||||
|
*/
|
||||||
|
AaveV3PoolProviderInterface internal constant aaveV3Provider =
|
||||||
|
AaveV3PoolProviderInterface(
|
||||||
|
0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb // Polygon address - PoolAddressesProvider
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Aave Protocol Data Provider
|
||||||
|
*/
|
||||||
|
AaveV2DataProviderInterface internal constant aaveV2Data =
|
||||||
|
AaveV2DataProviderInterface(0x7551b5D2763519d4e37e8B81929D336De671d46d); // v2 address
|
||||||
|
|
||||||
|
function getIsColl(address token, address user)
|
||||||
|
internal
|
||||||
|
view
|
||||||
|
returns (bool isCol)
|
||||||
|
{
|
||||||
|
(, , , , , , , , isCol) = aaveV2Data.getUserReserveData(token, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ImportData {
|
||||||
|
address[] _supplyTokens;
|
||||||
|
address[] _borrowTokens;
|
||||||
|
ATokenV2Interface[] 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 _AaveHelper is Helper {
|
||||||
|
function getBorrowAmountV2(address _token, address userAccount)
|
||||||
|
internal
|
||||||
|
view
|
||||||
|
returns (uint256 stableBorrow, uint256 variableBorrow)
|
||||||
|
{
|
||||||
|
(
|
||||||
|
,
|
||||||
|
address stableDebtTokenAddress,
|
||||||
|
address variableDebtTokenAddress
|
||||||
|
) = aaveV2Data.getReserveTokensAddresses(_token);
|
||||||
|
|
||||||
|
stableBorrow = ATokenV2Interface(stableDebtTokenAddress).balanceOf(
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
variableBorrow = ATokenV2Interface(variableDebtTokenAddress).balanceOf(
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBorrowAmountsV2(
|
||||||
|
address userAccount,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
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.stableBorrowAmts = new uint256[](
|
||||||
|
inputData.borrowTokens.length
|
||||||
|
);
|
||||||
|
data.totalBorrowAmts = 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] == maticAddr
|
||||||
|
? wmaticAddr
|
||||||
|
: inputData.borrowTokens[i];
|
||||||
|
data._borrowTokens[i] = _token;
|
||||||
|
|
||||||
|
(
|
||||||
|
data.stableBorrowAmts[i],
|
||||||
|
data.variableBorrowAmts[i]
|
||||||
|
) = getBorrowAmountV2(_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(aaveV2), _amt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSupplyAmountsV2(
|
||||||
|
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 ATokenV2Interface[](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] == maticAddr
|
||||||
|
? wmaticAddr
|
||||||
|
: inputData.supplyTokens[i];
|
||||||
|
(address _aToken, , ) = aaveV2Data.getReserveTokensAddresses(
|
||||||
|
_token
|
||||||
|
);
|
||||||
|
data._supplyTokens[i] = _token;
|
||||||
|
data.aTokens[i] = ATokenV2Interface(_aToken);
|
||||||
|
data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _paybackBehalfOneV2(
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
address token,
|
||||||
|
uint256 amt,
|
||||||
|
uint256 rateMode,
|
||||||
|
address user
|
||||||
|
) private {
|
||||||
|
aaveV2.repay(token, amt, rateMode, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _PaybackStableV2(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address user
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_paybackBehalfOneV2(aaveV2, tokens[i], amts[i], 1, user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _PaybackVariableV2(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address user
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_paybackBehalfOneV2(aaveV2, tokens[i], amts[i], 2, user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _TransferAtokensV2(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
ATokenV2Interface[] 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))) {
|
||||||
|
aaveV2.setUserUseReserveAsCollateral(tokens[i], true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _WithdrawTokensFromV2(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV2Interface aaveV2,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address[] memory tokens
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
uint256 _amt = amts[i];
|
||||||
|
address _token = tokens[i];
|
||||||
|
aaveV2.withdraw(_token, _amt, address(this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _depositTokensV3(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV3Interface aaveV3,
|
||||||
|
uint256[] memory amts,
|
||||||
|
address[] memory tokens
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
uint256 _amt = amts[i];
|
||||||
|
address _token = tokens[i];
|
||||||
|
TokenInterface tokenContract = TokenInterface(_token);
|
||||||
|
require(
|
||||||
|
tokenContract.balanceOf(address(this)) >= _amt,
|
||||||
|
"Insufficient funds to deposit in v3"
|
||||||
|
);
|
||||||
|
approve(tokenContract, address(aaveV3), _amt);
|
||||||
|
aaveV3.supply(_token, _amt, address(this), referalCode);
|
||||||
|
|
||||||
|
if (!getIsColl(_token, address(this))) {
|
||||||
|
aaveV3.setUserUseReserveAsCollateral(_token, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _BorrowVariableV3(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV3Interface aaveV3,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_borrowOneV3(aaveV3, tokens[i], amts[i], 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _BorrowStableV3(
|
||||||
|
uint256 _length,
|
||||||
|
AaveV3Interface aaveV3,
|
||||||
|
address[] memory tokens,
|
||||||
|
uint256[] memory amts
|
||||||
|
) internal {
|
||||||
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
|
if (amts[i] > 0) {
|
||||||
|
_borrowOneV3(aaveV3, tokens[i], amts[i], 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _borrowOneV3(
|
||||||
|
AaveV3Interface aaveV3,
|
||||||
|
address token,
|
||||||
|
uint256 amt,
|
||||||
|
uint256 rateMode
|
||||||
|
) private {
|
||||||
|
aaveV3.borrow(token, amt, rateMode, referalCode, address(this));
|
||||||
|
}
|
||||||
|
}
|
||||||
150
contracts/polygon/connectors/aave/v2-to-v3-import/interfaces.sol
Normal file
150
contracts/polygon/connectors/aave/v2-to-v3-import/interfaces.sol
Normal file
|
|
@ -0,0 +1,150 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
// aave v2
|
||||||
|
interface AaveV2Interface {
|
||||||
|
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 getUserAccountData(address user)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (
|
||||||
|
uint256 totalCollateralETH,
|
||||||
|
uint256 totalDebtETH,
|
||||||
|
uint256 availableBorrowsETH,
|
||||||
|
uint256 currentLiquidationThreshold,
|
||||||
|
uint256 ltv,
|
||||||
|
uint256 healthFactor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AaveV2LendingPoolProviderInterface {
|
||||||
|
function getLendingPool() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aave Protocol Data Provider
|
||||||
|
interface AaveV2DataProviderInterface {
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
// function getReserveConfigurationData(address asset)
|
||||||
|
// external
|
||||||
|
// view
|
||||||
|
// returns (
|
||||||
|
// uint256 decimals,
|
||||||
|
// uint256 ltv,
|
||||||
|
// uint256 liquidationThreshold,
|
||||||
|
// uint256 liquidationBonus,
|
||||||
|
// uint256 reserveFactor,
|
||||||
|
// bool usageAsCollateralEnabled,
|
||||||
|
// bool borrowingEnabled,
|
||||||
|
// bool stableBorrowRateEnabled,
|
||||||
|
// bool isActive,
|
||||||
|
// bool isFrozen
|
||||||
|
// );
|
||||||
|
|
||||||
|
function getReserveTokensAddresses(address asset)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (
|
||||||
|
address aTokenAddress,
|
||||||
|
address stableDebtTokenAddress,
|
||||||
|
address variableDebtTokenAddress
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ATokenV2Interface {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// aave v3
|
||||||
|
interface AaveV3Interface {
|
||||||
|
function supply(
|
||||||
|
address asset,
|
||||||
|
uint256 amount,
|
||||||
|
address onBehalfOf,
|
||||||
|
uint16 referralCode
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function withdraw(
|
||||||
|
address asset,
|
||||||
|
uint256 amount,
|
||||||
|
address to
|
||||||
|
) external returns (uint256);
|
||||||
|
|
||||||
|
function borrow(
|
||||||
|
address asset,
|
||||||
|
uint256 amount,
|
||||||
|
uint256 interestRateMode,
|
||||||
|
uint16 referralCode,
|
||||||
|
address onBehalfOf
|
||||||
|
) external;
|
||||||
|
|
||||||
|
function repay(
|
||||||
|
address asset,
|
||||||
|
uint256 amount,
|
||||||
|
uint256 interestRateMode,
|
||||||
|
address onBehalfOf
|
||||||
|
) external returns (uint256);
|
||||||
|
|
||||||
|
function setUserUseReserveAsCollateral(address asset, bool useAsCollateral)
|
||||||
|
external;
|
||||||
|
|
||||||
|
function swapBorrowRateMode(address asset, uint256 interestRateMode)
|
||||||
|
external;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AaveV3PoolProviderInterface {
|
||||||
|
function getPool() external view returns (address);
|
||||||
|
}
|
||||||
160
contracts/polygon/connectors/aave/v2-to-v3-import/main.sol
Normal file
160
contracts/polygon/connectors/aave/v2-to-v3-import/main.sol
Normal file
|
|
@ -0,0 +1,160 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title Aave v2 to v3 import connector .
|
||||||
|
* @dev migrate aave V2 position to aave v3 position
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol";
|
||||||
|
import "./interfaces.sol";
|
||||||
|
import "./helpers.sol";
|
||||||
|
import "./events.sol";
|
||||||
|
|
||||||
|
contract _AaveV2ToV3MigrationResolver is _AaveHelper {
|
||||||
|
/**
|
||||||
|
* @dev Import aave position .
|
||||||
|
* @notice Import EOA's or DSA's aave V2 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
|
||||||
|
* @param doImport boolean, to support DSA v2->v3 migration
|
||||||
|
*/
|
||||||
|
function _importAave(
|
||||||
|
address userAccount,
|
||||||
|
ImportInputData memory inputData,
|
||||||
|
bool doImport
|
||||||
|
) internal returns (string memory _eventName, bytes memory _eventParam) {
|
||||||
|
if (doImport) {
|
||||||
|
// check only when we are importing from user's address
|
||||||
|
require(
|
||||||
|
AccountInterface(address(this)).isAuth(userAccount),
|
||||||
|
"user-account-not-auth"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
require(inputData.supplyTokens.length > 0, "0-length-not-allowed");
|
||||||
|
|
||||||
|
ImportData memory data;
|
||||||
|
|
||||||
|
AaveV2Interface aaveV2 = AaveV2Interface(
|
||||||
|
aaveV2Provider.getLendingPool()
|
||||||
|
);
|
||||||
|
AaveV3Interface aaveV3 = AaveV3Interface(aaveV3Provider.getPool());
|
||||||
|
|
||||||
|
data = getBorrowAmountsV2(userAccount, aaveV2, inputData, data);
|
||||||
|
data = getSupplyAmountsV2(userAccount, inputData, data);
|
||||||
|
|
||||||
|
// payback borrowed amount;
|
||||||
|
_PaybackStableV2(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV2,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.stableBorrowAmts,
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
_PaybackVariableV2(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV2,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.variableBorrowAmts,
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
|
||||||
|
if (doImport) {
|
||||||
|
// transfer atokens to user's DSA address;
|
||||||
|
_TransferAtokensV2(
|
||||||
|
data._supplyTokens.length,
|
||||||
|
aaveV2,
|
||||||
|
data.aTokens,
|
||||||
|
data.supplyAmts,
|
||||||
|
data._supplyTokens,
|
||||||
|
userAccount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// withdraw v2 supplied tokens
|
||||||
|
_WithdrawTokensFromV2(
|
||||||
|
data._supplyTokens.length,
|
||||||
|
aaveV2,
|
||||||
|
data.supplyAmts,
|
||||||
|
data._supplyTokens
|
||||||
|
);
|
||||||
|
// deposit tokens in v3
|
||||||
|
_depositTokensV3(
|
||||||
|
data._supplyTokens.length,
|
||||||
|
aaveV3,
|
||||||
|
data.supplyAmts,
|
||||||
|
data._supplyTokens
|
||||||
|
);
|
||||||
|
|
||||||
|
// borrow assets in aave v3 after migrating position
|
||||||
|
if (data.convertStable) {
|
||||||
|
_BorrowVariableV3(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV3,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.totalBorrowAmtsWithFee
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
_BorrowStableV3(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV3,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.stableBorrowAmtsWithFee
|
||||||
|
);
|
||||||
|
_BorrowVariableV3(
|
||||||
|
data._borrowTokens.length,
|
||||||
|
aaveV3,
|
||||||
|
data._borrowTokens,
|
||||||
|
data.variableBorrowAmtsWithFee
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
_eventName = "LogAaveImportV2ToV3(address,bool,bool,address[],address[],uint256[],uint256[],uint256[],uint256[])";
|
||||||
|
_eventParam = abi.encode(
|
||||||
|
userAccount,
|
||||||
|
doImport,
|
||||||
|
inputData.convertStable,
|
||||||
|
inputData.supplyTokens,
|
||||||
|
inputData.borrowTokens,
|
||||||
|
inputData.flashLoanFees,
|
||||||
|
data.supplyAmts,
|
||||||
|
data.stableBorrowAmts,
|
||||||
|
data.variableBorrowAmts
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Import aave position .
|
||||||
|
* @notice Import EOA's aave V2 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 importAaveV2ToV3(
|
||||||
|
address userAccount,
|
||||||
|
ImportInputData memory inputData
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
(_eventName, _eventParam) = _importAave(userAccount, inputData, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Migrate aave position .
|
||||||
|
* @notice Migrate DSA's aave V2 position to DSA's aave v3 position
|
||||||
|
* @param inputData The struct containing all the neccessary input data
|
||||||
|
*/
|
||||||
|
function migrateAaveV2ToV3(ImportInputData memory inputData)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
(_eventName, _eventParam) = _importAave(msg.sender, inputData, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2AaveV2ToV3MigrationPolygon is _AaveV2ToV3MigrationResolver {
|
||||||
|
string public constant name = "Aave-Import-v2-to-v3";
|
||||||
|
}
|
||||||
|
|
@ -24,8 +24,8 @@ async function deployRunner() {
|
||||||
name: "chain",
|
name: "chain",
|
||||||
message: "What chain do you want to deploy on?",
|
message: "What chain do you want to deploy on?",
|
||||||
type: "list",
|
type: "list",
|
||||||
choices: ["mainnet", "polygon", "avalanche", "arbitrum"],
|
choices: ["mainnet", "polygon", "avalanche", "arbitrum", "optimism"]
|
||||||
},
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// let connector = await connectorSelect(chain);
|
// let connector = await connectorSelect(chain);
|
||||||
|
|
@ -48,8 +48,8 @@ async function deployRunner() {
|
||||||
{
|
{
|
||||||
name: "connector",
|
name: "connector",
|
||||||
message: "Enter the connector contract name? (ex: ConnectV2Paraswap)",
|
message: "Enter the connector contract name? (ex: ConnectV2Paraswap)",
|
||||||
type: "input",
|
type: "input"
|
||||||
},
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let { choice } = await inquirer.prompt([
|
let { choice } = await inquirer.prompt([
|
||||||
|
|
@ -57,39 +57,29 @@ async function deployRunner() {
|
||||||
name: "choice",
|
name: "choice",
|
||||||
message: "Do you wanna try deploy on hardhat first?",
|
message: "Do you wanna try deploy on hardhat first?",
|
||||||
type: "list",
|
type: "list",
|
||||||
choices: ["yes", "no"],
|
choices: ["yes", "no"]
|
||||||
},
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
runchain = choice === "yes" ? "hardhat" : chain;
|
runchain = choice === "yes" ? "hardhat" : chain;
|
||||||
|
|
||||||
console.log(
|
console.log(`Deploying ${connector} on ${runchain}, press (ctrl + c) to stop`);
|
||||||
`Deploying ${connector} on ${runchain}, press (ctrl + c) to stop`
|
|
||||||
);
|
|
||||||
|
|
||||||
start = Date.now();
|
start = Date.now();
|
||||||
await execScript({
|
await execScript({
|
||||||
cmd: "npx",
|
cmd: "npx",
|
||||||
args: [
|
args: ["hardhat", "run", "scripts/deployment/deploy.ts", "--network", `${runchain}`],
|
||||||
"hardhat",
|
|
||||||
"run",
|
|
||||||
"scripts/deployment/deploy.ts",
|
|
||||||
"--network",
|
|
||||||
`${runchain}`,
|
|
||||||
],
|
|
||||||
env: {
|
env: {
|
||||||
connectorName: connector,
|
connectorName: connector,
|
||||||
networkType: chain,
|
networkType: chain
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
end = Date.now();
|
end = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
deployRunner()
|
deployRunner()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log(
|
console.log(`Done successfully, total time taken: ${(end - start) / 1000} sec`);
|
||||||
`Done successfully, total time taken: ${(end - start) / 1000} sec`
|
|
||||||
);
|
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { ethers } from "hardhat";
|
import { ethers, network } from "hardhat";
|
||||||
|
|
||||||
import { impersonateAccounts } from "./impersonate";
|
import { impersonateAccounts } from "./impersonate";
|
||||||
import { tokenMapping as mainnetMapping } from "./mainnet/tokens";
|
import { tokenMapping as mainnetMapping } from "./mainnet/tokens";
|
||||||
|
|
@ -12,7 +12,7 @@ const mineTx = async (tx: any) => {
|
||||||
const tokenMapping: Record<string, Record<string, any>> = {
|
const tokenMapping: Record<string, Record<string, any>> = {
|
||||||
mainnet: mainnetMapping,
|
mainnet: mainnetMapping,
|
||||||
polygon: polygonMapping,
|
polygon: polygonMapping,
|
||||||
avalanche: avalancheMapping,
|
avalanche: avalancheMapping
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function addLiquidity(tokenName: string, address: any, amt: any) {
|
export async function addLiquidity(tokenName: string, address: any, amt: any) {
|
||||||
|
|
@ -20,20 +20,16 @@ export async function addLiquidity(tokenName: string, address: any, amt: any) {
|
||||||
tokenName = tokenName.toLowerCase();
|
tokenName = tokenName.toLowerCase();
|
||||||
const chain = String(process.env.networkType);
|
const chain = String(process.env.networkType);
|
||||||
if (!tokenMapping[chain][tokenName]) {
|
if (!tokenMapping[chain][tokenName]) {
|
||||||
throw new Error(
|
throw new Error(`Add liquidity doesn't support the following token: ${tokenName}`);
|
||||||
`Add liquidity doesn't support the following token: ${tokenName}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = tokenMapping[chain][tokenName];
|
const token = tokenMapping[chain][tokenName];
|
||||||
const [impersonatedSigner] = await impersonateAccounts([
|
const [impersonatedSigner] = await impersonateAccounts([token.impersonateSigner]);
|
||||||
token.impersonateSigner,
|
|
||||||
]);
|
|
||||||
|
|
||||||
// send 2 eth to cover any tx costs.
|
// send 2 eth to cover any tx costs.
|
||||||
await network.provider.send("hardhat_setBalance", [
|
await network.provider.send("hardhat_setBalance", [
|
||||||
impersonatedSigner.address,
|
impersonatedSigner.address,
|
||||||
ethers.utils.parseEther("2").toHexString(),
|
ethers.utils.parseEther("2").toHexString()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await token.process(impersonatedSigner, address, amt);
|
await token.process(impersonatedSigner, address, amt);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user