0.3.1-rc.0 • Published 17 days ago

@kodadot1/sub-api v0.3.1-rc.0

Weekly downloads
-
License
MIT
Repository
github
Last release
17 days ago

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.

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.

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.

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.

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

Types

There is just a set of more readable types.

export type Extrinsic = SubmittableExtrinsic<'promise'>

Usage Examples

Import Factory object like

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

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

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.

import { onApiConnect } from '@kodadot1/sub-api'
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)
})
0.3.1-rc.0

17 days ago

0.3.0-rc.0

4 months ago

0.2.0-rc.0

9 months ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.1-alpha.4

1 year ago

0.1.1-alpha.5

1 year ago

0.1.1-alpha.1

2 years ago

0.1.1-alpha.3

2 years ago

0.1.0-alpha.1

2 years ago

0.0.1-rc.4

2 years ago

0.1.0-alpha.3

2 years ago

0.1.0-alpha.2

2 years ago

0.0.1-rc.3

2 years ago

0.0.1-rc.2

2 years ago

0.0.1-rc.1

2 years ago

0.0.1-rc.0

2 years ago