Giter Site home page Giter Site logo

neo-js's Introduction

City of Zion logo

neo-js

A package for running a node on the neo blockchain.

Build Status npm version

Overview

The neo-js package is designed to interface with the Neo blockchain in a number of different ways that are configured by options that are used to initialize a node. A few examples of these different interaction mechanics are defined in the quickstart below as well as in the examples.

note: All blockchain events (Invocation and Deploy) use the RPC calls to interface with the blockchain unless they can be run locally (sometimes referred to as a 'test invoke')

A note on local storage

Currently this module only support MongoDB for synchronizing the blockchain. Future storage types are planning including other NoSQL databases, SQL databases, and in-memory solutions.

This module uses 'lazy caching' to improve performance when using local storage. Blocks are initially downloaded and stored in three collections (blocks, transactions, and addresses) as a result of the sync process. Upon the first request for a specific transaction (as a result of any number of the methods), the transaction will be expanded as described in Neon wallet architecture and updated in the collection. The next time the block is requested, the expanded transaction will already be available in the collection.

This mechanic is also used for address balances. Upon requesting an update for an asset balance, the transaction collection is analyzed and the asset balance is stored in an account collection along with the max block height during the calculation. Upon future requests for the asset balance, the asset collection is first queried for previous balance. The asset balance is then updated using only the new blocks since the previous calculation event.

System Recommendations

  • NodeJS 8+
  • MongoDB 3.0+

Installation

Install the package using:

$ npm install --save @cityofzion/neo-js

Alternatively, to access to the latest available code, you can reference to the git repository directly:

$ npm install --save CityOfZion/neo-js#develop

note: neo-js requires that MongoDB server is installed to run the instance as a full node. Installation instructions can be found in MongoDB installation manual.

Quick Start

const Neo = require('@cityofzion/neo-js')

To create a new blockchain instance:

// Create a neo instances to interface with RPC methods
const testnetNeo = new Neo({ network: 'testnet' })

// Wait for mesh to be ready before attempt to fetch block information
testnetNeo.mesh.on('ready', () => {
  testnetNeo.mesh.rpc('getBlock', 1000)
    .then((res) => console.log('Testnet getBlock(1000).hash:', res.hash))
})

// To connect to the mainnet:
const mainnetNeo = new Neo({ network: 'mainnet' })

mainnetNeo.mesh.on('ready', () => {
  mainnetNeo.mesh.rpc('getBlock', 1000)
    .then((res) => console.log('Mainnet getBlock(1000).hash:', res.hash))
})

This will create a new node instance and configure it to sync the blockchain to the defined mongoDB collections:

const options = {
  network: 'testnet',
  storageOptions: {
    model: 'mongoDB'
  }
}

// Create a neo instance
const neo = new Neo(options)

// Get block count
neo.storage.getBlockCount()
  .then((res) => console.log('Block count:', res))

Documentation

Documentation for the project can be found at:

Self-documented code examples are available as part of the project source code:

Contribution

neo-js always encourages community code contribution. Before contributing please read the contributor guidelines and search the issue tracker as your issue may have already been discussed or fixed. To contribute, fork neo-js, commit your changes and submit a pull request.

By contributing to neo-js, you agree that your contributions will be licensed under its MIT license.

License

neo-js's People

Contributors

hmcq6 avatar iryusa avatar rockacola avatar slipo avatar thomaslobker avatar tudorconstantin avatar

Watchers

 avatar  avatar

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.