All Projects → paco0x → amm-arbitrageur

paco0x / amm-arbitrageur

Licence: WTFPL license
An arbitrage bot between Uniswap AMMs

Programming Languages

solidity
1140 projects
typescript
32286 projects

Projects that are alternatives of or similar to amm-arbitrageur

rockfish
Rockfish is an arbitrage bot for the Stellar Decentralized Exchange (SDEX)
Stars: ✭ 58 (-94.74%)
Mutual labels:  arbitrage, arbitrage-bot
uniswap-sushiswap-arbitrage-bot
Two bots written in JS that uses flashswaps and normal swaps to arbitrage Uniswap. Includes an automated demostration.
Stars: ✭ 351 (-68.15%)
Mutual labels:  arbitrage, arbitrage-bot
api-version-2
Executium API Version 2 - A comprehensive trading system API which connects traders with dozens of exchanges. Currently in closed beta
Stars: ✭ 82 (-92.56%)
Mutual labels:  arbitrage, arbitrage-bot
arbitrage-bot
An arbitrage bot that detects arbitrage opportunities between pancake and bakery swaps and manages flash swap calls, to the flash swap contract
Stars: ✭ 132 (-88.02%)
Mutual labels:  arbitrage, arbitrage-bot
MultiDexArbBot
This is an arbitrage bot that uses existing price aggregators such as 1inch, Paraswap, dex.ag, matcha and more to get the best exchange rates across different decentralized exchanges on different blockchains and ecosystems.
Stars: ✭ 67 (-93.92%)
Mutual labels:  arbitrage
poe-currency-flip-planner
This tool is an attempt at planning short-term arbitrage deals of currency in Path of Exile.
Stars: ✭ 95 (-91.38%)
Mutual labels:  arbitrage
Simple-Game-ERC-721-Token-Template
🔮 Very Simple ERC-721 Smart Contract Template to create your own ERC-721 Tokens on the Ethereum Blockchain, with many customizable Options 🔮
Stars: ✭ 83 (-92.47%)
Mutual labels:  ethereum-contract
bitrader
Bitcoin Arbitrage utilities
Stars: ✭ 45 (-95.92%)
Mutual labels:  arbitrage
bsc-genesis-contract
The genesis contracts of BNB Smart Chain.
Stars: ✭ 321 (-70.87%)
Mutual labels:  bsc
botdexdamar
🤖 multichain trading bot with sniper, frontrun, backrun, sandwich
Stars: ✭ 124 (-88.75%)
Mutual labels:  bsc
ehr-blockchain
Electronic Health Record (EHR) and Electronic Medical Record (EMR) systems. However, they still face some issues regarding the security of medical records, user ownership of data, data integrity etc. The solution to these issues could be the use of a novel technology, i.e., Blockchain. This technology offers to provide a secure, temper-proof pl…
Stars: ✭ 41 (-96.28%)
Mutual labels:  ethereum-contract
uniswap-arbitrage-flash-swap
Uniswap flash swap arbitrage solidity contracts
Stars: ✭ 341 (-69.06%)
Mutual labels:  bsc
bsc-archive-snapshot
Free public Binance Smart Chain (BSC) Archive Snapshot
Stars: ✭ 71 (-93.56%)
Mutual labels:  bsc
Poolz-Back
smart contracts using solidity for erc20 tokens to eth and erc20 to erc20
Stars: ✭ 15 (-98.64%)
Mutual labels:  ethereum-contract
angle-core
Smart contracts for the Core Module and the governance of the Angle Protocol
Stars: ✭ 56 (-94.92%)
Mutual labels:  ethereum-contract
solidity-contracts
📦 Resources for the Ethereum Smart Contract Development tutorial series.
Stars: ✭ 64 (-94.19%)
Mutual labels:  ethereum-contract
PancakeSwapPredictionBot
PancakeSwap V2 Prediction Bot With Machine Learning | Automated Strategy, Auto Betting, Auto Claim
Stars: ✭ 21 (-98.09%)
Mutual labels:  bsc
FWI
RTM
Stars: ✭ 30 (-97.28%)
Mutual labels:  bsc
roq-samples
How to use the Roq C++20 API for Live Cryptocurrency Algorithmic and High-Frequency Trading as well as for Back-Testing and Historical Simulation
Stars: ✭ 119 (-89.2%)
Mutual labels:  arbitrage
eth-self-token-example
在本地网络发布一个名叫 YuTou 的 token
Stars: ✭ 8 (-99.27%)
Mutual labels:  ethereum-contract

Note: This repo is not under maintenning. If you encounter problems when running. Please figure it out by yourself.

This repo is written only for POC. The bot is implemented simply for demostration. It's not robust enough to be competitve with other arb bots that are running in the network. If you want a production-ready arb bot, then you might need to implement a bot by yourself.

If you have any question, please read the issues first. You may find your answer there.

AMM Arbitrageur

An arbitrageur contract can be used to argitrage between Uniswap V2 like AMMs. For Chinese: 中文说明

The rationale

There are a lot of AMMs on Ethereum and other blockchains that support EVM. Many of these AMMs are UniswapV2 fork project or have same interface with Uniswap V2. A list of these AMMs:

  • Uniswap V2(Ethereum)
  • Sushi Swap(Ethereum)
  • Pancake Swap(BSC)
  • MDEX(BSC/heco) ...

We can do arbitrage between these AMMs once the prices of same token pair diverges on different AMMs. We can encapsulate arbitrage transactions in one EVM transaction so that we won't lose any money even the price moves before we send transaction.

Suppose we'd like to do arbitrage transactions on token pair TokenX/WETH. The TokenX/WETH pair must exists on multiple AMMs on Ethereum(or other EVM compatible blockchains such as BSC).

  • We call WETH as Base token. It can be any token that with actual value such as USDT/USDC/DAI/BUSD...
  • We call TokenX as Quote token. It can be any token even it's a fake token without value. Quote tokens won't be reserved after arbitrage's done.
  • After arbitrage, only the base tokens are reserved. So our profit is denominate in base token.
  • If two tokens in a pair can both be considered as base token. Either one can be reserved after arbitrage.

The arbitrage can be done by using flashswap of Uniswap V2:

  • Suppose pair0 and pair1 are two pairs of same two tokens on different AMMs. Once the price diverges, we can do arbitrage.
  • We call the FlashBot contract to start arbitrage
  • The contract calculates the price denominated in quote token. Suppose the price of quote token in Pair0 is lower:
  1. By using flash sawp, the contract first borrow some quote tokens from Pair0, the amount is x. The contract need to repay the debt to Pair0. The deby can be denominated in base token. This is a functionality of Uniswap V2.
  2. Sell all the borrowed quote tokens on Pair1. The contract get base tokens of amount y2.
  3. Repay the debt to Pair0 in base token of amount y1.
  4. The contract get profit of y2 - y1.

The point of the process is to calculate how much of the amount x so we can get as much profit as possible.

Supoose the initial state of Pair0 and Pair1 are as follows:

Pair0 Pair1
Base Token amount a1 a2
Quote Token amount b1 b2

So we get:

The amount borrowed Quote Token are some, so Delta b1 = Delta b2, let x = \Delta b, then the profit as a function of x is:

We wanna calculate the x value when the function get a maximum value. First we need to get the derivative of function:

When the derivative function is 0, the function has a maximum/minimum value, and we can set some conditions to ignore the solution at the minimum. It is possible to solve

Let:

The previous equation is reduced to a general quadratic equation:

We can get the solution:

The solution x is the amount we need to borrow from Pair0.

Deploy the contract

  1. Edit network config in hardhat.config.ts.(Currently it is BSC in the repo, you alse can use Ethereum mainnet)

  2. Copy the secret sample config:

$ cp .secret.ts.sample .secret.ts
  1. Edit the private and address field in above config.

  2. Then run the script to deploy. By default, it deploys to BSC. If you wanna dpeloy to other network, you may need to change the network settings in hardhat.config.ts. You also need to change the WETH or other token address in the deploy.ts, it's WBNB address by default.

$ hardhart --network XXX run scripts/deploy.ts

For example,

$ npx hardhat --network bscTestnet run scripts/deploy.ts

Bot implementation

The contract has a function getProfit(address pool1, address pool2), which can be used to calculate the maximum profit between two pairs(denominated in base token).

The bot need to call getProfit() to get the possible profit between token pairs. Once it is profitable, bot calls flashArbitrage(pool1, pool2) to do the arbitrage. The profit will leaves in the contract address.

Contract owner can call withdraw() to withdraw the profit.

There already implemented a bot in typescript, to run it:

$ yarn run bot

Available AMMs on BSC

Run UT

$ hardhat test

FAQ

Too much math, what the hell is this contract for?

To be simple, it moves the prices between different AMMs to the same level. You'll get profit by doing that. This contract helps you to get the maximum profit. And it uses flashswap so you only need little money(just some gas fees) to run it.

How do I know the correctness of the contract?

By default, the tests use a forking network of the BSC mainnet(thanks to the powerful hardhat tool). The tests in SwapTest.ts demonstrate that the contract works correctly for arbitrage. You can check it by yourself.

But I didn't make any profit by running your bot

Well, there are too many bots running in the wild, especially in ETH and BSC. The bot code in this repo is too simple to be competitive. You can't expect just running my code and earning a bunch of money. You need to find out some strategies to make your own money.

How can I change the token pairs the bot is monitoring?

At the first time, the bot uses bscBaseTokens, bscQuoteTokens, and bscDexes in tokens.ts to automatically get all possible token pairs and saves them into bsc-pairs.json. So it can reuse the json file next time the bot launches.

If you want some other pairs. You can delete the bsc-pairs.json and edit the three variables above. Rerun the bot so it uses the new pairs. You can check the new pairs in bsc-pairs.json.

Any suggestions to be competitive?

  • Lower the network latency, you can use your own node.
  • Higher gas price, make sure your transaction gets handled quickly enough to take the profit. This is just like a competition between bots if they find a profitable chance at the same time.
  • Monitoring lesser tokens, the more you're monitoring, the lesser frequency the bot is looping. You can run multiple bots to monitor separate token pairs.
  • Go to some other networks such as FTM/Matic/..., there may be lesser bots running on them.
  • Do some other works such as liquidation, arbitrage in balancer/curve/0x... You need to implement these features by yourself.

Why terminal stucks when runnging "npx hardhat compile"?

If you are running hardhat behind a proxy, maybe you will encounter the error like HH502: Couldn't download compiler versions list. Please check your connection when running npx hardhat compile. In order to go through this error, you need to set HTTP_PROXY or HTTPS_PROXY in your terminal. According to this issue, the hardhat version 2.4.0 and the later version has supported HTTP_PROXY or HTTPS_PROXY. So you need to change the hardhat version from 2.1.2 to 2.4.0 or later in package.json.

Error occurs when running "npx hardhat run --network xxx bot/index.ts"

The detailed error is TSError: x Unable to compile TypeScript. bot/index.ts:63:13 - error TS2571: Object is of type 'unknown'. Please make run your TypeScript version is ^4.2.4. You may meet this error if your TypeScipt version is above 4.4.x.

Error "cannot estimate gas; transaction may fail or may require manual gas limit"

If you encounter this error, please don't be surprised. First of all, this bot hasn't tested well in bscTestnet. So if you want to run it well you would like better deploy it in bscMainnet. Secondly, since there are a lot of token pairs are deprecated so you need to modify the pairs-bsc.json.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].