# Indexing all blockchain transactions

## 1. Use case

* Indexers/ Data analytics providers would like to track all DEX, LENDING, STAKING ... actions with least effort to decode every protocols.
* Handling core infrastructure to build block explorers
* Wallet/Portfolio services providers want to track all money flows

## 2. Steps

* Connect to endpoint `wss://[chain_name]-api.txdecoder.xyz/ws`
* Send message to receive all data

```
{
    "type" : "all"
}
```

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

&#x20;

## 3. Example Code (Javascript)

```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: 'all'}
     
     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

                switch(action) {
                  case 'DEX': 
                  
                  // HANDLING DEX actions here: swap/ add_liquidity, remove_liquidity
                  break
                  
                  case 'LENDING':
                  
                   // HANDLING LENDING actions here
                  break
                  
                  case 'STAKING': 
                   // HANDLING STAKING actions here
                  break

                 case 'BRIDGE': 
                   // HANDLING BRIDGE actions here
                  break
                  
                  /// ...
                }
            }
        }
        
    } 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)
```

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://txdecoder.gitbook.io/docs/tutorials/indexing-all-blockchain-transactions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
