# Watching a wallet

## 1. Use case

* Wallet tracking, wallet notifications
* Copy trade

## 2. Steps

* Connect to endpoint `wss://[chain_name]-api.txdecoder.xyz/ws`
* Send optional parameters to filter transactions of the given wallet
  * **participant**: watching address

```json
{
    "participant" : "0xEC3C2D51b8A6ca9Cf244F709EA3AdE0c7B21238F"
}
```

* 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 = { participant: '0xEC3C2D51b8A6ca9Cf244F709EA3AdE0c7B21238F'}
     
     ws.send(JSON.stringify(message))
     console.log('Sent message:', 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: processing watching wallet transactions
            }
        }
        
    } 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/watching-a-wallet.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.
