Coder Social home page Coder Social logo

ethereumjs-connect's Introduction

ethereumjs-connect

Build Status Coverage Status npm version

ethereumjs-connect handles basic Ethereum network connection tasks: it gets the network ID, selects the right set of contracts for you (by default, it uses the Augur contracts), looks up the coinbase address, etc.

Usage

$ npm install ethereumjs-connect

To use ethereumjs-connect in Node.js, simply require it:

var connector = require("ethereumjs-connect");

A minified, browserified file dist/ethereumjs-connect.min.js is included for use in the browser. Including this file attaches a connector object to window:

<script src="dist/ethereumjs-connect.min.js" type="text/javascript"></script>

ethereumjs-connect can connect to an Ethereum node, which can be either remote (hosted) or local. To specify the connection endpoint, pass your RPC/IPC connection info to connector.connect:

// Connect with only HTTP RPC support
connector.connect({http: "http://localhost:8545"});

// Connect to a local node using HTTP (on port 8545) and WebSockets (on port 8546)
connector.connect({http: "http://localhost:8545", ws: "ws://localhost:8546"});

After connecting, several network properties are attached to the connector object:

connector.network_id
connector.from       // sets "from" for outgoing transactions
connector.coinbase
connector.connection
connector.contracts  // contracts (indexed by network ID)
connector.tx         // ethrpc transaction objects
connector.debug      // turns on debugging logs

connector.from is used to set the from field for outgoing transactions. By default, it is set to the coinbase address. However, if you manually set it to something else (for example, for client-side transactions):

connector.from = "0x05ae1d0ca6206c6168b42efcd1fbe0ed144e821b";
connector.connect({http: "http://localhost:8545"});

To connect to a remote Ethereum node, pass its address to connector.connect. For example, to connect to one of Augur's public nodes:

connector.connect({http: "https://eth3.augur.net", ws: "wss://ws.augur.net"});

The argument to connector.connect can also contain an ipc field, which is the path to geth's IPC (inter-process communication) file. IPC creates a persistent connection using a Unix domain socket (named pipe on Windows). This is significantly faster than HTTP RPC, but requires access to the filesystem, so it cannot be used from the browser.

For example, if your Ethereum data directory is ~/.ethereum, then to connect to a local Ethereum node with IPC support:

var ipcpath = require("path").join(process.env.HOME, ".ethereum", "geth.ipc");
connector.connect({http: "http://localhost:8545", ipcpath: ipcpath});

(Note: the .ethereum/testnet subdirectory has its own geth.ipc file which must be used to connect to geth via IPC on the Morden testnet.)

If the last argument provided to connector.connect is a function, it will connect asynchronously. This is recommended, especially for in-browser use! For example:

connector.connect({http: "https://eth3.augur.net", ws: "ws://ws.augur.net"}, function (connected) {
    // fun times may be had here
});

Tests

ethereumjs-connect uses Mocha for unit tests. To run them:

$ npm test

ethereumjs-connect's People

Contributors

tinybike 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.