Cover photo

on bdex, my x*y=k amm arbitrage toolkit

tl; dr

today i go over bdex, a tool i built that scrutinizes arbitrage cases on x*y=k AMMs on ethereum.


🎶 today’s mood

https://open.spotify.com/track/6BuGA3ontF6D4jluQ0DHZY?si=7e7a411cbdd241dc


The following are the resources we are fetching (but feel free to add your favorite token and pool):

Token pairs:

  • WETH/DAI

Exchanges:

  • Uniswap (0xa478c2975ab1ea89e8196811f51a7b7ade33eb11)

  • Sushiswap (0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f)

  • Shebaswap (0x8faf958e36c6970497386118030e6297fff8d275)

  • Sakeswap (0x2ad95483ac838e2884563ad278e933fba96bc242)

  • Croswap (0x60a26d69263ef43e9a68964ba141263f19d71d51)

Let’s start by taking a look at what this tool does, and then I will share the source code with you.


Setting your environment

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 the CLI

You can run the CLI with:

bdex

post image

Checking the latest block

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

bdex -c

💡 The block number can be checked against ETHstat.

post image

💡 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

post image

Getting all token balances for all the exchanges

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

bdex -a

post image

Getting all token balances for all exchanges, but now with web3.py

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

post image

💡 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:

post image

Quote for trading 10 WETH:

post image

Quote for trading 100 WETH:

post image

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 Uniswap V2, 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 arbitrage

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

bdex -x QUANTITY

Arbitrage opportunities for 10 WETH:

post image

Arbitrage opportunities for 1 WETH:

post image

Arbitrage opportunities for 0.01 WETH:

post image

Running arbitrage algorithm in a loop

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

bdex -r MIN

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} ...


The Source Code for the main loop

post image

The arbitrage API class

post image

The util class

post image

◻️ motherofbots.eth