From c793ce0238223f60db13c1d23573dbb7d956de54 Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Sun, 6 Mar 2022 18:19:42 +0530 Subject: [PATCH 01/14] added connector on polygon --- .../aave/v2-to-v3-import/events.sol | 14 + .../aave/v2-to-v3-import/helpers.sol | 326 ++++++++++++++++++ .../aave/v2-to-v3-import/interfaces.sol | 150 ++++++++ .../connectors/aave/v2-to-v3-import/main.sol | 130 +++++++ 4 files changed, 620 insertions(+) create mode 100644 contracts/polygon/connectors/aave/v2-to-v3-import/events.sol create mode 100644 contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol create mode 100644 contracts/polygon/connectors/aave/v2-to-v3-import/interfaces.sol create mode 100644 contracts/polygon/connectors/aave/v2-to-v3-import/main.sol diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol new file mode 100644 index 00000000..3aa98805 --- /dev/null +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol @@ -0,0 +1,14 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogAaveV2ImportToV3( + address indexed user, + bool convertStable, + address[] supplyTokens, + address[] borrowTokens, + uint256[] flashLoanFees, + uint256[] supplyAmts, + uint256[] stableBorrowAmts, + uint256[] variableBorrowAmts + ); +} diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol new file mode 100644 index 00000000..c05dc259 --- /dev/null +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol @@ -0,0 +1,326 @@ +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( + 0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5 // v2 address TODO: need to update this + ); + + /** + * @dev AaveV3 Lending Pool Provider + */ + AaveV3PoolProviderInterface internal constant aaveV3Provider = + AaveV3PoolProviderInterface( + 0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5 // v2 address TODO: need to update this + ); + + /** + * @dev Aave Protocol Data Provider + */ + AaveV2DataProviderInterface internal constant aaveData = + AaveV2DataProviderInterface(0x057835Ad21a177dbdd3090bB1CAE03EaCF78Fc6d); // TODO: need to update this + + function getIsColl(address token, address user) + internal + view + returns (bool isCol) + { + (, , , , , , , , isCol) = aaveData.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 getBorrowAmount(address _token, address userAccount) + internal + view + returns (uint256 stableBorrow, uint256 variableBorrow) + { + ( + , + address stableDebtTokenAddress, + address variableDebtTokenAddress + ) = aaveData.getReserveTokensAddresses(_token); + + stableBorrow = ATokenV2Interface(stableDebtTokenAddress).balanceOf( + userAccount + ); + variableBorrow = ATokenV2Interface(variableDebtTokenAddress).balanceOf( + userAccount + ); + } + + function getBorrowAmounts( + 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] + ) = 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(aaveV2), _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 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, , ) = aaveData.getReserveTokensAddresses(_token); + data._supplyTokens[i] = _token; + data.aTokens[i] = ATokenV2Interface(_aToken); + data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount); + } + + return data; + } + + function _paybackBehalfOne( + AaveV2Interface aaveV2, + address token, + uint256 amt, + uint256 rateMode, + address user + ) private { + aaveV2.repay(token, amt, rateMode, user); + } + + function _PaybackStable( + uint256 _length, + AaveV2Interface aaveV2, + address[] memory tokens, + uint256[] memory amts, + address user + ) internal { + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _paybackBehalfOne(aaveV2, tokens[i], amts[i], 1, user); + } + } + } + + function _PaybackVariable( + uint256 _length, + AaveV2Interface aaveV2, + address[] memory tokens, + uint256[] memory amts, + address user + ) internal { + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _paybackBehalfOne(aaveV2, tokens[i], amts[i], 2, user); + } + } + } + + function _TransferAtokens( + 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 _depositTokensInV3( + 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 _BorrowVariable( + uint256 _length, + AaveV3Interface aaveV3, + address[] memory tokens, + uint256[] memory amts + ) internal { + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _borrowOne(aaveV3, tokens[i], amts[i], 2); + } + } + } + + function _BorrowStable( + uint256 _length, + AaveV3Interface aaveV3, + address[] memory tokens, + uint256[] memory amts + ) internal { + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _borrowOne(aaveV3, tokens[i], amts[i], 1); + } + } + } + + function _borrowOne( + AaveV3Interface aaveV3, + address token, + uint256 amt, + uint256 rateMode + ) private { + aaveV3.borrow(token, amt, rateMode, referalCode, address(this)); + } +} diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/interfaces.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/interfaces.sol new file mode 100644 index 00000000..b28f49e0 --- /dev/null +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/interfaces.sol @@ -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); +} diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol new file mode 100644 index 00000000..d4574198 --- /dev/null +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol @@ -0,0 +1,130 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol"; +import "./interfaces.sol"; +import "./helpers.sol"; +import "./events.sol"; + +contract _AaveV2ToV3MigrationResolver is _AaveHelper { + 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; + + AaveV2Interface aaveV2 = AaveV2Interface( + aaveV2Provider.getLendingPool() + ); + AaveV3Interface aaveV3 = AaveV3Interface(aaveV3Provider.getPool()); + + data = getBorrowAmounts(userAccount, aaveV2, inputData, data); + data = getSupplyAmounts(userAccount, inputData, data); + + // payback borrowed amount; + _PaybackStable( + data._borrowTokens.length, + aaveV2, + data._borrowTokens, + data.stableBorrowAmts, + userAccount + ); + _PaybackVariable( + data._borrowTokens.length, + aaveV2, + data._borrowTokens, + data.variableBorrowAmts, + userAccount + ); + + // transfer atokens to this address; + _TransferAtokens( + 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 + _depositTokensInV3( + data._supplyTokens.length, + aaveV3, + data.supplyAmts, + data._supplyTokens + ); + + // borrow assets in aave v3 after migrating position + if (data.convertStable) { + _BorrowVariable( + data._borrowTokens.length, + aaveV3, + data._borrowTokens, + data.totalBorrowAmtsWithFee + ); + } else { + _BorrowStable( + data._borrowTokens.length, + aaveV3, + data._borrowTokens, + data.stableBorrowAmtsWithFee + ); + _BorrowVariable( + data._borrowTokens.length, + aaveV3, + data._borrowTokens, + data.variableBorrowAmtsWithFee + ); + } + + _eventName = "LogAaveV2ImportToV3(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 + ); + } + + function importAaveV2ToV3( + address userAccount, + ImportInputData memory inputData + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + (_eventName, _eventParam) = _importAave(userAccount, inputData); + } + + function migrateAaveV2ToV3(ImportInputData memory inputData) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + (_eventName, _eventParam) = _importAave(msg.sender, inputData); + } +} + +contract AaveV2ToV3MigrationResolverPolygon is _AaveV2ToV3MigrationResolver { + string public constant name = "Aave-v2-to-v3-import"; +} From ae6598b2f6a1006d5e2f4a15191110c74d9c5a55 Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Sun, 6 Mar 2022 18:19:55 +0530 Subject: [PATCH 02/14] added connector on avalanche --- .../aave/v2-to-v3-import/events.sol | 14 + .../aave/v2-to-v3-import/helpers.sol | 326 ++++++++++++++++++ .../aave/v2-to-v3-import/interfaces.sol | 150 ++++++++ .../connectors/aave/v2-to-v3-import/main.sol | 130 +++++++ 4 files changed, 620 insertions(+) create mode 100644 contracts/avalanche/connectors/aave/v2-to-v3-import/events.sol create mode 100644 contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol create mode 100644 contracts/avalanche/connectors/aave/v2-to-v3-import/interfaces.sol create mode 100644 contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/events.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/events.sol new file mode 100644 index 00000000..3aa98805 --- /dev/null +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/events.sol @@ -0,0 +1,14 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogAaveV2ImportToV3( + address indexed user, + bool convertStable, + address[] supplyTokens, + address[] borrowTokens, + uint256[] flashLoanFees, + uint256[] supplyAmts, + uint256[] stableBorrowAmts, + uint256[] variableBorrowAmts + ); +} diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol new file mode 100644 index 00000000..d4dab63a --- /dev/null +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol @@ -0,0 +1,326 @@ +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( + 0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5 // v2 address TODO: need to update this + ); + + /** + * @dev AaveV3 Lending Pool Provider + */ + AaveV3PoolProviderInterface internal constant aaveV3Provider = + AaveV3PoolProviderInterface( + 0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5 // v2 address TODO: need to update this + ); + + /** + * @dev Aave Protocol Data Provider + */ + AaveV2DataProviderInterface internal constant aaveData = + AaveV2DataProviderInterface(0x057835Ad21a177dbdd3090bB1CAE03EaCF78Fc6d); // TODO: need to update this + + function getIsColl(address token, address user) + internal + view + returns (bool isCol) + { + (, , , , , , , , isCol) = aaveData.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 getBorrowAmount(address _token, address userAccount) + internal + view + returns (uint256 stableBorrow, uint256 variableBorrow) + { + ( + , + address stableDebtTokenAddress, + address variableDebtTokenAddress + ) = aaveData.getReserveTokensAddresses(_token); + + stableBorrow = ATokenV2Interface(stableDebtTokenAddress).balanceOf( + userAccount + ); + variableBorrow = ATokenV2Interface(variableDebtTokenAddress).balanceOf( + userAccount + ); + } + + function getBorrowAmounts( + 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] + ) = 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(aaveV2), _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 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, , ) = aaveData.getReserveTokensAddresses(_token); + data._supplyTokens[i] = _token; + data.aTokens[i] = ATokenV2Interface(_aToken); + data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount); + } + + return data; + } + + function _paybackBehalfOne( + AaveV2Interface aaveV2, + address token, + uint256 amt, + uint256 rateMode, + address user + ) private { + aaveV2.repay(token, amt, rateMode, user); + } + + function _PaybackStable( + uint256 _length, + AaveV2Interface aaveV2, + address[] memory tokens, + uint256[] memory amts, + address user + ) internal { + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _paybackBehalfOne(aaveV2, tokens[i], amts[i], 1, user); + } + } + } + + function _PaybackVariable( + uint256 _length, + AaveV2Interface aaveV2, + address[] memory tokens, + uint256[] memory amts, + address user + ) internal { + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _paybackBehalfOne(aaveV2, tokens[i], amts[i], 2, user); + } + } + } + + function _TransferAtokens( + 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 _depositTokensInV3( + 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 _BorrowVariable( + uint256 _length, + AaveV3Interface aaveV3, + address[] memory tokens, + uint256[] memory amts + ) internal { + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _borrowOne(aaveV3, tokens[i], amts[i], 2); + } + } + } + + function _BorrowStable( + uint256 _length, + AaveV3Interface aaveV3, + address[] memory tokens, + uint256[] memory amts + ) internal { + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _borrowOne(aaveV3, tokens[i], amts[i], 1); + } + } + } + + function _borrowOne( + AaveV3Interface aaveV3, + address token, + uint256 amt, + uint256 rateMode + ) private { + aaveV3.borrow(token, amt, rateMode, referalCode, address(this)); + } +} diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/interfaces.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/interfaces.sol new file mode 100644 index 00000000..b28f49e0 --- /dev/null +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/interfaces.sol @@ -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); +} diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol new file mode 100644 index 00000000..a8b74e8f --- /dev/null +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol @@ -0,0 +1,130 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol"; +import "./interfaces.sol"; +import "./helpers.sol"; +import "./events.sol"; + +contract _AaveV2ToV3MigrationResolver is _AaveHelper { + 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; + + AaveV2Interface aaveV2 = AaveV2Interface( + aaveV2Provider.getLendingPool() + ); + AaveV3Interface aaveV3 = AaveV3Interface(aaveV3Provider.getPool()); + + data = getBorrowAmounts(userAccount, aaveV2, inputData, data); + data = getSupplyAmounts(userAccount, inputData, data); + + // payback borrowed amount; + _PaybackStable( + data._borrowTokens.length, + aaveV2, + data._borrowTokens, + data.stableBorrowAmts, + userAccount + ); + _PaybackVariable( + data._borrowTokens.length, + aaveV2, + data._borrowTokens, + data.variableBorrowAmts, + userAccount + ); + + // transfer atokens to this address; + _TransferAtokens( + 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 + _depositTokensInV3( + data._supplyTokens.length, + aaveV3, + data.supplyAmts, + data._supplyTokens + ); + + // borrow assets in aave v3 after migrating position + if (data.convertStable) { + _BorrowVariable( + data._borrowTokens.length, + aaveV3, + data._borrowTokens, + data.totalBorrowAmtsWithFee + ); + } else { + _BorrowStable( + data._borrowTokens.length, + aaveV3, + data._borrowTokens, + data.stableBorrowAmtsWithFee + ); + _BorrowVariable( + data._borrowTokens.length, + aaveV3, + data._borrowTokens, + data.variableBorrowAmtsWithFee + ); + } + + _eventName = "LogAaveV2ImportToV3(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 + ); + } + + function importAaveV2ToV3( + address userAccount, + ImportInputData memory inputData + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + (_eventName, _eventParam) = _importAave(userAccount, inputData); + } + + function migrateAaveV2ToV3(ImportInputData memory inputData) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + (_eventName, _eventParam) = _importAave(msg.sender, inputData); + } +} + +contract AaveV2ToV3MigrationResolverAvalanche is _AaveV2ToV3MigrationResolver { + string public constant name = "Aave-v2-to-v3-import"; +} From 22728d776a14c4ba35986460c3e98254554eb16a Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Wed, 9 Mar 2022 00:12:39 +0400 Subject: [PATCH 03/14] fixed DSA v2 to v3 import logic --- .../aave/v2-to-v3-import/events.sol | 1 + .../connectors/aave/v2-to-v3-import/main.sol | 46 +++++++++++-------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol index 3aa98805..0d1ed7ae 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol @@ -3,6 +3,7 @@ pragma solidity ^0.7.0; contract Events { event LogAaveV2ImportToV3( address indexed user, + bool doImport, bool convertStable, address[] supplyTokens, address[] borrowTokens, diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol index d4574198..34001a15 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol @@ -7,14 +7,21 @@ import "./helpers.sol"; import "./events.sol"; contract _AaveV2ToV3MigrationResolver is _AaveHelper { - function _importAave(address userAccount, ImportInputData memory inputData) + function _importAave( + address userAccount, + ImportInputData memory inputData, + bool doImport + ) internal returns (string memory _eventName, bytes memory _eventParam) - { - require( - AccountInterface(address(this)).isAuth(userAccount), - "user-account-not-auth" - ); + { + 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"); @@ -44,15 +51,17 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { userAccount ); - // transfer atokens to this address; - _TransferAtokens( - data._supplyTokens.length, - aaveV2, - data.aTokens, - data.supplyAmts, - data._supplyTokens, - userAccount - ); + if (doImport) { + // transfer atokens to user's DSA address; + _TransferAtokens( + data._supplyTokens.length, + aaveV2, + data.aTokens, + data.supplyAmts, + data._supplyTokens, + userAccount + ); + } // withdraw v2 supplied tokens _WithdrawTokensFromV2( @@ -92,9 +101,10 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { ); } - _eventName = "LogAaveV2ImportToV3(address,bool,address[],address[],uint256,uint256[],uint256[],uint256[])"; + _eventName = "LogAaveV2ImportToV3(address,bool,bool,address[],address[],uint256[],uint256[],uint256[],uint256[])"; _eventParam = abi.encode( userAccount, + doImport, inputData.convertStable, inputData.supplyTokens, inputData.borrowTokens, @@ -113,7 +123,7 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { payable returns (string memory _eventName, bytes memory _eventParam) { - (_eventName, _eventParam) = _importAave(userAccount, inputData); + (_eventName, _eventParam) = _importAave(userAccount, inputData, false); } function migrateAaveV2ToV3(ImportInputData memory inputData) @@ -121,7 +131,7 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { payable returns (string memory _eventName, bytes memory _eventParam) { - (_eventName, _eventParam) = _importAave(msg.sender, inputData); + (_eventName, _eventParam) = _importAave(msg.sender, inputData, true); } } From d59cd4fc7c79d8543ec4467b7e5cebcb16ec9676 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Wed, 9 Mar 2022 00:24:59 +0400 Subject: [PATCH 04/14] Polygon: Code refactoring --- .../aave/v2-to-v3-import/events.sol | 2 +- .../aave/v2-to-v3-import/helpers.sol | 38 +++++++++---------- .../connectors/aave/v2-to-v3-import/main.sol | 24 ++++++------ 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol index 0d1ed7ae..84bb146f 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/events.sol @@ -1,7 +1,7 @@ pragma solidity ^0.7.0; contract Events { - event LogAaveV2ImportToV3( + event LogAaveImportV2ToV3( address indexed user, bool doImport, bool convertStable, diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol index c05dc259..9b397e8c 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol @@ -39,7 +39,7 @@ abstract contract Helper is DSMath, Basic { view returns (bool isCol) { - (, , , , , , , , isCol) = aaveData.getUserReserveData(token, user); + (, , , , , , , , isCol) = aaveV2Data.getUserReserveData(token, user); } struct ImportData { @@ -65,7 +65,7 @@ abstract contract Helper is DSMath, Basic { } contract _AaveHelper is Helper { - function getBorrowAmount(address _token, address userAccount) + function getBorrowAmountV2(address _token, address userAccount) internal view returns (uint256 stableBorrow, uint256 variableBorrow) @@ -74,7 +74,7 @@ contract _AaveHelper is Helper { , address stableDebtTokenAddress, address variableDebtTokenAddress - ) = aaveData.getReserveTokensAddresses(_token); + ) = aaveV2Data.getReserveTokensAddresses(_token); stableBorrow = ATokenV2Interface(stableDebtTokenAddress).balanceOf( userAccount @@ -84,7 +84,7 @@ contract _AaveHelper is Helper { ); } - function getBorrowAmounts( + function getBorrowAmountsV2( address userAccount, AaveV2Interface aaveV2, ImportInputData memory inputData, @@ -119,7 +119,7 @@ contract _AaveHelper is Helper { ( data.stableBorrowAmts[i], data.variableBorrowAmts[i] - ) = getBorrowAmount(_token, userAccount); + ) = getBorrowAmountV2(_token, userAccount); if (data.variableBorrowAmts[i] != 0) { data.variableBorrowAmtsWithFee[i] = add( @@ -152,7 +152,7 @@ contract _AaveHelper is Helper { return data; } - function getSupplyAmounts( + function getSupplyAmountsV2( address userAccount, ImportInputData memory inputData, ImportData memory data @@ -175,7 +175,7 @@ contract _AaveHelper is Helper { address _token = inputData.supplyTokens[i] == maticAddr ? wmaticAddr : inputData.supplyTokens[i]; - (address _aToken, , ) = aaveData.getReserveTokensAddresses(_token); + (address _aToken, , ) = aaveV2Data.getReserveTokensAddresses(_token); data._supplyTokens[i] = _token; data.aTokens[i] = ATokenV2Interface(_aToken); data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount); @@ -184,7 +184,7 @@ contract _AaveHelper is Helper { return data; } - function _paybackBehalfOne( + function _paybackBehalfOneV2( AaveV2Interface aaveV2, address token, uint256 amt, @@ -194,7 +194,7 @@ contract _AaveHelper is Helper { aaveV2.repay(token, amt, rateMode, user); } - function _PaybackStable( + function _PaybackStableV2( uint256 _length, AaveV2Interface aaveV2, address[] memory tokens, @@ -203,12 +203,12 @@ contract _AaveHelper is Helper { ) internal { for (uint256 i = 0; i < _length; i++) { if (amts[i] > 0) { - _paybackBehalfOne(aaveV2, tokens[i], amts[i], 1, user); + _paybackBehalfOneV2(aaveV2, tokens[i], amts[i], 1, user); } } } - function _PaybackVariable( + function _PaybackVariableV2( uint256 _length, AaveV2Interface aaveV2, address[] memory tokens, @@ -217,12 +217,12 @@ contract _AaveHelper is Helper { ) internal { for (uint256 i = 0; i < _length; i++) { if (amts[i] > 0) { - _paybackBehalfOne(aaveV2, tokens[i], amts[i], 2, user); + _paybackBehalfOneV2(aaveV2, tokens[i], amts[i], 2, user); } } } - function _TransferAtokens( + function _TransferAtokensV2( uint256 _length, AaveV2Interface aaveV2, ATokenV2Interface[] memory atokenContracts, @@ -264,7 +264,7 @@ contract _AaveHelper is Helper { } } - function _depositTokensInV3( + function _depositTokensV3( uint256 _length, AaveV3Interface aaveV3, uint256[] memory amts, @@ -289,7 +289,7 @@ contract _AaveHelper is Helper { } } - function _BorrowVariable( + function _BorrowVariableV3( uint256 _length, AaveV3Interface aaveV3, address[] memory tokens, @@ -297,12 +297,12 @@ contract _AaveHelper is Helper { ) internal { for (uint256 i = 0; i < _length; i++) { if (amts[i] > 0) { - _borrowOne(aaveV3, tokens[i], amts[i], 2); + _borrowOneV3(aaveV3, tokens[i], amts[i], 2); } } } - function _BorrowStable( + function _BorrowStableV3( uint256 _length, AaveV3Interface aaveV3, address[] memory tokens, @@ -310,12 +310,12 @@ contract _AaveHelper is Helper { ) internal { for (uint256 i = 0; i < _length; i++) { if (amts[i] > 0) { - _borrowOne(aaveV3, tokens[i], amts[i], 1); + _borrowOneV3(aaveV3, tokens[i], amts[i], 1); } } } - function _borrowOne( + function _borrowOneV3( AaveV3Interface aaveV3, address token, uint256 amt, diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol index 34001a15..9cb1683c 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol @@ -32,18 +32,18 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { ); AaveV3Interface aaveV3 = AaveV3Interface(aaveV3Provider.getPool()); - data = getBorrowAmounts(userAccount, aaveV2, inputData, data); - data = getSupplyAmounts(userAccount, inputData, data); + data = getBorrowAmountsV2(userAccount, aaveV2, inputData, data); + data = getSupplyAmountsV2(userAccount, inputData, data); // payback borrowed amount; - _PaybackStable( + _PaybackStableV2( data._borrowTokens.length, aaveV2, data._borrowTokens, data.stableBorrowAmts, userAccount ); - _PaybackVariable( + _PaybackVariableV2( data._borrowTokens.length, aaveV2, data._borrowTokens, @@ -53,7 +53,7 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { if (doImport) { // transfer atokens to user's DSA address; - _TransferAtokens( + _TransferAtokensV2( data._supplyTokens.length, aaveV2, data.aTokens, @@ -71,7 +71,7 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { data._supplyTokens ); // deposit tokens in v3 - _depositTokensInV3( + _depositTokensV3( data._supplyTokens.length, aaveV3, data.supplyAmts, @@ -80,20 +80,20 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { // borrow assets in aave v3 after migrating position if (data.convertStable) { - _BorrowVariable( + _BorrowVariableV3( data._borrowTokens.length, aaveV3, data._borrowTokens, data.totalBorrowAmtsWithFee ); } else { - _BorrowStable( + _BorrowStableV3( data._borrowTokens.length, aaveV3, data._borrowTokens, data.stableBorrowAmtsWithFee ); - _BorrowVariable( + _BorrowVariableV3( data._borrowTokens.length, aaveV3, data._borrowTokens, @@ -101,7 +101,7 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { ); } - _eventName = "LogAaveV2ImportToV3(address,bool,bool,address[],address[],uint256[],uint256[],uint256[],uint256[])"; + _eventName = "LogAaveImportV2ToV3(address,bool,bool,address[],address[],uint256[],uint256[],uint256[],uint256[])"; _eventParam = abi.encode( userAccount, doImport, @@ -135,6 +135,6 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { } } -contract AaveV2ToV3MigrationResolverPolygon is _AaveV2ToV3MigrationResolver { - string public constant name = "Aave-v2-to-v3-import"; +contract ConnectV2AaveV2ToV3MigrationPolygon is _AaveV2ToV3MigrationResolver { + string public constant name = "Aave-Import-v2-to-v3"; } From 31a497e21b03d76f3a028fdc0fcf8d49e7357710 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Wed, 9 Mar 2022 00:25:21 +0400 Subject: [PATCH 05/14] Updated polygon addresses --- .../polygon/connectors/aave/v2-to-v3-import/helpers.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol index 9b397e8c..caf76763 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol @@ -17,7 +17,7 @@ abstract contract Helper is DSMath, Basic { */ AaveV2LendingPoolProviderInterface internal constant aaveV2Provider = AaveV2LendingPoolProviderInterface( - 0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5 // v2 address TODO: need to update this + 0xd05e3E715d945B59290df0ae8eF85c1BdB684744 // v2 address ); /** @@ -25,14 +25,14 @@ abstract contract Helper is DSMath, Basic { */ AaveV3PoolProviderInterface internal constant aaveV3Provider = AaveV3PoolProviderInterface( - 0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5 // v2 address TODO: need to update this + 0x7013523049CeC8b06F594edb8c5fb7F232c0Df7C // Polygon address - PoolAddressesProvider ); /** * @dev Aave Protocol Data Provider */ - AaveV2DataProviderInterface internal constant aaveData = - AaveV2DataProviderInterface(0x057835Ad21a177dbdd3090bB1CAE03EaCF78Fc6d); // TODO: need to update this + AaveV2DataProviderInterface internal constant aaveV2Data = + AaveV2DataProviderInterface(0x7551b5D2763519d4e37e8B81929D336De671d46d); // v2 address function getIsColl(address token, address user) internal From 270a3f79b9ff1a2b881add85486bd40476e8969c Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Wed, 9 Mar 2022 17:33:30 +0530 Subject: [PATCH 06/14] update v2->v3 import avax --- .../aave/v2-to-v3-import/events.sol | 3 +- .../aave/v2-to-v3-import/helpers.sol | 48 ++++++------- .../connectors/aave/v2-to-v3-import/main.sol | 69 ++++++++++--------- .../connectors/aave/v2-to-v3-import/main.sol | 5 +- 4 files changed, 65 insertions(+), 60 deletions(-) diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/events.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/events.sol index 3aa98805..84bb146f 100644 --- a/contracts/avalanche/connectors/aave/v2-to-v3-import/events.sol +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/events.sol @@ -1,8 +1,9 @@ pragma solidity ^0.7.0; contract Events { - event LogAaveV2ImportToV3( + event LogAaveImportV2ToV3( address indexed user, + bool doImport, bool convertStable, address[] supplyTokens, address[] borrowTokens, diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol index d4dab63a..36f067d9 100644 --- a/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol @@ -17,7 +17,7 @@ abstract contract Helper is DSMath, Basic { */ AaveV2LendingPoolProviderInterface internal constant aaveV2Provider = AaveV2LendingPoolProviderInterface( - 0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5 // v2 address TODO: need to update this + 0xb6A86025F0FE1862B372cb0ca18CE3EDe02A318f // v2 address: LendingPoolAddressProvider avax ); /** @@ -25,21 +25,21 @@ abstract contract Helper is DSMath, Basic { */ AaveV3PoolProviderInterface internal constant aaveV3Provider = AaveV3PoolProviderInterface( - 0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5 // v2 address TODO: need to update this + 0x7013523049CeC8b06F594edb8c5fb7F232c0Df7C // v3 - PoolAddressesProvider Avalanche ); /** * @dev Aave Protocol Data Provider */ - AaveV2DataProviderInterface internal constant aaveData = - AaveV2DataProviderInterface(0x057835Ad21a177dbdd3090bB1CAE03EaCF78Fc6d); // TODO: need to update this + AaveV2DataProviderInterface internal constant aaveV2Data = + AaveV2DataProviderInterface(0x65285E9dfab318f57051ab2b139ccCf232945451); // aave v2 - avax function getIsColl(address token, address user) internal view returns (bool isCol) { - (, , , , , , , , isCol) = aaveData.getUserReserveData(token, user); + (, , , , , , , , isCol) = aaveV2Data.getUserReserveData(token, user); } struct ImportData { @@ -65,7 +65,7 @@ abstract contract Helper is DSMath, Basic { } contract _AaveHelper is Helper { - function getBorrowAmount(address _token, address userAccount) + function getBorrowAmountV2(address _token, address userAccount) internal view returns (uint256 stableBorrow, uint256 variableBorrow) @@ -74,7 +74,7 @@ contract _AaveHelper is Helper { , address stableDebtTokenAddress, address variableDebtTokenAddress - ) = aaveData.getReserveTokensAddresses(_token); + ) = aaveV2Data.getReserveTokensAddresses(_token); stableBorrow = ATokenV2Interface(stableDebtTokenAddress).balanceOf( userAccount @@ -84,7 +84,7 @@ contract _AaveHelper is Helper { ); } - function getBorrowAmounts( + function getBorrowAmountsV2( address userAccount, AaveV2Interface aaveV2, ImportInputData memory inputData, @@ -119,7 +119,7 @@ contract _AaveHelper is Helper { ( data.stableBorrowAmts[i], data.variableBorrowAmts[i] - ) = getBorrowAmount(_token, userAccount); + ) = getBorrowAmountV2(_token, userAccount); if (data.variableBorrowAmts[i] != 0) { data.variableBorrowAmtsWithFee[i] = add( @@ -152,7 +152,7 @@ contract _AaveHelper is Helper { return data; } - function getSupplyAmounts( + function getSupplyAmountsV2( address userAccount, ImportInputData memory inputData, ImportData memory data @@ -175,7 +175,9 @@ contract _AaveHelper is Helper { address _token = inputData.supplyTokens[i] == avaxAddr ? wavaxAddr : inputData.supplyTokens[i]; - (address _aToken, , ) = aaveData.getReserveTokensAddresses(_token); + (address _aToken, , ) = aaveV2Data.getReserveTokensAddresses( + _token + ); data._supplyTokens[i] = _token; data.aTokens[i] = ATokenV2Interface(_aToken); data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount); @@ -184,7 +186,7 @@ contract _AaveHelper is Helper { return data; } - function _paybackBehalfOne( + function _paybackBehalfOneV2( AaveV2Interface aaveV2, address token, uint256 amt, @@ -194,7 +196,7 @@ contract _AaveHelper is Helper { aaveV2.repay(token, amt, rateMode, user); } - function _PaybackStable( + function _PaybackStableV2( uint256 _length, AaveV2Interface aaveV2, address[] memory tokens, @@ -203,12 +205,12 @@ contract _AaveHelper is Helper { ) internal { for (uint256 i = 0; i < _length; i++) { if (amts[i] > 0) { - _paybackBehalfOne(aaveV2, tokens[i], amts[i], 1, user); + _paybackBehalfOneV2(aaveV2, tokens[i], amts[i], 1, user); } } } - function _PaybackVariable( + function _PaybackVariableV2( uint256 _length, AaveV2Interface aaveV2, address[] memory tokens, @@ -217,12 +219,12 @@ contract _AaveHelper is Helper { ) internal { for (uint256 i = 0; i < _length; i++) { if (amts[i] > 0) { - _paybackBehalfOne(aaveV2, tokens[i], amts[i], 2, user); + _paybackBehalfOneV2(aaveV2, tokens[i], amts[i], 2, user); } } } - function _TransferAtokens( + function _TransferAtokensV2( uint256 _length, AaveV2Interface aaveV2, ATokenV2Interface[] memory atokenContracts, @@ -264,7 +266,7 @@ contract _AaveHelper is Helper { } } - function _depositTokensInV3( + function _depositTokensV3( uint256 _length, AaveV3Interface aaveV3, uint256[] memory amts, @@ -289,7 +291,7 @@ contract _AaveHelper is Helper { } } - function _BorrowVariable( + function _BorrowVariableV3( uint256 _length, AaveV3Interface aaveV3, address[] memory tokens, @@ -297,12 +299,12 @@ contract _AaveHelper is Helper { ) internal { for (uint256 i = 0; i < _length; i++) { if (amts[i] > 0) { - _borrowOne(aaveV3, tokens[i], amts[i], 2); + _borrowOneV3(aaveV3, tokens[i], amts[i], 2); } } } - function _BorrowStable( + function _BorrowStableV3( uint256 _length, AaveV3Interface aaveV3, address[] memory tokens, @@ -310,12 +312,12 @@ contract _AaveHelper is Helper { ) internal { for (uint256 i = 0; i < _length; i++) { if (amts[i] > 0) { - _borrowOne(aaveV3, tokens[i], amts[i], 1); + _borrowOneV3(aaveV3, tokens[i], amts[i], 1); } } } - function _borrowOne( + function _borrowOneV3( AaveV3Interface aaveV3, address token, uint256 amt, diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol index a8b74e8f..f695c23f 100644 --- a/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol @@ -7,15 +7,18 @@ import "./helpers.sol"; import "./events.sol"; contract _AaveV2ToV3MigrationResolver is _AaveHelper { - 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" - ); - + 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; @@ -25,18 +28,18 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { ); AaveV3Interface aaveV3 = AaveV3Interface(aaveV3Provider.getPool()); - data = getBorrowAmounts(userAccount, aaveV2, inputData, data); - data = getSupplyAmounts(userAccount, inputData, data); + data = getBorrowAmountsV2(userAccount, aaveV2, inputData, data); + data = getSupplyAmountsV2(userAccount, inputData, data); // payback borrowed amount; - _PaybackStable( + _PaybackStableV2( data._borrowTokens.length, aaveV2, data._borrowTokens, data.stableBorrowAmts, userAccount ); - _PaybackVariable( + _PaybackVariableV2( data._borrowTokens.length, aaveV2, data._borrowTokens, @@ -44,16 +47,17 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { userAccount ); - // transfer atokens to this address; - _TransferAtokens( - data._supplyTokens.length, - aaveV2, - data.aTokens, - data.supplyAmts, - data._supplyTokens, - 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, @@ -62,7 +66,7 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { data._supplyTokens ); // deposit tokens in v3 - _depositTokensInV3( + _depositTokensV3( data._supplyTokens.length, aaveV3, data.supplyAmts, @@ -71,20 +75,20 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { // borrow assets in aave v3 after migrating position if (data.convertStable) { - _BorrowVariable( + _BorrowVariableV3( data._borrowTokens.length, aaveV3, data._borrowTokens, data.totalBorrowAmtsWithFee ); } else { - _BorrowStable( + _BorrowStableV3( data._borrowTokens.length, aaveV3, data._borrowTokens, data.stableBorrowAmtsWithFee ); - _BorrowVariable( + _BorrowVariableV3( data._borrowTokens.length, aaveV3, data._borrowTokens, @@ -92,9 +96,10 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { ); } - _eventName = "LogAaveV2ImportToV3(address,bool,address[],address[],uint256,uint256[],uint256[],uint256[])"; + _eventName = "LogAaveImportV2ToV3(address,bool,bool,address[],address[],uint256[],uint256[],uint256[],uint256[])"; _eventParam = abi.encode( userAccount, + doImport, inputData.convertStable, inputData.supplyTokens, inputData.borrowTokens, @@ -113,7 +118,7 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { payable returns (string memory _eventName, bytes memory _eventParam) { - (_eventName, _eventParam) = _importAave(userAccount, inputData); + (_eventName, _eventParam) = _importAave(userAccount, inputData, false); } function migrateAaveV2ToV3(ImportInputData memory inputData) @@ -121,10 +126,10 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { payable returns (string memory _eventName, bytes memory _eventParam) { - (_eventName, _eventParam) = _importAave(msg.sender, inputData); + (_eventName, _eventParam) = _importAave(msg.sender, inputData, false); } } -contract AaveV2ToV3MigrationResolverAvalanche is _AaveV2ToV3MigrationResolver { - string public constant name = "Aave-v2-to-v3-import"; +contract ConnectV2AaveV2ToV3MigrationPolygon is _AaveV2ToV3MigrationResolver { + string public constant name = "Aave-Import-v2-to-v3"; } diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol index 9cb1683c..2e5dad19 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol @@ -11,10 +11,7 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { address userAccount, ImportInputData memory inputData, bool doImport - ) - internal - returns (string memory _eventName, bytes memory _eventParam) - { + ) internal returns (string memory _eventName, bytes memory _eventParam) { if (doImport) { // check only when we are importing from user's address require( From 74b0eccec06c2b74076aba7739aabf068b430b84 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Wed, 9 Mar 2022 17:33:47 +0530 Subject: [PATCH 07/14] minor script fixes --- scripts/deployment/deployConnectorsFromCmd.ts | 32 +++++++------------ scripts/tests/addLiquidity.ts | 14 +++----- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/scripts/deployment/deployConnectorsFromCmd.ts b/scripts/deployment/deployConnectorsFromCmd.ts index 60fb880a..9ed56c14 100644 --- a/scripts/deployment/deployConnectorsFromCmd.ts +++ b/scripts/deployment/deployConnectorsFromCmd.ts @@ -24,8 +24,8 @@ async function deployRunner() { name: "chain", message: "What chain do you want to deploy on?", type: "list", - choices: ["mainnet", "polygon", "avalanche", "arbitrum"], - }, + choices: ["mainnet", "polygon", "avalanche", "arbitrum", "optimism"] + } ]); // let connector = await connectorSelect(chain); @@ -48,8 +48,8 @@ async function deployRunner() { { name: "connector", message: "Enter the connector contract name? (ex: ConnectV2Paraswap)", - type: "input", - }, + type: "input" + } ]); let { choice } = await inquirer.prompt([ @@ -57,39 +57,29 @@ async function deployRunner() { name: "choice", message: "Do you wanna try deploy on hardhat first?", type: "list", - choices: ["yes", "no"], - }, + choices: ["yes", "no"] + } ]); runchain = choice === "yes" ? "hardhat" : chain; - console.log( - `Deploying ${connector} on ${runchain}, press (ctrl + c) to stop` - ); + console.log(`Deploying ${connector} on ${runchain}, press (ctrl + c) to stop`); start = Date.now(); await execScript({ cmd: "npx", - args: [ - "hardhat", - "run", - "scripts/deployment/deploy.ts", - "--network", - `${runchain}`, - ], + args: ["hardhat", "run", "scripts/deployment/deploy.ts", "--network", `${runchain}`], env: { connectorName: connector, - networkType: chain, - }, + networkType: chain + } }); end = Date.now(); } deployRunner() .then(() => { - console.log( - `Done successfully, total time taken: ${(end - start) / 1000} sec` - ); + console.log(`Done successfully, total time taken: ${(end - start) / 1000} sec`); process.exit(0); }) .catch((err) => { diff --git a/scripts/tests/addLiquidity.ts b/scripts/tests/addLiquidity.ts index a18d172f..a1250bcc 100644 --- a/scripts/tests/addLiquidity.ts +++ b/scripts/tests/addLiquidity.ts @@ -1,4 +1,4 @@ -import { ethers } from "hardhat"; +import { ethers, network } from "hardhat"; import { impersonateAccounts } from "./impersonate"; import { tokenMapping as mainnetMapping } from "./mainnet/tokens"; @@ -12,7 +12,7 @@ const mineTx = async (tx: any) => { const tokenMapping: Record> = { mainnet: mainnetMapping, polygon: polygonMapping, - avalanche: avalancheMapping, + avalanche: avalancheMapping }; 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(); const chain = String(process.env.networkType); if (!tokenMapping[chain][tokenName]) { - throw new Error( - `Add liquidity doesn't support the following token: ${tokenName}` - ); + throw new Error(`Add liquidity doesn't support the following token: ${tokenName}`); } const token = tokenMapping[chain][tokenName]; - const [impersonatedSigner] = await impersonateAccounts([ - token.impersonateSigner, - ]); + const [impersonatedSigner] = await impersonateAccounts([token.impersonateSigner]); // send 2 eth to cover any tx costs. await network.provider.send("hardhat_setBalance", [ impersonatedSigner.address, - ethers.utils.parseEther("2").toHexString(), + ethers.utils.parseEther("2").toHexString() ]); await token.process(impersonatedSigner, address, amt); From d32c6288122fddbe76ecb83967d35cc0b5e8cac8 Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Wed, 9 Mar 2022 19:22:53 +0530 Subject: [PATCH 08/14] corrected doImport logic + added comments --- .../connectors/aave/v2-to-v3-import/main.sol | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol index 2e5dad19..28dcb312 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol @@ -112,6 +112,12 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { ); } + /** + * @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 @@ -120,15 +126,20 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { payable returns (string memory _eventName, bytes memory _eventParam) { - (_eventName, _eventParam) = _importAave(userAccount, inputData, false); + (_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, true); + (_eventName, _eventParam) = _importAave(msg.sender, inputData, false); } } From 766b95c7871e05a0360472496f723a894787f63e Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Wed, 9 Mar 2022 19:26:07 +0530 Subject: [PATCH 09/14] did the same changes on avalanche's --- .../connectors/aave/v2-to-v3-import/main.sol | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol index f695c23f..7a2b60b0 100644 --- a/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol @@ -110,6 +110,12 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { ); } + /** + * @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 @@ -118,9 +124,14 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { payable returns (string memory _eventName, bytes memory _eventParam) { - (_eventName, _eventParam) = _importAave(userAccount, inputData, false); + (_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 From 4a3382d342db848f084215392038a69b18290a0b Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Wed, 9 Mar 2022 22:06:42 +0530 Subject: [PATCH 10/14] added comments --- contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol index 7a2b60b0..f3b86f3a 100644 --- a/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol @@ -6,6 +6,10 @@ 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, From 8c2fab7bd58d788ce44af2ac5a097762ac4a49e4 Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Wed, 9 Mar 2022 22:08:55 +0530 Subject: [PATCH 11/14] minor fix --- contracts/polygon/connectors/aave/v2-to-v3-import/main.sol | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol index 28dcb312..fbe6730c 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol @@ -6,6 +6,10 @@ 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, From ea5d7f611df589463abb6992efbf03a240d51292 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Thu, 10 Mar 2022 16:32:34 +0530 Subject: [PATCH 12/14] minor fix --- .../avalanche/connectors/aave/v2-to-v3-import/main.sol | 2 +- .../polygon/connectors/aave/v2-to-v3-import/helpers.sol | 4 +++- contracts/polygon/connectors/aave/v2-to-v3-import/main.sol | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol index f3b86f3a..8e360e81 100644 --- a/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol @@ -145,6 +145,6 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper { } } -contract ConnectV2AaveV2ToV3MigrationPolygon is _AaveV2ToV3MigrationResolver { +contract ConnectV2AaveV2ToV3MigrationAvalanche is _AaveV2ToV3MigrationResolver { string public constant name = "Aave-Import-v2-to-v3"; } diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol index caf76763..0882c791 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol @@ -175,7 +175,9 @@ contract _AaveHelper is Helper { address _token = inputData.supplyTokens[i] == maticAddr ? wmaticAddr : inputData.supplyTokens[i]; - (address _aToken, , ) = aaveV2Data.getReserveTokensAddresses(_token); + (address _aToken, , ) = aaveV2Data.getReserveTokensAddresses( + _token + ); data._supplyTokens[i] = _token; data.aTokens[i] = ATokenV2Interface(_aToken); data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount); diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol index fbe6730c..df8b273d 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol @@ -11,6 +11,13 @@ import "./events.sol"; * @dev migrate aave V2 position to aave v3 position */ 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, From d41b3e65aa0f4694536ce6a03a3ca8012c0987c5 Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Sat, 12 Mar 2022 13:53:38 +0530 Subject: [PATCH 13/14] updated aave v3 addresses --- contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol | 2 +- contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol | 2 +- contracts/polygon/connectors/aave/v2-to-v3-import/main.sol | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol b/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol index 36f067d9..d53dba5a 100644 --- a/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol +++ b/contracts/avalanche/connectors/aave/v2-to-v3-import/helpers.sol @@ -25,7 +25,7 @@ abstract contract Helper is DSMath, Basic { */ AaveV3PoolProviderInterface internal constant aaveV3Provider = AaveV3PoolProviderInterface( - 0x7013523049CeC8b06F594edb8c5fb7F232c0Df7C // v3 - PoolAddressesProvider Avalanche + 0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb // v3 - PoolAddressesProvider Avalanche ); /** diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol index 0882c791..5da70943 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/helpers.sol @@ -25,7 +25,7 @@ abstract contract Helper is DSMath, Basic { */ AaveV3PoolProviderInterface internal constant aaveV3Provider = AaveV3PoolProviderInterface( - 0x7013523049CeC8b06F594edb8c5fb7F232c0Df7C // Polygon address - PoolAddressesProvider + 0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb // Polygon address - PoolAddressesProvider ); /** diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol index df8b273d..d172ff48 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol @@ -10,6 +10,7 @@ import "./events.sol"; * @title Aave v2 to v3 import connector . * @dev migrate aave V2 position to aave v3 position */ + contract _AaveV2ToV3MigrationResolver is _AaveHelper { /** * @dev Import aave position . From 21c68c3290d49e2a59e80117c7916ac4300a1fad Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 14 Mar 2022 20:58:58 +0530 Subject: [PATCH 14/14] fixed netspec comments --- .../polygon/connectors/aave/v2-to-v3-import/main.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol index d172ff48..06a7d43b 100644 --- a/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol +++ b/contracts/polygon/connectors/aave/v2-to-v3-import/main.sol @@ -1,16 +1,16 @@ 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 */ +import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol"; +import "./interfaces.sol"; +import "./helpers.sol"; +import "./events.sol"; + contract _AaveV2ToV3MigrationResolver is _AaveHelper { /** * @dev Import aave position .