From 7282f7678be6b34b4d07c5c26f5e13813643c3f6 Mon Sep 17 00:00:00 2001 From: David Racero Date: Mon, 2 Nov 2020 17:51:54 +0100 Subject: [PATCH] Allow WETHGateway to be used by multiple pools. Remove logger. --- contracts/misc/WETHGateway.sol | 35 +++++++++++----------- contracts/misc/interfaces/IWETHGateway.sol | 17 +++++++++-- deployed-contracts.json | 4 +-- helpers/configuration.ts | 7 +++-- helpers/contracts-deployments.ts | 13 +------- helpers/contracts-getters.ts | 6 ---- tasks/dev/5_initialize.ts | 11 ++----- tasks/full/5_initialize.ts | 10 ++----- test/__setup.spec.ts | 2 +- test/helpers/actions.ts | 5 ++-- test/helpers/make-suite.ts | 1 - test/weth-gateway.spec.ts | 22 +++++++------- 12 files changed, 58 insertions(+), 75 deletions(-) diff --git a/contracts/misc/WETHGateway.sol b/contracts/misc/WETHGateway.sol index 98db8a50..0bd916db 100644 --- a/contracts/misc/WETHGateway.sol +++ b/contracts/misc/WETHGateway.sol @@ -5,56 +5,56 @@ pragma experimental ABIEncoderV2; import {IWETH} from './interfaces/IWETH.sol'; import {IWETHGateway} from './interfaces/IWETHGateway.sol'; import {ILendingPool} from '../interfaces/ILendingPool.sol'; -import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; import {IAToken} from '../tokenization/interfaces/IAToken.sol'; import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol'; import {ReserveConfiguration} from '../libraries/configuration/ReserveConfiguration.sol'; import {UserConfiguration} from '../libraries/configuration/UserConfiguration.sol'; import {Helpers} from '../libraries/helpers/Helpers.sol'; -import '@nomiclabs/buidler/console.sol'; contract WETHGateway is IWETHGateway { using ReserveConfiguration for ReserveConfiguration.Map; using UserConfiguration for UserConfiguration.Map; IWETH public immutable WETH; - ILendingPoolAddressesProvider public immutable ADDRESSES_PROVIDER; /** * @dev Sets the WETH address and the LendingPoolAddressesProvider address. Infinite approves lending pool. * @param _WETH Address of the Wrapped Ether contract - * @param _ADDRESSES_PROVIDER Address of the LendingPoolAddressesProvider contract **/ - constructor(address _WETH, address _ADDRESSES_PROVIDER) public { + constructor(address _WETH) public { WETH = IWETH(_WETH); - ADDRESSES_PROVIDER = ILendingPoolAddressesProvider(_ADDRESSES_PROVIDER); - IWETH(_WETH).approve( - address(ILendingPool(ILendingPoolAddressesProvider(_ADDRESSES_PROVIDER).getLendingPool())), - uint256(-1) - ); } /** * @dev deposits WETH into the reserve, using native ETH. A corresponding amount of the overlying asset (aTokens) * is minted. + * @param pool lending pool address to 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. **/ - function depositETH(address onBehalfOf, uint16 referralCode) external override payable { - ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool()); + function depositETH( + ILendingPool pool, + address onBehalfOf, + uint16 referralCode + ) external override payable { require(address(pool) != address(0)); WETH.deposit{value: msg.value}(); + WETH.approve(address(pool), msg.value); pool.deposit(address(WETH), msg.value, onBehalfOf, referralCode); } /** * @dev withdraws the WETH _reserves of msg.sender. - * @param amount address of the user who will receive the aTokens representing the deposit + * @param pool lending pool address to withdraw + * @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(uint256 amount, address onBehalfOf) external override { - ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool()); + function withdrawETH( + ILendingPool pool, + uint256 amount, + address onBehalfOf + ) external override { require(address(pool) != address(0)); - uint256 userBalance = IAToken(pool.getReserveData(address(WETH)).aTokenAddress).balanceOf( msg.sender ); @@ -81,11 +81,11 @@ contract WETHGateway is IWETHGateway { * @param onBehalfOf the address for which msg.sender is repaying */ function repayETH( + ILendingPool pool, uint256 amount, uint256 rateMode, address onBehalfOf ) external override payable { - ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool()); require(address(pool) != address(0)); (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebtMemory( onBehalfOf, @@ -102,6 +102,7 @@ contract WETHGateway is IWETHGateway { } require(msg.value >= paybackAmount, 'msg.value is less than repayment amount'); WETH.deposit{value: paybackAmount}(); + WETH.approve(address(pool), msg.value); pool.repay(address(WETH), msg.value, rateMode, onBehalfOf); // refund remaining dust eth diff --git a/contracts/misc/interfaces/IWETHGateway.sol b/contracts/misc/interfaces/IWETHGateway.sol index 51bdc337..abe7c228 100644 --- a/contracts/misc/interfaces/IWETHGateway.sol +++ b/contracts/misc/interfaces/IWETHGateway.sol @@ -1,12 +1,23 @@ // SPDX-License-Identifier: agpl-3.0 pragma solidity ^0.6.8; -interface IWETHGateway { - function depositETH(address onBehalfOf, uint16 referralCode) external payable; +import {ILendingPool} from '../../interfaces/ILendingPool.sol'; - function withdrawETH(uint256 amount, address onBehalfOf) external; +interface IWETHGateway { + function depositETH( + ILendingPool pool, + address onBehalfOf, + uint16 referralCode + ) external payable; + + function withdrawETH( + ILendingPool pool, + uint256 amount, + address onBehalfOf + ) external; function repayETH( + ILendingPool pool, uint256 amount, uint256 rateMode, address onBehalfOf diff --git a/deployed-contracts.json b/deployed-contracts.json index 411abff0..29940096 100644 --- a/deployed-contracts.json +++ b/deployed-contracts.json @@ -279,13 +279,13 @@ }, "MintableDelegationERC20": { "buidlerevm": { - "address": "0x77B0b5636fEA30eA79BB65AeCCdb599997A849A8", + "address": "0x78Ee8Fb9fE5abD5e347Fc94c2fb85596d1f60e3c", "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" } }, "AToken": { "buidlerevm": { - "address": "0x78Ee8Fb9fE5abD5e347Fc94c2fb85596d1f60e3c", + "address": "0x920d847fE49E54C19047ba8bc236C45A8068Bca7", "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" } } diff --git a/helpers/configuration.ts b/helpers/configuration.ts index c841225f..b4bda09c 100644 --- a/helpers/configuration.ts +++ b/helpers/configuration.ts @@ -15,7 +15,7 @@ import {ZERO_ADDRESS} from './constants'; import {BRE} from './misc-utils'; import {tEthereumAddress} from './types'; import {getParamPerNetwork} from './contracts-helpers'; -import {deployWETH} from './contracts-deployments'; +import {deployWETHMocked} from './contracts-deployments'; export enum ConfigNames { Commons = 'Commons', @@ -88,6 +88,9 @@ export const getWethAddress = async (config: ICommonConfiguration) => { if (wethAddress) { return wethAddress; } - const weth = await deployWETH(); + if (currentNetwork.includes('main')) { + throw new Error('WETH not set at mainnet configuration.'); + } + const weth = await deployWETHMocked(); return weth.address; }; diff --git a/helpers/contracts-deployments.ts b/helpers/contracts-deployments.ts index a2749ab4..ce3ba1f7 100644 --- a/helpers/contracts-deployments.ts +++ b/helpers/contracts-deployments.ts @@ -421,10 +421,7 @@ export const deployATokensAndRatesHelper = async ( verify ); -export const deployWETHGateway = async ( - args: [tEthereumAddress, tEthereumAddress], - verify?: boolean -) => +export const deployWETHGateway = async (args: [tEthereumAddress], verify?: boolean) => withSaveAndVerify( await new WethGatewayFactory(await getFirstSigner()).deploy(...args), eContractid.WETHGateway, @@ -443,14 +440,6 @@ export const deployMockStableDebtToken = async ( verify ); -export const deployWETH = async (verify?: boolean) => - withSaveAndVerify( - await new Weth9Factory(await getFirstSigner()).deploy(), - eContractid.WETH, - [], - verify - ); - export const deployWETHMocked = async (verify?: boolean) => withSaveAndVerify( await new Weth9MockedFactory(await getFirstSigner()).deploy(), diff --git a/helpers/contracts-getters.ts b/helpers/contracts-getters.ts index e8bcb39a..3e7ddf9a 100644 --- a/helpers/contracts-getters.ts +++ b/helpers/contracts-getters.ts @@ -236,12 +236,6 @@ export const getWETHGateway = async (address?: tEthereumAddress) => await getFirstSigner() ); -export const getWETH = async (address?: tEthereumAddress) => - await Weth9Factory.connect( - address || (await getDb().get(`${eContractid.WETH}.${BRE.network.name}`).value()).address, - await getFirstSigner() - ); - export const getWETHMocked = async (address?: tEthereumAddress) => await Weth9MockedFactory.connect( address || (await getDb().get(`${eContractid.WETHMocked}.${BRE.network.name}`).value()).address, diff --git a/tasks/dev/5_initialize.ts b/tasks/dev/5_initialize.ts index 46bfde5f..205dc209 100644 --- a/tasks/dev/5_initialize.ts +++ b/tasks/dev/5_initialize.ts @@ -22,12 +22,7 @@ import { } from '../../helpers/init-helpers'; import {getAllTokenAddresses} from '../../helpers/mock-helpers'; import {ZERO_ADDRESS} from '../../helpers/constants'; -import { - getAllMockedTokens, - getLendingPool, - getLendingPoolConfiguratorProxy, - getLendingPoolAddressesProvider, -} from '../../helpers/contracts-getters'; +import {getAllMockedTokens, getLendingPoolAddressesProvider} from '../../helpers/contracts-getters'; import {insertContractAddressInDb} from '../../helpers/contracts-helpers'; task('dev:initialize-lending-pool', 'Initialize lending pool configuration.') @@ -38,8 +33,6 @@ task('dev:initialize-lending-pool', 'Initialize lending pool configuration.') const poolConfig = loadPoolConfig(pool); const mockTokens = await getAllMockedTokens(); - const lendingPoolProxy = await getLendingPool(); - const lendingPoolConfiguratorProxy = await getLendingPoolConfiguratorProxy(); const allTokenAddresses = getAllTokenAddresses(mockTokens); const addressesProvider = await getLendingPoolAddressesProvider(); @@ -87,5 +80,5 @@ task('dev:initialize-lending-pool', 'Initialize lending pool configuration.') await insertContractAddressInDb(eContractid.AaveProtocolTestHelpers, testHelpers.address); const wethAddress = await getWethAddress(poolConfig); - await deployWETHGateway([wethAddress, addressesProvider.address]); + await deployWETHGateway([wethAddress]); }); diff --git a/tasks/full/5_initialize.ts b/tasks/full/5_initialize.ts index 0adeaae5..6a311db5 100644 --- a/tasks/full/5_initialize.ts +++ b/tasks/full/5_initialize.ts @@ -15,11 +15,7 @@ import { enableReservesAsCollateralByHelper, } from '../../helpers/init-helpers'; import {exit} from 'process'; -import { - getLendingPool, - getLendingPoolConfiguratorProxy, - getLendingPoolAddressesProvider, -} from '../../helpers/contracts-getters'; +import {getLendingPoolAddressesProvider} from '../../helpers/contracts-getters'; import {ZERO_ADDRESS} from '../../helpers/constants'; task('full:initialize-lending-pool', 'Initialize lending pool configuration.') @@ -33,8 +29,6 @@ task('full:initialize-lending-pool', 'Initialize lending pool configuration.') const {ReserveAssets, ReservesConfig} = poolConfig as ICommonConfiguration; const reserveAssets = await getParamPerNetwork(ReserveAssets, network); - const lendingPoolProxy = await getLendingPool(); - const lendingPoolConfiguratorProxy = await getLendingPoolConfiguratorProxy(); const addressesProvider = await getLendingPoolAddressesProvider(); @@ -57,7 +51,7 @@ task('full:initialize-lending-pool', 'Initialize lending pool configuration.') await deployWalletBalancerProvider(addressesProvider.address, verify); const wethAddress = await getWethAddress(poolConfig); - await deployWETHGateway([wethAddress, addressesProvider.address]); + await deployWETHGateway([wethAddress]); } catch (err) { console.error(err); exit(1); diff --git a/test/__setup.spec.ts b/test/__setup.spec.ts index aee70d55..21fae571 100644 --- a/test/__setup.spec.ts +++ b/test/__setup.spec.ts @@ -250,7 +250,7 @@ const buildTestEnv = async (deployer: Signer, secondaryWallet: Signer) => { await deployWalletBalancerProvider(addressesProvider.address); - await deployWETHGateway([mockTokens.WETH.address, addressesProvider.address]); + await deployWETHGateway([mockTokens.WETH.address]); console.timeEnd('setup'); }; diff --git a/test/helpers/actions.ts b/test/helpers/actions.ts index 6adfee29..d998962b 100644 --- a/test/helpers/actions.ts +++ b/test/helpers/actions.ts @@ -18,17 +18,16 @@ import { import {getReserveAddressFromSymbol, getReserveData, getUserData} from './utils/helpers'; import {convertToCurrencyDecimals} from '../../helpers/contracts-helpers'; -import {getAToken, getMintableErc20, getWETH} from '../../helpers/contracts-getters'; +import {getAToken, getMintableErc20} from '../../helpers/contracts-getters'; import {MAX_UINT_AMOUNT, ONE_YEAR} from '../../helpers/constants'; import {SignerWithAddress, TestEnv} from './make-suite'; import {BRE, increaseTime, timeLatest, waitForTx} from '../../helpers/misc-utils'; import chai from 'chai'; import {ReserveData, UserReserveData} from './utils/interfaces'; -import {ContractReceipt, Signer} from 'ethers'; +import {ContractReceipt} from 'ethers'; import {AToken} from '../../types/AToken'; import {RateMode, tEthereumAddress} from '../../helpers/types'; -import {getWethAddress} from '../../helpers/configuration'; const {expect} = chai; diff --git a/test/helpers/make-suite.ts b/test/helpers/make-suite.ts index df728c97..ca661471 100644 --- a/test/helpers/make-suite.ts +++ b/test/helpers/make-suite.ts @@ -9,7 +9,6 @@ import { getLendingPoolConfiguratorProxy, getPriceOracle, getLendingPoolAddressesProviderRegistry, - getWETH, getWETHMocked, getWETHGateway, } from '../../helpers/contracts-getters'; diff --git a/test/weth-gateway.spec.ts b/test/weth-gateway.spec.ts index 5c8536d7..81e57bf5 100644 --- a/test/weth-gateway.spec.ts +++ b/test/weth-gateway.spec.ts @@ -13,12 +13,12 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => const depositSize = parseEther('5'); it('Deposit WETH', async () => { - const {users, wethGateway, aWETH} = testEnv; + const {users, wethGateway, aWETH, pool} = testEnv; const user = users[1]; // Deposit with native ETH - await wethGateway.connect(user.signer).depositETH(user.address, '0', {value: depositSize}); + await wethGateway.connect(user.signer).depositETH(pool.address, user.address, '0', {value: depositSize}); const aTokensBalance = await aWETH.balanceOf(user.address); @@ -27,7 +27,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => }); it('Withdraw WETH - Partial', async () => { - const {users, wethGateway, aWETH} = testEnv; + const {users, wethGateway, aWETH, pool} = testEnv; const user = users[1]; const priorEthersBalance = await user.signer.getBalance(); @@ -46,7 +46,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => // Partial Withdraw and send native Ether to user const {gasUsed: withdrawGas} = await waitForTx( - await wethGateway.connect(user.signer).withdrawETH(partialWithdraw, user.address) + await wethGateway.connect(user.signer).withdrawETH(pool.address, partialWithdraw, user.address) ); const afterPartialEtherBalance = await user.signer.getBalance(); @@ -64,7 +64,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => }); it('Withdraw WETH - Full', async () => { - const {users, aWETH, wethGateway} = testEnv; + const {users, aWETH, wethGateway, pool} = testEnv; const user = users[1]; const priorEthersBalance = await user.signer.getBalance(); @@ -80,7 +80,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => // Full withdraw const {gasUsed: withdrawGas} = await waitForTx( - await wethGateway.connect(user.signer).withdrawETH(MAX_UINT_AMOUNT, user.address) + await wethGateway.connect(user.signer).withdrawETH(pool.address, MAX_UINT_AMOUNT, user.address) ); 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); // Deposit with native ETH - await wethGateway.connect(user.signer).depositETH(user.address, '0', {value: depositSize}); + await wethGateway.connect(user.signer).depositETH(pool.address, user.address, '0', {value: depositSize}); const aTokensBalance = await aWETH.balanceOf(user.address); @@ -125,7 +125,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => await waitForTx( await wethGateway .connect(user.signer) - .repayETH(MAX_UINT_AMOUNT, '1', user.address, {value: repaySize}) + .repayETH(pool.address, MAX_UINT_AMOUNT, '1', user.address, {value: repaySize}) ); 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); // Deposit with native ETH - await wethGateway.connect(user.signer).depositETH(user.address, '0', {value: depositSize}); + await wethGateway.connect(user.signer).depositETH(pool.address, user.address, '0', {value: depositSize}); const aTokensBalance = await aWETH.balanceOf(user.address); @@ -166,7 +166,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => await waitForTx( await wethGateway .connect(user.signer) - .repayETH(partialPayment, '2', user.address, {value: partialPayment}) + .repayETH(pool.address, partialPayment, '2', user.address, {value: partialPayment}) ); const debtBalanceAfterPartialRepay = await varDebtToken.balanceOf(user.address); @@ -176,7 +176,7 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => await waitForTx( await wethGateway .connect(user.signer) - .repayETH(MAX_UINT_AMOUNT, '2', user.address, {value: repaySize}) + .repayETH(pool.address, MAX_UINT_AMOUNT, '2', user.address, {value: repaySize}) ); const debtBalanceAfterFullRepay = await varDebtToken.balanceOf(user.address); expect(debtBalanceAfterFullRepay).to.be.eq(zero);