# Stream Transaction

Stream transactions will help you to track the activity of the subscribed address on the blockchain.

### Response Schema:

<table><thead><tr><th width="184">Field</th><th>Description</th></tr></thead><tbody><tr><td>blockHash</td><td>Hash of the block.</td></tr><tr><td>blockNumber</td><td>Number of the block.</td></tr><tr><td>chainId</td><td><code>0x1</code> denotes ethereum.</td></tr><tr><td>from</td><td>Ethereum address of the sender of the transaction.</td></tr><tr><td>gas</td><td>Gas provided by the sender.</td></tr><tr><td>gasPrice</td><td>Amount of Ether that the sender is willing to pay for each unit of gas.</td></tr><tr><td>hash</td><td>Hash of the transaction.</td></tr><tr><td>input</td><td>The data sent along with the transaction.</td></tr><tr><td>nonce</td><td>Unique value that is incremented with each transaction and is unique to each account.</td></tr><tr><td>v,r,s</td><td>Signature values that are used to verify the transaction's authenticity.</td></tr><tr><td>to</td><td>Ethereum address of the recipient of the transaction.</td></tr><tr><td>transactionIndex</td><td>Integer of the transactions index position in the block.</td></tr><tr><td>type</td><td>Type of transaction.</td></tr><tr><td>value</td><td>Amount of Ether to be transferred in the transaction.</td></tr></tbody></table>

### Streaming transactions:

Users can stream transactions in two ways:

#### Example 1: via wscat script (For Linux based distro)

First, the user needs to run the following command in their terminal to install wscat in their system:&#x20;

```shell
npm install -g wscat 
```

After installing wscat, the user needs to create a shell script and paste the following sample code:

**Client Side Sample Socket Code** :

```shell
#!/bin/bash

echo "Price Discovery\n"
read -p "x-api-key: " YOUR_API_KEY
wscat -c wss://pricediscovery.expand.network -H authorization:secret-token -H x-api-key:$YOUR_API_KEY
```

{% hint style="info" %}
Once the file has been saved, the user will need to grant permission for it to be executed. This can be accomplished by executing the following command:
{% endhint %}

```bash
chmod +x YOUR_FILE_NAME.sh
```

After following the above steps, the user needs to run the file. This can be done by entering the filename in the console.

**Executing a query**:&#x20;

After executing the aforementioned file(shell script), the user will be prompted to input the appropriate API key.

After entering the API key, user can subscribe to following actions:

```shell
{"action": "streamTransactions"}
```

{% hint style="info" %}
Make sure you are connected to the WebSocket before you perform the following queries.&#x20;
{% endhint %}

**Applying a filter in the query**:&#x20;

Users can apply the following filters in their query:-

* addresses
* minAmount(Wei)
* maxAmount(Wei)

**The above filters can be applied in any combination, using the following syntax**:&#x20;

```shell
{"action": "streamTransactions", "addresses": "array_of_strings", "minAmount": "minimum_amount_in_wei", "maxAmount": "maximum_amount_in_wei"}
```

**Sample Query**:

```shell
{"action": "streamTransactions", "addresses": ["0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D","0xdafea492d9c6733ae3d56b7ed1adb60692c98bc5"], "minAmount": "0", "maxAmount": "1000000000000000000000000000000000000"}
```

#### Example 2. via javascript code

First, the user needs to run the following command in their terminal to install wscat in their system:&#x20;

```shell
npm i ws
```

After installing ws, the user needs to run the following sample code on their device to start streaming:&#x20;

**Client Side Sample Socket Code** :

```javascript
const WebSocket = require('ws');
const url = 'wss://pricediscovery.expand.network';
const options = {
  headers: {
    Authorization: 'secret-token',
    'x-api-key': 'YOUR_API_KEY', // Replace this with your API Key
  }
};

const ws = new WebSocket(url, options);

ws.on('open', () => {
  console.log('WebSocket connection established.');
  const payload = {
    action: "streamTransactions",
    addresses: ["0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D","0xdafea492d9c6733ae3d56b7ed1adb60692c98bc5"],
    minAmount: "0",
    maxAmount: "10000000000000000000000000000000000000000000"
  };
  ws.send(JSON.stringify(payload));
});


ws.on('message', (data) => {
  console.log(`Received data: ${data}`);
});

ws.on('close', () => {
  console.log('WebSocket connection closed.');
});
```

{% hint style="info" %}
The payload can be modified, depending on the action.
{% endhint %}

### Expected Output:

```javascript
{
  "blockHash": "0x528f4648ffd86cf89f0bd331cdad2185193bedf183c239235c3a84e83f9aa03c",
  "blockNumber": 16461655,
  "chainId": "0x1",
  "from": "0x27E1D375B3Bed078EcA97a5132A8e702ff222528",
  "gas": 502594,
  "gasPrice": "17409031646",
  "hash": "0x0d1775fc18e65485aced1ccbcdb333ef7a16f2312198e68a553452b957957379",
  "input": "0x791ac9470000000000000000000000000000000000000000000000000000009184e72a000000000000000000000000000000000000000000000000000057a67078b9a31b00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000027e1d375b3bed078eca97a5132a8e702ff2225280000000000000000000000000000000000000000000000000000000063cd0c0e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000077c67b8401d8dca854a3a0929c85ed030b76f670000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
  "nonce": 24,
  "r": "0x7052bbb63c73c3447ebef1649e89e0ed4659aeedddda1cfedb00cbd63b737a7d",
  "s": "0x4f9bfa47d1f958b0673d574012e1b202a7aa4092b77d0fa756a3611290d9eca9",
  "to": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
  "transactionIndex": 6,
  "type": 2,
  "v": "0x1",
  "value": "0"
}
```

### How to disconnect:&#x20;

To disconnect from our API, enter `ctrl-c`  in the connected console.
