Skip to content

Instantly share code, notes, and snippets.

@darkerego
Last active December 6, 2023 04:11
Show Gist options
  • Save darkerego/014d88154ff1813f8703ee0e344b963e to your computer and use it in GitHub Desktop.
Save darkerego/014d88154ff1813f8703ee0e344b963e to your computer and use it in GitHub Desktop.
equalizer flash loan
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
interface IERC20 {
function approve(address spender, uint256 amount) external returns (bool);
}
interface IERC3156FlashBorrower {
function onFlashLoan(
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external returns (bytes32);
}
contract FlashBorrowerExample is IERC3156FlashBorrower {
uint256 private constant MAX_INT = type(uint256).max;
address private flashLoanProvider; // Address of the flash loan provider
// Event for logging the execution result
event ExecutionResult(bool success, bytes data);
// Constructor to set the flash loan provider's address
constructor(address _flashLoanProvider) {
flashLoanProvider = _flashLoanProvider;
}
function onFlashLoan(
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external override returns (bytes32) {
require(msg.sender == flashLoanProvider, "FlashBorrower: Invalid flash loan provider");
// Approve the token for the flash loan fee
require(IERC20(token).approve(msg.sender, amount + fee), "FlashBorrower: Approval failed");
// Decode the call data and execute the intended operation
(address target, bytes memory callData) = abi.decode(data, (address, bytes));
// Execute the call
(bool success, bytes memory returnData) = target.call(callData);
emit ExecutionResult(success, returnData);
// Return success to the lender
return keccak256('ERC3156FlashBorrower.onFlashLoan');
}
// Additional functions and logic as needed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment