# @kodadot1/sub-api

> Smart wrapper for Substrate API

Latest version **0.4.0-rc.2** (published 2024-09-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @kodadot1/sub-api
pnpm add @kodadot1/sub-api
yarn add @kodadot1/sub-api
bun add @kodadot1/sub-api
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.4.0-rc.2 |
| Published | 2024-09-20 |
| First published | 2022-01-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Viki Val |
| Maintainers | vikiival, yangwao |

## Links

- npm: https://www.npmjs.com/package/@kodadot1/sub-api
- Repository: https://github.com/kodadot/packages
- Homepage: https://github.com/kodadot/packages#readme
- Issues: https://github.com/kodadot/packages/issues
- npm.io page: https://npm.io/package/@kodadot1/sub-api

## Dependencies (1)

- [@kodadot1/static](https://npm.io/package/@kodadot1/static.md) ^0.0.6

## Recent versions

- 0.4.0-rc.2 (latest) — 2024-09-20
- 0.4.0-rc.1 — 2024-09-20
- 0.4.0-rc.0 — 2024-09-20
- 0.3.2-rc.0 — 2024-06-10
- 0.3.1-rc.0 — 2024-04-23
- 0.3.0-rc.0 — 2024-01-24
- 0.2.0-rc.0 — 2023-08-24
- 0.1.2 — 2023-05-11
- 0.1.1 — 2023-04-17
- 0.1.1-alpha.5 — 2023-01-12
- 0.1.1-alpha.4 — 2022-12-14
- 0.1.1-alpha.3 — 2022-09-26
- 0.1.1-alpha.1 — 2022-09-14
- 0.1.0-alpha.3 — 2022-07-25
- 0.1.0-alpha.2 — 2022-07-25
- … 6 more at https://npm.io/package/@kodadot1/sub-api/versions

## README

<h1 align="center">sub-api</h1>

<h5 align="center">Substrate API with batteries included</h5>

## Installation

`yarn add @kodadot1/sub-api`

## Docs

### Why this?

In KodaDot, we need to manage the connection to the network of multiple parachain. This is a complicated task, and we need a simple way to do it. The old implementation was easy to use but not easy to manage.

That's why we decided to create a new implementation that is more robust and suitable for the other Dotsama builders.

### What's in the box?

**Factory**

The Factory handles a single connection per each parachain.
To directly connect to a parachain, just call.

```ts
import { ApiFactory } from '@kodadot1/sub-api'
const endpoint = 'wss://kusama-rpc.polkadot.io'
const api = await ApiFactory.useApiInstance(endpoint)
```

**InstaApi**

Feeling a bit adventurous? You can use the `InstaApi` and feel free to manage the connection directly.
Suitable also for an app with a single connection.

```ts
import { InstantApi as Api } from '@kodadot1/sub-api'
const endpoint = 'wss://kusama-rpc.polkadot.io'
const API = await (new API(endpoint)).getInstance()
```

**Helpers**

There is a set of helpers to make your life easier.

```ts
import { balanceOf, currentBlock } from '@kodadot1/sub-api'

const API = await ApiFactory.useApiInstance(endpoint)
const balance = await balanceOf(API, acc)
const block = await currentBlock(API)
console.log(balance, block)
```

**Hooks**

On API Connect hook is available.

```ts
import { onApiConnect } from '@kodadot1/sub-api'
```

**Types**

There is just a set of more readable types.

```ts
export type Extrinsic = SubmittableExtrinsic<'promise'>
```

## Usage Examples

Import Factory object like

```ts
import { ApiFactory } from '@kodadot1/sub-api'
```

Get an API instance for the selected URL and fetch the data in the component.

```ts
async function getChainProperties() {
  const endpoint = 'wss://kusama-rpc.polkadot.io'
  const API = await ApiFactory.useApiInstance(endpoint)
  const { chainSS58, chainDecimals, chainToken } = API.registry
  console.log('[API] Connect <3', { chainSS58, chainDecimals, chainToken })
}
```

Alternatively, you can listen to the `connect` event and get the data only when it is ready.

```ts
import { onApiConnect } from '@kodadot1/sub-api'
```

```ts
const endpoint = 'wss://kusama-rpc.polkadot.io'
onApiConnect(endpoint, async (API) => {
  const block = await API.query.system.number()
  console.log('[API] Connected to', endpoint, 'block', block)
})
```

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