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:

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

猜你喜欢

转载自www.cnblogs.com/wanghui-garcia/p/9958759.html
今日推荐