npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@unique-nft/api

v0.1.13

Published

Definitely typed JS API for the Unique Network blockchain

Downloads

379

Readme

@unique-nft/api

Definitely typed JS API for the Unique Network

Install

yarn add @unique-nft/api
npm install @unique-nft/api

Since this project requires the BigInt support, there may be needed some additional bundler settings.

For example, you may need to add the following to your tsconfig.json file:

"compilerOptions" {
...
"target": "es2020",
...
}

For Vite, you can do the same. Example of vite.config.ts:

import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
  // ...
  build: {
    target: 'es2020'
  }
})

One more thing before you start, please make sure that you delete the ^ symbol in the package.json file. This is needed to avoid compatibility conflicts. The version of the library must be without this symbol:

"dependencies": {
    "@unique-nft/api": "0.0.7",

Quick Start

Feel free to execute the code below to check some library features.

import {init, Substrate, utils} from '@unique-nft/api'

const run = async () => {
  await init({})

  console.log(utils.address.normalizeSubstrateAddress('yGHGXr2qCKygrxFw16XXEYRLmQwQt8RN8eMN5UuuJ17ZFPosP'))

  const chain = new Substrate.Unique()
  await chain.connect(`wss://quartz.unique.network`)

  console.log(chain.ss58Prefix)
  console.log(chain.coin.format(1_500_000_000_000_000_000n))

  console.log(await chain.getChainProperties())

  await chain.disconnect()
}
run().catch(err => console.error(err))

Initializing

Initializing with Polkadot extension enabling (works only in browser)

import {init} from '@unique-nft/api'

await init({connectToPolkadotExtensionsAs: 'my app'})

or, another way to do the same:

import {init, Substrate} from '@unique-nft/api'

await init()
await Substrate.extension.connectAs('my app')

Signer

An extrinsic may be signed with keyring as well as with an account from the Polkadot extension.

With keyring (available in both browser and Node.js):

import {Substrate, init, WS_RPC} from '@unique-nft/api'

await init()

const chain = new Substrate.Unique()
await chain.connect(WS_RPC.quartz)

const keyring = Substrate.signer.keyringFromSeed('electric suit...')

const result = await chain.transferCoins({toAddress: "5C...", amountInWei: 1_500_000_000_000_000_000n}).signAndSend(keyring)

With the polkadot extension (available in browser only):

import {Substrate, WS_RPC} from '@unique-nft/api'

const quartz = new Substrate.Unique()
const kusama = new Substrate.Common()

await init({connectToPolkadotExtensionsAs: 'my app'})

// we can create instances before init 
// but connect must be invoked only after init call
await quartz.connect(WS_RPC.quartz)
await kusama.connect(WS_RPC.kusama)

const accounts = await Substrate.extension.getAllAccounts()
const account = accounts[0]
// or, better option take some specific account
// accounts.find(account => account.address === '5...')

const KSMTransfer = await kusama.transferCoins({...}).signAndSend(account)

const QTZTransfer = await quartz.transferCoins({...}).signAndSend(account)

Note: in case of Substrate.Unique.transferCoins (but not for Substrate.Common),
we can pass both substrate address (starting from 5 in normal form) and an Ethereum address (starting from 0x...).

Unique's transferCoins (and other functions where it makes sense) can accept any address - substrate or ethereum.


Extrinsics

Substrate class provides methods which take extrinsic parameters and return an Extrinsic instance.

Example:

const result = await quartz
  .transferCoins({toAddress: '5...', amountInWei: 1n})
  .signAndSend(keyringOrAccount)

More verbose example:

const quartz = new Substrate.Unique()
const tx = quartz.transferCoins({toAddress: '5...', amountInWei: 1n})
await tx.sign(keyringOrAccount)
console.log(tx.getRawTx().toJSON())
const result = await tx.send()

All params have typings which can be imported this way:

import {SubstrateMethodsParams} from '@unique-nft/api'

const params: SubstrateMethodsParams.TransferCoins = {
  toAddress: '5...',
  amountInWei: 1n
}

Extrinsic instance fields and methods:

  • async sign(signer: KeyringPair | InjectedAccountWithMeta) - returns it's instance
  • async send() - returns extrinsic result
  • async signAndSend(signer: KeyringPair | InjectedAccountWithMeta) - returns extrinsic result
  • isSigned - boolean
  • getRawTx() - returns SubmittableExtrinsic object