dsa-connectors/contracts/avalanche/connectors/aave/v2-to-v3-import/main.sol

151 lines
3.8 KiB
Solidity
Raw Normal View History

2022-03-06 12:49:55 +00:00
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol";
import "./interfaces.sol";
import "./helpers.sol";
import "./events.sol";
2022-03-09 16:36:42 +00:00
/**
* @title Aave v2 to v3 import connector .
* @dev migrate aave V2 position to aave v3 position
*/
2022-03-06 12:49:55 +00:00
contract _AaveV2ToV3MigrationResolver is _AaveHelper {
2022-03-09 12:03:30 +00:00
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"
);
}
2022-03-06 12:49:55 +00:00
require(inputData.supplyTokens.length > 0, "0-length-not-allowed");
ImportData memory data;
AaveV2Interface aaveV2 = AaveV2Interface(
aaveV2Provider.getLendingPool()
);
AaveV3Interface aaveV3 = AaveV3Interface(aaveV3Provider.getPool());
2022-03-09 12:03:30 +00:00
data = getBorrowAmountsV2(userAccount, aaveV2, inputData, data);
data = getSupplyAmountsV2(userAccount, inputData, data);
2022-03-06 12:49:55 +00:00
// payback borrowed amount;
2022-03-09 12:03:30 +00:00
_PaybackStableV2(
2022-03-06 12:49:55 +00:00
data._borrowTokens.length,
aaveV2,
data._borrowTokens,
data.stableBorrowAmts,
userAccount
);
2022-03-09 12:03:30 +00:00
_PaybackVariableV2(
2022-03-06 12:49:55 +00:00
data._borrowTokens.length,
aaveV2,
data._borrowTokens,
data.variableBorrowAmts,
userAccount
);
2022-03-09 12:03:30 +00:00
if (doImport) {
// transfer atokens to this address;
_TransferAtokensV2(
data._supplyTokens.length,
aaveV2,
data.aTokens,
data.supplyAmts,
data._supplyTokens,
userAccount
);
}
2022-03-06 12:49:55 +00:00
// withdraw v2 supplied tokens
_WithdrawTokensFromV2(
data._supplyTokens.length,
aaveV2,
data.supplyAmts,
data._supplyTokens
);
// deposit tokens in v3
2022-03-09 12:03:30 +00:00
_depositTokensV3(
data._supplyTokensV3.length,
2022-03-06 12:49:55 +00:00
aaveV3,
data.supplyAmts,
data._supplyTokensV3
2022-03-06 12:49:55 +00:00
);
// borrow assets in aave v3 after migrating position
if (data.convertStable) {
2022-03-09 12:03:30 +00:00
_BorrowVariableV3(
data._borrowTokensV3.length,
2022-03-06 12:49:55 +00:00
aaveV3,
data._borrowTokensV3,
2022-03-06 12:49:55 +00:00
data.totalBorrowAmtsWithFee
);
} else {
2022-03-09 12:03:30 +00:00
_BorrowStableV3(
data._borrowTokensV3.length,
2022-03-06 12:49:55 +00:00
aaveV3,
data._borrowTokensV3,
2022-03-06 12:49:55 +00:00
data.stableBorrowAmtsWithFee
);
2022-03-09 12:03:30 +00:00
_BorrowVariableV3(
data._borrowTokensV3.length,
2022-03-06 12:49:55 +00:00
aaveV3,
data._borrowTokensV3,
2022-03-06 12:49:55 +00:00
data.variableBorrowAmtsWithFee
);
}
2022-03-09 12:03:30 +00:00
_eventName = "LogAaveImportV2ToV3(address,bool,bool,address[],address[],uint256[],uint256[],uint256[],uint256[])";
2022-03-06 12:49:55 +00:00
_eventParam = abi.encode(
userAccount,
2022-03-09 12:03:30 +00:00
doImport,
2022-03-06 12:49:55 +00:00
inputData.convertStable,
inputData.supplyTokens,
inputData.borrowTokens,
inputData.flashLoanFees,
data.supplyAmts,
data.stableBorrowAmts,
data.variableBorrowAmts
);
}
2022-03-09 13:56:07 +00:00
/**
* @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
*/
2022-03-06 12:49:55 +00:00
function importAaveV2ToV3(
address userAccount,
ImportInputData memory inputData
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
2022-03-09 13:56:07 +00:00
(_eventName, _eventParam) = _importAave(userAccount, inputData, true);
2022-03-06 12:49:55 +00:00
}
2022-03-09 13:56:07 +00:00
/**
* @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
*/
2022-03-06 12:49:55 +00:00
function migrateAaveV2ToV3(ImportInputData memory inputData)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
2022-03-09 12:03:30 +00:00
(_eventName, _eventParam) = _importAave(msg.sender, inputData, false);
2022-03-06 12:49:55 +00:00
}
}
2022-03-10 11:02:34 +00:00
contract ConnectV2AaveV2ToV3MigrationAvalanche is _AaveV2ToV3MigrationResolver {
2022-03-09 12:03:30 +00:00
string public constant name = "Aave-Import-v2-to-v3";
2022-03-06 12:49:55 +00:00
}