> For the complete documentation index, see [llms.txt](https://txdecoder.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://txdecoder.gitbook.io/docs/tutorials/decoding-transactions-from-specific-dex-protocol.md).

# Decoding transactions from specific DEX protocol

## 1. Use case

* Trading tools like GeckoTerminal, Dexscreener, Codex, Covalent ...

## 2. Optional 1:  Using websocket

## <sup>a. Steps</sup>

* Connect to endpoint `wss://[chain_name]-api.txdecoder.xyz/ws`&#x20;
* Available chain: <kbd>ethereum, bsc, base</kbd>
* Send message to receive all DEX data

```javascript
{
    "type" : "DEX",
    "protocol": "Smardex"
}
```

* Wait to receive new data
* Response data in **User Action** format

&#x20;

## <sup>b. Example Code (Javascript)</sup>

```javascript
const WebSocket = require('ws')

const main = async () => {
  // Connect through the WebSocket proxy with authentication
  const apiKey = process.env.API_KEY
  const proxyUrl = 'wss://bsc-api.txdecoder.xyz/ws'

  const ws = new WebSocket(proxyUrl, {
    headers: {
      'x-api-key': apiKey,
    },
    rejectUnauthorized: false,
  })

  ws.on('open', () => {
    console.log('Connected to TxDecoder WebSocket server')
   const message = { type: 'DEX'}
     
     ws.send(JSON.stringify(message))
  })

  ws.on('message', (data) => {
    try {
      const message = JSON.parse(data)
      
        for (const txHash in message) {
            const userActions = message[txHash]
            if (!Array.isArray(userActions)) continue
            for (const userAction of userActions) {
                const { tokens, participants, tx_hash: txHash, value_usd: valueUsd } = userAction
                if (!txHash) continue

                // TODO: process DEX data here
            }
        }
        
    } catch (error) {
      console.log('Received raw data:', data.toString())
    }
  })

  ws.on('error', (error) => {
    console.error('WebSocket error:', error)
  })

  ws.on('close', (code, reason) => {
    console.log(`Connection closed. Code: ${code}, Reason: ${reason}`)
  })

  // Handle graceful shutdown
  process.on('SIGINT', () => {
    console.log('Closing WebSocket connection...')
    ws.close()
    process.exit(0)
  })
}

main().catch(console.error)
```

## 3. Optional 2:  Using API

## <sup>a. Steps</sup>

* Whenever new block coming, call below API
* Endpoint `https://[chain_name]-api.txdecoder.xyz/premium/block/decode`&#x20;
* Query params:
  * **block\_number**
  * **type**: <kbd>DEX</kbd>
  * **protocol**: protocol name ([ Listed here ](https://txdecoder.gitbook.io/docs/chains-and-protocols/supported-protocols))

## <sup>b. Example Code (Javascript)</sup>

**Example code**<br>

```javascript
const axios = require('axios')

const main = async() => {
    const { data } = await axios.get(
    `https://ethereum-api.txdecoder.xyz/premium/block/decode?block_number=23187124`,
    {
        headers: {
            "x-api-key": process.env.API_KEY
        }
    })
    console.log(data)
}
main()

```

\
\
\
**Response format**

```javascript
{
    [transaction_hash_1]: [
        // List of user actions in transaction 1
    ],
    [transaction_hash_2]: [
        // List of user actions in transaction 2
    ]
}
```

**Example Response**<br>

```json
{
    "0x09782db139021ffa8df9994d74a082d242835690806325537ef4b33f1e4099c7": [
        {
            "type": "DEX",
            "protocol": "Uniswap V3",
            "source": "Uniswap V3",
            "action": "swap",
            "participants": [
                {
                    "address": "0x6532fEcD7f475119Eb2548294A9371728FA447a9",
                    "type": "signer"
                },
                {
                    "address": "0xEff6cb8b614999d130E537751Ee99724D01aA167",
                    "type": "to"
                }
            ],
            "pool_id": "0xd31d41DfFa3589bB0c0183e46a1eed983a5E5978",
            "tokens": [
                {
                    "address": "0xbe0Ed4138121EcFC5c0E56B40517da27E6c5226B",
                    "name": "Aethir Token",
                    "symbol": "ATH",
                    "decimals": 18,
                    "ui_amount": "208874.425628805902533533",
                    "amount": "2.08874425628805902533533e+23",
                    "type": "swap_from"
                },
                {
                    "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
                    "name": "Wrapped Ether",
                    "symbol": "WETH",
                    "decimals": 18,
                    "ui_amount": "1.653190493186746577",
                    "amount": "1653190493186746577",
                    "type": "swap_to"
                }
            ],
            "value_usd": 7801.25715020387,
            "log_index": 101,
            "block_number": 23187124,
            "block_hash": "0x0226dfc0c5ab0b5cc331d94bbda20664e28ce8aa730777a9855d42c7cd79ccae",
            "tx_hash": "0x09782db139021ffa8df9994d74a082d242835690806325537ef4b33f1e4099c7",
            "timestamp": 1755751139
        }
    ]
}

```
