Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont commuicate wrong flash premium #822

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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