mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
Rollback to 1 WETHGateway per LendingPool
This commit is contained in:
parent
7282f7678b
commit
525c816bbe
|
@ -16,46 +16,38 @@ contract WETHGateway is IWETHGateway {
|
||||||
using UserConfiguration for UserConfiguration.Map;
|
using UserConfiguration for UserConfiguration.Map;
|
||||||
|
|
||||||
IWETH public immutable WETH;
|
IWETH public immutable WETH;
|
||||||
|
ILendingPool public immutable POOL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Sets the WETH address and the LendingPoolAddressesProvider address. Infinite approves lending pool.
|
* @dev Sets the WETH address and the LendingPoolAddressesProvider address. Infinite approves lending pool.
|
||||||
* @param _WETH Address of the Wrapped Ether contract
|
* @param _WETH Address of the Wrapped Ether contract
|
||||||
|
* @param _POOL Address of the LendingPool contract
|
||||||
**/
|
**/
|
||||||
constructor(address _WETH) public {
|
constructor(address _WETH, address _POOL) public {
|
||||||
|
require(_WETH != address(0), '_WETH not 0 address');
|
||||||
|
require(_POOL != address(0), '_POOL not 0 address');
|
||||||
WETH = IWETH(_WETH);
|
WETH = IWETH(_WETH);
|
||||||
|
POOL = ILendingPool(_POOL);
|
||||||
|
IWETH(_WETH).approve(_POOL, uint256(-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev deposits WETH into the reserve, using native ETH. A corresponding amount of the overlying asset (aTokens)
|
* @dev deposits WETH into the reserve, using native ETH. A corresponding amount of the overlying asset (aTokens)
|
||||||
* is minted.
|
* is minted.
|
||||||
* @param pool lending pool address to deposit
|
|
||||||
* @param onBehalfOf address of the user who will receive the aTokens representing the deposit
|
* @param onBehalfOf address of the user who will receive the aTokens representing the deposit
|
||||||
* @param referralCode integrators are assigned a referral code and can potentially receive rewards.
|
* @param referralCode integrators are assigned a referral code and can potentially receive rewards.
|
||||||
**/
|
**/
|
||||||
function depositETH(
|
function depositETH(address onBehalfOf, uint16 referralCode) external override payable {
|
||||||
ILendingPool pool,
|
|
||||||
address onBehalfOf,
|
|
||||||
uint16 referralCode
|
|
||||||
) external override payable {
|
|
||||||
require(address(pool) != address(0));
|
|
||||||
WETH.deposit{value: msg.value}();
|
WETH.deposit{value: msg.value}();
|
||||||
WETH.approve(address(pool), msg.value);
|
POOL.deposit(address(WETH), msg.value, onBehalfOf, referralCode);
|
||||||
pool.deposit(address(WETH), msg.value, onBehalfOf, referralCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev withdraws the WETH _reserves of msg.sender.
|
* @dev withdraws the WETH _reserves of msg.sender.
|
||||||
* @param pool lending pool address to withdraw
|
* @param amount address of the user who will receive the aTokens representing the deposit
|
||||||
* @param amount amount of aWETH to burn and withdraw ETH asset
|
|
||||||
* @param onBehalfOf address of the user who will receive the ETH withdrawal
|
|
||||||
*/
|
*/
|
||||||
function withdrawETH(
|
function withdrawETH(uint256 amount, address onBehalfOf) external override {
|
||||||
ILendingPool pool,
|
uint256 userBalance = IAToken(POOL.getReserveData(address(WETH)).aTokenAddress).balanceOf(
|
||||||
uint256 amount,
|
|
||||||
address onBehalfOf
|
|
||||||
) external override {
|
|
||||||
require(address(pool) != address(0));
|
|
||||||
uint256 userBalance = IAToken(pool.getReserveData(address(WETH)).aTokenAddress).balanceOf(
|
|
||||||
msg.sender
|
msg.sender
|
||||||
);
|
);
|
||||||
uint256 amountToWithdraw = amount;
|
uint256 amountToWithdraw = amount;
|
||||||
|
@ -64,12 +56,12 @@ contract WETHGateway is IWETHGateway {
|
||||||
if (amount == type(uint256).max) {
|
if (amount == type(uint256).max) {
|
||||||
amountToWithdraw = userBalance;
|
amountToWithdraw = userBalance;
|
||||||
}
|
}
|
||||||
IAToken(pool.getReserveData(address(WETH)).aTokenAddress).transferFrom(
|
IAToken(POOL.getReserveData(address(WETH)).aTokenAddress).transferFrom(
|
||||||
msg.sender,
|
msg.sender,
|
||||||
address(this),
|
address(this),
|
||||||
amountToWithdraw
|
amountToWithdraw
|
||||||
);
|
);
|
||||||
pool.withdraw(address(WETH), amountToWithdraw, address(this));
|
POOL.withdraw(address(WETH), amountToWithdraw, address(this));
|
||||||
WETH.withdraw(amountToWithdraw);
|
WETH.withdraw(amountToWithdraw);
|
||||||
safeTransferETH(onBehalfOf, amountToWithdraw);
|
safeTransferETH(onBehalfOf, amountToWithdraw);
|
||||||
}
|
}
|
||||||
|
@ -81,15 +73,13 @@ contract WETHGateway is IWETHGateway {
|
||||||
* @param onBehalfOf the address for which msg.sender is repaying
|
* @param onBehalfOf the address for which msg.sender is repaying
|
||||||
*/
|
*/
|
||||||
function repayETH(
|
function repayETH(
|
||||||
ILendingPool pool,
|
|
||||||
uint256 amount,
|
uint256 amount,
|
||||||
uint256 rateMode,
|
uint256 rateMode,
|
||||||
address onBehalfOf
|
address onBehalfOf
|
||||||
) external override payable {
|
) external override payable {
|
||||||
require(address(pool) != address(0));
|
|
||||||
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebtMemory(
|
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebtMemory(
|
||||||
onBehalfOf,
|
onBehalfOf,
|
||||||
pool.getReserveData(address(WETH))
|
POOL.getReserveData(address(WETH))
|
||||||
);
|
);
|
||||||
|
|
||||||
uint256 paybackAmount = ReserveLogic.InterestRateMode(rateMode) ==
|
uint256 paybackAmount = ReserveLogic.InterestRateMode(rateMode) ==
|
||||||
|
@ -102,8 +92,7 @@ contract WETHGateway is IWETHGateway {
|
||||||
}
|
}
|
||||||
require(msg.value >= paybackAmount, 'msg.value is less than repayment amount');
|
require(msg.value >= paybackAmount, 'msg.value is less than repayment amount');
|
||||||
WETH.deposit{value: paybackAmount}();
|
WETH.deposit{value: paybackAmount}();
|
||||||
WETH.approve(address(pool), msg.value);
|
POOL.repay(address(WETH), msg.value, rateMode, onBehalfOf);
|
||||||
pool.repay(address(WETH), msg.value, rateMode, onBehalfOf);
|
|
||||||
|
|
||||||
// refund remaining dust eth
|
// refund remaining dust eth
|
||||||
if (msg.value > paybackAmount) safeTransferETH(msg.sender, msg.value - paybackAmount);
|
if (msg.value > paybackAmount) safeTransferETH(msg.sender, msg.value - paybackAmount);
|
||||||
|
|
|
@ -1,23 +1,12 @@
|
||||||
// SPDX-License-Identifier: agpl-3.0
|
// SPDX-License-Identifier: agpl-3.0
|
||||||
pragma solidity ^0.6.8;
|
pragma solidity ^0.6.8;
|
||||||
|
|
||||||
import {ILendingPool} from '../../interfaces/ILendingPool.sol';
|
|
||||||
|
|
||||||
interface IWETHGateway {
|
interface IWETHGateway {
|
||||||
function depositETH(
|
function depositETH(address onBehalfOf, uint16 referralCode) external payable;
|
||||||
ILendingPool pool,
|
|
||||||
address onBehalfOf,
|
|
||||||
uint16 referralCode
|
|
||||||
) external payable;
|
|
||||||
|
|
||||||
function withdrawETH(
|
function withdrawETH(uint256 amount, address onBehalfOf) external;
|
||||||
ILendingPool pool,
|
|
||||||
uint256 amount,
|
|
||||||
address onBehalfOf
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function repayETH(
|
function repayETH(
|
||||||
ILendingPool pool,
|
|
||||||
uint256 amount,
|
uint256 amount,
|
||||||
uint256 rateMode,
|
uint256 rateMode,
|
||||||
address onBehalfOf
|
address onBehalfOf
|
||||||
|
|
|
@ -153,18 +153,30 @@
|
||||||
"buidlerevm": {
|
"buidlerevm": {
|
||||||
"address": "0xa4bcDF64Cdd5451b6ac3743B414124A6299B65FF",
|
"address": "0xa4bcDF64Cdd5451b6ac3743B414124A6299B65FF",
|
||||||
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
|
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
|
||||||
|
},
|
||||||
|
"kovan": {
|
||||||
|
"address": "0x3eb4217Fa94b39b98D8f1Cc34E7c1F40E29C9F68",
|
||||||
|
"deployer": "0x85e4A467343c0dc4aDAB74Af84448D9c45D8ae6F"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"LendingPoolAddressesProviderRegistry": {
|
"LendingPoolAddressesProviderRegistry": {
|
||||||
"buidlerevm": {
|
"buidlerevm": {
|
||||||
"address": "0x5A0773Ff307Bf7C71a832dBB5312237fD3437f9F",
|
"address": "0x5A0773Ff307Bf7C71a832dBB5312237fD3437f9F",
|
||||||
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
|
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
|
||||||
|
},
|
||||||
|
"kovan": {
|
||||||
|
"address": "0xCE43E55eC6A758BEDFBa593520D73ced43D9ef3f",
|
||||||
|
"deployer": "0x85e4A467343c0dc4aDAB74Af84448D9c45D8ae6F"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ReserveLogic": {
|
"ReserveLogic": {
|
||||||
"buidlerevm": {
|
"buidlerevm": {
|
||||||
"address": "0xFAe0fd738dAbc8a0426F47437322b6d026A9FD95",
|
"address": "0xFAe0fd738dAbc8a0426F47437322b6d026A9FD95",
|
||||||
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
|
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
|
||||||
|
},
|
||||||
|
"kovan": {
|
||||||
|
"address": "0x027fB58038dB299199413aA05004362a21946FD9",
|
||||||
|
"deployer": "0x85e4A467343c0dc4aDAB74Af84448D9c45D8ae6F"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"GenericLogic": {
|
"GenericLogic": {
|
||||||
|
|
|
@ -421,7 +421,10 @@ export const deployATokensAndRatesHelper = async (
|
||||||
verify
|
verify
|
||||||
);
|
);
|
||||||
|
|
||||||
export const deployWETHGateway = async (args: [tEthereumAddress], verify?: boolean) =>
|
export const deployWETHGateway = async (
|
||||||
|
args: [tEthereumAddress, tEthereumAddress],
|
||||||
|
verify?: boolean
|
||||||
|
) =>
|
||||||
withSaveAndVerify(
|
withSaveAndVerify(
|
||||||
await new WethGatewayFactory(await getFirstSigner()).deploy(...args),
|
await new WethGatewayFactory(await getFirstSigner()).deploy(...args),
|
||||||
eContractid.WETHGateway,
|
eContractid.WETHGateway,
|
||||||
|
|
|
@ -79,6 +79,7 @@ task('dev:initialize-lending-pool', 'Initialize lending pool configuration.')
|
||||||
|
|
||||||
await insertContractAddressInDb(eContractid.AaveProtocolTestHelpers, testHelpers.address);
|
await insertContractAddressInDb(eContractid.AaveProtocolTestHelpers, testHelpers.address);
|
||||||
|
|
||||||
|
const lendingPoolAddress = await addressesProvider.getLendingPool();
|
||||||
const wethAddress = await getWethAddress(poolConfig);
|
const wethAddress = await getWethAddress(poolConfig);
|
||||||
await deployWETHGateway([wethAddress]);
|
await deployWETHGateway([wethAddress, lendingPoolAddress]);
|
||||||
});
|
});
|
||||||
|
|
|
@ -51,7 +51,9 @@ task('full:initialize-lending-pool', 'Initialize lending pool configuration.')
|
||||||
await deployWalletBalancerProvider(addressesProvider.address, verify);
|
await deployWalletBalancerProvider(addressesProvider.address, verify);
|
||||||
|
|
||||||
const wethAddress = await getWethAddress(poolConfig);
|
const wethAddress = await getWethAddress(poolConfig);
|
||||||
await deployWETHGateway([wethAddress]);
|
const lendingPoolAddress = await addressesProvider.getLendingPool();
|
||||||
|
|
||||||
|
await deployWETHGateway([wethAddress, lendingPoolAddress]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
|
@ -108,8 +108,8 @@ const buildTestEnv = async (deployer: Signer, secondaryWallet: Signer) => {
|
||||||
|
|
||||||
await waitForTx(await addressesProvider.setLendingPoolImpl(lendingPoolImpl.address));
|
await waitForTx(await addressesProvider.setLendingPoolImpl(lendingPoolImpl.address));
|
||||||
|
|
||||||
const address = await addressesProvider.getLendingPool();
|
const lendingPoolAddress = await addressesProvider.getLendingPool();
|
||||||
const lendingPoolProxy = await getLendingPool(address);
|
const lendingPoolProxy = await getLendingPool(lendingPoolAddress);
|
||||||
|
|
||||||
await insertContractAddressInDb(eContractid.LendingPool, lendingPoolProxy.address);
|
await insertContractAddressInDb(eContractid.LendingPool, lendingPoolProxy.address);
|
||||||
|
|
||||||
|
@ -250,7 +250,7 @@ const buildTestEnv = async (deployer: Signer, secondaryWallet: Signer) => {
|
||||||
|
|
||||||
await deployWalletBalancerProvider(addressesProvider.address);
|
await deployWalletBalancerProvider(addressesProvider.address);
|
||||||
|
|
||||||
await deployWETHGateway([mockTokens.WETH.address]);
|
await deployWETHGateway([mockTokens.WETH.address, lendingPoolAddress]);
|
||||||
|
|
||||||
console.timeEnd('setup');
|
console.timeEnd('setup');
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,7 +18,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
|
||||||
const user = users[1];
|
const user = users[1];
|
||||||
|
|
||||||
// Deposit with native ETH
|
// Deposit with native ETH
|
||||||
await wethGateway.connect(user.signer).depositETH(pool.address, user.address, '0', {value: depositSize});
|
await wethGateway.connect(user.signer).depositETH(user.address, '0', {value: depositSize});
|
||||||
|
|
||||||
const aTokensBalance = await aWETH.balanceOf(user.address);
|
const aTokensBalance = await aWETH.balanceOf(user.address);
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
|
||||||
|
|
||||||
// Partial Withdraw and send native Ether to user
|
// Partial Withdraw and send native Ether to user
|
||||||
const {gasUsed: withdrawGas} = await waitForTx(
|
const {gasUsed: withdrawGas} = await waitForTx(
|
||||||
await wethGateway.connect(user.signer).withdrawETH(pool.address, partialWithdraw, user.address)
|
await wethGateway.connect(user.signer).withdrawETH(partialWithdraw, user.address)
|
||||||
);
|
);
|
||||||
|
|
||||||
const afterPartialEtherBalance = await user.signer.getBalance();
|
const afterPartialEtherBalance = await user.signer.getBalance();
|
||||||
|
@ -80,7 +80,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
|
||||||
|
|
||||||
// Full withdraw
|
// Full withdraw
|
||||||
const {gasUsed: withdrawGas} = await waitForTx(
|
const {gasUsed: withdrawGas} = await waitForTx(
|
||||||
await wethGateway.connect(user.signer).withdrawETH(pool.address, MAX_UINT_AMOUNT, user.address)
|
await wethGateway.connect(user.signer).withdrawETH(MAX_UINT_AMOUNT, user.address)
|
||||||
);
|
);
|
||||||
|
|
||||||
const afterFullEtherBalance = await user.signer.getBalance();
|
const afterFullEtherBalance = await user.signer.getBalance();
|
||||||
|
@ -105,7 +105,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
|
||||||
const stableDebtToken = await getStableDebtToken(stableDebtTokenAddress);
|
const stableDebtToken = await getStableDebtToken(stableDebtTokenAddress);
|
||||||
|
|
||||||
// Deposit with native ETH
|
// Deposit with native ETH
|
||||||
await wethGateway.connect(user.signer).depositETH(pool.address, user.address, '0', {value: depositSize});
|
await wethGateway.connect(user.signer).depositETH(user.address, '0', {value: depositSize});
|
||||||
|
|
||||||
const aTokensBalance = await aWETH.balanceOf(user.address);
|
const aTokensBalance = await aWETH.balanceOf(user.address);
|
||||||
|
|
||||||
|
@ -125,7 +125,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
|
||||||
await waitForTx(
|
await waitForTx(
|
||||||
await wethGateway
|
await wethGateway
|
||||||
.connect(user.signer)
|
.connect(user.signer)
|
||||||
.repayETH(pool.address, MAX_UINT_AMOUNT, '1', user.address, {value: repaySize})
|
.repayETH(MAX_UINT_AMOUNT, '1', user.address, {value: repaySize})
|
||||||
);
|
);
|
||||||
|
|
||||||
const debtBalanceAfterRepay = await stableDebtToken.balanceOf(user.address);
|
const debtBalanceAfterRepay = await stableDebtToken.balanceOf(user.address);
|
||||||
|
@ -145,7 +145,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
|
||||||
const varDebtToken = await getVariableDebtToken(variableDebtTokenAddress);
|
const varDebtToken = await getVariableDebtToken(variableDebtTokenAddress);
|
||||||
|
|
||||||
// Deposit with native ETH
|
// Deposit with native ETH
|
||||||
await wethGateway.connect(user.signer).depositETH(pool.address, user.address, '0', {value: depositSize});
|
await wethGateway.connect(user.signer).depositETH(user.address, '0', {value: depositSize});
|
||||||
|
|
||||||
const aTokensBalance = await aWETH.balanceOf(user.address);
|
const aTokensBalance = await aWETH.balanceOf(user.address);
|
||||||
|
|
||||||
|
@ -166,7 +166,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
|
||||||
await waitForTx(
|
await waitForTx(
|
||||||
await wethGateway
|
await wethGateway
|
||||||
.connect(user.signer)
|
.connect(user.signer)
|
||||||
.repayETH(pool.address, partialPayment, '2', user.address, {value: partialPayment})
|
.repayETH(partialPayment, '2', user.address, {value: partialPayment})
|
||||||
);
|
);
|
||||||
|
|
||||||
const debtBalanceAfterPartialRepay = await varDebtToken.balanceOf(user.address);
|
const debtBalanceAfterPartialRepay = await varDebtToken.balanceOf(user.address);
|
||||||
|
@ -176,7 +176,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
|
||||||
await waitForTx(
|
await waitForTx(
|
||||||
await wethGateway
|
await wethGateway
|
||||||
.connect(user.signer)
|
.connect(user.signer)
|
||||||
.repayETH(pool.address, MAX_UINT_AMOUNT, '2', user.address, {value: repaySize})
|
.repayETH(MAX_UINT_AMOUNT, '2', user.address, {value: repaySize})
|
||||||
);
|
);
|
||||||
const debtBalanceAfterFullRepay = await varDebtToken.balanceOf(user.address);
|
const debtBalanceAfterFullRepay = await varDebtToken.balanceOf(user.address);
|
||||||
expect(debtBalanceAfterFullRepay).to.be.eq(zero);
|
expect(debtBalanceAfterFullRepay).to.be.eq(zero);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user