Added borrowETH to WETHGateway.

This commit is contained in:
David Racero 2020-11-06 13:22:03 +01:00
parent b5a52c2b98
commit 167bca74ab
3 changed files with 66 additions and 0 deletions

View File

@ -96,6 +96,22 @@ contract WETHGateway is IWETHGateway, Ownable {
if (msg.value > paybackAmount) _safeTransferETH(msg.sender, msg.value - paybackAmount);
}
/**
* @dev borrow WETH, unwraps to ETH and send both the ETH and DebtTokens to msg.sender, via `approveDelegation` and onBehalf argument in `LendingPool.borrow`.
* @param amount the amount of ETH to borrow
* @param interesRateMode the interest rate mode
* @param referralCode integrators are assigned a referral code and can potentially receive rewards
*/
function borrowETH(
uint256 amount,
uint256 interesRateMode,
uint16 referralCode
) external override {
POOL.borrow(address(WETH), amount, interesRateMode, referralCode, msg.sender);
WETH.withdraw(amount);
_safeTransferETH(msg.sender, amount);
}
/**
* @dev transfer ETH to an address, revert if it fails.
* @param to recipient of the transfer

View File

@ -11,4 +11,10 @@ interface IWETHGateway {
uint256 rateMode,
address onBehalfOf
) external payable;
function borrowETH(
uint256 amount,
uint256 interesRateMode,
uint16 referralCode
) external;
}

View File

@ -8,6 +8,8 @@ import {getStableDebtToken, getVariableDebtToken} from '../helpers/contracts-get
import {WethGateway} from '../types/WethGateway';
import {use} from 'chai';
import {deploySelfdestructTransferMock} from '../helpers/contracts-deployments';
import {boolean} from '@nomiclabs/buidler/internal/core/params/argumentTypes';
import {borrow} from './helpers/actions';
const {expect} = require('chai');
@ -185,6 +187,48 @@ makeSuite('Use native ETH at LendingPool via WETHGateway', (testEnv: TestEnv) =>
expect(debtBalanceAfterFullRepay).to.be.eq(zero);
});
it('Borrow ETH via delegateApprove ETH and repays back', async () => {
const {users, wethGateway, aWETH, weth, helpersContract} = testEnv;
const borrowSize = parseEther('1');
const user = users[2];
const {variableDebtTokenAddress} = await helpersContract.getReserveTokensAddresses(
weth.address
);
const varDebtToken = await getVariableDebtToken(variableDebtTokenAddress);
const priorDebtBalance = await varDebtToken.balanceOf(user.address);
expect(priorDebtBalance).to.be.eq(zero);
// Deposit WETH 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);
// Delegates borrowing power of WETH to WETHGateway
await waitForTx(
await varDebtToken.connect(user.signer).approveDelegation(wethGateway.address, borrowSize)
);
// Borrows ETH with WETH as collateral
await waitForTx(await wethGateway.connect(user.signer).borrowETH(borrowSize, '2', '0'));
const debtBalance = await varDebtToken.balanceOf(user.address);
expect(debtBalance).to.be.gt(zero);
// Full Repay WETH loan with native ETH
await waitForTx(
await wethGateway
.connect(user.signer)
.repayETH(MAX_UINT_AMOUNT, '2', user.address, {value: borrowSize.mul(2)})
);
const debtBalanceAfterFullRepay = await varDebtToken.balanceOf(user.address);
expect(debtBalanceAfterFullRepay).to.be.eq(zero);
});
it('Should revert if receiver function receives Ether if not WETH', async () => {
const {users, wethGateway} = testEnv;
const user = users[0];