Skip to content

Commit

Permalink
fix: Make flashloan premium to protocol a fraction of total premium
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Jan 6, 2022
1 parent 6fe3b30 commit d24c962
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 68 deletions.
4 changes: 2 additions & 2 deletions contracts/protocol/libraries/logic/FlashLoanLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ library FlashLoanLogic {
) {
vars.currentATokenAddress = vars.aTokenAddresses[vars.i];
vars.currentAmountPlusPremium = vars.currentAmount + vars.totalPremiums[vars.i];
vars.currentPremiumToProtocol = vars.currentAmount.percentMul(
vars.currentPremiumToProtocol = vars.totalPremiums[vars.i].percentMul(
vars.flashloanPremiumToProtocol
);
vars.currentPremiumToLP = vars.totalPremiums[vars.i] - vars.currentPremiumToProtocol;
Expand Down Expand Up @@ -251,7 +251,7 @@ library FlashLoanLogic {
Errors.INVALID_FLASHLOAN_EXECUTOR_RETURN
);

vars.premiumToProtocol = params.amount.percentMul(params.flashLoanPremiumToProtocol);
vars.premiumToProtocol = vars.totalPremium.percentMul(params.flashLoanPremiumToProtocol);
vars.premiumToLP = vars.totalPremium - vars.premiumToProtocol;

DataTypes.ReserveCache memory reserveCache = reserve.cache();
Expand Down
2 changes: 0 additions & 2 deletions contracts/protocol/pool/PoolConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ contract PoolConfigurator is VersionedInitializable, IPoolConfigurator {
Errors.FLASHLOAN_PREMIUM_INVALID
);
uint256 premiumToProtocol = _pool.FLASHLOAN_PREMIUM_TO_PROTOCOL();
require(newFlashloanPremiumTotal >= premiumToProtocol, Errors.FLASHLOAN_PREMIUMS_MISMATCH);
uint256 oldFlashloanPremiumTotal = _pool.FLASHLOAN_PREMIUM_TOTAL();
_pool.updateFlashloanPremiums(newFlashloanPremiumTotal, premiumToProtocol);
emit FlashloanPremiumTotalUpdated(oldFlashloanPremiumTotal, newFlashloanPremiumTotal);
Expand All @@ -418,7 +417,6 @@ contract PoolConfigurator is VersionedInitializable, IPoolConfigurator {
Errors.FLASHLOAN_PREMIUM_INVALID
);
uint256 premiumTotal = _pool.FLASHLOAN_PREMIUM_TOTAL();
require(newFlashloanPremiumToProtocol <= premiumTotal, Errors.FLASHLOAN_PREMIUMS_MISMATCH);
uint256 oldFlashloanPremiumToProtocol = _pool.FLASHLOAN_PREMIUM_TO_PROTOCOL();
_pool.updateFlashloanPremiums(premiumTotal, newFlashloanPremiumToProtocol);
emit FlashloanPremiumToProtocolUpdated(
Expand Down
33 changes: 0 additions & 33 deletions test-suites/configurator-edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,30 +118,6 @@ makeSuite('PoolConfigurator: Edge cases', (testEnv: TestEnv) => {
);
});

it('Tries to update flashloan premium total < FLASHLOAN_PREMIUM_TO_PROTOCOL (revert expected)', async () => {
const { pool, configurator } = testEnv;

const oldFlashloanPremiumTotal = await pool.FLASHLOAN_PREMIUM_TOTAL();
const oldFlashloanPremiumToProtocol = await pool.FLASHLOAN_PREMIUM_TO_PROTOCOL();

const newPremiumToProtocol = 40;
const newPremiumTotal = 100;
const wrongPremiumTotal = 39;

// Update FLASHLOAN_PREMIUM_TO_PROTOCOL to non-zero
expect(await configurator.updateFlashloanPremiumTotal(newPremiumTotal))
.to.emit(configurator, 'FlashloanPremiumTotalUpdated')
.withArgs(oldFlashloanPremiumTotal, newPremiumTotal);

expect(await configurator.updateFlashloanPremiumToProtocol(newPremiumToProtocol))
.to.emit(configurator, 'FlashloanPremiumToProtocolUpdated')
.withArgs(oldFlashloanPremiumToProtocol, newPremiumToProtocol);

await expect(configurator.updateFlashloanPremiumTotal(wrongPremiumTotal)).to.be.revertedWith(
FLASHLOAN_PREMIUMS_MISMATCH
);
});

it('Tries to update flashloan premium to protocol > PERCENTAGE_FACTOR (revert expected)', async () => {
const { configurator } = testEnv;

Expand All @@ -151,15 +127,6 @@ makeSuite('PoolConfigurator: Edge cases', (testEnv: TestEnv) => {
).to.be.revertedWith(FLASHLOAN_PREMIUM_INVALID);
});

it('Tries to update flashloan premium to protocol > FLASHLOAN_PREMIUM_TOTAL (revert expected)', async () => {
const { configurator } = testEnv;

const newPremiumToProtocol = 101;
await expect(
configurator.updateFlashloanPremiumToProtocol(newPremiumToProtocol)
).to.be.revertedWith(FLASHLOAN_PREMIUMS_MISMATCH);
});

it('Tries to update borrowCap > MAX_BORROW_CAP (revert expected)', async () => {
const { configurator, weth } = testEnv;
await expect(
Expand Down
23 changes: 11 additions & 12 deletions test-suites/pool-flashloan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {
} = ProtocolErrors;

const TOTAL_PREMIUM = 9;
const PREMIUM_TO_PROTOCOL = 3;
const PREMIUM_TO_LP = TOTAL_PREMIUM - PREMIUM_TO_PROTOCOL;
const PREMIUM_TO_PROTOCOL = 3000;

before(async () => {
_mockFlashLoanReceiver = await getMockFlashLoanReceiver();
});

it('Configurator sets total premium = 9 bps, premium to protocol = 3 bps', async () => {
it('Configurator sets total premium = 9 bps, premium to protocol = 30%', async () => {
const { configurator, pool } = testEnv;
await configurator.updateFlashloanPremiumTotal(TOTAL_PREMIUM);
await configurator.updateFlashloanPremiumToProtocol(PREMIUM_TO_PROTOCOL);
Expand Down Expand Up @@ -67,11 +66,11 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {
const wethFlashBorrowedAmount = ethers.utils.parseEther('0.8');
const daiFlashBorrowedAmount = ethers.utils.parseEther('0.3');
const wethTotalFees = wethFlashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const wethFeesToProtocol = wethFlashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const wethFeesToLp = wethFlashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const wethFeesToProtocol = wethTotalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const wethFeesToLp = wethTotalFees.sub(wethFeesToProtocol);
const daiTotalFees = daiFlashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const daiFeesToProtocol = daiFlashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const daiFeesToLp = daiFlashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const daiFeesToProtocol = daiTotalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const daiFeesToLp = daiTotalFees.sub(daiFeesToProtocol);

const wethLiquidityIndexAdded = wethFeesToLp
.mul(BigNumber.from(10).pow(27))
Expand Down Expand Up @@ -205,8 +204,8 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {
const flashBorrowedAmount = totalLiquidityBefore;

const totalFees = flashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const feesToProtocol = flashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = flashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const feesToProtocol = totalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = totalFees.sub(feesToProtocol);
const liquidityIndexBefore = reserveData.liquidityIndex;
const liquidityIndexAdded = feesToLp
.mul(BigNumber.from(10).pow(27))
Expand Down Expand Up @@ -241,7 +240,7 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {
expect(currentLiquidityIndex).to.be.equal(liquidityIndexBefore.add(liquidityIndexAdded));
expect(
reservesAfter.sub(feesToProtocol).mul(liquidityIndexBefore).div(currentLiquidityIndex)
).to.be.equal(reservesBefore);
).to.be.closeTo(reservesBefore, 2);
});
it('Takes WETH flashloan, does not return the funds with mode = 0 (revert expected)', async () => {
const { pool, weth, users } = testEnv;
Expand Down Expand Up @@ -412,8 +411,8 @@ makeSuite('Pool: FlashLoan', (testEnv: TestEnv) => {

const flashBorrowedAmount = await convertToCurrencyDecimals(usdc.address, '500');
const totalFees = flashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const feesToProtocol = flashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = flashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const feesToProtocol = totalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = totalFees.sub(feesToProtocol);
const liquidityIndexAdded = feesToLp
.mul(ethers.BigNumber.from(10).pow(27))
.div(await aUsdc.totalSupply());
Expand Down
19 changes: 9 additions & 10 deletions test-suites/pool-normal-flashloan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ makeSuite('Pool: FlashLoan for gas comparison', (testEnv: TestEnv) => {
ProtocolErrors;

const TOTAL_PREMIUM = 9;
const PREMIUM_TO_PROTOCOL = 3;
const PREMIUM_TO_LP = TOTAL_PREMIUM - PREMIUM_TO_PROTOCOL;
const PREMIUM_TO_PROTOCOL = 3000;

before(async () => {
_mockFlashLoanReceiver = await getMockFlashLoanReceiver();
});

it('Configurator sets total premium = 9 bps, premium to protocol = 3 bps', async () => {
it('Configurator sets total premium = 9 bps, premium to protocol = 30%', async () => {
const { configurator, pool } = testEnv;
await configurator.updateFlashloanPremiumTotal(TOTAL_PREMIUM);
await configurator.updateFlashloanPremiumToProtocol(PREMIUM_TO_PROTOCOL);
Expand Down Expand Up @@ -62,8 +61,8 @@ makeSuite('Pool: FlashLoan for gas comparison', (testEnv: TestEnv) => {

const wethFlashBorrowedAmount = ethers.utils.parseEther('0.8');
const wethTotalFees = wethFlashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const wethFeesToProtocol = wethFlashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const wethFeesToLp = wethFlashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const wethFeesToProtocol = wethTotalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const wethFeesToLp = wethTotalFees.sub(wethFeesToProtocol);

const wethLiquidityIndexAdded = wethFeesToLp
.mul(BigNumber.from(10).pow(27))
Expand Down Expand Up @@ -116,8 +115,8 @@ makeSuite('Pool: FlashLoan for gas comparison', (testEnv: TestEnv) => {
const flashBorrowedAmount = totalLiquidityBefore;

const totalFees = flashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const feesToProtocol = flashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = flashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const feesToProtocol = totalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = totalFees.sub(feesToProtocol);
const liquidityIndexBefore = reserveData.liquidityIndex;
const liquidityIndexAdded = feesToLp
.mul(BigNumber.from(10).pow(27))
Expand Down Expand Up @@ -152,7 +151,7 @@ makeSuite('Pool: FlashLoan for gas comparison', (testEnv: TestEnv) => {
expect(currentLiquidityIndex).to.be.equal(liquidityIndexBefore.add(liquidityIndexAdded));
expect(
reservesAfter.sub(feesToProtocol).mul(liquidityIndexBefore).div(currentLiquidityIndex)
).to.be.equal(reservesBefore);
).to.be.closeTo(reservesBefore, 2);
});
it('Takes WETH flashloan, does not return the funds with mode = 0 (revert expected)', async () => {
const { pool, weth, users } = testEnv;
Expand Down Expand Up @@ -250,8 +249,8 @@ makeSuite('Pool: FlashLoan for gas comparison', (testEnv: TestEnv) => {

const flashBorrowedAmount = await convertToCurrencyDecimals(usdc.address, '500');
const totalFees = flashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const feesToProtocol = flashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = flashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const feesToProtocol = totalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = totalFees.sub(feesToProtocol);
const liquidityIndexAdded = feesToLp
.mul(ethers.BigNumber.from(10).pow(27))
.div(await aUsdc.totalSupply());
Expand Down
17 changes: 8 additions & 9 deletions test-suites/pool-simple-flashloan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ makeSuite('Pool: Simple FlashLoan', (testEnv: TestEnv) => {
const { ERC20_TRANSFER_AMOUNT_EXCEEDS_BALANCE, INVALID_FLASHLOAN_EXECUTOR_RETURN } =
ProtocolErrors;
const TOTAL_PREMIUM = 9;
const PREMIUM_TO_PROTOCOL = 3;
const PREMIUM_TO_LP = TOTAL_PREMIUM - PREMIUM_TO_PROTOCOL;
const PREMIUM_TO_PROTOCOL = 3000;

before(async () => {
const { addressesProvider, deployer } = testEnv;
Expand All @@ -32,7 +31,7 @@ makeSuite('Pool: Simple FlashLoan', (testEnv: TestEnv) => {
).deploy(addressesProvider.address);
});

it('Configurator sets total premium = 9 bps, premium to protocol = 3 bps', async () => {
it('Configurator sets total premium = 9 bps, premium to protocol = 30%', async () => {
const { configurator, pool } = testEnv;
await configurator.updateFlashloanPremiumTotal(TOTAL_PREMIUM);
await configurator.updateFlashloanPremiumToProtocol(PREMIUM_TO_PROTOCOL);
Expand Down Expand Up @@ -69,8 +68,8 @@ makeSuite('Pool: Simple FlashLoan', (testEnv: TestEnv) => {

const wethFlashBorrowedAmount = ethers.utils.parseEther('0.8');
const wethTotalFees = wethFlashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const wethFeesToProtocol = wethFlashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const wethFeesToLp = wethFlashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const wethFeesToProtocol = wethTotalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const wethFeesToLp = wethTotalFees.sub(wethFeesToProtocol);

const wethLiquidityIndexAdded = wethFeesToLp
.mul(BigNumber.from(10).pow(27))
Expand Down Expand Up @@ -142,8 +141,8 @@ makeSuite('Pool: Simple FlashLoan', (testEnv: TestEnv) => {
const flashBorrowedAmount = totalLiquidityBefore;

const totalFees = flashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const feesToProtocol = flashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = flashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const feesToProtocol = totalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = totalFees.sub(feesToProtocol);
const liquidityIndexBefore = reserveData.liquidityIndex;
const liquidityIndexAdded = feesToLp
.mul(BigNumber.from(10).pow(27))
Expand Down Expand Up @@ -261,8 +260,8 @@ makeSuite('Pool: Simple FlashLoan', (testEnv: TestEnv) => {

const flashBorrowedAmount = await convertToCurrencyDecimals(usdc.address, '500');
const totalFees = flashBorrowedAmount.mul(TOTAL_PREMIUM).div(10000);
const feesToProtocol = flashBorrowedAmount.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = flashBorrowedAmount.mul(PREMIUM_TO_LP).div(10000);
const feesToProtocol = totalFees.mul(PREMIUM_TO_PROTOCOL).div(10000);
const feesToLp = totalFees.sub(feesToProtocol);
const liquidityIndexAdded = feesToLp
.mul(ethers.BigNumber.from(10).pow(27))
.div(await aUsdc.totalSupply());
Expand Down

0 comments on commit d24c962

Please sign in to comment.