> For the complete documentation index, see [llms.txt](https://confluxnetwork.gitbook.io/js-conflux-sdk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://confluxnetwork.gitbook.io/js-conflux-sdk/api/baseprovider.md).

# Provider

### BaseProvider

**Kind**: global class

* [BaseProvider](#BaseProvider)
  * [new BaseProvider(\[options\])](#new_BaseProvider_new)
  * [.requestId()](#BaseProvider+requestId) ⇒ `string`
  * [.request(data)](#BaseProvider+request) ⇒ `Promise.<*>`
  * [.call(method, ...params)](#BaseProvider+call) ⇒ `Promise.<*>`
  * [.send(method, \[params\])](#BaseProvider+send) ⇒ `Promise.<*>`
  * [.batch(array)](#BaseProvider+batch) ⇒ `Promise.<Array>`

#### new BaseProvider(\[options])

| Param                | Type      | Default   | Description                             |
| -------------------- | --------- | --------- | --------------------------------------- |
| \[options]           | `object`  |           |                                         |
| options.url          | `string`  |           | Full json rpc http url                  |
| \[options.timeout]   | `number`  | `30*1000` | Request time out in ms                  |
| \[options.retry]     | `number`  | `1`       | Retry number                            |
| \[options.keepAlive] | `boolean` | `false`   | Whether open the http keep-alive option |
| \[options.logger]    | `object`  |           | Logger with `info` and `error`          |

#### baseProvider.requestId() ⇒ `string`

Gen a random json rpc id. It is used in `call` method, overwrite it to gen your own id.

**Kind**: instance method of [`BaseProvider`](#BaseProvider)<br>

#### baseProvider.request(data) ⇒ `Promise.<*>`

Call a json rpc method with params

**Kind**: instance method of [`BaseProvider`](#BaseProvider)\
**Returns**: `Promise.<*>` - Json rpc method return value.

| Param          | Type     | Description             |
| -------------- | -------- | ----------------------- |
| data           | `object` |                         |
| data.method    | `string` | Json rpc method name.   |
| \[data.params] | `array`  | Json rpc method params. |

**Example**

```js
> await provider.request({method: 'cfx_epochNumber'});
> await provider.request({method: 'cfx_getBlockByHash', params: [blockHash]});
```

#### baseProvider.call(method, ...params) ⇒ `Promise.<*>`

Call a json rpc method with params

**Kind**: instance method of [`BaseProvider`](#BaseProvider)\
**Returns**: `Promise.<*>` - Json rpc method return value.

| Param     | Type          | Description             |
| --------- | ------------- | ----------------------- |
| method    | `string`      | Json rpc method name.   |
| ...params | `Array.<any>` | Json rpc method params. |

**Example**

```js
> await provider.call('cfx_epochNumber');
> await provider.call('cfx_getBlockByHash', blockHash);
```

#### baseProvider.send(method, \[params]) ⇒ `Promise.<*>`

Send a json rpc method request

**Kind**: instance method of [`BaseProvider`](#BaseProvider)\
**Returns**: `Promise.<*>` - Json rpc method return value.

| Param     | Type     | Description             |
| --------- | -------- | ----------------------- |
| method    | `string` | Json rpc method name.   |
| \[params] | `array`  | Json rpc method params. |

**Example**

```js
> await provider.send('cfx_epochNumber');
> await provider.send('cfx_getBlockByHash', [blockHash]);
```

#### baseProvider.batch(array) ⇒ `Promise.<Array>`

Batch call json rpc methods with params

**Kind**: instance method of [`BaseProvider`](#BaseProvider)

| Param | Type             | Description                                |
| ----- | ---------------- | ------------------------------------------ |
| array | `Array.<object>` | Array of object with "method" and "params" |

**Example**

```js
> await provider.batch([
  { method: 'cfx_epochNumber' },
  { method: 'cfx_getBalance', params: ['cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp'] },
  { method: 'InValidInput' },
])
   [ '0x3b734d', '0x22374d959c622f74728', RPCError: Method not found ]
```
