ethereumjs-connect
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!)
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>
To specify the connection endpoint, pass your RPC/IPC connection info to connector.connect
:
connector.connect({http: "http://localhost:8545"});
connector.connect({http: "http://localhost:8545", ws: "ws://localhost:8546"});
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
connector.coinbase
connector.connection
connector.contracts
connector.api
connector.debug
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");
Tests
ethereumjs-connect uses Mocha for automated testing.
$ npm test