ethjs-1-了解

https://github.com/ethjs/ethjs/blob/master/docs/user-guide.md

Install

npm install --save ethjs

Usage

const Eth = require('ethjs');
const eth = new Eth(new Eth.HttpProvider('https://ropsten.infura.io'));

eth.getBlockByNumber(45300, (err, block) => {
  // result null { ...block data... }
});

const etherValue = Eth.toWei(72, 'ether');

// result <BN ...>

const tokenABI = [{
  "constant": true,
  "inputs": [],
  "name": "totalSupply",
  "outputs":[{"name": "","type": "uint256"}],
  "payable": false,
  "type": "function",
}];

const token = eth.contract(tokenABI).at('0x6e0E0e02377Bc1d90E8a7c21f12BA385C2C35f78');

token.totalSupply().then((totalSupply) => {
  // result <BN ...>  4500000
});

一个高度优化,轻量级的用于以太坊的js

A highly optimised, light-weight JS utility for Ethereum based on web3.js, but lighter, async only and using BN.js.

Only 106 kB minified!

它与web3.js比的优点就是它更轻,都是异步处理并使用了BN.js

 ⚠️Note, bn.js "BN" is not the same as bignumber.js "BigNumber" used by web3.

 We use bn.js because it does not support any decimal numbers, and can manage absolute precision of large integers (this lib is also used by ethereumjs).

不支持小数,并且能够保证大数字的绝对精度

 When working with Ethereum number values, try to avoid or never use actual Number type values (i.e. value: 45038000000,) or decimal numbers (value: 1000.003). This may lead to incorrect values conversion, number precision loss or worse, all your or your users ether!

要避免直接使用数字或者小数,因为这样可能会导致不正确的数值转换,小数会丢失等

Try to always use BN Big Numbers or if you have to strings. ethjs will attempt to convert your type String number into a BN properly, however, the best way is to always provide a type Object BN instance (e.g. value: new Eth.BN('4000001'), instead of value: 4000001,).

当你使用数字时,写成new Eth.BN('4000001')格式

If you have to handle decimal amounts of value like ether (e.g. 4500.302 ether), simply convert the value down to weiusing the toWei method (e.g. Eth.toWei('4500.302', 'ether')) and then do your handling with BN.

如果使用了小数,就将其转成最小单位wei

Our Relationship with Ethereum & EthereumJS

We would like to mention that we are not in any way affiliated with the Ethereum Foundation. 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.

Notice/Warning

ethjs is still in development and is highly experimental. Use at your own risk. While we test everything as against standards, specifications and existing test cases (layed out by both the community and the Ethereum Foundation), this module is not ready for production use. More user testing is needed, so please, help out!

还在开发阶段

Modules

ethjs is made from a series of smaller modules:

接下来学习这些模块的使用

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.js or dist/ethjs.min.js directly into an HTML file to start using this module. Note, an Ethobject is made available globally.

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

Webpack Figures

Minified: 103 kB.

Hash: b267c64f72c936248871
Version: webpack 2.1.0-beta.15
Time: 928ms
       Asset    Size  Chunks             Chunk Names
    ethjs.js  235 kB       0  [emitted]  main
ethjs.js.map  291 kB       0  [emitted]  main
  [24] multi main 28 bytes {0} [built]
    + 24 hidden modules

Hash: b7b0fe38a80ebbca42e2
Version: webpack 2.1.0-beta.15
Time: 3373ms
       Asset    Size  Chunks             Chunk Names
ethjs.min.js  103 kB       0  [emitted]  main
  [24] multi main 28 bytes {0} [built]
    + 24 hidden modules

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/

 

Other Awesome Modules, Tools and Frameworks

Ethereum Foundation

  • web3.js -- the original Ethereum JS swiss army knife
  • ethereumjs -- critical ethereum javascript infrastructure/community
  • browser-solidity -- an in browser Solidity IDE
  • mists -- the official Ethereum wallet and browser

Nodes

  • geth Go-Ethereum (Ethereum Foundation)
  • parity Rust-Ethereum build in Rust (Ethcore)
  • testrpc JS-Testing Node (Ethereumjs)

Testing

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

Wallets

  • mist -- turns your browser into an Ethereum enabled browser =D
  • geth -- standard Ethereum wallet
  • parity -- standard Ethereum wallet
  • ethers-wallet -- an amazingly small Ethereum wallet
  • metamask -- turns your browser into an Ethereum enabled browser =D, a one click install Chrome Extention
  • eth-lightwallet -- an HD wallet built in Javascript
原文地址:https://www.cnblogs.com/wanghui-garcia/p/9958759.html