Added WETHGateway tests

This commit is contained in:
David Racero 2020-10-29 15:50:32 +01:00
parent 744befc46c
commit a9e7fa3516
6 changed files with 4729 additions and 9448 deletions

View File

@ -11,7 +11,6 @@ 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;
@ -41,15 +40,10 @@ contract WETHGateway is IWETHGateway {
* @param referralCode integrators are assigned a referral code and can potentially receive rewards.
**/
function depositETH(address onBehalfOf, uint16 referralCode) external override payable {
console.log('hi');
console.log(msg.value);
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool());
require(address(pool) != address(0));
console.log('deposit');
WETH.deposit{value: msg.value}();
console.log('weth in');
pool.deposit(address(WETH), msg.value, onBehalfOf, referralCode);
console.log('deposited');
}
/**
@ -69,9 +63,12 @@ contract WETHGateway is IWETHGateway {
if (amount == type(uint256).max) {
amountToWithdraw = userBalance;
}
WETH.transferFrom(msg.sender, address(this), amountToWithdraw);
pool.withdraw(address(WETH), amount);
IAToken(pool.getReserveData(address(WETH)).aTokenAddress).transferFrom(
msg.sender,
address(this),
amountToWithdraw
);
pool.withdraw(address(WETH), amountToWithdraw);
WETH.withdraw(amountToWithdraw);
safeTransferETH(msg.sender, amountToWithdraw);
}

View File

@ -277,4 +277,4 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
}
}
}

13970
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -72,7 +72,7 @@
"chai-bignumber": "3.0.0",
"chai-bn": "^0.2.1",
"eth-sig-util": "2.5.3",
"ethereum-waffle": "3.0.2",
"ethereum-waffle": "^3.1.2",
"ethereumjs-util": "7.0.2",
"ethers": "5.0.8",
"globby": "^11.0.1",

View File

@ -31,8 +31,11 @@ import {getEthersSigners} from '../../helpers/contracts-helpers';
import {Weth9} from '../../types/Weth9';
import {Weth9Mocked} from '../../types/Weth9Mocked';
import {WethGateway} from '../../types/WethGateway';
import {solidity} from 'ethereum-waffle';
chai.use(bignumberChai());
chai.use(almostEqual());
chai.use(solidity);
export interface SignerWithAddress {
signer: Signer;

View File

@ -1,20 +1,19 @@
import BigNumber from 'bignumber.js';
import {MAX_UINT_AMOUNT} from '../helpers/constants';
import {convertToCurrencyDecimals} from '../helpers/contracts-helpers';
import {deposit} from './helpers/actions';
import {makeSuite, TestEnv} from './helpers/make-suite';
import {parseEther} from 'ethers/lib/utils';
import {BRE, waitForTx} from '../helpers/misc-utils';
import {BigNumber} from 'ethers';
import {getStableDebtToken, getVariableDebtToken} from '../helpers/contracts-getters';
const chai = require('chai');
const {expect} = chai;
const {expect} = require('chai');
makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => {
const zero = new BigNumber('0');
const zero = BigNumber.from('0');
const depositSize = parseEther('5');
console.log(depositSize.toString());
it('Deposit WETH', async () => {
const {users, weth, wethGateway, aWETH} = testEnv;
const {users, wethGateway, aWETH} = testEnv;
const user = users[1];
@ -23,47 +22,163 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
const aTokensBalance = await aWETH.balanceOf(user.address);
expect(aTokensBalance.toString()).to.be.bignumber.gt(zero.toString());
expect(aTokensBalance.toString()).to.be.bignumber.gte(depositSize.toString());
expect(aTokensBalance).to.be.gt(zero);
expect(aTokensBalance).to.be.gte(depositSize);
});
it('Withdraw WETH', async () => {
const {users, weth, wethGateway, aWETH} = testEnv;
it('Withdraw WETH - Partial', async () => {
const {users, wethGateway, aWETH} = testEnv;
const user = users[1];
const priorEthersBalance = await user.signer.getBalance();
const aTokensBalance = await aWETH.balanceOf(user.address);
expect(aTokensBalance.toString()).to.be.bignumber.gt(
zero.toString(),
'User should have aTokens.'
);
expect(aTokensBalance.toString()).to.be.bignumber.gte(
depositSize.toString(),
'User should have the deposited aTokens.'
);
expect(aTokensBalance).to.be.gt(zero, 'User should have aTokens.');
// Partially withdraw native ETH
console.log('prior partial');
const partialWithdraw = await convertToCurrencyDecimals(weth.address, '2');
await wethGateway.connect(user.signer).withdrawETH(partialWithdraw.toString());
console.log('after partial');
const partialWithdraw = await convertToCurrencyDecimals(aWETH.address, '2');
const afterPartialEtherBalance = await user.signer.getBalance();
expect(afterPartialEtherBalance.toString()).to.be.bignumber.eq(
priorEthersBalance.add(partialWithdraw).toString(),
'User ETHER balance should contain the partial withdraw'
// Approve the aTokens to Gateway so Gateway can withdraw and convert to Ether
const approveTx = await aWETH
.connect(user.signer)
.approve(wethGateway.address, MAX_UINT_AMOUNT);
const {gasUsed: approveGas} = await waitForTx(approveTx);
// Partial Withdraw and send native Ether to user
const {gasUsed: withdrawGas} = await waitForTx(
await wethGateway.connect(user.signer).withdrawETH(partialWithdraw)
);
console.log('prior full');
// Full withdraw
await wethGateway.connect(user.signer).withdrawETH(MAX_UINT_AMOUNT);
const afterFullEtherBalance = await user.signer.getBalance();
expect(afterFullEtherBalance.toString()).to.be.bignumber.gte(
afterPartialEtherBalance.add('3').toString(),
'User ETHER balance should contain the full withdraw'
const afterPartialEtherBalance = await user.signer.getBalance();
const afterPartialATokensBalance = await aWETH.balanceOf(user.address);
const gasCosts = approveGas.add(withdrawGas).mul(approveTx.gasPrice);
expect(afterPartialEtherBalance).to.be.equal(
priorEthersBalance.add(partialWithdraw).sub(gasCosts),
'User ETHER balance should contain the partial withdraw'
);
expect(afterPartialATokensBalance).to.be.equal(
aTokensBalance.sub(partialWithdraw),
'User aWETH balance should be substracted'
);
});
xit('Borrow and Repay WETH', async () => {});
it('Withdraw WETH - Full', async () => {
const {users, aWETH, wethGateway} = testEnv;
const user = users[1];
const priorEthersBalance = await user.signer.getBalance();
const aTokensBalance = await aWETH.balanceOf(user.address);
expect(aTokensBalance).to.be.gt(zero, 'User should have aTokens.');
// Approve the aTokens to Gateway so Gateway can withdraw and convert to Ether
const approveTx = await aWETH
.connect(user.signer)
.approve(wethGateway.address, MAX_UINT_AMOUNT);
const {gasUsed: approveGas} = await waitForTx(approveTx);
// Full withdraw
const {gasUsed: withdrawGas} = await waitForTx(
await wethGateway.connect(user.signer).withdrawETH(MAX_UINT_AMOUNT)
);
const afterFullEtherBalance = await user.signer.getBalance();
const afterFullATokensBalance = await aWETH.balanceOf(user.address);
const gasCosts = approveGas.add(withdrawGas).mul(approveTx.gasPrice);
expect(afterFullEtherBalance).to.be.eq(
priorEthersBalance.add(aTokensBalance).sub(gasCosts),
'User ETHER balance should contain the full withdraw'
);
expect(afterFullATokensBalance).to.be.eq(0, 'User aWETH balance should be zero');
});
it('Borrow stable WETH and Full Repay with ETH', async () => {
const {users, wethGateway, aWETH, weth, pool, helpersContract} = testEnv;
const borrowSize = parseEther('1');
const repaySize = borrowSize.add(borrowSize.mul(5).div(100));
const user = users[1];
const {stableDebtTokenAddress} = await helpersContract.getReserveTokensAddresses(weth.address);
const stableDebtToken = await getStableDebtToken(stableDebtTokenAddress);
// Deposit with native ETH
await wethGateway.connect(user.signer).depositETH(user.address, '0', {value: depositSize});
const aTokensBalance = await aWETH.balanceOf(user.address);
expect(aTokensBalance).to.be.gt(zero);
expect(aTokensBalance).to.be.gte(depositSize);
// Borrow WETH with WETH as collateral
await waitForTx(
await pool.connect(user.signer).borrow(weth.address, borrowSize, '1', '0', user.address)
);
const debtBalance = await stableDebtToken.balanceOf(user.address);
expect(debtBalance).to.be.gt(zero);
// Full Repay WETH with native ETH
await waitForTx(
await wethGateway
.connect(user.signer)
.repayETH(MAX_UINT_AMOUNT, '1', user.address, {value: repaySize})
);
const debtBalanceAfterRepay = await stableDebtToken.balanceOf(user.address);
expect(debtBalanceAfterRepay).to.be.eq(zero);
});
it('Borrow variable WETH and Full Repay with ETH', async () => {
const {users, wethGateway, aWETH, weth, pool, helpersContract} = testEnv;
const borrowSize = parseEther('1');
const repaySize = borrowSize.add(borrowSize.mul(5).div(100));
const user = users[1];
const {variableDebtTokenAddress} = await helpersContract.getReserveTokensAddresses(
weth.address
);
const varDebtToken = await getVariableDebtToken(variableDebtTokenAddress);
// Deposit with native ETH
await wethGateway.connect(user.signer).depositETH(user.address, '0', {value: depositSize});
const aTokensBalance = await aWETH.balanceOf(user.address);
expect(aTokensBalance).to.be.gt(zero);
expect(aTokensBalance).to.be.gte(depositSize);
// Borrow WETH with WETH as collateral
await waitForTx(
await pool.connect(user.signer).borrow(weth.address, borrowSize, '2', '0', user.address)
);
const debtBalance = await varDebtToken.balanceOf(user.address);
expect(debtBalance).to.be.gt(zero);
// Partial Repay WETH loan with native ETH
const partialPayment = repaySize.div(2);
await waitForTx(
await wethGateway
.connect(user.signer)
.repayETH(partialPayment, '2', user.address, {value: partialPayment})
);
const debtBalanceAfterPartialRepay = await varDebtToken.balanceOf(user.address);
expect(debtBalanceAfterPartialRepay).to.be.lt(debtBalance);
// Full Repay WETH loan with native ETH
await waitForTx(
await wethGateway
.connect(user.signer)
.repayETH(MAX_UINT_AMOUNT, '2', user.address, {value: repaySize})
);
const debtBalanceAfterFullRepay = await varDebtToken.balanceOf(user.address);
expect(debtBalanceAfterFullRepay).to.be.eq(zero);
});
});