Skip to content

Instantly share code, notes, and snippets.

@z0r0z
Created December 27, 2021 00:02
Show Gist options
  • Save z0r0z/93b61442ba0835ed02efa6c753b610e7 to your computer and use it in GitHub Desktop.
Save z0r0z/93b61442ba0835ed02efa6c753b610e7 to your computer and use it in GitHub Desktop.
test flash borrowing on flashpot - just send a fee's worth of ETH to this address then call flashBorrow (using 0 address for 'address')
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.4;
import "https://github.com/Rari-Capital/solmate/blob/audit-fixes/src/utils/SafeTransferLib.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/interfaces/IERC3156FlashLender.sol";
contract FlashPotBorrow is IERC3156FlashBorrower {
using SafeTransferLib for address;
enum Action {NORMAL, OTHER}
IERC3156FlashLender lender;
constructor (
IERC3156FlashLender lender_
) {
lender = lender_;
}
/// @dev ERC-3156 Flash loan callback
function onFlashLoan(
address initiator,
address,
uint256 amount,
uint256 fee,
bytes calldata data
) public override returns(bytes32) {
uint256 _repayment = amount + fee;
require(
msg.sender == address(lender),
"FlashBorrower: Untrusted lender"
);
require(
initiator == address(this),
"FlashBorrower: Untrusted loan initiator"
);
(Action action) = abi.decode(data, (Action));
if (action == Action.NORMAL) {
address(lender).safeTransferETH(_repayment);
} else if (action == Action.OTHER) {
// do another
}
return keccak256("ERC3156FlashBorrower.onFlashLoan");
}
/// @dev Initiate a flash loan
function flashBorrow(
address,
uint256 amount
) public {
bytes memory data = abi.encode(Action.NORMAL);
lender.flashLoan(IERC3156FlashBorrower(this), address(0), amount, data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment