Arbitrage Bot: Sandwich Attack on Uniswap
Concept:
We want to implement the design described in this research paper: https://arxiv.org/abs/2009.14021
Data Pipeline:
- Data collection: we will setup geth nodes (a client for Ethereum) that collect pending transactions from the Ethereum network. If we want to maximize our chances, we have to be the first one to spot the victim’s transaction.
- Data processing: A script will iterate through all the pending transactions and look for those that interact with the Uniswap contract. It will parse the transaction data and use the information there (target token, amount transferred, transaction fee, max slippage, etc.) to analyze if it’s profitable to initiate the attack.
- The attack: Send two txns that sandwich the victim's txn
System Design:
Milestones:
Milestones are 1-2 week increments:
Host a full geth node on an AWS EC2/Azure VM/AWS Managed Blockchain (AMB) and establish a websocket connection to a local script on the VMWrite scripts to interact with a Gas API (there are multiple ones)- We should also ask the bot to get new gas data periodically (30-60 minutes, we will try to tune it,) since gas prices fluctuate.
- Compile a list of token addresses that we want to target. This will be a static list that lives in each VM.
- Google Cloud Platform and AWS have nice query languages and datasets to collect such a static list efficiently. This would be queried once every couple weeks to refresh the static list.
- Write the code that gives us money predicts if this transaction tv will be profitable
- Optional: it helps to read through Uniswap’s smart contract solidity code
- tv has to interact with the Uniswap router and sends ether to an address on that token list
- Decode transaction data under
input
with Uniswap application binary interface - Now with all the data parsed and decoded, develop a strategy (eg. trading logic) and implement the strategy in code
- Write code for Wallet to efficiently submit transactions.
- There is a strategy here detailed in a few of the papers we looked at.
- Set up node deployments so our blockchain clients can be distributed in multiple datacenters.
- Set up a stream into a database from trading logic.
- Write some straightforward analytics on our database.
- If we have time at the end: write a frontend tool that tracks and visualizes all nodes and transactions, etc.
AMB Setup:
Currently, we have a full ethereum node hosted under Amazon Managed Blockchain (AMB). Those .js scripts use Ethereum JSON-RPC API to interact with that node. We will update this README as we go (eg. add more nodes).
You can either run your own node on AMB and use the scripts here to connect to it (the club can reimburse the cost, with a receipt of course), or you can connect to Cornell Blockchain's node. In that case, you have to dm me for access key and other credentials.
- You must have node version manager (nvm) and Node.js installed on your machine. Type
node --version
and verify that you are using Node version 14 or later. If necessary, you can use thenvm install 14
command followed by thenvm use 14
command to install version 14. npm install aws-sdk web3 xhr2
- Setup environment variables
AWS_ACCESS_KEY_ID
,AWS_SECRET_ACCESS_KEY
,AMB_HTTP_ENDPOINT
, andAMB_WS_ENDPOINT
by typingexport AWS_ACCESS_KEY_ID="your_stuff"
- Run
node web3-http.js
. This script prints out data from this txn - Run
node web3-websocket.js
. This scripts listens for pending txn and prints out those that interact with the Uniswap contract