-
-
Save anthowen/58e3ba8508a5cd129f13d4474af482f9 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')
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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