Skip to content

Instantly share code, notes, and snippets.

@afope
Last active April 28, 2020 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afope/cae14f5af9a13774f97464496538a113 to your computer and use it in GitHub Desktop.
Save afope/cae14f5af9a13774f97464496538a113 to your computer and use it in GitHub Desktop.
Listening to events
coconst Web3 = require('web3');
const username = 'YOUR_USERNAME_HERE';
const password = 'YOUR_PASSWORD_HERE';
const url = 'YOUR_URL_HERE'; // make sure to strip the wss:// prefix!
const wsSecURL = 'wss://' + username + ':' + password + '@' + url;
const web3ws = new Web3(new Web3.providers.WebsocketProvider(wsSecURL));
const tether_abi = require('./tether_abi.json'); // you can copy the ABI for the Tether contract here: https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code
const contractAddress = "0xdAC17F958D2ee523a2206206994597C13D831ec7"; // tether contract address, check here: https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7
var tetherContract = new web3ws.eth.Contract(tether_abi, contractAddress);
console.log('starting listener');
tetherContract.events.allEvents()
.on('data', (event) => {
console.log(event)
// store event to local database or present to client
})
.on('changed', (event) => {
console.log(event)
// i.e. remove event from local database or remove for client state
})
.on('error', (error) => {
// something went wrong
console.log(error);
});
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment