Skip to content

Commit

Permalink
fix: communicate correct premium in case of debt-bearing flashloan (#822
Browse files Browse the repository at this point in the history
)

* pick: cherry-pick fl fix from #820

* Update test-suites/pool-flashloan.spec.ts

* Update contracts/protocol/libraries/logic/FlashLoanLogic.sol
  • Loading branch information
sakulstra authored Mar 14, 2023
1 parent 29ff9b9 commit 7b2a284
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
5 changes: 4 additions & 1 deletion contracts/protocol/libraries/logic/FlashLoanLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ library FlashLoanLogic {

for (vars.i = 0; vars.i < params.assets.length; vars.i++) {
vars.currentAmount = params.amounts[vars.i];
vars.totalPremiums[vars.i] = vars.currentAmount.percentMul(vars.flashloanPremiumTotal);
vars.totalPremiums[vars.i] = DataTypes.InterestRateMode(params.interestRateModes[vars.i]) ==
DataTypes.InterestRateMode.NONE
? vars.currentAmount.percentMul(vars.flashloanPremiumTotal)
: 0;
IAToken(reservesData[params.assets[vars.i]].aTokenAddress).transferUnderlyingTo(
params.receiverAddress,
vars.currentAmount
Expand Down
61 changes: 35 additions & 26 deletions test-suites/pool-flashloan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {
}
});

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

const totalLiquidityBefore = reserveData.totalAToken;

await pool
.connect(authorizedUser.signer)
.flashLoan(
_mockFlashLoanReceiver.address,
[aave.address],
[flashBorrowedAmount],
[0],
_mockFlashLoanReceiver.address,
'0x10',
'0'
);
await expect(
pool
.connect(authorizedUser.signer)
.flashLoan(
_mockFlashLoanReceiver.address,
[aave.address],
[flashBorrowedAmount],
[0],
_mockFlashLoanReceiver.address,
'0x10',
'0'
)
)
.to.emit(_mockFlashLoanReceiver, 'ExecutedWithSuccess')
.withArgs([aave.address], [flashBorrowedAmount], [0]);

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

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

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 () => {
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 () => {
const { usdc, pool, weth, users, helpersContract } = testEnv;

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

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

await _mockFlashLoanReceiver.setFailExecutionTransfer(true);

const flashloanAmount = await convertToCurrencyDecimals(usdc.address, '500');

await pool
.connect(caller.signer)
.flashLoan(
_mockFlashLoanReceiver.address,
[usdc.address],
[flashloanAmount],
[2],
caller.address,
'0x10',
'0'
);
await _mockFlashLoanReceiver.setFailExecutionTransfer(false);

await expect(
pool
.connect(caller.signer)
.flashLoan(
_mockFlashLoanReceiver.address,
[usdc.address],
[flashloanAmount],
[2],
caller.address,
'0x10',
'0'
)
)
.to.emit(_mockFlashLoanReceiver, 'ExecutedWithSuccess')
.withArgs([usdc.address], [flashloanAmount], [0]);

const { variableDebtTokenAddress } = await helpersContract.getReserveTokensAddresses(
usdc.address
);
Expand Down

0 comments on commit 7b2a284

Please sign in to comment.