Skip to content

Commit

Permalink
ToB improvement: emit event when Flashloan taken (#731)
Browse files Browse the repository at this point in the history
* ToB improvement: emit event when Flashloan taken

* Changes after review - add comment

* Set optimizers run to 100
  • Loading branch information
grandizzy committed Apr 11, 2023
1 parent a375bf7 commit eed9f63
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ build :; forge clean && forge build

# Tests
test :; forge test --no-match-test "testLoad|invariant|test_regression" # --ffi # enable if you need the `ffi` cheat code on HEVM
test-with-gas-report :; FOUNDRY_PROFILE=optimized forge test --no-match-test "testLoad|invariant|test_regression" --gas-report # --ffi # enable if you need the `ffi` cheat code on HEVM
test-load :; FOUNDRY_PROFILE=optimized forge test --match-test testLoad --gas-report
test-with-gas-report :; forge test --no-match-test "testLoad|invariant|test_regression" --gas-report # --ffi # enable if you need the `ffi` cheat code on HEVM
test-load :; forge test --match-test testLoad --gas-report
test-invariant :; eval QUOTE_PRECISION=${QUOTE_PRECISION} COLLATERAL_PRECISION=${COLLATERAL_PRECISION} forge t --mt invariant --nmc RegressionTest
test-invariant-erc20 :; eval QUOTE_PRECISION=${QUOTE_PRECISION} COLLATERAL_PRECISION=${COLLATERAL_PRECISION} forge t --mt invariant --nmc RegressionTest --mc ERC20
test-invariant-erc721 :; eval QUOTE_PRECISION=${QUOTE_PRECISION} forge t --mt invariant --nmc RegressionTest --mc ERC721
Expand Down
1 change: 1 addition & 0 deletions brownie-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ compiler:
version: 0.8.14
optimizer:
enabled: true
runs: 100
remappings:
- "@ds-math=lib/ds-math/src/"
- "@openzeppelin/contracts=lib/openzeppelin-contracts/contracts"
Expand Down
6 changes: 2 additions & 4 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ block_timestamp = 1_672_372_127
block_number = 16_295_000
fork_block_number = 16_295_000
rpc_storage_caching = { chains = ["mainnet"], endpoints = "all" }

[profile.optimized]
optimizer = true
optimizer_runs = 200
optimizer = true
optimizer_runs = 100

[fuzz]
runs = 300
Expand Down
4 changes: 3 additions & 1 deletion src/base/FlashloanablePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract contract FlashloanablePool is Pool {
* @notice Called by flashloan borrowers to borrow liquidity which must be repaid in the same transaction.
* @param receiver_ Address of the contract which implements the appropriate interface to receive tokens.
* @param token_ Address of the ERC20 token caller wants to borrow.
* @param amount_ The amount of tokens to borrow.
* @param amount_ The denormalized amount (dependent upon token precision) of tokens to borrow.
* @param data_ User-defined calldata passed to the receiver.
* @return success_ True if flashloan was successful.
*/
Expand Down Expand Up @@ -54,6 +54,8 @@ abstract contract FlashloanablePool is Pool {
if (tokenContract.balanceOf(address(this)) != initialBalance) revert FlashloanIncorrectBalance();

success_ = true;

emit Flashloan(address(receiver_), token_, amount_);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/interfaces/pool/commons/IPoolEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ interface IPoolEvents {
uint256 lpForfeited
);

/**
* @notice Emitted when a flashloan is taken from pool.
* @param receiver The address receiving the flashloan.
* @param token The address of token flashloaned from pool.
* @param amount The amount of tokens flashloaned from pool.
*/
event Flashloan(
address indexed receiver,
address indexed token,
uint256 amount
);

/**
* @notice Emitted when a loan Neutral Price is restamped.
* @param borrower Identifies the loan to update the Neutral Price.
Expand Down
11 changes: 10 additions & 1 deletion tests/forge/unit/ERC20Pool/ERC20PoolFlashloan.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity 0.8.14;

import '@openzeppelin/contracts/token/ERC20/ERC20.sol';

import { ERC20HelperContract } from './ERC20DSTestPlus.sol';
import { ERC20HelperContract } from './ERC20DSTestPlus.sol';
import {
FlashloanBorrower,
SomeDefiStrategy,
Expand Down Expand Up @@ -79,6 +79,9 @@ contract ERC20PoolFlashloanTest is ERC20HelperContract {
// Use a flashloan to interact with the strategy
assertEq(_collateral.balanceOf(address(flasher)), 0);
assertTrue(!flasher.callbackInvoked());

vm.expectEmit(true, true, false, true);
emit Flashloan(address(flasher), address(_collateral), loanAmount);
_pool.flashLoan(flasher, address(_collateral), loanAmount, new bytes(0));
assertTrue(flasher.callbackInvoked());
assertEq(_collateral.balanceOf(address(flasher)), 3.5 * 1e18);
Expand Down Expand Up @@ -228,6 +231,9 @@ contract ERC20PoolFlashloanPrecisionTest is ERC20HelperContract {
// Use a flashloan to interact with the strategy
assertEq(WBTC.balanceOf(address(flasher)), 0);
assertTrue(!flasher.callbackInvoked());

vm.expectEmit(true, true, false, true);
emit Flashloan(address(flasher), address(WBTC), loanAmount);
_pool.flashLoan(flasher, address(WBTC), loanAmount, new bytes(0));
assertTrue(flasher.callbackInvoked());
assertEq(WBTC.balanceOf(address(flasher)), 0.35 * 1e8);
Expand Down Expand Up @@ -258,6 +264,9 @@ contract ERC20PoolFlashloanPrecisionTest is ERC20HelperContract {
// Use a flashloan to interact with the strategy
assertEq(USDC.balanceOf(address(flasher)), 0);
assertTrue(!flasher.callbackInvoked());

vm.expectEmit(true, true, false, true);
emit Flashloan(address(flasher), address(USDC), loanAmount);
_pool.flashLoan(flasher, address(USDC), loanAmount, new bytes(0));
assertTrue(flasher.callbackInvoked());
assertEq(USDC.balanceOf(address(flasher)), 350 * 1e6);
Expand Down
3 changes: 3 additions & 0 deletions tests/forge/unit/ERC721Pool/ERC721PoolFlashloan.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ contract ERC721PoolFlashloanTest is ERC721HelperContract {
// Use a flashloan to interact with the strategy
assertEq(_quote.balanceOf(address(flasher)), 0);
assertTrue(!flasher.callbackInvoked());

vm.expectEmit(true, true, false, true);
emit Flashloan(address(flasher), address(_quote), loanAmount);
_pool.flashLoan(flasher, address(_quote), loanAmount, new bytes(0));
assertTrue(flasher.callbackInvoked());
assertEq(_quote.balanceOf(address(flasher)), 3.5 * 1e18);
Expand Down

0 comments on commit eed9f63

Please sign in to comment.