Skip to content

Abhay-2811/Docker_ContractEventSubscribe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Subscribe to ERC20 Blockchain Events using public Docker Container

The goal of this docker image is to run a websocket locally that listens to samrt contract event ( ERC20 Transfer Event ), and polls the logs. It can be used to integrate with frontend where transaction data of a token is required.

To use this service follow given steps:

  1. Pull the docker image

     docker pull devabhay28/blockchain-eventlistener
    
    
  2. [ Important ] In the working directory create a .env file and paste your ws rpc url (get one from alchemy for free) make sure you're using same network for both RPC url and Contract deployment

    Env Example:

     RPC_URL=<ws-rpc-url>
     CONTRACT_ADD=<contract-address>
    
  3. Run docker in same dir, here -dp take is used to map a local port (6000, localhost port where websocket will run) to docker port (3000, dont change this)

    
     docker run -dp 6000:3000 --env-file .env devabhay28/blockchain-eventlistener
    
    
  4. Now that the docker is running and websocket is ready. You can use packages such as ws to listen to transactions. Below is an example client code snippet

     const WebSocket = require("ws");
     
     const ws = new WebSocket("ws://localhost:6000");
     
     ws.on("open", () => {
       console.log("Connected to WebSocket server");
     });
     
     ws.on("message", (message) => {
       const jsonString = message.toString();
       const eventData = JSON.parse(jsonString);
       console.log("New transaction:", eventData);
     });
    
    

    Give repo a star if it was helpful ♥︎

About

Subscribe to ERC20 Transfer events using docker container

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published