Event Subscription

Event Subscription is used to subscribe to specific events or transactions occurring within a blockchain network. It enables participants to get updates or notifications in real time whenever a specific event happens.

Following are the event subscriptions that a user can subscribe to-

  1. WETH

  2. ERC-20

  3. ERC-721

  4. ERC-1155

Streaming Events:

Users can stream events 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:

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 :

#!/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

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:

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:

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

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

{"action": "events"}

Make sure you are connected to the WebSocket before you perform the following queries.

Applying a filter in the query:

Users can apply the following filters in their query:-

WETH

The above filters can be applied in any combination, using the following syntax:

{"action": "events", "event":"WETH"}

Sample Query:

{"action": "events", "event":"WETH"}

Example 2. via javascript code

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

npm i ws

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

Client Side Sample Socket Code :

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: "events",
    event: "WETH"
  };
  ws.send(JSON.stringify(payload));
});


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

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

The payload can be modified, depending on the action.

Expected Output:

"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
  "topics": [
    "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
    "0x000000000000000000000000def1c0ded9bec7f1a1670819833240f027b25eff",
    "0x000000000000000000000000dce93ed9ae7c53143e19cf799d156b72d1cc2777"
  ],
  "params": {
    "src": "0xDef1C0ded9bec7F1a1670819833240f027b25EfF",
    "dst": "0xdCe93ed9ae7C53143e19cf799d156B72D1cC2777",
    "wad": "111121914630589080"
  },
  "data": "0x000000000000000000000000000000000000000000000000018ac8cae8514a98",
  "blockNumber": 17336261,
  "blockHash": "0xc8a8f98d12e0eaf23c1cb3a0014a899b7b3781056332bfb67b0f5cf736eaca41",
  "logIndex": 35,
  "transactionHash": "0x922084d84f63a025e6f5ede2dc74c6c349655746ae26481a9160b7547b6737a5",
  "transactionIndex": 28

ERC-20

The above filters can be applied in any combination, using the following syntax:

{"action": "events", "event":"ERC20", "contract":[{"id":"contractId","eventType":["Transfer","Approval"]}]}

Sample Query:

{"action":"events", "event":"ERC20", "contract":[{"id":"0x6b175474e89094c44da98b954eedeac495271d0f","eventType":["Transfer","Approval"]}]}

Example 2. via javascript code

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

npm i ws

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

Client Side Sample Socket Code :

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: "events",
    event: "WETH"
  };
  ws.send(JSON.stringify(payload));
});


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

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

The payload can be modified, depending on the action.

Expected Output:

"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
  "topics": [
    "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
    "0x0000000000000000000000008cd509c290bbb7a3eadce5fb1fd4017610b26d47",
    "0x00000000000000000000000041d7534ddf2b5d3e5308f960a6cda7449c3dbf21"
  ],
  "params": {
    "src": "0x8Cd509C290Bbb7a3EaDCe5Fb1Fd4017610b26D47",
    "dst": "0x41D7534DdF2B5d3E5308f960a6cDA7449C3DbF21",
    "wad": "1000000000000000000000"
  },
  "data": "0x00000000000000000000000000000000000000000000003635c9adc5dea00000",
  "blockNumber": 17336103,
  "blockHash": "0xa1b370a58ba54a788fb5f7639596ba40dacc0aedecc10271c55b2bf22950ef97",
  "logIndex": 341,
  "transactionHash": "0xfe84151eae6b29f984af059e07ccd47d918b6f3bbccd04b6b64d55a4526b770a",
  "transactionIndex": 144

ERC-721

The above filters can be applied in any combination, using the following syntax:

{"action": "events", "event":"ERC721", "contract" : [{"id":"contractId", "eventType":["Transfer","Approval","ApprovalForAll"]}}]}}

Sample Query:

{"action":"events", "event":"ERC721", "contract":[{"id":"0x5af0d9827e0c53e4799bb226655a1de152a425a5","eventType":["Transfer","Approval","ApprovalForAll"]}]}

Example 2. via javascript code

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

npm i ws

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

Client Side Sample Socket Code :

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":"events","event":"ERC721","contract":[{"id":"0x5af0d9827e0c53e4799bb226655a1de152a425a5","eventType":["Transfer","Approval","ApprovalForAll"]}]
  };
  ws.send(JSON.stringify(payload));
});


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

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

The payload can be modified, depending on the action.

Expected Output:

"address": "0x5Af0D9827E0c53E4799BB226655A1de152A425a5",
  "topics": [
    "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
    "0x00000000000000000000000029469395eaf6f95920e59f858042f0e28d98a20b",
    "0x0000000000000000000000003f0fb61b79ceea17dcf7a7f38446485c2fc10ed1",
    "0x00000000000000000000000000000000000000000000000000000000000015a8"
  ],
  "params": {
    "from": "0x29469395eAf6f95920E59F858042f0e28D98a20B",
    "to": "0x3f0FB61B79cEea17dcF7a7f38446485C2fC10ED1",
    "tokenId": "5544"
  },
  "data": "0x",
  "blockNumber": 17336136,
  "blockHash": "0xdc31e10f6dacb22456527e5812d8f866096111156094664226dfa0b6416c2d56",
  "logIndex": 341,
  "transactionHash": "0xbc2711cafb9ffe3f92aa05102490af821572cb4d16c33d5c9c132bbb704eb21c",
  "transactionIndex": 194

ERC-1155

The above filters can be applied in any combination, using the following syntax:

{"action": "events", "event":"ERC1155", "contract" : [{"id":"contractId", "eventType":["TransferSingle","TransferBatch","ApprovalForAll","URI"]}]}

Sample Query:

{"action":"events", "event":"ERC1155", "contract":[{"id":"0x7DaEC605E9e2a1717326eeDFd660601e2753A057","eventType":["TransferSingle","TransferBatch","ApprovalForAll","URI"]}]}

Example 2. via javascript code

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

npm i ws

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

Client Side Sample Socket Code :

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:"events",
  event:"ERC1155",
  contract:[
  {
  "id":"0x7DaEC605E9e2a1717326eeDFd660601e2753A057",
  eventType:["TransferSingle","TransferBatch","ApprovalForAll","URI"]
  }
  ]
  };
  ws.send(JSON.stringify(payload));
});


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

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

The payload can be modified, depending on the action.

Expected Output:

"address": "0x7DaEC605E9e2a1717326eeDFd660601e2753A057",
  "topics": [
    "0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62",
    "0x000000000000000000000000740c569f20076f1d96be1222240d55a5eed29df5",
    "0x000000000000000000000000740c569f20076f1d96be1222240d55a5eed29df5",
    "0x000000000000000000000000c7cddcc2b7c5868401744e75ede94dd56ed03970"
  ],
  "params": {
    "operator": "0x740C569F20076F1D96be1222240d55A5eED29Df5",
    "from": "0x740C569F20076F1D96be1222240d55A5eED29Df5",
    "to": "0xC7CDDcc2b7C5868401744E75EDE94dd56ED03970",
    "id": "65538",
    "value": "1"
  },
  "data": "0x00000000000000000000000000000000000000000000000000000000000100020000000000000000000000000000000000000000000000000000000000000001",
  "blockNumber": 17336254,
  "blockHash": "0xdaf226b629e93b547cf393021ce3c0c9d30f0e6b8de667a018aa5608a0a94687",
  "logIndex": 230,
  "transactionHash": "0x8c2912036f0bcb2cc65635d0e3fe53d329ccbcf11d1f205d0a40274fecbf8327",
  "transactionIndex": 124

How to disconnect:

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

Last updated