@0x/web3-providers-fork 中文文档教程

发布于 3年前 浏览 34 项目主页 更新于 3年前

web3-providers

这是 web3.js 的子模块。

Installation

npm install web3-providers

Usage Examples

HttpProvider

您可以通过选项对象传递超时和所有已知的 HTTP 标头。

```js 从 'web3-providers' 导入 {HttpProvider};

常量选项 = { 超时:20000, 标题:[ { 名称:“访问控制允许来源”,值:“*” }, …… ] };

const httpProvider = new HttpProvider('http://localhost:8545', options);

#### WebsocketProvider

Instead of setting a authorization header you could also define the credentials over the URL with:

ws://用户名:密码@localhost:8546```

``js 从 'web3-providers' 导入 {WebsocketProvider}; const credentials = Buffer.from('用户名:密码').toString('base64') 常量选项 = { 超时:30000, 标题:{ 授权:基本 ${credentials}` } };

const websocketProvider = new WebsocketProvider('ws://localhost:8546', options);

#### IpcProvider

js 从 'web3-providers' 导入 {IpcProvider}; 从“网络”导入网络;

const ipcProvider = new IpcProvider('/Users/me/Library/Ethereum/geth.ipc', net);

#### BatchRequest
The BatchRequest provides the possibility to send JSON-RPC requests as batch.
Please read the [documentation][docs] for more.

js 导入 {ProviderResolver, BatchRequest} 'web3-providers';

const provider = new ProviderResolver().resolve('ws://localhost:8546'); const batchRequest = new BatchRequest(provider);

batchRequest.add(web3.eth.getBalance.request( '0x0000000000000000000000000000000000000000', '最新的', 打回来 ));

等待 batchRequest.execute();

#### ProviderDetector
Checks if an provider is given from the environment (Mist, MetaMask) and returns the provider.

js 从 'web3-providers' 导入 {ProviderDetector};

const givenProvider = ProviderDetector.detect();

#### ProviderResolver
The ProviderResolver resolves an url or an given provider object to the correct provider class. 
Because of the resolves does web3 has internally just one provider interface and we have no direct dependency to third party providers.

js 导入 {ProviderResolver} 'web3-providers';

const socketProviderAdapter = new ProviderResolver().resolve('ws://localhost:8546'); ```

Types

所有打字稿类型都放在类型文件夹中。

web3-providers

This is a sub module of web3.js

Installation

npm install web3-providers

Usage Examples

HttpProvider

You can pass with the options object the timeout and all known HTTP headers.

```js import {HttpProvider} from 'web3-providers';

const options = { timeout: 20000, headers: [ { name: 'Access-Control-Allow-Origin', value: '*' }, … ] };

const httpProvider = new HttpProvider('http://localhost:8545', options);

#### WebsocketProvider

Instead of setting a authorization header you could also define the credentials over the URL with:

ws://username:password@localhost:8546```

``js import {WebsocketProvider} from 'web3-providers'; const credentials = Buffer.from('username:password').toString('base64') const options = { timeout: 30000, headers: { authorization:Basic ${credentials}` } };

const websocketProvider = new WebsocketProvider('ws://localhost:8546', options);

#### IpcProvider

js import {IpcProvider} from 'web3-providers'; import net from 'net';

const ipcProvider = new IpcProvider('/Users/me/Library/Ethereum/geth.ipc', net);

#### BatchRequest
The BatchRequest provides the possibility to send JSON-RPC requests as batch.
Please read the [documentation][docs] for more.

js import {ProviderResolver, BatchRequest} 'web3-providers';

const provider = new ProviderResolver().resolve('ws://localhost:8546'); const batchRequest = new BatchRequest(provider);

batchRequest.add(web3.eth.getBalance.request( '0x0000000000000000000000000000000000000000', 'latest', callback ));

await batchRequest.execute();

#### ProviderDetector
Checks if an provider is given from the environment (Mist, MetaMask) and returns the provider.

js import {ProviderDetector} from 'web3-providers';

const givenProvider = ProviderDetector.detect();

#### ProviderResolver
The ProviderResolver resolves an url or an given provider object to the correct provider class. 
Because of the resolves does web3 has internally just one provider interface and we have no direct dependency to third party providers.

js import {ProviderResolver} 'web3-providers';

const socketProviderAdapter = new ProviderResolver().resolve('ws://localhost:8546'); ```

Types

All the typescript typings are placed in the types folder.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文