Skip to content

โ›“๐Ÿงฐ๐Ÿ’ฐ๐—ฐ๐—น๐—ถ ๐˜๐—ผ๐—ผ๐—น ๐˜๐—ผ ๐—ด๐—ฒ๐˜ ๐—ฑ๐—ฎ๐˜๐—ฎ ๐—ฎ๐—ป๐—ฑ ๐—ฎ๐—ฟ๐—ฏ๐—ถ๐˜๐—ฟ๐—ฎ๐—ด๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—ด๐—ถ๐˜ƒ๐—ฒ๐—ป ๐˜๐—ผ๐—ธ๐—ฒ๐—ป๐˜€/๐—ฒ๐˜…๐—ฐ๐—ต๐—ฎ๐—ป๐—ด๐—ฒ ๐—ฝ๐—ผ๐—ผ๐—น๐˜€

Notifications You must be signed in to change notification settings

DerXanRam/amm-arb-toolkit-py

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

54 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

โ›“๐Ÿ’ฐ bdex: an amm-toolkit



tl; dr

๐Ÿ’ฐ my package and CLI tool to extract data and arbitrage for specified tokens/exchange pools.

๐Ÿ“š for more details, check my mirror post about this tool.






available token pairs:

  • WETH/DAI

available exchanges:




installation

Add your Alchemy API key and endpoint to a file named .env:

cp .env_example .env
vim .env

Create a virtual environment:

virtualenv venv
source venv/bin/activate

Install dependencies:

make install_deps

Install the CLI:

make install



running

You can run the CLI with:

bdex

Screen Shot 2022-04-04 at 1 35 46 AM


checking the latest block

We leverage Alchemy's API endpoint eth_blockNumber_hex to get the latest block:

bdex -c

Screen Shot 2022-03-31 at 1 15 25 PM



๐Ÿ’ก The block number can be checked against ETHstat.


Screen Shot 2022-03-31 at 1 15 19 PM


๐Ÿ’ก We are crafting the checksum address string by hand without directly Keccak-256 hashing the methods and parameters.


getting the token balance for an exchange

We leverage Alchemy API endpoint eth_call to retrieve the current token balance for a specific exchange:

bdex -b TOKEN EXCHANGE

Screen Shot 2022-03-31 at 10 26 21 PM


getting all token balances for all the exchanges

We loop over the previous method for a list of tokens and exchanges:

bdex -a

Screen Shot 2022-03-31 at 10 26 51 PM


[extra] getting all token balances for all exchanges with Python's web3 library

To be able to compare our results from the previous steps, we implemented an alternative way to fetch pair balances utilizing the Python web3 library:

bdex -w

Screen Shot 2022-03-31 at 10 27 25 PM



๐Ÿ’ก For this library, it's necessary to supply the contracts' ABI (in our case, for DAI and WETH, located at ./docs)

๐Ÿ’ก A third option to verify token balances is through Etherscan tokenholdings dashboard.


getting trading prices for all the exchanges

To get the current price for WETH/DAI in all exchanges (e.g., as shown in the projects' dashboards), run:

bdex -p QUANTITY TOKEN1 TOKEN2

Quote for trading 1 WETH:


Screen Shot 2022-04-03 at 8 27 55 PM


Quote for trading 10 WETH:


Screen Shot 2022-04-03 at 8 28 20 PM


Quote for trading 100 WETH:


Screen Shot 2022-04-03 at 8 28 46 PM


how the price is calculated

An AMM replaces the buy and sell orders in an order book market with a liquidity pool of two assets, both valued relative to each other. As one asset is traded for the other, the relative prices of the two assets shift, and the new market rate for both is determined.

The constant product is:

token_a_pool_size * token_b_pool_size = constant_product

All the exchanges are forks from UniswapV2, so they all use the same price formula for trading:

market_price_token1 = token2_balance / token1_balance

For example, in a pool with 2,000,000 DAI and 1,000 WETH, the constant product is 2,000,000,000 and the market price for WETH is $2,000.


Buy price (e.g., buying WETH in a WETH/DAI pool)

To find the buy price for a certain quantity, first, we calculate how much WETH needs to remain in balance to keep the constant product unchanged:

token1_balance_buy = constant_product / (token2_balance + quantity)

Then we calculate how much WETH goes out to keep this constant:

 t1_amount_out_buy = token1_balance - token1_balance_buy

The buy price to reflect this ratio is:

buy_price = quantity / t1_amount_out_buy

sell price (e.g., selling WETH in a WETH/DAI pool)

To find how much we can sell a certain quantity of WETH for DAI, first, we calculate the ratio of DAI in the new pool, as we add WETH:

token2_balance_buy = constant_product / (token1_balance + quantity)

We then calculate how much DAI will go out:

t2_amount_out_buy = token2_balance + token2_balance_buy

We calculate the DAI balance reflected with the income WETH:

token1_balance_sell = constant_product / (token2_balance - quantity)

And what's the proportion of WETH in the new balance:

t1_amount_in_sell = token1_balance + token1_balance_sell

We can now calculate the sell price to reflect the balance change, keeping the constant:

sell_price = t2_amount_out_buy / t1_amount_in_sell


getting arbitrages

Run an algorithm to search for arbitrage in the supported exchanges for a certain buy quantity:

bdex -x QUANTITY

Arbitrage opportunities for 10 WETH:


Screen Shot 2022-04-04 at 6 02 09 PM


Arbitrage opportunities for 1 WETH:


Screen Shot 2022-04-04 at 6 02 31 PM


Arbitrage opportunities for 0.01 WETH:


Screen Shot 2022-04-04 at 6 03 20 PM




running arbitrage algorithm in a loop

To run the arbitrage algorithm for a certain amount of minutes:

bdex -r MIN

Screen Shot 2022-04-04 at 1 05 07 PM



Results are saved into results/<arbitrage_TIMESTAMP>.txt.


Here is a sample of the results running this algorithm for 100 minutes for trading 1 WETH:

{'buy_exchange': 'SUSHISWAP', 'sell_exchange': 'UNISWAP', 'arbitrage': '7.01', 'buy_price': 3475.14, 'sell_price': 3482.15}
{'buy_exchange': 'SUSHISWAP', 'sell_exchange': 'SHEBASWAP', 'arbitrage': '4.27', 'buy_price': 3475.14, 'sell_price': 3479.41}
{'buy_exchange': 'SHEBASWAP', 'sell_exchange': 'UNISWAP', 'arbitrage': '2.06', 'buy_price': 3480.09, 'sell_price': 3482.15}
{'buy_exchange': 'CROSWAP', 'sell_exchange': 'UNISWAP', 'arbitrage': '13.06', 'buy_price': 3469.09, 'sell_price': 3482.15}
{'buy_exchange': 'CROSWAP', 'sell_exchange': 'SUSHISWAP', 'arbitrage': '5.79', 'buy_price': 3469.09, 'sell_price': 3474.88}
{'buy_exchange': 'CROSWAP', 'sell_exchange': 'SHEBASWAP', 'arbitrage': '10.32', 'buy_price': 3469.09, 'sell_price': 3479.41}
...

running arbitrage algorithm in a loop in a Docker container

To run the algorithm in a separated container, first install Docker, then build the Docker image:

docker build -t bdex .

Finally, run the container (in a separate terminal tab):

docker run -v $(pwd):/results -it bdex sleep infinity

Results are available at results/<arbitrage_TIMESTAMP>.txt.

๐Ÿ’ก You can inspect your container at any time with these commands:

docker ps
docker exec -it <container_id> /bin/bash
docker inspect bdex

Cleaning up:

docker volumes prune



development

Install dependencies (in a virtual env):

make install

Useful commands:

make lint
make test

About

โ›“๐Ÿงฐ๐Ÿ’ฐ๐—ฐ๐—น๐—ถ ๐˜๐—ผ๐—ผ๐—น ๐˜๐—ผ ๐—ด๐—ฒ๐˜ ๐—ฑ๐—ฎ๐˜๐—ฎ ๐—ฎ๐—ป๐—ฑ ๐—ฎ๐—ฟ๐—ฏ๐—ถ๐˜๐—ฟ๐—ฎ๐—ด๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—ด๐—ถ๐˜ƒ๐—ฒ๐—ป ๐˜๐—ผ๐—ธ๐—ฒ๐—ป๐˜€/๐—ฒ๐˜…๐—ฐ๐—ต๐—ฎ๐—ป๐—ด๐—ฒ ๐—ฝ๐—ผ๐—ผ๐—น๐˜€

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 96.5%
  • Makefile 2.3%
  • Dockerfile 1.2%