ERC3156FlashBorrowerMock.sol 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {IERC20} from "../token/ERC20/IERC20.sol";
  4. import {IERC3156FlashBorrower} from "../interfaces/IERC3156.sol";
  5. import {Address} from "../utils/Address.sol";
  6. /**
  7. * @dev WARNING: this IERC3156FlashBorrower mock implementation is for testing purposes ONLY.
  8. * Writing a secure flash lock borrower is not an easy task, and should be done with the utmost care.
  9. * This is not an example of how it should be done, and no pattern present in this mock should be considered secure.
  10. * Following best practices, always have your contract properly audited before using them to manipulate important funds on
  11. * live networks.
  12. */
  13. contract ERC3156FlashBorrowerMock is IERC3156FlashBorrower {
  14. bytes32 internal constant _RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");
  15. bool immutable _enableApprove;
  16. bool immutable _enableReturn;
  17. event BalanceOf(address token, address account, uint256 value);
  18. event TotalSupply(address token, uint256 value);
  19. constructor(bool enableReturn, bool enableApprove) {
  20. _enableApprove = enableApprove;
  21. _enableReturn = enableReturn;
  22. }
  23. function onFlashLoan(
  24. address /*initiator*/,
  25. address token,
  26. uint256 amount,
  27. uint256 fee,
  28. bytes calldata data
  29. ) public returns (bytes32) {
  30. require(msg.sender == token);
  31. emit BalanceOf(token, address(this), IERC20(token).balanceOf(address(this)));
  32. emit TotalSupply(token, IERC20(token).totalSupply());
  33. if (data.length > 0) {
  34. // WARNING: This code is for testing purposes only! Do not use.
  35. Address.functionCall(token, data);
  36. }
  37. if (_enableApprove) {
  38. IERC20(token).approve(token, amount + fee);
  39. }
  40. return _enableReturn ? _RETURN_VALUE : bytes32(0);
  41. }
  42. }