We coded the trading bot, now we connect it to the decentralized exchange

We coded the trading bot, now we connect it to the decentralized exchange

In the first article, I told what IT tools we used to code a cryptocurrency trading bot. This is the second article where I reflect on the steps taken towards a decentralized exchange. I will be grateful for feedback in the comments.

We study subjects

First, let’s list the characteristics that differ when trading in a centralized exchange (CEX) and decentralized (DEX).

Table 1

CEX

DEX

Examples of exchanges

Binance, ByBit, OKX

UniSwap, dYdX, PancakeSwap, Curve

Trading volumes of one exchange (24 hours)

total: $1-10 billion

total: $0.1-1 billion

UniSwap:
Ethereum: WETH/USDT $150M
Arbitrum: WETH/USDC $73 million

Commissions (in spot trade)

<0.1%

UniSwap: 0.05%; 0.3%; 1%
dYdX: <0.05%.
+
commission for gas

KYC

necessarily

Not available

Assortment of tokens

tokens are filtered by the exchange

you can add any pair of tokens yourself

Features

Partially filled, Limit order

Price slippage

We can see that the daily trading volume of the popular tokens WETH, WBTC is more than 10 million dollars in DEX. We have 1 lot of approximately $1,000, so these volumes are enough. Let’s look at the size of the DEX commission. We are willing to pay a percentage comparable to a centralized exchange ie. 0.1%. Part will go to cover exchange fees (for example, UniSwap fee) and part – for blockchain gas (for example, Arbitrum One). Because we have set the limit from above (0.1%), then you will have to spend tokens only in the liquidity pool with an exchange commission of 0.05% plus pay a network gas commission of 0.05%. But the gas fee is an absolute value and is measured in ETH. Since we agreed that our lot in the trade is equal to $1,000, we are willing to pay a gas commission of less than 0.05% x $1,000 = 50 cents.

Choose your favorite network

Note that the gas fee for a transaction on a Layer 1 network (Ethereum) can be close to $10, and on Layer 2 networks (Arbitrum, Optimism, Polygon) it is less than $1. Therefore, from a financial point of view, we can only trade in L2 for now. Arbitrum was chosen for fairly high trading volumes on the popular UniSwap V3 decentralized exchange and for low fees: 26 cents on average in fall 2023, $1 to $2 in winter 2024, 10 cents after upgrade in mid-March 2024.

Cod

In order to organize the storage and execution of limit orders, we had to code our mini-exchange. Its main functions:

  • “walk” to the blockchain via RPC Node (for example, alchemy or infura).

  • continuously monitor exchange events (Event Swap) in Arbitrum network blocks. UniSwap V3 itself filters events in liquidity pools.

  • determine that in one of the liquidity pools it is possible to exchange tokens at an acceptable price, and at the same time send a transaction to this pool. Then wait for the network’s response.

We solve problems

A transaction can be executed for various reasons. Most often, we encountered STF, Too little received, Fail errors. To avoid these mistakes, preventive measures are needed:

  • We estimate the cost of gas that will be required for the transaction. We make sure it’s below our commission limit and the account has the required amount of ETH.

  • we put 0.2% slippage in our price.

User privacy is all ours

In the previous step, we sent requests to the UniSwap router, because it is not possible to send Swap directly to the liquidity pool. The request specifies a pair of tokens and the amount of the commission. These three parameters clearly define the liquidity pool. For example, for WETH/USDT 0.05%, the pool address is 0x641C00A822e8b671738d32a431a4Fb6074E5c79d.

But in this case, it is necessary to have the user’s private key and sign the transaction with the key. Of course, having a user’s PC is wrong, because in this case the owner of the PC will be able to dispose of the user’s money until it is transferred to someone else’s wallet. To avoid sharing the user’s private key, a smart contract is required that has the right to redeem tokens for the user, but not to withdraw money from the user’s address. It’s a fairly simple contract that took just over an hour to audit.

Related posts