ethereumjs-connect automates a few basic Ethereum network connection tasks: looks up the network ID, the coinbase address, sets the from
field for transaction objects, and (optionally) will setup functions and events APIs for use with ethrpc. For examples of contracts and API inputs, see augur-contracts. (Important note: the static API setup in ethereumjs-connect is not yet compatible with web3!)
$ 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>
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"});
// 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", ipc: ipcpath});
If the last argument provided to connector.connect
is a function, it will connect asynchronously:
connector.connect({http: "https://eth3.augur.net", ws: "ws://ws.augur.net"}, function (connected) { ... });
After connecting, several network properties are attached to the connector
object:
connector.networkID
connector.from // sets the "from" address for outgoing transactions
connector.coinbase
connector.connection // connection info
connector.contracts // contract addresses
connector.api // static API data (for use with ethrpc transactions)
connector.debug // display debugging logs
connector.from
is used to set the from
field for outgoing transactions. By default, it is set to the coinbase address. However, you can manually set it to something else (for example, for client-side transactions):
connector.setFrom("0x05ae1d0ca6206c6168b42efcd1fbe0ed144e821b");
ethereumjs-connect uses Mocha for automated testing.
$ npm test