# web3x
[![Version](https://img.shields.io/npm/v/web3x.svg)](https://www.npmjs.com/package/web3x)
[![Downloads](https://img.shields.io/npm/dw/web3x.svg)](https://www.npmjs.com/package/web3x)
[![Downloads](https://img.shields.io/npm/dw/web3x-es.svg)](https://www.npmjs.com/package/web3x-es)
[![GitHub Code Size](https://img.shields.io/github/languages/code-size/xf00f/web3x.svg)](https://github.com/xf00f/web3x)
[![GitHub Stars](https://img.shields.io/github/stars/xf00f/web3x.svg)](https://github.com/xf00f/web3x/stargazers)
[![GitHub Issues](https://img.shields.io/github/issues/xf00f/web3x.svg)](https://github.com/xf00f/web3x/issues)
[![Coverage](https://img.shields.io/coveralls/github/xf00f/web3x/v1.2.0.svg)](https://coveralls.io/github/xf00f/web3x)
[![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://github.com/xf00f/web3x/blob/master/LICENSE)
TypeScript port of web3.js - for perfect types and tiny builds.
![Demo](https://user-images.githubusercontent.com/44038056/47954060-9d6ef200-df7d-11e8-829c-99eb984f9bf1.gif)
## Table of contents
- [Why?](#why)
- [Usage](#usage)
- [Contract type safety](#contract-type-safety)
- [Differences](#differences)
- [Example projects](#example-projects)
- [Documentation](#documentation)
- [Packages](#packages)
## Why?
web3.js is a very popular Ethereum library, but:
- It has inaccurate typings and there's no way to to introduce type safety to contract code.
- It's large, weighing in at ~800k uncompressed.
web3x solves the above issues.
- It's pure TypeScript and generates contract types from ABIs.
- It's small, with a minimum sized contract interaction weighing in at ~150k uncompressed.
## Usage
There are two builds of the library. `web3x` uses CommonJS style imports and is best used for Node.js backends. `web3x-es` uses ES6 imports and is best used for ES6 aware tools like Webpack.
Example usage:
```typescript
import { WebsocketProvider } from 'web3x-es/providers';
import { Eth } from 'web3x-es/eth';
import { fromWei } from 'web3x-es/utils';
async function main() {
const provider = new WebsocketProvider('wss://mainnet.infura.io/ws');
const eth = Eth.fromProvider(provider);
const balance = await eth.getBalance('0x0000000000000000000000000000000000000000');
document.body.innerText = `Balance of 0 address ETH: ${fromWei(balance, 'ether')}`;
}
main().catch(console.error);
```
See example projects for more complex examples.
## Contract type safety
Interacting with contracts without type safety is tedious at best, and dangerous at worst. web3x provides a code generator called `web3x-codegen` to generate typings for contract ABIs either local, or remote from a simple configuration file called `contracts.json`.
### Defining contracts.json
An example `contracts.json` looks like:
```json
{
"outputPath": "./src/contracts",
"contracts": {
"DaiContract": "http://api.etherscan.io/api?module=contract&action=getabi&address=0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359&format=raw",
"MyContract": "../truffle-project/build/contracts/MyContract.json"
}
}
```
Run the code generator:
```
yarn web3x-codegen
```
The generator downloads the ABI for the DAI token from the given location on etherscan, and generates the interface at `./src/contracts/DaiContract.ts`. It also specifies a local ABI file and generates its interface at `./src/contracts/MyContract.ts`. The json files output by truffle are not pure ABIs, but the code generator will detect it's a truffle output and will extract the ABI accordingly.
For an example of the code generated, take a look at this [example](example-projects/node/src/contracts/DaiContract.ts).
### Using generated contracts
The following code demonstrates how to use the generated contract class. It's the exact same API as used in web3.js, only now with typesafety.
```typescript
import { fromWei } from 'web3x/utils';
import { WebsocketProvider } from 'web3x/providers';
import { Eth } from 'web3x/eth';
import { DaiContract } from './contracts/DaiContract';
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
const DAI_CONTRACT_ADDRESS = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359';
async function main() {
const provider = new WebsocketProvider('wss://mainnet.infura.io/ws');
const eth = Eth.fromProvider(provider);
try {
const contract = new DaiContract(eth, DAI_CONTRACT_ADDRESS);
const daiBalance = await contract.methods.balanceOf(ZERO_ADDRESS).call();
console.log(`Balance of 0 address DAI: ${fromWei(daiBalance, 'ether')}`);
} finally {
provider.disconnect();
}
}
main().catch(console.error);
```
## Differences
This is not a perfect drop in replacement for web3.js, there are small differences.
- Callbacks for request/response style calls no longer supported, promises only.
- You should explicitly import parts of the library rather then accessing them via the web3 object.
- Sanitized some hybrid types, e.g. access wallet accounts via `wallet.get(0)` rather than `wallet[0]`.
## Example projects
Two example TypeScript projects are included, one for [webpack](example-projects/webpack) and one for [node.js](example-projects/node). They are configured to work with jest for testing. Adapting them to pure JavaScript if you don't want to use TypeScript should be trivial.
## Documentation
API documentation has not yet been ported from web3.js. For now the recommended approach for familiarising yourself with the library would be the following, in preferential order:
- Read the web3.js documentation at https://web3js.readthedocs.io/en/1.0/ to familiarise yourself with its API.
- Take a look at the example projects such as the [webpack example](example-projects/webpack/src/index.ts).
- Rely on your IDE and TypeScript to provide insight into the API.
- Delve into the code. It's significantly easier to follow and understand than web3.js.
## Packages
- [web3x](https://www.npmjs.com/package/web3x) (for Node.js)
- [web3x-es](https://www.npmjs.com/package/web3x-es) (for ES6 aware tools such as Webpack)
没有合适的资源?快使用搜索试试~ 我知道了~
web3js的一个Typescript移植
共191个文件
ts:158个
json:16个
js:5个
需积分: 32 4 下载量 131 浏览量
2019-08-09
05:16:18
上传
评论
收藏 486KB ZIP 举报
温馨提示
web3.js的一个Typescript移植,web3.js是一个非常受欢迎的以太坊库。
资源推荐
资源详情
资源评论
收起资源包目录
web3js的一个Typescript移植 (191个子文件)
.gitignore 55B
.gitignore 34B
.gitignore 18B
index.html 104B
package.js 1KB
webpack.dev.js 673B
webpack.prod.js 423B
index.js 199B
babel.config.js 79B
resolver.json 6KB
abi.json 4KB
registry.json 3KB
package.json 2KB
package.json 1KB
package.json 571B
tsconfig.es.json 526B
tsconfig.cjs.json 513B
tsconfig.json 375B
tsconfig.json 300B
tsconfig.json 246B
contracts.json 201B
contracts.json 201B
contracts.json 132B
contracts.json 103B
nodemon.json 96B
LICENSE 7KB
yarn.lock 287KB
yarn.lock 193KB
yarn.lock 165KB
README.md 6KB
.prettierrc 73B
.prettierrc 72B
.prettierrc 72B
contract.test.ts 54KB
encode-parameters.test.ts 29KB
abi-coder.ts 29KB
decode-parameter.test.ts 29KB
decode-parameters.test.ts 28KB
encode-parameter.test.ts 25KB
sign-transaction.test.ts 24KB
eth.ts 15KB
index.ts 15KB
contract.ts 14KB
eth.test.ts 13KB
hash.ts 11KB
solidity-sha3.test.ts 11KB
index.ts 10KB
encode-event-abi.test.ts 10KB
DaiContractAbi.ts 9KB
DaiContractAbi.ts 9KB
decode-event-abi.test.ts 9KB
subscription.ts 9KB
confirm-transaction.ts 8KB
scrypt.ts 8KB
bytes.ts 8KB
eth-request-payloads.ts 8KB
wallet.test.ts 7KB
ipc.ts 7KB
iban.ts 7KB
decode-log.test.ts 7KB
solidity-sha3.ts 6KB
ws.ts 6KB
tx.ts 6KB
jsonrpc.test.ts 6KB
EnsResolverAbi.ts 6KB
bn.test.ts 5KB
index.ts 5KB
bloom.test.ts 5KB
utf8.ts 5KB
bignumber.ts 5KB
request-manager.ts 5KB
ens.ts 5KB
iban.test.ts 5KB
units-ethjs-unit.ts 5KB
index.ts 5KB
shh.ts 5KB
tx.test.ts 5KB
bytes.ts 5KB
sign.test.ts 5KB
encryption.test.ts 5KB
encryption.ts 5KB
address.ts 4KB
DaiContract.ts 4KB
DaiContract.ts 4KB
sign-transaction.ts 4KB
output-block-formatter.test.ts 4KB
TestContractAbi.ts 4KB
wallet.ts 4KB
hex.test.ts 4KB
shh-request-payloads.ts 4KB
rlp.ts 4KB
units.ts 4KB
tx-encode-abi.test.ts 4KB
tx-deploy.ts 4KB
errors.ts 4KB
properties.ts 4KB
hex-number.test.ts 4KB
http.ts 3KB
EnsResolver.ts 3KB
account.ts 3KB
共 191 条
- 1
- 2
资源评论
weixin_39840515
- 粉丝: 446
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功