ERC3156FlashBorrowerMockUpgradeable.sol 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../token/ERC20/IERC20Upgradeable.sol";
  4. import "../interfaces/IERC3156Upgradeable.sol";
  5. import "../utils/AddressUpgradeable.sol";
  6. import "../proxy/utils/Initializable.sol";
  7. /**
  8. * @dev WARNING: this IERC3156FlashBorrower mock implementation is for testing purposes ONLY.
  9. * Writing a secure flash lock borrower is not an easy task, and should be done with the utmost care.
  10. * This is not an example of how it should be done, and no pattern present in this mock should be considered secure.
  11. * Following best practices, always have your contract properly audited before using them to manipulate important funds on
  12. * live networks.
  13. */
  14. contract ERC3156FlashBorrowerMockUpgradeable is Initializable, IERC3156FlashBorrowerUpgradeable {
  15. bytes32 internal constant _RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");
  16. bool _enableApprove;
  17. bool _enableReturn;
  18. event BalanceOf(address token, address account, uint256 value);
  19. event TotalSupply(address token, uint256 value);
  20. function __ERC3156FlashBorrowerMock_init(bool enableReturn, bool enableApprove) internal onlyInitializing {
  21. __ERC3156FlashBorrowerMock_init_unchained(enableReturn, enableApprove);
  22. }
  23. function __ERC3156FlashBorrowerMock_init_unchained(bool enableReturn, bool enableApprove) internal onlyInitializing {
  24. _enableApprove = enableApprove;
  25. _enableReturn = enableReturn;
  26. }
  27. function onFlashLoan(
  28. address, /*initiator*/
  29. address token,
  30. uint256 amount,
  31. uint256 fee,
  32. bytes calldata data
  33. ) public override returns (bytes32) {
  34. require(msg.sender == token);
  35. emit BalanceOf(token, address(this), IERC20Upgradeable(token).balanceOf(address(this)));
  36. emit TotalSupply(token, IERC20Upgradeable(token).totalSupply());
  37. if (data.length > 0) {
  38. // WARNING: This code is for testing purposes only! Do not use.
  39. AddressUpgradeable.functionCall(token, data);
  40. }
  41. if (_enableApprove) {
  42. IERC20Upgradeable(token).approve(token, amount + fee);
  43. }
  44. return _enableReturn ? _RETURN_VALUE : bytes32(0);
  45. }
  46. /**
  47. * @dev This empty reserved space is put in place to allow future versions to add new
  48. * variables without shifting down storage in the inheritance chain.
  49. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  50. */
  51. uint256[50] private __gap;
  52. }