update v2->v3 import avax

This commit is contained in:
pradyuman-verma 2022-03-09 17:33:30 +05:30
parent 31a497e21b
commit 270a3f79b9
No known key found for this signature in database
GPG Key ID: E36FD6BC8923221F
4 changed files with 65 additions and 60 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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";
}

View File

@ -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(