Giter VIP home page Giter VIP logo

node-launcher's Introduction

Node Launcher

Programmatically create, start, and stop crypto nodes in Node JS using Docker. Check out our video demo.

Getting Started

Install Docker

https://docs.docker.com/engine/install/

Clone the library

$ git clone https://github.com/rBurgett/da-node-runner.git
$ cd da-node-runner

Build the library

$ npm install
$ npm run build

Try it out!

// Start a mainnet node
const nr = require('[path to da-node-runner]/lib');

const bitcoin = new nr.BTC({});

// Listen for events
bitcoin
  .on(nr.constants.NodeEvent.OUTPUT, console.log)
  .on(nr.constants.NodeEvent.ERROR, console.error)
  .on(nr.constants.NodeEvent.CLOSE, code => console.log(`Exited with code ${code}.`));

// Start node
bitcoin.start();
// Start a mainnet node and stop it after thirty seconds
const nr = require('[path to da-node-runner]/lib');

const bitcoin = new nr.BTC({});

// Listen for events
bitcoin
  .on(nr.constants.NodeEvent.OUTPUT, console.log)
  .on(nr.constants.NodeEvent.ERROR, console.error)
  .on(nr.constants.NodeEvent.CLOSE, code => console.log(`Exited with code ${code}.`));

// Start node
bitcoin.start();

setTimeout(() => {
  bitcoin.stop()
    .then(() => console.log('Stopped!'))
    .catch(console.error);
}, 30000);
// Start a testnet node
const nr = require('[path to da-node-runner]/lib');
const { NetworkType } = nr.constants;

const bitcoin = new nr.BTC({
  network: NetworkType.TESTNET
});

// Listen for events
bitcoin
  .on(nr.constants.NodeEvent.OUTPUT, console.log)
  .on(nr.constants.NodeEvent.ERROR, console.error)
  .on(nr.constants.NodeEvent.CLOSE, code => console.log(`Exited with code ${code}.`));

// Start node
bitcoin.start();
// Start a testnet node and persist data
const fs = require('fs-extra');
const path = require('path');
const nr = require('[path to da-node-runner]/lib');
const { NetworkType } = nr.constants;

const rootPath = path.join(process.env.HOME, 'my-bitcoin-node');
const nodeJsonPath = path.join(rootPath, 'node.json');

let bitcoin;
if(fs.pathExistsSync(nodeJsonPath)) { // start using previous configuration and data

  bitcoin = new nr.BTC(fs.readJsonSync(nodeJsonPath));
  
} else { // start and persist configuration and data

  // Create local data paths
  const dataDir = path.join(rootPath, 'data');
  const walletDir = path.join(rootPath, 'wallets');
  [rootPath, dataDir, walletDir].forEach(dir => fs.ensureDirSync(dir));
  const configPath = path.join(rootPath, 'bitcoin.config');

  // Create node
  bitcoin = new nr.BTC({
    id: 'my-bitcoin-node',
    network: NetworkType.TESTNET,
    dataDir,
    walletDir,
    configPath
  });
  // Persist node data
  fs.writeJsonSync(nodeJsonPath, bitcoin.toObject(), {spaces: 2});
}

// Listen for events
bitcoin
  .on(nr.constants.NodeEvent.OUTPUT, console.log)
  .on(nr.constants.NodeEvent.ERROR, console.error)
  .on(nr.constants.NodeEvent.CLOSE, code => console.log(`Exited with code ${code}.`));

// Start node
bitcoin.start();
// use the rpcGetVersion() method to get the node client version and test that the RPC server is live
const nr = require('[path to da-node-runner]/lib');

const bitcoin = new nr.BTC({});

// Listen for events
bitcoin
  .on(nr.constants.NodeEvent.OUTPUT, console.log)
  .on(nr.constants.NodeEvent.ERROR, console.error)
  .on(nr.constants.NodeEvent.CLOSE, code => console.log(`Exited with code ${code}.`));

// Start node
bitcoin.start();

// get the client version from the live RPC server
setTimeout(() => {
  bitcoin.rpcGetVersion()
    .then(console.log)
    .catch(console.error);
}, 10000);

Roadmap

  • Add support for Ethereum, Pocket, and many other chains
  • Use event listeners rather than callbacks to subscribe to node output and errors
  • Create standard methods for getting memory usage and other metadata including block height and sync status
  • Publish comprehensive API docs
  • Reach 100% test coverage

Contributions

Contributions are welcome! If you have any issues and/or contributions you would like to make, feel free to file an issue and/or issue a pull request.

License

Apache License Version 2.0

Copyright (c) 2021 by Ryan Burgett.

node-launcher's People

Contributors

rburgett avatar itsnoproblem avatar eman0011 avatar moatus 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.