Skip to main content

DEX Swaps

Updated on
Dec 14, 2023

Overview

Decentralized exchange (DEX) swaps refer to a method of trading assets directly between users, facilitated by a decentralized platform that operates on blockchain technology, without the need for an intermediary or centralized authority, like a bank or traditional exchange platform. The working mechanism involves the use of smart contracts which are self-executing contracts with the terms directly written into code, allowing trades to be conducted automatically and securely.

Swaps occur based on liquidity pools rather than order books. Liquidity pools are essentially pools of funds provided by users, known as liquidity providers, to facilitate trading by offering liquidity. In return, liquidity providers earn fees based on the trading volume of that pool.

Each token is paired with the native token of the chain to create a liquidity pool of native tokens. There are various pools of native tokens including ARB or WETH on Arbitrum, WBNB on BNB Smart Chain, WETH on Ethereum, OP or WETH on Optimism, MATIC on Polygon POS. In the next section, we will learn how we can track swaps using the pool address and swap event.

Uniswap v3 Swaps

To create our expression, we will need to target two values:

  1. The Uniswap v3 liquidity pool - There's a liquidity pool smart contract deployed on a chain for a token pair. We have used a USDC/(WETH, WBNB, MATIC) pool in this example and gathered the addresses of pools from GeckoTerminal. This value gets set as the tx_logs_address, as we will be looking for any transactions from this address (the contract).
  2. The event the contract emits when the action we're interested in occurs - In the case of the v3 contract, it is the Swap event (event documentation from Uniswap). On EVM blockchains, the event is encoded and can be found in tx_logs_topic0. To get the encoded value, you will need to take the event definition (event name and parameters' types), run it through the Keccak-256 hash function, and prefix it with 0x.
Swap(address,address,int256,int256,uint160,uint128,int24)

c42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67

0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67
Expression
Deploy
1
tx_logs_address == '0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640'
2
&&
3
tx_logs_topic0 == '0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67'

Uniswap v2 Swaps

To create our expression, we will need to target two values:

  1. The Uniswap v2 liquidity pool - There's a liquidity pool smart contract deployed on a chain for a token pair. We have used a USDC/(WETH, WBNB, MATIC) pool in this example and gathered addresses of pools from GeckoTerminal. This value gets set as the tx_logs_address, as we will be looking for any transactions from this address (the contract).
  2. The event the contract emits when the action we're interested in occurs - In the case of the v2 contract, it is the Swap event (event documentation from Uniswap). On EVM blockchains, the event is encoded and can be found in tx_logs_topic0. To get the encoded value, you will need to take the event definition (event name and parameters' types), run it through the Keccak-256 hash function, and prefix it with 0x.
Swap(address,uint256,uint256,uint256,uint256,address)

d78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822

0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822
Expression
Deploy
1
tx_logs_address == '0x5289a8dbf7029ee0b0498a84777ed3941d9acfec'
2
&&
3
tx_logs_topic0 == '0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822'

PancakeSwap V3 Swaps

To create our expression, we will need to target two values:

  1. The PancakeSwap v3 liquidity pool - There's a liquidity pool smart contract deployed on a chain for a token pair. We have used a USDC/(WETH, WBNB, MATIC) pool in this example and gathered the addresses of pools from GeckoTerminal. This value gets set as the tx_logs_address, as we will be looking for any transactions from this address (the contract).
  2. The event the contract emits when the action we're interested in occurs - In the case of the v3 contract, it is the Swap event. On EVM blockchains, the event is encoded and can be found in tx_logs_topic0. To get the encoded value, you will need to take the event definition (event name and parameters' types), run it through the Keccak-256 hash function, and prefix it with 0x.
Swap(address,address,int256,int256,uint160,uint128,int24,uint128,uint128)

19b47279256b2a23a1665c810c8d55a1758940ee09377d4f8d26497a3577dc83

0x19b47279256b2a23a1665c810c8d55a1758940ee09377d4f8d26497a3577dc83
Expression
Deploy
1
tx_logs_address == '0xd9e2a1a61b6e61b275cec326465d417e52c1b95c'
2
&&
3
tx_logs_topic0 == '0x19b47279256b2a23a1665c810c8d55a1758940ee09377d4f8d26497a3577dc83'

PancakeSwap V2 Swaps

To create our expression, we will need to target two values:

  1. The PancakeSwap v2 liquidity pool - There's a liquidity pool smart contract deployed on a chain for a token pair. We have used a USDT/(WETH, WBNB, MATIC) pool in this example and gathered the addresses of pools from GeckoTerminal. This value gets set as the tx_logs_address, as we will be looking for any transactions from this address (the contract).
  2. The event the contract emits when the action we're interested in occurs - In the case of the v2 contract, it is the Swap event. On EVM blockchains, the event is encoded and can be found in tx_logs_topic0. To get the encoded value, you will need to take the event definition (event name and parameters' types), run it through the Keccak-256 hash function, and prefix it with 0x.
Swap(address,uint256,uint256,uint256,uint256,address)

d78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822

0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822
Expression
Deploy
1
tx_logs_address == '0x630388b8a8d22bb401e0c6a91c3a7956535cb30e'
2
&&
3
tx_logs_topic0 == '0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822'
Share this doc