Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chain-syncer

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chain-syncer

chain-syncer is a JS module which allows you to synchronize your app with any ethereum-compatible blockchain/contract state. Fast. Realtime. Reliable.

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
decreased by-90.91%
Maintainers
1
Weekly downloads
 
Created
Source

Chain Syncer

Chain Syncer is a JS module which allows you to synchronize your app with any ethereum-compatible blockchain/contract state. Fast. Realtime. Reliable.


Install

Works only with ethers package, so don't forget to install it:

$ npm i chain-syncer ethers

Example usage

Using Ethers package:

const { ChainSyncer, InMemoryAdapter } = require('chain-syncer');

const default_adapter_which_you_need_to_change_to_any_other = new InMemoryAdapter();

const ethersjs_provider = new Ethers.providers.JsonRpcProvider('https://data-seed-prebsc-1-s1.binance.org:8545'); // BSC testnet rpc

const contracts = {
  'Items': {
    abi: [ /* ... */ ],
    network: {
      address: '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56', 
      deployed_transaction: '0x0f01fc521030f178115c880e200b09a40c9510f49de227aa880276f92670a3d6'
    }
  }
}

const syncer = new ChainSyncer(default_adapter_which_you_need_to_change_to_any_other, {

  tick_interval: 3000,
  
  query_block_limit: 1000,
  
  verbose: true,
  
  block_time: 3500,
  
  ethers_provider: ethersjs_provider,
  
  async contractsGetter(contract_name) {
    const contract = contracts[contract_name];
    return {
      inst: new Ethers.Contract(contract.network.address, contract.abi, ethersjs_provider),
      deployed_transaction_hash: contract.network.deployed_transaction,
    };
  },
});

syncer.start();

syncer.on('Items.Transfer#default-stream', async (
  from, 
  to, 
  token_id, 
  { global_index, from_address, block_number, block_timestamp, transaction_hash }
) => {

  // global_index is a uniq id of event which is created from block number and logIndex padded with zeros

  const item = await Item.findOne({ _id: token_id });

  if(!item) { // postpone until item created
    return false;
  }

  item.owner = to;
  item.updatedAt = new Date(block_timestamp * 1000);
  await item.save();
  
  // Best practise is doing something like that, so you are sure that you have the latest state of 'owner' field
  //
  // await Item.updateOne({
  //   _id: token_id,
  //   'syncdata.owner': { $lt: global_index }
  // }, {
  //   'syncdata.owner': global_index,
  //   owner: to,
  // })
  
  // you can notify user that he has new items
}));


API

constructor(adapter, options)

NameDescriptionRequiredExample
adapterAn adapter interface for storing event data. As a test you can use built-in InMemoryAdapter, but better build your own adapter to any DB.requirednew InMemoryAdapter()
optionsAn options objectrequired-
options.tick_intervaldeterminates how often will process unprocessed eventsoptional (default: 2000)3000 (every 3 seconds)
options.query_block_limitMaximum amount of blocks that can be scanned per tick. For example official BSC RPC allows up to 2000 blocks per request.optional (default: 100)2000
options.query_unprocessed_events_limitMaximum amount of events that can be scanned per tickoptional (default: 100)5000
options.verboseA flag which enables debug mode and loggingoptional (default: false)true
options.modeModule mode. Possible: 'events' or 'processing' or 'universal'. 'events' mode only scans events without processing. 'processing' mode is only processing new events. 'universal' doing both.optional (default: 'universal')'processing'
options.block_timeBlock time of a network you are working with. For example 3500 for BSC.required3500 (BSC network)
options.ethers_providerEthers.js providerrequirednew Ethers.providers.JsonRpcProvider('https://data-seed-prebsc-1-s1.binance.org:8545')
options.contractsGetterAn async function that returns object with ethers.js contract instance and tx hash of its deployrequiredasync () => ({ inst: new Ethers.Contract(contracts[contract_name].network.address, contracts[contract_name].abi, ethersjs_provider), deployed_transaction_hash: contracts[contract_name].network.deployed_transaction })

on(stream_name, listener)

NameDescriptionRequiredExample
stream_nameSteam name is a string which contains contract, event and stream id (actually just id of this listener if you have microservices for example)required'Items.Transfer#default-stream'
listenerListener function, last argument is always object of event parameters. If false returned from listener - event will be postponed till next processing tickrequiredasync ({ global_index, from_address, block_number, block_timestamp, transaction_hash }) => { ... }

start()

Starts scanner and processor


License

Chain Syncer is released under the MIT license. © 2022 Miroslaw Shpak

Keywords

FAQs

Package last updated on 01 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc