Merge pull request #193 from Instadapp/Avalanche-Aave-v2-to-v3-connector-fix

Fixed Aave V2 to V3 Migration
This commit is contained in:
Thrilok kumar 2022-03-17 00:02:44 +04:00 committed by GitHub
commit 5f28557f16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 11 deletions

View File

@ -5,8 +5,10 @@ contract Events {
address indexed user, address indexed user,
bool doImport, bool doImport,
bool convertStable, bool convertStable,
address[] supplyTokens, address[] supplyTokensV2,
address[] borrowTokens, address[] supplyTokensV3,
address[] borrowTokensV2,
address[] borrowTokensV3,
uint256[] flashLoanFees, uint256[] flashLoanFees,
uint256[] supplyAmts, uint256[] supplyAmts,
uint256[] stableBorrowAmts, uint256[] stableBorrowAmts,

View File

@ -44,7 +44,9 @@ abstract contract Helper is DSMath, Basic {
struct ImportData { struct ImportData {
address[] _supplyTokens; address[] _supplyTokens;
address[] _supplyTokensV3;
address[] _borrowTokens; address[] _borrowTokens;
address[] _borrowTokensV3;
ATokenV2Interface[] aTokens; ATokenV2Interface[] aTokens;
uint256[] supplyAmts; uint256[] supplyAmts;
uint256[] variableBorrowAmts; uint256[] variableBorrowAmts;
@ -65,6 +67,21 @@ abstract contract Helper is DSMath, Basic {
} }
contract _AaveHelper is Helper { contract _AaveHelper is Helper {
/*
** Convert Avalanche Bridge tokens to Offical tokens. Like USDC.e to USDC
*/
function convertABTokens(address _token) internal pure returns (address) {
if (_token == 0xc7198437980c041c805A1EDcbA50c1Ce5db95118) {
// USDT.e => USDT
return 0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7;
} else if (_token == 0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664) {
// USDC.e => USDC
return 0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E;
} else {
return _token;
}
}
function getBorrowAmountV2(address _token, address userAccount) function getBorrowAmountV2(address _token, address userAccount)
internal internal
view view
@ -92,13 +109,24 @@ contract _AaveHelper is Helper {
) internal returns (ImportData memory) { ) internal returns (ImportData memory) {
if (inputData.borrowTokens.length > 0) { if (inputData.borrowTokens.length > 0) {
data._borrowTokens = new address[](inputData.borrowTokens.length); data._borrowTokens = new address[](inputData.borrowTokens.length);
data._borrowTokensV3 = new address[](inputData.borrowTokens.length);
data.variableBorrowAmts = new uint256[]( data.variableBorrowAmts = new uint256[](
inputData.borrowTokens.length inputData.borrowTokens.length
); );
data.stableBorrowAmts = new uint256[]( data.stableBorrowAmts = new uint256[](
inputData.borrowTokens.length inputData.borrowTokens.length
); );
data.variableBorrowAmtsWithFee = new uint256[](
inputData.borrowTokens.length
);
data.stableBorrowAmtsWithFee = new uint256[](
inputData.borrowTokens.length
);
data.totalBorrowAmtsWithFee = new uint256[](
inputData.borrowTokens.length
);
data.totalBorrowAmts = new uint256[](inputData.borrowTokens.length); data.totalBorrowAmts = new uint256[](inputData.borrowTokens.length);
for (uint256 i = 0; i < inputData.borrowTokens.length; i++) { for (uint256 i = 0; i < inputData.borrowTokens.length; i++) {
for (uint256 j = i; j < inputData.borrowTokens.length; j++) { for (uint256 j = i; j < inputData.borrowTokens.length; j++) {
if (j != i) { if (j != i) {
@ -115,6 +143,7 @@ contract _AaveHelper is Helper {
? wavaxAddr ? wavaxAddr
: inputData.borrowTokens[i]; : inputData.borrowTokens[i];
data._borrowTokens[i] = _token; data._borrowTokens[i] = _token;
data._borrowTokensV3[i] = convertABTokens(_token);
( (
data.stableBorrowAmts[i], data.stableBorrowAmts[i],
@ -159,6 +188,7 @@ contract _AaveHelper is Helper {
) internal view returns (ImportData memory) { ) internal view returns (ImportData memory) {
data.supplyAmts = new uint256[](inputData.supplyTokens.length); data.supplyAmts = new uint256[](inputData.supplyTokens.length);
data._supplyTokens = new address[](inputData.supplyTokens.length); data._supplyTokens = new address[](inputData.supplyTokens.length);
data._supplyTokensV3 = new address[](inputData.supplyTokens.length);
data.aTokens = new ATokenV2Interface[](inputData.supplyTokens.length); data.aTokens = new ATokenV2Interface[](inputData.supplyTokens.length);
for (uint256 i = 0; i < inputData.supplyTokens.length; i++) { for (uint256 i = 0; i < inputData.supplyTokens.length; i++) {
@ -179,6 +209,8 @@ contract _AaveHelper is Helper {
_token _token
); );
data._supplyTokens[i] = _token; data._supplyTokens[i] = _token;
data._supplyTokensV3[i] = convertABTokens(_token);
data.aTokens[i] = ATokenV2Interface(_aToken); data.aTokens[i] = ATokenV2Interface(_aToken);
data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount); data.supplyAmts[i] = data.aTokens[i].balanceOf(userAccount);
} }

View File

@ -71,42 +71,44 @@ contract _AaveV2ToV3MigrationResolver is _AaveHelper {
); );
// deposit tokens in v3 // deposit tokens in v3
_depositTokensV3( _depositTokensV3(
data._supplyTokens.length, data._supplyTokensV3.length,
aaveV3, aaveV3,
data.supplyAmts, data.supplyAmts,
data._supplyTokens data._supplyTokensV3
); );
// borrow assets in aave v3 after migrating position // borrow assets in aave v3 after migrating position
if (data.convertStable) { if (data.convertStable) {
_BorrowVariableV3( _BorrowVariableV3(
data._borrowTokens.length, data._borrowTokensV3.length,
aaveV3, aaveV3,
data._borrowTokens, data._borrowTokensV3,
data.totalBorrowAmtsWithFee data.totalBorrowAmtsWithFee
); );
} else { } else {
_BorrowStableV3( _BorrowStableV3(
data._borrowTokens.length, data._borrowTokensV3.length,
aaveV3, aaveV3,
data._borrowTokens, data._borrowTokensV3,
data.stableBorrowAmtsWithFee data.stableBorrowAmtsWithFee
); );
_BorrowVariableV3( _BorrowVariableV3(
data._borrowTokens.length, data._borrowTokensV3.length,
aaveV3, aaveV3,
data._borrowTokens, data._borrowTokensV3,
data.variableBorrowAmtsWithFee data.variableBorrowAmtsWithFee
); );
} }
_eventName = "LogAaveImportV2ToV3(address,bool,bool,address[],address[],uint256[],uint256[],uint256[],uint256[])"; _eventName = "LogAaveImportV2ToV3(address,bool,bool,address[],address[],address[],address[],uint256[],uint256[],uint256[],uint256[])";
_eventParam = abi.encode( _eventParam = abi.encode(
userAccount, userAccount,
doImport, doImport,
inputData.convertStable, inputData.convertStable,
inputData.supplyTokens, inputData.supplyTokens,
data._supplyTokensV3,
inputData.borrowTokens, inputData.borrowTokens,
data._borrowTokensV3,
inputData.flashLoanFees, inputData.flashLoanFees,
data.supplyAmts, data.supplyAmts,
data.stableBorrowAmts, data.stableBorrowAmts,

View File

@ -98,6 +98,15 @@ contract _AaveHelper is Helper {
data.stableBorrowAmts = new uint256[]( data.stableBorrowAmts = new uint256[](
inputData.borrowTokens.length inputData.borrowTokens.length
); );
data.variableBorrowAmtsWithFee = new uint256[](
inputData.borrowTokens.length
);
data.stableBorrowAmtsWithFee = new uint256[](
inputData.borrowTokens.length
);
data.totalBorrowAmtsWithFee = new uint256[](
inputData.borrowTokens.length
);
data.totalBorrowAmts = new uint256[](inputData.borrowTokens.length); data.totalBorrowAmts = new uint256[](inputData.borrowTokens.length);
for (uint256 i = 0; i < inputData.borrowTokens.length; i++) { for (uint256 i = 0; i < inputData.borrowTokens.length; i++) {
for (uint256 j = i; j < inputData.borrowTokens.length; j++) { for (uint256 j = i; j < inputData.borrowTokens.length; j++) {