Skip to content

Instantly share code, notes, and snippets.

@a2468834
Created June 20, 2022 14:07
Show Gist options
  • Save a2468834/841f94177b9ec946b17680e5ae88595d to your computer and use it in GitHub Desktop.
Save a2468834/841f94177b9ec946b17680e5ae88595d to your computer and use it in GitHub Desktop.
// Import
const Web3 = require("web3");
// Constants
const USDC_mainnet = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const Provider_URL = "wss://eth-mainnet.alchemyapi.io/v2/";
async function eventSubscribe() {
const web3 = new Web3(Provider_URL);
var subscription = web3.eth.subscribe(
"logs", {
address: USDC_mainnet,
topic: [web3.utils.keccak256("Transfer(address,address,uint256)")]
}, (error, result) => {
if(error)
console.error(error);
else
console.log(result);
}
).on(
"connected", (subscriptionId) => {
console.log("Subscription ID:", subscriptionId);
}
).on(
"data", (log) => {
console.log("----------------------------------------");
console.log(log);
}
);
// subscription.unsubscribe(
// (error, success) => {
// if(success)
// console.log("Un-subscription status:", success);
// }
// );
}
// Main function
async function main() {
await eventSubscribe();
}
main()
.then(() => {
//process.exit(EXIT_SUCCESS); // DO NOT LEAVE THIS LINE UN-COMMENTED
})
.catch((error) => {
console.error(error);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment