How to Implement and Deploy a Smart Contract Event Listener with AWS CDK

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • smart-contract-event-listener

    A smart contract event listener implemented in Node.js, using Web3.js, and deployed on AWS using AWS CDK.

  • Before we start, make sure to check or clone the sample repository, which contains all the necessary code and files for the smart contract event listener. We'll refer to this repository throughout the guide.

  • jsbi

    JSBI is a pure-JavaScript implementation of the official ECMAScript BigInt proposal.

  • import 'dotenv/config'; import * as erc20abi from './abi.json'; import Web3, { Contract, WebSocketProvider } from 'web3'; /* Workaround for JSON.stringify() event logs with BigInt values. We need to stringify event logs for more readable logging in CloudWatch. https://github.com/GoogleChromeLabs/jsbi/issues/30 */ (BigInt.prototype as any).toJSON = function () { return this.toString(); }; /** * Starts the smart contract event listener. * Websocket Provider config: https://docs.web3js.org/api/web3-providers-ws/class/WebSocketProvider * @param chain - Name of the blockchain network for logging purposes. * @param wssEndpoint - Websocket endpoint for the blockchain network. * @param contractAddress - Smart contract address. */ const startEventListener = async (chain: string, wssEndpoint: string, contractAddress: string) => { const provider = new WebSocketProvider( wssEndpoint, {}, { autoReconnect: true, delay: 10000, // Default: 5000 ms maxAttempts: 10, // Default: 5 }, ); provider.on('connect', () => { console.log(`Connected to ${chain} websocket provider`); }); provider.on('disconnect', error => { console.error(`Closed ${chain} webSocket connection`, error); }); const web3 = new Web3(provider); /* Smart contract event listeners Listening to events: - Transfer - Approval */ const contract = new web3.eth.Contract(erc20abi, contractAddress); await subscribeToEvent(chain, contract, 'Transfer'); await subscribeToEvent(chain, contract, 'Approval'); }; /** * Subscribes to a smart contract event. * @param chain - Name of the blockchain network for logging purposes. * @param contract - Smart contract address. * @param eventName - Name of the event to subscribe to. */ const subscribeToEvent = async (chain: string, contract: Contract, eventName: string) => { const subscription = await contract.events[eventName](); subscription.on('connected', subscriptionId => { console.log(`${chain} USDT '${eventName}' SubID:`, subscriptionId); }); subscription.on('data', event => { console.log(`${chain} USDT '${eventName}'`, JSON.stringify({ event })); // cannot json.stringify BigInt... }); subscription.on('changed', event => { // Remove event from local database }); subscription.on('error', error => { console.error(`${chain} USDT '${eventName}' error:`, error); }); }; /* Start smart contract event listeners Chains: - Ethereum */ startEventListener('Ethereum', process.env.ETH_WSS_ENDPOINT!, process.env.ETH_SMART_CONTRACT_ADDRESS!);

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • TypeScript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • JavaScript, TypeScript, and Node.js

  • Docker Swarm

    Source repo for Docker's Documentation (by docker)

  • Docker

  • aws-cdk

    The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code

  • Now that you have a working smart contract event listener, we'll deploy the resources to AWS using AWS CDK, which is an Infrastructure as Code (IaC) tool. AWS CDK allows you to configure, deploy, and manage AWS cloud resources using popular programming languages such as TypeScript.

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts