Skip to content

Commit 7b2a284

Browse files
authored
fix: communicate correct premium in case of debt-bearing flashloan (#822)
* pick: cherry-pick fl fix from #820 * Update test-suites/pool-flashloan.spec.ts * Update contracts/protocol/libraries/logic/FlashLoanLogic.sol
1 parent 29ff9b9 commit 7b2a284

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

contracts/protocol/libraries/logic/FlashLoanLogic.sol

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ library FlashLoanLogic {
9191

9292
for (vars.i = 0; vars.i < params.assets.length; vars.i++) {
9393
vars.currentAmount = params.amounts[vars.i];
94-
vars.totalPremiums[vars.i] = vars.currentAmount.percentMul(vars.flashloanPremiumTotal);
94+
vars.totalPremiums[vars.i] = DataTypes.InterestRateMode(params.interestRateModes[vars.i]) ==
95+
DataTypes.InterestRateMode.NONE
96+
? vars.currentAmount.percentMul(vars.flashloanPremiumTotal)
97+
: 0;
9598
IAToken(reservesData[params.assets[vars.i]].aTokenAddress).transferUnderlyingTo(
9699
params.receiverAddress,
97100
vars.currentAmount

test-suites/pool-flashloan.spec.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {
176176
}
177177
});
178178

179-
it('Takes an authorized AAVE flash loan with mode = 0, returns the funds correctly', async () => {
179+
it('Takes an authorized AAVE flash loan with mode = 0, returns the funds correctly, premium should be 0', async () => {
180180
const {
181181
pool,
182182
helpersContract,
@@ -194,17 +194,21 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {
194194

195195
const totalLiquidityBefore = reserveData.totalAToken;
196196

197-
await pool
198-
.connect(authorizedUser.signer)
199-
.flashLoan(
200-
_mockFlashLoanReceiver.address,
201-
[aave.address],
202-
[flashBorrowedAmount],
203-
[0],
204-
_mockFlashLoanReceiver.address,
205-
'0x10',
206-
'0'
207-
);
197+
await expect(
198+
pool
199+
.connect(authorizedUser.signer)
200+
.flashLoan(
201+
_mockFlashLoanReceiver.address,
202+
[aave.address],
203+
[flashBorrowedAmount],
204+
[0],
205+
_mockFlashLoanReceiver.address,
206+
'0x10',
207+
'0'
208+
)
209+
)
210+
.to.emit(_mockFlashLoanReceiver, 'ExecutedWithSuccess')
211+
.withArgs([aave.address], [flashBorrowedAmount], [0]);
208212

209213
await pool.mintToTreasury([aave.address]);
210214

@@ -570,7 +574,7 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {
570574
).to.be.revertedWith(COLLATERAL_BALANCE_IS_ZERO);
571575
});
572576

573-
it('Caller deposits 5 WETH as collateral, Takes a USDC flashloan with mode = 2, does not return the funds. A loan for caller is created', async () => {
577+
it('Caller deposits 5 WETH as collateral, Takes a USDC flashloan with mode = 2, does not return the funds. A loan for caller is created, premium should be 0', async () => {
574578
const { usdc, pool, weth, users, helpersContract } = testEnv;
575579

576580
const caller = users[2];
@@ -585,21 +589,26 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {
585589

586590
await pool.connect(caller.signer).deposit(weth.address, amountToDeposit, caller.address, '0');
587591

588-
await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
589-
590592
const flashloanAmount = await convertToCurrencyDecimals(usdc.address, '500');
591593

592-
await pool
593-
.connect(caller.signer)
594-
.flashLoan(
595-
_mockFlashLoanReceiver.address,
596-
[usdc.address],
597-
[flashloanAmount],
598-
[2],
599-
caller.address,
600-
'0x10',
601-
'0'
602-
);
594+
await _mockFlashLoanReceiver.setFailExecutionTransfer(false);
595+
596+
await expect(
597+
pool
598+
.connect(caller.signer)
599+
.flashLoan(
600+
_mockFlashLoanReceiver.address,
601+
[usdc.address],
602+
[flashloanAmount],
603+
[2],
604+
caller.address,
605+
'0x10',
606+
'0'
607+
)
608+
)
609+
.to.emit(_mockFlashLoanReceiver, 'ExecutedWithSuccess')
610+
.withArgs([usdc.address], [flashloanAmount], [0]);
611+
603612
const { variableDebtTokenAddress } = await helpersContract.getReserveTokensAddresses(
604613
usdc.address
605614
);

0 commit comments

Comments
 (0)