Skip to content

Latest commit

 

History

History
214 lines (167 loc) · 12.3 KB

user-guide.md

File metadata and controls

214 lines (167 loc) · 12.3 KB

User Guide

All information for developers using ethjs-query should consult this document.

Install

npm install --save ethjs-query

Usage

const HttpProvider = require('ethjs-provider-http');
const Eth = require('ethjs-query');
const eth = new Eth(new HttpProvider('http://localhost:8545'));

eth.getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', cb);

// result null <BigNumber ...>

eth.sendTransaction({
  from: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
  to: '0x987d73d8a49eeb85d32cf462207dd71d50710033',
  value: new BN('29384'),
  gas: 3000000,
  data: '0x',
}).then(cb).catch(cb);

// result null 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470

Debugging Options

ethjs-query comes equip with a full debug options for all data inputs and outputs.

const HttpProvider = require('ethjs-provider-http');
const Eth = require('ethjs-query');
const eth = new Eth(new HttpProvider('http://localhost:8545'), { debug: true, logger: console, jsonSpace: 0 });

eth.accounts(cb);

/* result
[ethjs-query 2016-11-27T19:37:54.917Z] attempting method accounts with params [null]
[ethjs-query 2016-11-27T19:37:54.917Z] [method 'accounts'] callback provided: true
[ethjs-query 2016-11-27T19:37:54.917Z] [method 'accounts'] attempting input formatting of 0 inputs
[ethjs-query 2016-11-27T19:37:54.917Z] [method 'accounts'] formatted inputs: []
[ethjs-query 2016-11-27T19:37:54.917Z] [method 'accounts'] attempting query with formatted inputs...
[ethjs-query 2016-11-27T19:37:54.919Z] [method 'accounts'] callback success, attempting formatting of raw outputs: ["0xb88643569c19d05dc67b960f91d9d696eebf808e","0xf...]
[ethjs-query 2016-11-27T19:37:54.919Z] [method 'accounts'] formatted outputs: ["0xb88643569c19d05dc67b960f91d9d696eebf808e","0xf...]
*/

Amorphic Data Formatting

ethjs-query uses the ethjs-format module to format incoming and outgoing RPC data payloads. The primary formatting task is numbers. Number values can be inputed as: BigNumber, BN, string, hex or actual numbers. Because the blockchain does not support decimal or negative numbers, any kind of decimal or negative number will cause an error return. All received number values are returned as BN.js object instances.

Read more about the formatting layer here: ethjs-format

Async Only

All methods are async only, requiring either a callback or promise. ethjs-query supports both callbacks and promises for all RPC methods.

Error handling

Error handling is done through function callbacks or promised catches.

Supported Methods

ethjs-query supports all Ethereum spec RPC methods. Note, all eth RPC methods are attached as methods to the Eth object without the eth_ prefix. All other methods (e.g. web3_, net_ and ssh_ etc.) require the full RPC method name (note, this policy may change in the future).

Why BN.js?

ethjs has made a policy of using BN.js across all of its repositories. Here are some of the reasons why:

  1. lighter than alternatives (BigNumber.js)
  2. faster than most alternatives, see benchmarks
  3. used by the Ethereum foundation across all ethereumjs repositories
  4. is already used by a critical JS dependency of many ethereum packages, see package elliptic
  5. purposefully does not support decimals or floats numbers (for greater precision), remember, the Ethereum blockchain cannot and will not support float values or decimal numbers.

Browser Builds

ethjs provides production distributions for all of its modules that are ready for use in the browser right away. Simply include either dist/ethjs-query.js or dist/ethjs-query.min.js directly into an HTML file to start using this module. Note, an Eth object is made available globally.

<script type="text/javascript" src="ethjs-query.min.js"></script>
<script type="text/javascript">
new Eth(...);
</script>

Note, even though ethjs should have transformed and polyfilled most of the requirements to run this module across most modern browsers. You may want to look at an additional polyfill for extra support.

Use a polyfill service such as Polyfill.io to ensure complete cross-browser support: https://polyfill.io/

Latest Webpack Figures

Hash: e5b1a721ec5ba5bc55c9
Version: webpack 2.1.0-beta.15
Time: 891ms
             Asset    Size  Chunks             Chunk Names
    ethjs-query.js  177 kB       0  [emitted]  main
ethjs-query.js.map  222 kB       0  [emitted]  main
   [5] ./lib/index.js 4.45 kB {0} [built]
    + 14 hidden modules

Version: webpack 2.1.0-beta.15
Time: 2786ms
             Asset     Size  Chunks             Chunk Names
ethjs-query.min.js  79.4 kB       0  [emitted]  main
   [5] ./lib/index.js 4.45 kB {0} [built]
    + 14 hidden modules

Other Awesome Modules, Tools and Frameworks

Foundation

  • web3.js -- the original Ethereum JS swiss army knife Ethereum Foundation
  • ethereumjs -- critical ethereum javascript infrastructure Ethereum Foundation
  • browser-solidity -- an in browser Solidity IDE Ethereum Foundation

Nodes

  • geth Go-Ethereum
  • parity Rust-Ethereum build in Rust
  • testrpc Testing Node (ethereumjs-vm)

Testing

  • wafr -- a super simple Solidity testing framework
  • truffle -- a solidity/js dApp framework
  • embark -- a solidity/js dApp framework
  • dapple -- a solidity dApp framework
  • chaitherium -- a JS web3 unit testing framework
  • contest -- a JS testing framework for contracts

Wallets

  • ethers-wallet -- an amazingly small Ethereum wallet
  • metamask -- turns your browser into an Ethereum enabled browser =D

Our Relationship with Ethereum & EthereumJS

We would like to mention that we are not in any way affiliated with the Ethereum Foundation or ethereumjs. However, we love the work they do and work with them often to make Ethereum great! Our aim is to support the Ethereum ecosystem with a policy of diversity, modularity, simplicity, transparency, clarity, optimization and extensibility.

Many of our modules use code from web3.js and the ethereumjs- repositories. We thank the authors where we can in the relevant repositories. We use their code carefully, and make sure all test coverage is ported over and where possible, expanded on.