Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding variables to readme for ease of use, change bundle executor to use a flashloan #5

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/node_modules
/build
/tsconfig.tsbuildinfo
/hardhat
/.vscode
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
simple-arbitrage
simple-arbitrage-FL-Variant
================
This repository contains a simple, mechanical system for discovering, evaluating, rating, and submitting arbitrage opportunities to the Flashbots bundle endpoint. This script is very unlikely to be profitable, as many users have access to it, and it is targeting well-known Ethereum opportunities.
This repository is a fork of the Flashbots simple arbitrage bot. It builds on top of their work by adding a flashloan capability, which lets anyone try to capture arbitrage opportunities regardless of how much ETH they have.

We hope you will use this repository as an example of how to integrate Flashbots into your own Flashbot searcher (bot). For more information, see the [Flashbots Searcher FAQ](https://hackmd.io/@flashbots/rk-qzgzCD)
*Note that the added gas cost of using a flashloan will make it difficult to capture arbitrage opportunities you might find.* Also be mindful that several people are currently running this bot, and it is unlikely to be profitable for you to do so.

Lastly, this fork is still using Flashbot's old API authentication, which is likely to be deprecated soon and which I think you can no longer get keys for. Searchers may need to upgrade to the new authentication method, and they can refer to the main repository for a reference.

Environment Variables
=====================
- ETHEREUM_RPC_URL - Ethereum RPC endpoint. Can not be the same as FLASHBOTS_RPC_URL
- PRIVATE_KEY - Private key for the Ethereum EOA that will be submitting Flashbots Ethereum transactions
- BUNDLE_EXECUTOR_ADDRESS - The address for the BundledExecutor.sol that you have deployed (see "Usage" below for details).
- FLASHBOTS_KEY_ID / FLASHBOTS_SECRET - Flashbots submissions requires an API key. [Apply for an API key here](https://docs.google.com/forms/d/e/1FAIpQLSd4AKrS-vcfW1X-dQvkFY73HysoKfkhcd-31Tj8frDAU6D6aQ/viewform)
- HEALTHCHECK_URL _[Optional]_ - Health check URL, hit only after successfully submitting a bundle.
- MINER_REWARD_PERCENTAGE _[Optional, default 80]_ - 0 -> 100, what percentage of overall profitability to send to miner.
Expand All @@ -22,9 +25,10 @@ _It is important to keep both the bot wallet private key and bundleExecutor owne

```
$ npm install
$ PRIVATE_KEY=__PRIVATE_KEY_FROM_ABOVE__ \
$ ETHEREUM_RPC_URL=__ETHEREUM_RPC_URL_FROM_ABOVE__ \
PRIVATE_KEY=__PRIVATE_KEY_FROM_ABOVE__ \
BUNDLE_EXECUTOR_ADDRESS=__DEPLOYED_ADDRESS_FROM_ABOVE__ \
FLASHBOTS_KEY_ID=__YOUR_PERSONAL_KEY_ID__ \
FLASHBOTS_SECRET=__YOUR_PERSONAL_SECRET__ \
npm run start
npm run start
```
2 changes: 1 addition & 1 deletion contracts/BundleExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ contract FlashBotsMultiCall {
require(_success);
return _result;
}
}
}
125 changes: 125 additions & 0 deletions contracts/BundleExecutorFL.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;

import "./FlashLoanReceiverBase.sol";
import "./Interfaces.sol";
import "./Libraries.sol";

interface IWETH is IERC20 {
function deposit() external payable;
function withdraw(uint) external;
}

contract FlashBotsMultiCallFL is FlashLoanReceiverBase {
using SafeMath for uint256;
address private immutable owner;
address private immutable executor;
IWETH private constant WETH = IWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
address private constant ETH_address = address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);

modifier onlyExecutor() {
require(msg.sender == executor);
_;
}

modifier onlyOwner() {
require(msg.sender == owner);
_;
}

constructor(address _executor, ILendingPoolAddressesProvider _addressProvider) FlashLoanReceiverBase(_addressProvider) public payable {
owner = msg.sender;
executor = _executor;
if (msg.value > 0) {
WETH.deposit{value: msg.value}();
}
}

/**
This function is called after your contract has received the flash loaned amount
*/
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
)
external
override
returns (bool)
{
uint amountOwing = amounts[0].add(premiums[0]);
uniswapWethFLParams(amounts[0], params, amountOwing);
WETH.approve(address(LENDING_POOL), amountOwing);

return true;
}

function flashloan(address borrowedTokenAddress, uint256 amountToBorrow, bytes memory _params) external onlyExecutor() {
address receiverAddress = address(this);

address[] memory assets = new address[](1);
assets[0] = borrowedTokenAddress;

uint256[] memory amounts = new uint256[](1);
amounts[0] = amountToBorrow;

uint256[] memory modes = new uint256[](1);
modes[0] = 0;

address onBehalfOf = address(this);
uint16 referralCode = 161;

LENDING_POOL.flashLoan(
receiverAddress,
assets,
amounts,
modes,
onBehalfOf,
_params,
referralCode
);
}

function uniswapWethFLParams(uint256 _amountToFirstMarket, bytes memory _params, uint256 totalAaveDebt) internal {
(uint256 _ethAmountToCoinbase, address[] memory _targets, bytes[] memory _payloads) = abi.decode(_params, (uint256, address[], bytes[]));
require(_targets.length == _payloads.length);

WETH.transfer(_targets[0], _amountToFirstMarket);
for (uint256 i = 0; i < _targets.length; i++) {
(bool _success, bytes memory _response) = _targets[i].call(_payloads[i]);
require(_success);
}

uint256 _wethBalanceAfter = WETH.balanceOf(address(this));
require(_wethBalanceAfter > totalAaveDebt + _ethAmountToCoinbase);

uint256 _ethBalance = address(this).balance;
if (_ethBalance < _ethAmountToCoinbase) {
WETH.withdraw(_ethAmountToCoinbase - _ethBalance);
}
block.coinbase.transfer(_ethAmountToCoinbase);
}

function call(address payable _to, uint256 _value, bytes calldata _data) external onlyOwner payable returns (bytes memory) {
require(_to != address(0));
(bool _success, bytes memory _result) = _to.call{value: _value}(_data);
require(_success);
return _result;
}

function withdraw(address token) external onlyOwner {
if (token == ETH_address) {
uint256 bal = address(this).balance;
msg.sender.transfer(bal);
} else if (token != ETH_address) {
uint256 bal = IERC20(token).balanceOf(address(this));
IERC20(token).transfer(address(msg.sender), bal);
}
}

receive() external payable {
}
}
18 changes: 18 additions & 0 deletions contracts/FlashLoanReceiverBase.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.12;

import "./Interfaces.sol";
import "./Libraries.sol";

abstract contract FlashLoanReceiverBase is IFlashLoanReceiver {
using SafeERC20 for IERC20;
using SafeMath for uint256;

ILendingPoolAddressesProvider public immutable ADDRESSES_PROVIDER;
ILendingPool public immutable LENDING_POOL;

constructor(ILendingPoolAddressesProvider provider) public {
ADDRESSES_PROVIDER = provider;
LENDING_POOL = ILendingPool(provider.getLendingPool());
}
}
Loading