- Added batch of modes to flashLoan()

This commit is contained in:
eboado 2020-10-29 15:14:28 +01:00
parent c4753bec66
commit 913a6a9237
5 changed files with 36 additions and 28 deletions

View File

@ -106,7 +106,7 @@ interface ILendingPool {
**/ **/
event FlashLoan( event FlashLoan(
address indexed target, address indexed target,
uint256 mode, uint256[] modes,
address[] assets, address[] assets,
uint256[] amounts, uint256[] amounts,
uint256[] premiums, uint256[] premiums,
@ -286,7 +286,7 @@ interface ILendingPool {
* @param receiver The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface. * @param receiver The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface.
* @param assets the address of the principal reserve * @param assets the address of the principal reserve
* @param amounts the amount requested for this flashloan * @param amounts the amount requested for this flashloan
* @param mode the flashloan mode * @param modes the flashloan mode
* @param params a bytes array to be sent to the flashloan executor * @param params a bytes array to be sent to the flashloan executor
* @param referralCode the referral code of the caller * @param referralCode the referral code of the caller
**/ **/
@ -294,7 +294,7 @@ interface ILendingPool {
address receiver, address receiver,
address[] calldata assets, address[] calldata assets,
uint256[] calldata amounts, uint256[] calldata amounts,
uint256 mode, uint256[] calldata modes,
bytes calldata params, bytes calldata params,
uint16 referralCode uint16 referralCode
) external; ) external;

View File

@ -502,7 +502,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
* @param receiverAddress The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface. * @param receiverAddress The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface.
* @param assets The addresss of the assets being flashborrowed * @param assets The addresss of the assets being flashborrowed
* @param amounts The amounts requested for this flashloan for each asset * @param amounts The amounts requested for this flashloan for each asset
* @param mode Type of the debt to open if the flash loan is not returned. 0 -> Don't open any debt, just revert, 1 -> stable, 2 -> variable * @param modes Types of the debt to open if the flash loan is not returned. 0 -> Don't open any debt, just revert, 1 -> stable, 2 -> variable
* @param params Variadic packed params to pass to the receiver as extra information * @param params Variadic packed params to pass to the receiver as extra information
* @param referralCode Referral code of the flash loan * @param referralCode Referral code of the flash loan
**/ **/
@ -510,7 +510,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
address receiverAddress, address receiverAddress,
address[] calldata assets, address[] calldata assets,
uint256[] calldata amounts, uint256[] calldata amounts,
uint256 mode, uint256[] calldata modes,
bytes calldata params, bytes calldata params,
uint16 referralCode uint16 referralCode
) external override { ) external override {
@ -518,13 +518,12 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
FlashLoanLocalVars memory vars; FlashLoanLocalVars memory vars;
ValidationLogic.validateFlashloan(assets, amounts, mode); ValidationLogic.validateFlashloan(assets, amounts, modes);
address[] memory aTokenAddresses = new address[](assets.length); address[] memory aTokenAddresses = new address[](assets.length);
uint256[] memory premiums = new uint256[](assets.length); uint256[] memory premiums = new uint256[](assets.length);
vars.receiver = IFlashLoanReceiver(receiverAddress); vars.receiver = IFlashLoanReceiver(receiverAddress);
vars.debtMode = ReserveLogic.InterestRateMode(mode);
for (vars.i = 0; vars.i < assets.length; vars.i++) { for (vars.i = 0; vars.i < assets.length; vars.i++) {
aTokenAddresses[vars.i] = _reserves[assets[vars.i]].aTokenAddress; aTokenAddresses[vars.i] = _reserves[assets[vars.i]].aTokenAddress;
@ -546,7 +545,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
vars.currentAmount = amounts[vars.i]; vars.currentAmount = amounts[vars.i];
vars.currentPremium = premiums[vars.i]; vars.currentPremium = premiums[vars.i];
vars.currentATokenAddress = aTokenAddresses[vars.i]; vars.currentATokenAddress = aTokenAddresses[vars.i];
vars.debtMode = ReserveLogic.InterestRateMode(modes[vars.i]);
vars.currentAmountPlusPremium = vars.currentAmount.add(vars.currentPremium); vars.currentAmountPlusPremium = vars.currentAmount.add(vars.currentPremium);
if (vars.debtMode == ReserveLogic.InterestRateMode.NONE) { if (vars.debtMode == ReserveLogic.InterestRateMode.NONE) {
@ -576,14 +575,14 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
msg.sender, msg.sender,
msg.sender, msg.sender,
vars.currentAmount, vars.currentAmount,
mode, modes[vars.i],
vars.currentATokenAddress, vars.currentATokenAddress,
referralCode, referralCode,
false false
) )
); );
} }
emit FlashLoan(receiverAddress, mode, assets, amounts, premiums, referralCode); emit FlashLoan(receiverAddress, modes, assets, amounts, premiums, referralCode);
} }
} }

View File

@ -326,17 +326,26 @@ library ValidationLogic {
/** /**
* @dev validates a flashloan action * @dev validates a flashloan action
* @param mode the flashloan mode (0 = classic flashloan, 1 = open a stable rate loan, 2 = open a variable rate loan) * @param modes the flashloan modes (0 = classic flashloan, 1 = open a stable rate loan, 2 = open a variable rate loan)
* @param assets the assets being flashborrowed * @param assets the assets being flashborrowed
* @param amounts the amounts for each asset being borrowed * @param amounts the amounts for each asset being borrowed
**/ **/
function validateFlashloan( function validateFlashloan(
address[] memory assets, address[] memory assets,
uint256[] memory amounts, uint256[] memory amounts,
uint256 mode uint256[] memory modes
) internal pure { ) internal pure {
require(mode <= uint256(ReserveLogic.InterestRateMode.VARIABLE), Errors.INVALID_FLASHLOAN_MODE); require(
require(assets.length == amounts.length, Errors.INCONSISTENT_FLASHLOAN_PARAMS); assets.length == amounts.length && assets.length == modes.length,
Errors.INCONSISTENT_FLASHLOAN_PARAMS
);
for (uint256 i = 0; i < modes.length; i++) {
require(
modes[i] <= uint256(ReserveLogic.InterestRateMode.VARIABLE),
Errors.INVALID_FLASHLOAN_MODE
);
}
} }
/** /**

View File

@ -47,7 +47,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
[weth.address], [weth.address],
[ethers.utils.parseEther('0.8')], [ethers.utils.parseEther('0.8')],
0, [0],
'0x10', '0x10',
'0' '0'
); );
@ -76,7 +76,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
[weth.address], [weth.address],
['1000720000000000000'], ['1000720000000000000'],
0, [0],
'0x10', '0x10',
'0' '0'
); );
@ -107,7 +107,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
[weth.address], [weth.address],
[ethers.utils.parseEther('0.8')], [ethers.utils.parseEther('0.8')],
0, [0],
'0x10', '0x10',
'0' '0'
) )
@ -127,7 +127,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
[weth.address], [weth.address],
[ethers.utils.parseEther('0.8')], [ethers.utils.parseEther('0.8')],
0, [0],
'0x10', '0x10',
'0' '0'
) )
@ -147,7 +147,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
[weth.address], [weth.address],
[ethers.utils.parseEther('0.8')], [ethers.utils.parseEther('0.8')],
4, [4],
'0x10', '0x10',
'0' '0'
) )
@ -175,7 +175,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
[weth.address], [weth.address],
[ethers.utils.parseEther('0.8')], [ethers.utils.parseEther('0.8')],
2, [2],
'0x10', '0x10',
'0' '0'
); );
@ -201,7 +201,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
[weth.address], [weth.address],
['1004415000000000000'], //slightly higher than the available liquidity ['1004415000000000000'], //slightly higher than the available liquidity
2, [2],
'0x10', '0x10',
'0' '0'
), ),
@ -213,7 +213,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
const {pool, deployer, weth} = testEnv; const {pool, deployer, weth} = testEnv;
await expect( await expect(
pool.flashLoan(deployer.address, [weth.address], ['1000000000000000000'], 2, '0x10', '0') pool.flashLoan(deployer.address, [weth.address], ['1000000000000000000'], [2], '0x10', '0')
).to.be.reverted; ).to.be.reverted;
}); });
@ -241,7 +241,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
[usdc.address], [usdc.address],
[flashloanAmount], [flashloanAmount],
0, [0],
'0x10', '0x10',
'0' '0'
); );
@ -283,7 +283,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
[usdc.address], [usdc.address],
[flashloanAmount], [flashloanAmount],
2, [2],
'0x10', '0x10',
'0' '0'
) )
@ -309,7 +309,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
await pool await pool
.connect(caller.signer) .connect(caller.signer)
.flashLoan(_mockFlashLoanReceiver.address, [usdc.address], [flashloanAmount], 2, '0x10', '0'); .flashLoan(_mockFlashLoanReceiver.address, [usdc.address], [flashloanAmount], [2], '0x10', '0');
const {variableDebtTokenAddress} = await helpersContract.getReserveTokensAddresses( const {variableDebtTokenAddress} = await helpersContract.getReserveTokensAddresses(
usdc.address usdc.address
); );
@ -344,7 +344,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
await expect( await expect(
pool pool
.connect(caller.signer) .connect(caller.signer)
.flashLoan(_mockFlashLoanReceiver.address, [weth.address], [flashAmount], 0, '0x10', '0') .flashLoan(_mockFlashLoanReceiver.address, [weth.address], [flashAmount], [0], '0x10', '0')
).to.be.revertedWith(SAFEERC20_LOWLEVEL_CALL); ).to.be.revertedWith(SAFEERC20_LOWLEVEL_CALL);
}); });
@ -359,7 +359,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
await pool await pool
.connect(caller.signer) .connect(caller.signer)
.flashLoan(_mockFlashLoanReceiver.address, [weth.address], [flashAmount], 1, '0x10', '0'); .flashLoan(_mockFlashLoanReceiver.address, [weth.address], [flashAmount], [1], '0x10', '0');
const {stableDebtTokenAddress} = await helpersContract.getReserveTokensAddresses(weth.address); const {stableDebtTokenAddress} = await helpersContract.getReserveTokensAddresses(weth.address);

View File

@ -187,7 +187,7 @@ makeSuite('Pausable Pool', (testEnv: TestEnv) => {
await expect( await expect(
pool pool
.connect(caller.signer) .connect(caller.signer)
.flashLoan(_mockFlashLoanReceiver.address, [weth.address], [flashAmount], 1, '0x10', '0') .flashLoan(_mockFlashLoanReceiver.address, [weth.address], [flashAmount], [1], '0x10', '0')
).revertedWith(IS_PAUSED); ).revertedWith(IS_PAUSED);
// Unpause pool // Unpause pool