Allow WETHGateway to be used by multiple pools. Remove logger.

This commit is contained in:
David Racero 2020-11-02 17:51:54 +01:00
parent cabff851d0
commit 7282f7678b
12 changed files with 58 additions and 75 deletions

View File

@ -5,56 +5,56 @@ pragma experimental ABIEncoderV2;
import {IWETH} from './interfaces/IWETH.sol'; import {IWETH} from './interfaces/IWETH.sol';
import {IWETHGateway} from './interfaces/IWETHGateway.sol'; import {IWETHGateway} from './interfaces/IWETHGateway.sol';
import {ILendingPool} from '../interfaces/ILendingPool.sol'; import {ILendingPool} from '../interfaces/ILendingPool.sol';
import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol';
import {IAToken} from '../tokenization/interfaces/IAToken.sol'; import {IAToken} from '../tokenization/interfaces/IAToken.sol';
import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol'; import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol';
import {ReserveConfiguration} from '../libraries/configuration/ReserveConfiguration.sol'; import {ReserveConfiguration} from '../libraries/configuration/ReserveConfiguration.sol';
import {UserConfiguration} from '../libraries/configuration/UserConfiguration.sol'; import {UserConfiguration} from '../libraries/configuration/UserConfiguration.sol';
import {Helpers} from '../libraries/helpers/Helpers.sol'; import {Helpers} from '../libraries/helpers/Helpers.sol';
import '@nomiclabs/buidler/console.sol';
contract WETHGateway is IWETHGateway { contract WETHGateway is IWETHGateway {
using ReserveConfiguration for ReserveConfiguration.Map; using ReserveConfiguration for ReserveConfiguration.Map;
using UserConfiguration for UserConfiguration.Map; using UserConfiguration for UserConfiguration.Map;
IWETH public immutable WETH; IWETH public immutable WETH;
ILendingPoolAddressesProvider public immutable ADDRESSES_PROVIDER;
/** /**
* @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 _ADDRESSES_PROVIDER Address of the LendingPoolAddressesProvider contract
**/ **/
constructor(address _WETH, address _ADDRESSES_PROVIDER) public { constructor(address _WETH) public {
WETH = IWETH(_WETH); 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) * @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(address onBehalfOf, uint16 referralCode) external override payable { function depositETH(
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool()); ILendingPool pool,
address onBehalfOf,
uint16 referralCode
) external override payable {
require(address(pool) != address(0)); 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 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 { function withdrawETH(
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool()); ILendingPool pool,
uint256 amount,
address onBehalfOf
) external override {
require(address(pool) != address(0)); require(address(pool) != address(0));
uint256 userBalance = IAToken(pool.getReserveData(address(WETH)).aTokenAddress).balanceOf( uint256 userBalance = IAToken(pool.getReserveData(address(WETH)).aTokenAddress).balanceOf(
msg.sender msg.sender
); );
@ -81,11 +81,11 @@ 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 {
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool());
require(address(pool) != address(0)); require(address(pool) != address(0));
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebtMemory( (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebtMemory(
onBehalfOf, onBehalfOf,
@ -102,6 +102,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

View File

@ -1,12 +1,23 @@
// SPDX-License-Identifier: agpl-3.0 // SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8; pragma solidity ^0.6.8;
interface IWETHGateway { import {ILendingPool} from '../../interfaces/ILendingPool.sol';
function depositETH(address onBehalfOf, uint16 referralCode) external payable;
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( function repayETH(
ILendingPool pool,
uint256 amount, uint256 amount,
uint256 rateMode, uint256 rateMode,
address onBehalfOf address onBehalfOf

View File

@ -279,13 +279,13 @@
}, },
"MintableDelegationERC20": { "MintableDelegationERC20": {
"buidlerevm": { "buidlerevm": {
"address": "0x77B0b5636fEA30eA79BB65AeCCdb599997A849A8", "address": "0x78Ee8Fb9fE5abD5e347Fc94c2fb85596d1f60e3c",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
"AToken": { "AToken": {
"buidlerevm": { "buidlerevm": {
"address": "0x78Ee8Fb9fE5abD5e347Fc94c2fb85596d1f60e3c", "address": "0x920d847fE49E54C19047ba8bc236C45A8068Bca7",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
} }

View File

@ -15,7 +15,7 @@ import {ZERO_ADDRESS} from './constants';
import {BRE} from './misc-utils'; import {BRE} from './misc-utils';
import {tEthereumAddress} from './types'; import {tEthereumAddress} from './types';
import {getParamPerNetwork} from './contracts-helpers'; import {getParamPerNetwork} from './contracts-helpers';
import {deployWETH} from './contracts-deployments'; import {deployWETHMocked} from './contracts-deployments';
export enum ConfigNames { export enum ConfigNames {
Commons = 'Commons', Commons = 'Commons',
@ -88,6 +88,9 @@ export const getWethAddress = async (config: ICommonConfiguration) => {
if (wethAddress) { if (wethAddress) {
return 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; return weth.address;
}; };

View File

@ -421,10 +421,7 @@ export const deployATokensAndRatesHelper = async (
verify verify
); );
export const deployWETHGateway = async ( export const deployWETHGateway = async (args: [tEthereumAddress], verify?: boolean) =>
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,
@ -443,14 +440,6 @@ export const deployMockStableDebtToken = async (
verify verify
); );
export const deployWETH = async (verify?: boolean) =>
withSaveAndVerify(
await new Weth9Factory(await getFirstSigner()).deploy(),
eContractid.WETH,
[],
verify
);
export const deployWETHMocked = async (verify?: boolean) => export const deployWETHMocked = async (verify?: boolean) =>
withSaveAndVerify( withSaveAndVerify(
await new Weth9MockedFactory(await getFirstSigner()).deploy(), await new Weth9MockedFactory(await getFirstSigner()).deploy(),

View File

@ -236,12 +236,6 @@ export const getWETHGateway = async (address?: tEthereumAddress) =>
await getFirstSigner() 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) => export const getWETHMocked = async (address?: tEthereumAddress) =>
await Weth9MockedFactory.connect( await Weth9MockedFactory.connect(
address || (await getDb().get(`${eContractid.WETHMocked}.${BRE.network.name}`).value()).address, address || (await getDb().get(`${eContractid.WETHMocked}.${BRE.network.name}`).value()).address,

View File

@ -22,12 +22,7 @@ import {
} from '../../helpers/init-helpers'; } from '../../helpers/init-helpers';
import {getAllTokenAddresses} from '../../helpers/mock-helpers'; import {getAllTokenAddresses} from '../../helpers/mock-helpers';
import {ZERO_ADDRESS} from '../../helpers/constants'; import {ZERO_ADDRESS} from '../../helpers/constants';
import { import {getAllMockedTokens, getLendingPoolAddressesProvider} from '../../helpers/contracts-getters';
getAllMockedTokens,
getLendingPool,
getLendingPoolConfiguratorProxy,
getLendingPoolAddressesProvider,
} from '../../helpers/contracts-getters';
import {insertContractAddressInDb} from '../../helpers/contracts-helpers'; import {insertContractAddressInDb} from '../../helpers/contracts-helpers';
task('dev:initialize-lending-pool', 'Initialize lending pool configuration.') 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 poolConfig = loadPoolConfig(pool);
const mockTokens = await getAllMockedTokens(); const mockTokens = await getAllMockedTokens();
const lendingPoolProxy = await getLendingPool();
const lendingPoolConfiguratorProxy = await getLendingPoolConfiguratorProxy();
const allTokenAddresses = getAllTokenAddresses(mockTokens); const allTokenAddresses = getAllTokenAddresses(mockTokens);
const addressesProvider = await getLendingPoolAddressesProvider(); const addressesProvider = await getLendingPoolAddressesProvider();
@ -87,5 +80,5 @@ task('dev:initialize-lending-pool', 'Initialize lending pool configuration.')
await insertContractAddressInDb(eContractid.AaveProtocolTestHelpers, testHelpers.address); await insertContractAddressInDb(eContractid.AaveProtocolTestHelpers, testHelpers.address);
const wethAddress = await getWethAddress(poolConfig); const wethAddress = await getWethAddress(poolConfig);
await deployWETHGateway([wethAddress, addressesProvider.address]); await deployWETHGateway([wethAddress]);
}); });

View File

@ -15,11 +15,7 @@ import {
enableReservesAsCollateralByHelper, enableReservesAsCollateralByHelper,
} from '../../helpers/init-helpers'; } from '../../helpers/init-helpers';
import {exit} from 'process'; import {exit} from 'process';
import { import {getLendingPoolAddressesProvider} from '../../helpers/contracts-getters';
getLendingPool,
getLendingPoolConfiguratorProxy,
getLendingPoolAddressesProvider,
} from '../../helpers/contracts-getters';
import {ZERO_ADDRESS} from '../../helpers/constants'; import {ZERO_ADDRESS} from '../../helpers/constants';
task('full:initialize-lending-pool', 'Initialize lending pool configuration.') 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, ReservesConfig} = poolConfig as ICommonConfiguration;
const reserveAssets = await getParamPerNetwork(ReserveAssets, network); const reserveAssets = await getParamPerNetwork(ReserveAssets, network);
const lendingPoolProxy = await getLendingPool();
const lendingPoolConfiguratorProxy = await getLendingPoolConfiguratorProxy();
const addressesProvider = await getLendingPoolAddressesProvider(); const addressesProvider = await getLendingPoolAddressesProvider();
@ -57,7 +51,7 @@ 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, addressesProvider.address]); await deployWETHGateway([wethAddress]);
} catch (err) { } catch (err) {
console.error(err); console.error(err);
exit(1); exit(1);

View File

@ -250,7 +250,7 @@ const buildTestEnv = async (deployer: Signer, secondaryWallet: Signer) => {
await deployWalletBalancerProvider(addressesProvider.address); await deployWalletBalancerProvider(addressesProvider.address);
await deployWETHGateway([mockTokens.WETH.address, addressesProvider.address]); await deployWETHGateway([mockTokens.WETH.address]);
console.timeEnd('setup'); console.timeEnd('setup');
}; };

View File

@ -18,17 +18,16 @@ import {
import {getReserveAddressFromSymbol, getReserveData, getUserData} from './utils/helpers'; import {getReserveAddressFromSymbol, getReserveData, getUserData} from './utils/helpers';
import {convertToCurrencyDecimals} from '../../helpers/contracts-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 {MAX_UINT_AMOUNT, ONE_YEAR} from '../../helpers/constants';
import {SignerWithAddress, TestEnv} from './make-suite'; import {SignerWithAddress, TestEnv} from './make-suite';
import {BRE, increaseTime, timeLatest, waitForTx} from '../../helpers/misc-utils'; import {BRE, increaseTime, timeLatest, waitForTx} from '../../helpers/misc-utils';
import chai from 'chai'; import chai from 'chai';
import {ReserveData, UserReserveData} from './utils/interfaces'; import {ReserveData, UserReserveData} from './utils/interfaces';
import {ContractReceipt, Signer} from 'ethers'; import {ContractReceipt} from 'ethers';
import {AToken} from '../../types/AToken'; import {AToken} from '../../types/AToken';
import {RateMode, tEthereumAddress} from '../../helpers/types'; import {RateMode, tEthereumAddress} from '../../helpers/types';
import {getWethAddress} from '../../helpers/configuration';
const {expect} = chai; const {expect} = chai;

View File

@ -9,7 +9,6 @@ import {
getLendingPoolConfiguratorProxy, getLendingPoolConfiguratorProxy,
getPriceOracle, getPriceOracle,
getLendingPoolAddressesProviderRegistry, getLendingPoolAddressesProviderRegistry,
getWETH,
getWETHMocked, getWETHMocked,
getWETHGateway, getWETHGateway,
} from '../../helpers/contracts-getters'; } from '../../helpers/contracts-getters';

View File

@ -13,12 +13,12 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
const depositSize = parseEther('5'); const depositSize = parseEther('5');
it('Deposit WETH', async () => { it('Deposit WETH', async () => {
const {users, wethGateway, aWETH} = testEnv; const {users, wethGateway, aWETH, pool} = testEnv;
const user = users[1]; const user = users[1];
// Deposit with native ETH // 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); 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 () => { it('Withdraw WETH - Partial', async () => {
const {users, wethGateway, aWETH} = testEnv; const {users, wethGateway, aWETH, pool} = testEnv;
const user = users[1]; const user = users[1];
const priorEthersBalance = await user.signer.getBalance(); 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 // Partial Withdraw and send native Ether to user
const {gasUsed: withdrawGas} = await waitForTx( 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(); 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 () => { it('Withdraw WETH - Full', async () => {
const {users, aWETH, wethGateway} = testEnv; const {users, aWETH, wethGateway, pool} = testEnv;
const user = users[1]; const user = users[1];
const priorEthersBalance = await user.signer.getBalance(); const priorEthersBalance = 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(MAX_UINT_AMOUNT, user.address) await wethGateway.connect(user.signer).withdrawETH(pool.address, 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(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); 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(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); 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(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); 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(partialPayment, '2', user.address, {value: partialPayment}) .repayETH(pool.address, 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(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); const debtBalanceAfterFullRepay = await varDebtToken.balanceOf(user.address);
expect(debtBalanceAfterFullRepay).to.be.eq(zero); expect(debtBalanceAfterFullRepay).to.be.eq(zero);