GithubHelp home page GithubHelp logo

openstfoundation / mosaic-anchor.js Goto Github PK

View Code? Open in Web Editor NEW
2.0 9.0 5.0 37 KB

Mosaic Anchor enables committing state-roots of complimentary chain.

License: GNU General Public License v3.0

JavaScript 100.00%

mosaic-anchor.js's Introduction

mosaic-anchor.js

Mosaic Anchor enables committing state-roots of complimentary chain.

Installation

Node.js

npm install mosaic-anchor.js

Creating a Job

  const MosaicAnchor = require('mosaic-anchor.js');
  let sourceWeb3 = new Web3('http://....');
  let destinationWeb3 = new Web3('http://....');;
  let address = "0x...";
  let worker = "0x...";
  let confirmations = 24;
  let anchorInterval = 1 * 60 * 1000; /* 1 minute */
  let oJob = new MosaicAnchor.Job(sourceWeb3, destinationWeb3, address, organization, confirmations, anchorInterval);

  
Parameter Description
sourceWeb3 Web3 object connected to source chain whose state needs to be anchored.
destinationWeb3 Web3 object connetced to destination chain where the state-root needs to be anchored.
address Address of Anchor contract.
organization Address of Organization contract owner.
confirmations Minimum no. of block confirmations needed before anchoring the state-root.
anchorInterval Minimum interval between two anchors.
iterations No. of times state-root should be commited by this job.

For complete mosaic setup, create 2 instances of Job. Make sure to add worker key has sufficient gas and is added to web3.eth.accounts.wallet.

Executing the job

let iterations = 10;
oJob.execute( iterations ).on('StateRootAvailable', function ( receipt ) {
  console.log('State root has been anchored. receipt', JSON.stringify(receipt, null, 2));
}).then(function () {
  console.log('Job completed.');
}).catch(function ( error ) {
  console.log("Something went wrong.", error);
})
Parameter Description
iterations No. of times state-root should be commited by this job.

Advanced Usage - Creating Anchor.

Anchor class gives developers complete control which enables them build their own logic for anchroing state-roots.

  const MosaicAnchor = require('mosaic-anchor.js');
  let sourceWeb3 = new Web3('http://....');
  let destinationWeb3 = new Web3('http://....');;
  let address = "0x...";
  let organization = "0x...";
  let confirmations = 24;
  let oAnchor = new MosaicAnchor.Anchor(sourceWeb3, destinationWeb3, address, organization, confirmations);

  //Validate Anchor Helper
  oAnchor.validate().then(function () {
    console.log('oAnchor is valid');
    //Do your task here.
  }).catch(function ( error ) {
    console.log('oAnchor is invalid. Error:', error);
  })

  //Get last anchored state-root block height
  oAnchor.getLatestStateRootBlockHeight().then(function ( blockHeight ) {
    console.log('blockHeight', blockHeight);
  });

  //Anchor state-root.
  oAnchor.anchorStateRoot();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

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

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.