mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
Added WETHGateway tests
This commit is contained in:
parent
744befc46c
commit
a9e7fa3516
|
@ -11,7 +11,6 @@ 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;
|
||||||
|
@ -41,15 +40,10 @@ contract WETHGateway is IWETHGateway {
|
||||||
* @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(address onBehalfOf, uint16 referralCode) external override payable {
|
||||||
console.log('hi');
|
|
||||||
console.log(msg.value);
|
|
||||||
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool());
|
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool());
|
||||||
require(address(pool) != address(0));
|
require(address(pool) != address(0));
|
||||||
console.log('deposit');
|
|
||||||
WETH.deposit{value: msg.value}();
|
WETH.deposit{value: msg.value}();
|
||||||
console.log('weth in');
|
|
||||||
pool.deposit(address(WETH), msg.value, onBehalfOf, referralCode);
|
pool.deposit(address(WETH), msg.value, onBehalfOf, referralCode);
|
||||||
console.log('deposited');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,9 +63,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(
|
||||||
WETH.transferFrom(msg.sender, address(this), amountToWithdraw);
|
msg.sender,
|
||||||
pool.withdraw(address(WETH), amount);
|
address(this),
|
||||||
|
amountToWithdraw
|
||||||
|
);
|
||||||
|
pool.withdraw(address(WETH), amountToWithdraw);
|
||||||
WETH.withdraw(amountToWithdraw);
|
WETH.withdraw(amountToWithdraw);
|
||||||
safeTransferETH(msg.sender, amountToWithdraw);
|
safeTransferETH(msg.sender, amountToWithdraw);
|
||||||
}
|
}
|
||||||
|
|
|
@ -277,4 +277,4 @@
|
||||||
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
|
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
13970
package-lock.json
generated
13970
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -72,7 +72,7 @@
|
||||||
"chai-bignumber": "3.0.0",
|
"chai-bignumber": "3.0.0",
|
||||||
"chai-bn": "^0.2.1",
|
"chai-bn": "^0.2.1",
|
||||||
"eth-sig-util": "2.5.3",
|
"eth-sig-util": "2.5.3",
|
||||||
"ethereum-waffle": "3.0.2",
|
"ethereum-waffle": "^3.1.2",
|
||||||
"ethereumjs-util": "7.0.2",
|
"ethereumjs-util": "7.0.2",
|
||||||
"ethers": "5.0.8",
|
"ethers": "5.0.8",
|
||||||
"globby": "^11.0.1",
|
"globby": "^11.0.1",
|
||||||
|
|
|
@ -31,8 +31,11 @@ import {getEthersSigners} from '../../helpers/contracts-helpers';
|
||||||
import {Weth9} from '../../types/Weth9';
|
import {Weth9} from '../../types/Weth9';
|
||||||
import {Weth9Mocked} from '../../types/Weth9Mocked';
|
import {Weth9Mocked} from '../../types/Weth9Mocked';
|
||||||
import {WethGateway} from '../../types/WethGateway';
|
import {WethGateway} from '../../types/WethGateway';
|
||||||
|
import {solidity} from 'ethereum-waffle';
|
||||||
|
|
||||||
chai.use(bignumberChai());
|
chai.use(bignumberChai());
|
||||||
chai.use(almostEqual());
|
chai.use(almostEqual());
|
||||||
|
chai.use(solidity);
|
||||||
|
|
||||||
export interface SignerWithAddress {
|
export interface SignerWithAddress {
|
||||||
signer: Signer;
|
signer: Signer;
|
||||||
|
|
|
@ -1,20 +1,19 @@
|
||||||
import BigNumber from 'bignumber.js';
|
|
||||||
import {MAX_UINT_AMOUNT} from '../helpers/constants';
|
import {MAX_UINT_AMOUNT} from '../helpers/constants';
|
||||||
import {convertToCurrencyDecimals} from '../helpers/contracts-helpers';
|
import {convertToCurrencyDecimals} from '../helpers/contracts-helpers';
|
||||||
import {deposit} from './helpers/actions';
|
|
||||||
import {makeSuite, TestEnv} from './helpers/make-suite';
|
import {makeSuite, TestEnv} from './helpers/make-suite';
|
||||||
import {parseEther} from 'ethers/lib/utils';
|
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} = require('chai');
|
||||||
const {expect} = chai;
|
|
||||||
|
|
||||||
makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => {
|
makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) => {
|
||||||
const zero = new BigNumber('0');
|
const zero = BigNumber.from('0');
|
||||||
const depositSize = parseEther('5');
|
const depositSize = parseEther('5');
|
||||||
|
|
||||||
console.log(depositSize.toString());
|
|
||||||
it('Deposit WETH', async () => {
|
it('Deposit WETH', async () => {
|
||||||
const {users, weth, wethGateway, aWETH} = testEnv;
|
const {users, wethGateway, aWETH} = testEnv;
|
||||||
|
|
||||||
const user = users[1];
|
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);
|
const aTokensBalance = await aWETH.balanceOf(user.address);
|
||||||
|
|
||||||
expect(aTokensBalance.toString()).to.be.bignumber.gt(zero.toString());
|
expect(aTokensBalance).to.be.gt(zero);
|
||||||
expect(aTokensBalance.toString()).to.be.bignumber.gte(depositSize.toString());
|
expect(aTokensBalance).to.be.gte(depositSize);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Withdraw WETH', async () => {
|
it('Withdraw WETH - Partial', async () => {
|
||||||
const {users, weth, wethGateway, aWETH} = testEnv;
|
const {users, wethGateway, aWETH} = testEnv;
|
||||||
|
|
||||||
const user = users[1];
|
const user = users[1];
|
||||||
const priorEthersBalance = await user.signer.getBalance();
|
const priorEthersBalance = await user.signer.getBalance();
|
||||||
const aTokensBalance = await aWETH.balanceOf(user.address);
|
const aTokensBalance = await aWETH.balanceOf(user.address);
|
||||||
|
|
||||||
expect(aTokensBalance.toString()).to.be.bignumber.gt(
|
expect(aTokensBalance).to.be.gt(zero, 'User should have aTokens.');
|
||||||
zero.toString(),
|
|
||||||
'User should have aTokens.'
|
|
||||||
);
|
|
||||||
expect(aTokensBalance.toString()).to.be.bignumber.gte(
|
|
||||||
depositSize.toString(),
|
|
||||||
'User should have the deposited aTokens.'
|
|
||||||
);
|
|
||||||
|
|
||||||
// Partially withdraw native ETH
|
// Partially withdraw native ETH
|
||||||
console.log('prior partial');
|
const partialWithdraw = await convertToCurrencyDecimals(aWETH.address, '2');
|
||||||
const partialWithdraw = await convertToCurrencyDecimals(weth.address, '2');
|
|
||||||
await wethGateway.connect(user.signer).withdrawETH(partialWithdraw.toString());
|
|
||||||
console.log('after partial');
|
|
||||||
|
|
||||||
const afterPartialEtherBalance = await user.signer.getBalance();
|
// Approve the aTokens to Gateway so Gateway can withdraw and convert to Ether
|
||||||
expect(afterPartialEtherBalance.toString()).to.be.bignumber.eq(
|
const approveTx = await aWETH
|
||||||
priorEthersBalance.add(partialWithdraw).toString(),
|
.connect(user.signer)
|
||||||
'User ETHER balance should contain the partial withdraw'
|
.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');
|
const afterPartialEtherBalance = await user.signer.getBalance();
|
||||||
// Full withdraw
|
const afterPartialATokensBalance = await aWETH.balanceOf(user.address);
|
||||||
await wethGateway.connect(user.signer).withdrawETH(MAX_UINT_AMOUNT);
|
const gasCosts = approveGas.add(withdrawGas).mul(approveTx.gasPrice);
|
||||||
const afterFullEtherBalance = await user.signer.getBalance();
|
|
||||||
expect(afterFullEtherBalance.toString()).to.be.bignumber.gte(
|
expect(afterPartialEtherBalance).to.be.equal(
|
||||||
afterPartialEtherBalance.add('3').toString(),
|
priorEthersBalance.add(partialWithdraw).sub(gasCosts),
|
||||||
'User ETHER balance should contain the full withdraw'
|
'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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user