Introduction
A smart contract emits an event on interaction.
You can use web3.js and the WebSocket connection to subscribe to events and receive them in real time.
This tutorial will guide you through subscribing to a smart contract event.
As an example, you will use the Transfer event of Chainlink.
Step-by-step
Get the address of the smart contract whose events you want to listen to. For example, Chainlink.
Each event type emitted by a smart contract is identified as an event signature. An event signature is Keccak-256 hashed as topic[0].
Each smart contract interaction has topic[0] identifying the type of event that occurred on the interaction.
To get the topic[0] hash of an event signature:
- You can get the event signature and use an online converter. For example, the most common event type is Transfer. The event signature is Transfer(address,address,uint256). The event signature converts to the topic[0] value: 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
- An easier way is to find your contract on Etherscan and check the contract's Events tab. All topic[0] values represent events. For example, Chainlink events.
Now that you have both the contract address and the hashed value of the event you want to subscribe to, create and run a Node.js instance:
const Web3 = require('web3');
const url = 'WSS_ENDPOINT';
const web3 = new Web3(url);
var options = {
address: 'CONTRACT',
topics: [
'TOPIC0'
]
};
var subscription = web3.eth.subscribe('logs', options, function(error, result){
if (!error) console.log('got result');
else console.log(error);
}).on("data", function(log){
console.log('got data', log);
}).on("changed", function(log){
console.log('changed');
});
where
- WSS_ENDPOINT — your node WSS endpoint.
- CONTRACT — the address of the contract whose events you subscribe to.
- TOPIC0 — the Keccak-256 value of the event.
For example, subscribing to the Transfer event of the Chainlink smart contract:
const Web3 = require('web3');
const url = 'wss://ws-nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531';
const web3 = new Web3(url);
var options = {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
]
};
var subscription = web3.eth.subscribe('logs', options, function(error, result){
if (!error) console.log('got result');
else console.log(error);
}).on("data", function(log){
console.log('got data', log);
}).on("changed", function(log){
console.log('changed');
});
You are now subscribed to the events and will receive them in real time as they are emitted.
You can also add the reconnect option to make sure you are always connected. See also web3.js reconnect documentation.
const Web3 = require('web3');
const url = 'wss://ws-nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531';
const web3 = new Web3(url);
var options = {
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
},
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
]
};
var subscription = web3.eth.subscribe('logs', options, function(error, result){
if (!error) console.log('got result');
else console.log(error);
}).on("data", function(log){
console.log('got data', log);
}).on("changed", function(log){
console.log('changed');
});
Comments
0 comments
Article is closed for comments.