How does a DEX aggregator work?

In simple terms, a DEX aggregator is a decentralized application that offers some on-fire functionalities. The question is, what are these functionalities? Let’s analyze it together, starting with the basic process that a user goes through when using a DEX aggregator.

How does a DEX aggregator work?

“What are DEX aggregators,” you ask? Learn the basics in our previous article!

  1. A user opens their browser and navigates to the DEX aggregator’s website.
  2. There, they are presented with a swap box where they can enter the tokens they wish to buy and sell, as well as the trade amount.
  3. The swap box displays the most profitable route to trade the specified tokens.
  4. And the user clicks the swap button, signs the transaction with their MetaMask wallet and, after a short time, receives the target token in their wallet.
YAD widget allows swaps for 3,500 tokens across six dominant blockchains

Magic? Technological magic, which means that there’s certainly a way to deconstruct it! So, let’s try digging a little deeper!

A typical DEX aggregator consists of three major parts: the frontend, smart contracts, and backend.

  1. The frontend architecture of such a router is normally a two-part system consisting of the service’s website and the MetaMask wallet.
  2. Next comes the router itself, which builds optimized routes. The router is a crucial component that searches for liquidity pools with the highest total value locked and splits the order, “multihopping” (i.e., swaping to other tokens along the way) when it is more efficient.
  3. Finally, smart contracts execute the transactions in accordance with the calculated route. The smart contracts integrated into the DEX aggregator’s architecture are responsible for implementing the order’s execution logic.

There is the option to build a frontend-only DEX aggregator that pulls quotes from the smart contracts, which would make it a truly decentralized solution. However, a router designed without a backend would likely operate at a pretty sluggish pace and wouldn’t be able to suggest the best rate possible.

With all this in mind, let’s look behind the scenes at exactly what happens when you push the swap button.

Step 1

On the aggregator’s website, a user accesses a swap box and enters the tokens to buy and to sell. Regardless of the service (e.g., 1inch, Matcha, or YAD), once they enter the limits for slippage tolerance, gas price, and the buy/sell token amount, the swap box will return the exchange rate and transaction cost.  

Step 2

As soon as the user starts interacting with the swap box, their browser builds a request and sends it to the server. This server typically stores the aggregator's backend as well as a constantly updating snapshot of the blockchain. At regular intervals (say, every 10 seconds), the API calculates the optimal route and returns it to the user’s swap box.

Step 3

Once the optimal route has been selected, everything is ready for the swap. When the user pushes the “Swap” button, the frontend sends a request to sign the transaction to the user’s wallet. The user sees the request to sign the transaction in their wallet (it can be any non-custodial wallet like TrustWallet or Metamask), verifies the details to make sure they are signing the right transaction and interacting with the right smart contract, and signs. After the user’s consent has been granted, the wallet sends the transaction to the mempool to be validated and included in one of the next blocks. From here on the user can only wait for the result. If the swap is successfully executed, the desired tokens will appear in the user’s wallet. However, if, for some reason, the swap can not be executed on the conditions approved, the smart contract will revert the sent tokens and return them back to the user’s wallet.

So, how can you choose the DEX aggregator best-suited for your needs? Check out the next article in the series to find out!