openethereum/js/src/api
2017-07-13 14:37:35 +02:00
..
contract Don't fail on wrong log decoding (#5813) 2017-06-13 18:36:51 +03:00
format Do not convert to Dates twice (#5563) 2017-05-09 12:55:52 +02:00
local Empty local middleware, until explicitly requested (#5912) 2017-06-23 13:40:16 +02:00
pubsub PubSub for parity-js (#5830) 2017-07-06 09:50:27 +02:00
rpc PubSub for parity-js (#5830) 2017-07-06 09:50:27 +02:00
subscriptions Fix wrongly called timeouts 2017-06-14 12:35:36 +02:00
transport Fix lint/build issue (#6050) 2017-07-13 14:37:35 +02:00
util CR fixes 2017-04-04 11:49:36 +02:00
api.js PubSub for parity-js (#5830) 2017-07-06 09:50:27 +02:00
api.spec.js Ignoring some methods in test. 2017-06-13 16:47:58 +02:00
index.js Fix whitespace (#4299) 2017-01-25 18:51:41 +01:00
README.md Updated docs slightly. (#5674) 2017-05-23 08:37:27 -04:00

ethapi-js

A thin, fast, low-level Promise-based wrapper around the Ethereum APIs.

Build Status Coverage Status Dependency Status devDependency Status

contributing

Clone the repo and install dependencies via npm install. Tests can be executed via

  • npm run testOnce (100% covered unit tests)
  • npm run testE2E (E2E against a running RPC-enabled testnet Parity/Geth instance, parity --testnet and for WebScokets, geth --testnet --ws --wsorigins '*' --rpc)
  • setting the environment DEBUG=true will display the RPC POST bodies and responses on E2E tests

installation

Install the package with npm install --save ethapi-js from the npm registry ethapi-js

usage

initialisation

// import the actual EthApi class
import EthApi from 'ethapi-js';

// do the setup
const transport = new EthApi.Transport.Http('http://localhost:8545');  // or .Ws('ws://localhost:8546')
const ethapi = new EthApi(transport);

You will require native Promises and fetch support (latest browsers only), they can be utilised by

import 'isomorphic-fetch';

import es6Promise from 'es6-promise';
es6Promise.polyfill();

making calls

perform a call

ethapi.eth
  .coinbase()
  .then((coinbase) => {
    console.log(`The coinbase is ${coinbase}`);
  });

multiple promises

Promise
  .all([
    ethapi.eth.coinbase(),
    ethapi.net.listening()
  ])
  .then(([coinbase, listening]) => {
    // do stuff here
  });

chaining promises

ethapi.eth
  .newFilter({...})
  .then((filterId) => ethapi.eth.getFilterChanges(filterId))
  .then((changes) => {
    console.log(changes);
  });

contracts

attach contract

const abi = [{ name: 'callMe', inputs: [{ type: 'bool', ...}, { type: 'string', ...}]}, ...abi...];
const contract = new ethapi.newContract(abi);

deploy

contract
  .deploy('0xc0de', [params], 'superPassword')
  .then((address) => {
    console.log(`the contract was deployed at ${address}`);
  });

attach a contract at address

// via the constructor & .at function
const contract = api.newContract(abi).at('0xa9280...7347b');
// or on an already initialised contract
contract.at('0xa9280...7347b');
// perform calls here

find & call a function

contract.instance
  .myContractMethodName
  .call({}, [myContractMethodParameter]) // or estimateGas or sendTransaction
  .then((result) => {
    console.log(`the result was ${result}`);
  });

parse events from transaction receipt

contract
  .parseTransactionEvents(txReceipt)
  .then((receipt) => {
    receipt.logs.forEach((log) => {
      console.log('log parameters', log.params);
    });
  });

apis

APIs implement the calls as exposed in the Ethcore JSON Ethereum RPC definitions. Mapping follows the naming conventions of the originals, i.e. eth_call becomes eth.call, personal_accounts becomes personal.accounts, etc.

As a verification step, all exposed interfaces are tested for existing and pointing to the correct endpoints by using the generated interfaces from the above repo.