i’m trying to listen to the ethereum chain for transactions on my usdt erc20 wallet address, using node.js and web3. Whenever usdt is sent into my wallet address, i intend to log the amount recieved in realtime.
This is my code:
<code>const {Web3} = require('web3');
const USDT_ABI = require('erc-20-abi');
//My QuickNode WSS endpoint
const wssProviderUrl = 'wss://thrilling-orbital-lake.quiknode.pro/********/';
// Initialize Web3 with WebSocket provider
const web3Client = new Web3(new Web3.providers.WebsocketProvider(wssProviderUrl));
// My wallet address
const yourAddress = '0x***********************';
// USDT contract address on Ethereum
const USDTContractAddress = '0xdAC17F958D2ee523aeeE4C20C7C1dE6e8FcB49aC';
/// Initialize the USDT contract
const USDTContract = new web3Client.eth.Contract(USDT_ABI, USDTContractAddress);
// Subscribe to Transfer events to your address
USDTContract.events.Transfer({ filter: { to: yourAddress } })
.on('data', (event) => {
const amount = event.returnValues.value;
console.log(`Received ${web3Client.utils.fromWei(amount, 'mwei')} USDT to your wallet address!`);
})
.on('error', (error) => {
console.error('Error subscribing to event:', error);
});
</code>
<code>const {Web3} = require('web3');
const USDT_ABI = require('erc-20-abi');
//My QuickNode WSS endpoint
const wssProviderUrl = 'wss://thrilling-orbital-lake.quiknode.pro/********/';
// Initialize Web3 with WebSocket provider
const web3Client = new Web3(new Web3.providers.WebsocketProvider(wssProviderUrl));
// My wallet address
const yourAddress = '0x***********************';
// USDT contract address on Ethereum
const USDTContractAddress = '0xdAC17F958D2ee523aeeE4C20C7C1dE6e8FcB49aC';
/// Initialize the USDT contract
const USDTContract = new web3Client.eth.Contract(USDT_ABI, USDTContractAddress);
// Subscribe to Transfer events to your address
USDTContract.events.Transfer({ filter: { to: yourAddress } })
.on('data', (event) => {
const amount = event.returnValues.value;
console.log(`Received ${web3Client.utils.fromWei(amount, 'mwei')} USDT to your wallet address!`);
})
.on('error', (error) => {
console.error('Error subscribing to event:', error);
});
</code>
const {Web3} = require('web3');
const USDT_ABI = require('erc-20-abi');
//My QuickNode WSS endpoint
const wssProviderUrl = 'wss://thrilling-orbital-lake.quiknode.pro/********/';
// Initialize Web3 with WebSocket provider
const web3Client = new Web3(new Web3.providers.WebsocketProvider(wssProviderUrl));
// My wallet address
const yourAddress = '0x***********************';
// USDT contract address on Ethereum
const USDTContractAddress = '0xdAC17F958D2ee523aeeE4C20C7C1dE6e8FcB49aC';
/// Initialize the USDT contract
const USDTContract = new web3Client.eth.Contract(USDT_ABI, USDTContractAddress);
// Subscribe to Transfer events to your address
USDTContract.events.Transfer({ filter: { to: yourAddress } })
.on('data', (event) => {
const amount = event.returnValues.value;
console.log(`Received ${web3Client.utils.fromWei(amount, 'mwei')} USDT to your wallet address!`);
})
.on('error', (error) => {
console.error('Error subscribing to event:', error);
});
But i keep getting this error:
<code> .on('error', (error) => {
^
TypeError: Cannot read properties of undefined (reading 'on')
</code>
<code> .on('error', (error) => {
^
TypeError: Cannot read properties of undefined (reading 'on')
</code>
.on('error', (error) => {
^
TypeError: Cannot read properties of undefined (reading 'on')
How can i fix this please?
I expect my code to log the transaction details whenever i receive tokens.
New contributor
Anything figures is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.