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

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

Studying test subjects

First, let’s write down the characteristics that differ when trading on a centralized exchange (CEX) and a decentralized one (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 $150 million
Arbitrum: WETH/USDC $73 million

Commissions (in spot trading)

<0.1%

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

KYC

Necessarily

absent

Range of tokens

tokens are filtered by the exchange

you can add any pair of tokens yourself

Peculiarities

Partially filled, Limit order

Price slippage

We see that in DEX the daily trading volumes of popular tokens WETH and WBTC are more than $10 million. We have 1 lot for approximately $1,000, so these volumes are sufficient. Let's look at the size of the composite commission in DEX. We are ready to pay a percentage comparable to a centralized exchange, i.e. 0.1%. Part will go to cover the exchange commission (for example, UniSwap fee) and part – for blockchain gas (for example, Arbitrum One). Because We set an upper limit (0.1%), then you will have to swap 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 in the Layer 1 network (Ethereum) can be about $10, and in Layer 2 networks (Arbitrum, Optimism, Polygon) less than $1. Therefore, from a financial point of view, we can only trade in L2 for now. We chose Arbitrum for fairly high trading volumes on the popular decentralized exchange UniSwap V3 and for low commissions: in the fall of 2023 an average of 26 cents, in the winter of 2024 from 1 to 2 dollars, after an upgrade in mid-March 2024 10 cents.

Code

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

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

  • continuously monitor exchange events (Event Swap) in blocks of the Arbitrum network. It is UniSwap V3 that filters events in liquidity pools.

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

We solve problems

A transaction may fail for various reasons. The most common errors we encountered were STF, Too little received, Fail. To avoid these mistakes, preventive measures are needed:

  • We estimate the cost of gas required for the transaction. We make sure that this is below our commission limit and the account has the required amount of ETH.

  • We include slippage of 0.2% in our price.

User privacy is everything

In the previous step we sent requests to UniSwap router, because it is not possible to send Swap directly to the liquidity pool. The request specifies a pair of tokens and the size of the commission. These three parameters uniquely define the liquidity pool. For example, for WETH/USDT 0.05% pool address: 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, since in this case the owner of the PC will be able to manage the user’s money up to the point of completely withdrawing it to someone else’s wallet. To avoid transferring the user’s private key, you need a smart contract that has the right to swap tokens in the user’s interests, but does not have the right to withdraw money from the user’s address. It's simple enough Contractthe audit of which took no more than an hour.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *