npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

connex-sync-event-fetch

v1.0.0

Published

Fetch the events from the Smart Contract in Backend synchronously.

Downloads

1

Readme

connex-sync-event-fetch Module

connex-sync-event-fetch Module is a module that implements server environment for syncing event fetching using connex in VeChain. This is most useful for backend developers who wants to fetch and handle events triggered in Smart Contract in VeChain. The backend server is based on Express JS + Node JS. Since it is working on VeChain, we recommend to use connex rather than Web3.js to connect to VeChain node.

How to implement

This module implements the following logic to update the attribute of metadata in Pinata.

  1. Get the latest Blocknumber that was previously saved in the internal db
  2. Use endless loop for fetching events and repeat it every 10 seconds or custom delay.
  3. In the loop, following logic is required.
    • Filter the events from the Chain with "from" and "to" parameters using connex.
    • Get the events within the range of "from" block and "to" block. Refer to the code in index.js.
    • Then loop the gained events array and use conditions to find a specific event occured.
    • Set custom delay of repeated loop.

Packages

As it works with connex to sync the backend and the VeChain node, it always works along with the package @vechain/connex-driver, @vechain/connex-framework.

npm i @vechain/connex-framework @vechain/connex-driver thor-devkit

Code Structure

To create a Framework instance:

import { Framework } from '@vechain/connex-framework'
import { Driver, SimpleNet } from '@vechain/connex-driver'

//Create a connex instance
const net = new SimpleNet('https://testnet.veblocks.net/');
const driver = await Driver.connect(net, wallet);
const connex = new Framework(driver)

// Get latest Block number in the Internal DB
let latestBlocknumber = await getHead(); 

for(;;){
    await new Promise(async (resolve,reject)=>{
        //Filter the events from the latest event fetched to the latest event triggered in the chain
        //latestBlocknumber: Latest Block number in the Internal DB
        //latestBlockNum: Latest Block number in Chain
        const filter = connex.thor.filter('event', [{ "address": SC_ADDRESS }]).range({ unit: "block", from: latestBlocknumber + 1, to: latestBlockNum });

        //Get events from the filter
        let offset = 0;
        let events = [];
        const decoder = newEventsDecoder(SC_ABI);
        for (; ;) {
            const newOutput = await filter.apply(offset, step).then(events => events.map(x => decoder.decode(x)));
            events = [...events, ...newOutput];
            if (newOutput.length == 0) {
            break;
            }
            offset += step
        }

        //Do actions
        for (let i = 0; i < events.length; i++) {
            console.log("New Event: ", events[i].event)
            if (events[i].event === "EventName") {
            //Do some actions when this event triggered. This action must include inserting event into internal DB so that next time you can fetch latest event from DB
            console.log("event happened");
            }
        }
        resolve();
    });

    //Create a Delay for block creation
    await new Promise((resolve) => {
        setTimeout(resolve, 10 * 1000)
    })
}

Usage / Installation

  • Installation
    
    npm i connex-sync-event-fetch
    
  • Usage
    
    var myBackend = require('connex-sync-event-fetch');
    myBackend.syncBackend();
      

Warning

This framework should be modified before using it. Insert your Smart Contract Address and ABI, Insert your logic to get events.

License

This package is licensed under the GNU Lesser General Public License v3.0, also included in LICENSE file in the repository.