Stream Gas

Stream Gas is a method used to obtain the gas fees given by a public address. (Currently Available for Ethereum Only)

Response Schema:

Field
Description

address

The public address that is subscribed for Gas Streaming

blockNumber

The BlockNumber where the Transaction Occured.

currentPrice

The Current Price for ETH (in USD).

gas

The Gas Object that consists of totalFeesSpent and the transactionList with the Current Price (in cents)

Streaming Gas:

Users can stream gas 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 needs 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": "streamGas"}

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:-

  • addresses

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

{"action":"streamGas","addresses":["address1" , "address2" , ...]}

Sample Query:

{"action":"streamGas","addresses":["0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990"]}

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: "streamGas",
    addresses: ["0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990"]
  };
  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:

"blockNumber": 17336355,
"currentPrice": "1799.59",
"gas": {
"totalFeesSpent": 41231312312212312,
"transactionList": {
"0x7a29adcfad36eb70c1fb1b7b15b7779ac347c41a758ac13bade5627d1afb3379": 41231312312212312
},
"totalCurrentPrice": "7480"
}}

How to disconnect:

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

Last updated