Developer Fundamentals

-- II. VET Transfers

Learn how to use WebSockets and filters to listen to live VET transfers on VeChainThor and decode the transfer data.

Listening to VET Transfers

This lesson will take you through the most important information about how to listen to specific VET transfers and how to decode the transfer details.

Similar to other listening methods, you'll first need to establish a direct connection with a VeChain node.

The connection is managed using WebSockets, which connect directly to a VeChain node.

How To Establish a Direct Connection

A simple connection can be established with this snippet:

import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';

const wsUrl = subscriptions.getVETtransfersSubscriptionUrl(
  'https://testnet.vechain.org'
);

const ws = new WebSocket(wsUrl);

ws.onopen = () => {
  console.log('Connected to', wsUrl);
};

This will receive all transfers on the blockchain as JSON-encoded strings.

The VeChain SDK facilitates the construction of filters to retrieve only the desired data by generating a subscription URL with specified parameters.

Using the subscriptions helper, you can create a custom subscription URL that listens exclusively to the specified event.

All options are optional, and a transfer must match all specified criteria:

const wsUrl = subscriptions.getVETtransfersSubscriptionUrl(
  'https://mainnet.vechain.org',
  {
    blockID: undefined, // block id to start from, defaults to the best block.
    signerAddress: undefined, // The address of the signer/origin of the transaction to filter transfers by.
    sender: undefined, // The sender address to filter transfers by.
    recipient: undefined, // The recipient address to filter transfers by.
  }
);

The transfers are received as JSON-encoded strings. These strings must be parsed into usable objects, resulting in an object of type TransferLogs.

An example result is:

{
  "sender": "0xff5ba88a17b2e16d23ff6647e9052e937acb1406",
  "recipient": "0xf1663a96eb4760d74a3084636b25eac161c238ed",
  "amount": "0xa7b7a750ac2f0400",
  "meta": {
    "blockID": "0x0117dfcc79e6cfd0002a99aca8af0123cf1b1f3c67df7d3eb1e22aaf286088b2",
    "blockNumber": 18341836,
    "blockTimestamp": 1713946150,
    "txID": "0xb5dc022265321a2bc3aef9faf9224544b0ff54fcaf1d4f5846fc6caee0187009",
    "txOrigin": "0xff5ba88a17b2e16d23ff6647e9052e937acb1406",
    "clauseIndex": 0
  },
  "obsolete": false
}

The amount is a hexlified BigInt that can be converted into a BigInt using BigInt(transferLog.amount).

Try It Yourself

The snippet below listens to VET Transfers from a specified sender address. Try adding other filters and inspect the different responses.

import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';

const wsUrl = subscriptions.getVETtransfersSubscriptionUrl(
  'https://testnet.vechain.org',
  {
    sender: '0x84ecd03eaE54bbd014A0753F6679782fD355cD28'
  }
);

const ws = new WebSocket(wsUrl);

ws.onopen = () => {
  console.log('Connected to', wsUrl);
};

ws.onmessage = (message) => {
  const transfer = JSON.parse(message.data);
  console.log('New Transfer', transfer);
};