Developer Fundamentals

3. Contract Whispers

Subscribe to smart contract events (e.g., VTHO Transfer) via filters.

Prerequisites

Contract Event Streams

Contracts whisper events—listen up! 👂 Use SDK for targeted streams.

In this example we are using VTHO contract address (0x0000000000000000000000000000456e65726779).

import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';
// Fetch ABI: await fetch('https://raw.githubusercontent.com/vechain/b32/master/ABIs/energy.json')
const wsUrl = subscriptions.getEventLogsSubscriptionUrl('https://mainnet.vechain.org', {
  address: '0x0000000000000000000000000000456e65726779',  // VTHO
  topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef']  // Transfer topic0
});
const ws = new WebSocket(wsUrl);
ws.onmessage = (msg) => {
  const logs = JSON.parse(msg.data);
  console.log('VTHO Transfer:', logs);  // Decode with ABIEvent
};
import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';
// Fetch ABI: await fetch('https://raw.githubusercontent.com/vechain/b32/master/ABIs/energy.json')
const wsUrl = subscriptions.getEventLogsSubscriptionUrl('https://mainnet.vechain.org', {
  address: '0x0000000000000000000000000000456e65726779',  // VTHO
  topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef']  // Transfer topic0
});
const ws = new WebSocket(wsUrl);
ws.onmessage = (msg) => {
  const logs = JSON.parse(msg.data);
  console.log('VTHO Transfer:', logs);  // Decode with ABIEvent
};
import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';
// Fetch ABI: await fetch('https://raw.githubusercontent.com/vechain/b32/master/ABIs/energy.json')
const wsUrl = subscriptions.getEventLogsSubscriptionUrl('https://mainnet.vechain.org', {
  address: '0x0000000000000000000000000000456e65726779',  // VTHO
  topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef']  // Transfer topic0
});
const ws = new WebSocket(wsUrl);
ws.onmessage = (msg) => {
  const logs = JSON.parse(msg.data);
  console.log('VTHO Transfer:', logs);  // Decode with ABIEvent
};

Multi-connect: Open separate WS for events + blocks. No polling needed!
Ears open!​

PRO TIP

No ABI? Can't decode—fetch from b32.

Another perk for you, the topic you find in the example above is common to ALL ERC20 tokens!