Socket
Socket
Sign inDemoInstall

@kodadot1/sub-api

Package Overview
Dependencies
3
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @kodadot1/sub-api

Smart wrapper for Substrate API


Version published
Weekly downloads
80
decreased by-2.44%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

sub-api

Substrate API with batteries included

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)
})

FAQs

Last updated on 24 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc