aave-protocol-v2/test/weth-gateway.spec.ts

210 lines
7.7 KiB
TypeScript
Raw Normal View History

2020-10-29 11:44:21 +00:00
import {MAX_UINT_AMOUNT} from '../helpers/constants';
import {convertToCurrencyDecimals} from '../helpers/contracts-helpers';
import {makeSuite, TestEnv} from './helpers/make-suite';
import {parseEther} from 'ethers/lib/utils';
2020-10-29 14:50:32 +00:00
import {BRE, waitForTx} from '../helpers/misc-utils';
import {BigNumber} from 'ethers';
import {getStableDebtToken, getVariableDebtToken} from '../helpers/contracts-getters';
2020-11-03 12:51:10 +00:00
import {WethGateway} from '../types/WethGateway';
2020-10-29 11:44:21 +00:00
2020-10-29 14:50:32 +00:00
const {expect} = require('chai');
2020-10-29 11:44:21 +00:00
makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => {
2020-10-29 14:50:32 +00:00
const zero = BigNumber.from('0');
2020-10-29 11:44:21 +00:00
const depositSize = parseEther('5');
it('Deposit WETH', async () => {
const {users, wethGateway, aWETH, pool} = testEnv;
2020-10-29 11:44:21 +00:00
const user = users[1];
// Deposit with native ETH
await wethGateway.connect(user.signer).depositETH(user.address, '0', {value: depositSize});
2020-10-29 11:44:21 +00:00
const aTokensBalance = await aWETH.balanceOf(user.address);
2020-10-29 14:50:32 +00:00
expect(aTokensBalance).to.be.gt(zero);
expect(aTokensBalance).to.be.gte(depositSize);
2020-10-29 11:44:21 +00:00
});
2020-10-29 14:50:32 +00:00
it('Withdraw WETH - Partial', async () => {
const {users, wethGateway, aWETH, pool} = testEnv;
2020-10-29 11:44:21 +00:00
const user = users[1];
const priorEthersBalance = await user.signer.getBalance();
const aTokensBalance = await aWETH.balanceOf(user.address);
2020-10-29 14:50:32 +00:00
expect(aTokensBalance).to.be.gt(zero, 'User should have aTokens.');
2020-10-29 11:44:21 +00:00
// Partially withdraw native ETH
2020-10-29 14:50:32 +00:00
const partialWithdraw = await convertToCurrencyDecimals(aWETH.address, '2');
// 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, user.address)
2020-10-29 14:50:32 +00:00
);
2020-10-29 11:44:21 +00:00
const afterPartialEtherBalance = await user.signer.getBalance();
2020-10-29 14:50:32 +00:00
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),
2020-10-29 11:44:21 +00:00
'User ETHER balance should contain the partial withdraw'
);
2020-10-29 14:50:32 +00:00
expect(afterPartialATokensBalance).to.be.equal(
aTokensBalance.sub(partialWithdraw),
'User aWETH balance should be substracted'
);
});
it('Withdraw WETH - Full', async () => {
const {users, aWETH, wethGateway, pool} = testEnv;
2020-10-29 14:50:32 +00:00
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);
2020-10-29 11:44:21 +00:00
// Full withdraw
2020-10-29 14:50:32 +00:00
const {gasUsed: withdrawGas} = await waitForTx(
await wethGateway.connect(user.signer).withdrawETH(MAX_UINT_AMOUNT, user.address)
2020-10-29 14:50:32 +00:00
);
2020-10-29 11:44:21 +00:00
const afterFullEtherBalance = await user.signer.getBalance();
2020-10-29 14:50:32 +00:00
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),
2020-10-29 11:44:21 +00:00
'User ETHER balance should contain the full withdraw'
);
2020-10-29 14:50:32 +00:00
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});
2020-10-29 14:50:32 +00:00
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})
2020-10-29 14:50:32 +00:00
);
const debtBalanceAfterRepay = await stableDebtToken.balanceOf(user.address);
expect(debtBalanceAfterRepay).to.be.eq(zero);
2020-10-29 11:44:21 +00:00
});
2020-10-29 14:50:32 +00:00
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});
2020-10-29 14:50:32 +00:00
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})
2020-10-29 14:50:32 +00:00
);
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})
2020-10-29 14:50:32 +00:00
);
const debtBalanceAfterFullRepay = await varDebtToken.balanceOf(user.address);
expect(debtBalanceAfterFullRepay).to.be.eq(zero);
});
2020-11-03 13:06:10 +00:00
it('Should revert if receive or fallback functions receives Ether', async () => {
const {users, wethGateway} = testEnv;
const user = users[0];
const amount = parseEther('1');
2020-11-03 13:06:10 +00:00
// Call receiver function (empty data + value)
await expect(
user.signer.sendTransaction({to: wethGateway.address, value: amount})
).to.be.revertedWith('Receive not allowed');
2020-11-03 12:51:10 +00:00
const fakeABI = ['function wantToCallFallback()'];
const abiCoder = new BRE.ethers.utils.Interface(fakeABI);
const fakeMethodEncoded = abiCoder.encodeFunctionData('wantToCallFallback', []);
2020-11-03 13:06:10 +00:00
// Call fallback function with value
2020-11-03 12:51:10 +00:00
await expect(
user.signer.sendTransaction({to: wethGateway.address, data: fakeMethodEncoded, value: amount})
2020-11-03 13:06:10 +00:00
).to.be.reverted;
// Call fallback function without value
2020-11-03 12:51:10 +00:00
await expect(user.signer.sendTransaction({to: wethGateway.address, data: fakeMethodEncoded})).to
.be.reverted;
});
2020-10-29 11:44:21 +00:00
});