# @zilliqa-js/core

> Core abstractions that power the zilliqa JS client.

Latest version **3.5.0** (published 2023-12-06) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @zilliqa-js/core
pnpm add @zilliqa-js/core
yarn add @zilliqa-js/core
bun add @zilliqa-js/core
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.5.0 |
| Published | 2023-12-06 |
| First published | 2018-10-31 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 428.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | lukozill, troelsfr, inamirandaz, zilliqateam, fluffypancake5436, zilliqaofficial |

## Links

- npm: https://www.npmjs.com/package/@zilliqa-js/core
- Repository: https://github.com/Zilliqa/zilliqa-developer
- Homepage: https://github.com/Zilliqa/zilliqa-developer#readme
- Issues: https://github.com/Zilliqa/zilliqa-js/issues
- npm.io page: https://npm.io/package/@zilliqa-js/core

## Dependencies (4)

- [mitt](https://npm.io/package/mitt.md) ^1.1.3
- [tslib](https://npm.io/package/tslib.md) 2.3.1
- [buffer](https://npm.io/package/buffer.md) ^6.0.3
- [cross-fetch](https://npm.io/package/cross-fetch.md) 2.2.6

## Recent versions

- 3.5.0 (latest) — 2023-12-06
- 2.0.0-alpha.1 (alpha) — 2020-10-07
- 0.2.2 (next) — 2018-11-15
- 3.4.4 — 2023-08-09
- 3.4.3 — 2023-06-05
- 3.4.3-rc.0 — 2023-06-01
- 3.4.2 — 2023-06-01
- 3.4.2-rc.0 — 2023-05-30
- 3.4.1 — 2023-05-26
- 3.4.0-rc.3 — 2023-05-26
- 3.4.0-rc.2 — 2023-05-25
- 3.4.0-rc.1 — 2023-05-25
- 3.4.0-rc.0 — 2023-05-25
- 3.4.0 — 2023-05-25
- 3.3.4 — 2022-02-03
- … 60 more at https://npm.io/package/@zilliqa-js/core/versions

## README

# @zilliqa-js/core

> Core abstractions required for interacting with the blockchain.

## Classes

### `BaseProvider`

Base class for concrete `Providers`.

#### `BaseProvider`

##### Parameters

- `nodeURL`: `string` - the URL of the lookup node to send requests to.
- `reqMiddleware`: `Map<Matcher, ReqMiddlewareFn[]>` - an ES6 `Map` of
  `Matcher`, `ReqMiddlewareFn[]` pairs.
- `reqMiddleware`: `Map<Matcher, ResMiddlewareFn[]>` - an ES6 `Map` of
  `Matcher`, `ResMiddlewareFn[]` pairs.

##### Returns

- `BaseProvider`

### Members

#### `middleware: { request: { use(fn: ReqMiddlewareFn, match: Matcher = '*') }, response: use(fn: ResMiddlewareFn, match: Matcher = '*') }`

An object that allows setting middleware on requests and responses. Middleware
allows fine-grained control over the request-reponse cycle.

Request middleware is called with details of the RPC request. Response
middleware, in addition to the response, is called with the originating request
object.

`Matcher` is either an RPC method, a regular expression, or the wildcard
matcher, the string `'*'`.

##### Example

In the following example, all requests sent through the module will
transparently JSON encode `CreateTransaction` requests in a format required by
the Zilliqa RPC server.

```typescript
// myMiddleware.js
// myMiddleware listens for CreateTransaction RPC requests, transforming
// `amount`, `gasLimit` and `gasPrice` to `string`, so that the RPC server will
// be able to process the transaction.
export function myMiddleware(req) {
  // This check is, in fact, not required if you make use of `Matcher`.
  if (
    req.payload.method === RPCMethod.CreateTransaction &&
    isTxParams(req.payload.params[0])
  ) {
    const txConfig = req.payload.params[0];

    const ret = {
      ...req,
      payload: {
        ...req.payload,
        params: [
          {
            ...txConfig,
            amount: txConfig.amount.toString(),
            gasLimit: txConfig.gasLimit.toString(),
            gasPrice: txConfig.gasPrice.toString(),
          },
        ],
      },
    };

    return ret;
  }

  return req;
}
```

```typescript
// myModule.js
import { myMiddleware } from './myMiddleware.js';

export class MyModule {
  // other code
  ...

  // use the middleware function. As `'CreateTransaction'` was passed as the
  // `Matcher`, myMiddleware will only be called on `CreateTransaction`
  // requests.
  constructor(provider: Provider) {
    this.provider = provider;
    this.provider.middleware.request.use(
      myMiddleware,
      'CreateTransaction',
    );
  }

  // other code
  ...
}

```

### `HTTPProvider`

Concrete `Provider`. Extends `BaseProvider`.

#### Instance methods

#### `send<P extends any[], R = any, E string>(method: RPCMethod, ...params: P): Promise<RPCResponse<R,E>>`

##### Parameters

- `method`: `RPCMethod` - a valid Zilliqa JSON-RPC method (`string`).
- `params`: `any[]` - an array of arbitrary parameters to send.

##### Returns

- `Promise<RPCResponse<R,E>>` - resolves with the reponse, or rejects with an
  error, if any.

## Decorators

### `sign`

Method decorator used to decorate methods whose _first_ argument is `Signable`,
i.e., have a `bytes` property.

#### Example

```typescript
  @sign
  async createTransaction(tx: Transaction): Promise<Transaction> {
    // `Transaction` satifies `Signable`.
    // As it is the first argument of `createTransaction`, `tx` is already
    // signed by the time `createTransaction` begins to execute.

    // code to send the transaction to the node or pass it on to another
    // method/function
  }
```

---
_Source: https://npm.io/package/@zilliqa-js/core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
