Skip to content

Repository files navigation

@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/exportdefaultdefineConfig({// ...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'construn=async()=>{awaitinit({})console.log(utils.address.normalizeSubstrateAddress('yGHGXr2qCKygrxFw16XXEYRLmQwQt8RN8eMN5UuuJ17ZFPosP'))constchain=newSubstrate.Unique()awaitchain.connect(`wss://quartz.unique.network`)console.log(chain.ss58Prefix)console.log(chain.coin.format(1_500_000_000_000_000_000n))console.log(awaitchain.getChainProperties())awaitchain.disconnect()}run().catch(err=>console.error(err))

Initializing

Initializing with Polkadot extension enabling (works only in browser)

import{init}from'@unique-nft/api'awaitinit({connectToPolkadotExtensionsAs: 'my app'})

or, another way to do the same:

import{init,Substrate}from'@unique-nft/api'awaitinit()awaitSubstrate.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'awaitinit()constchain=newSubstrate.Unique()awaitchain.connect(WS_RPC.quartz)constkeyring=Substrate.signer.keyringFromSeed('electric suit...')constresult=awaitchain.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'constquartz=newSubstrate.Unique()constkusama=newSubstrate.Common()awaitinit({connectToPolkadotExtensionsAs: 'my app'})// we can create instances before init // but connect must be invoked only after init callawaitquartz.connect(WS_RPC.quartz)awaitkusama.connect(WS_RPC.kusama)constaccounts=awaitSubstrate.extension.getAllAccounts()constaccount=accounts[0]// or, better option take some specific account// accounts.find(account => account.address === '5...')constKSMTransfer=awaitkusama.transferCoins({...}).signAndSend(account)constQTZTransfer=awaitquartz.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:

constresult=awaitquartz.transferCoins({toAddress: '5...',amountInWei: 1n}).signAndSend(keyringOrAccount)

More verbose example:

constquartz=newSubstrate.Unique()consttx=quartz.transferCoins({toAddress: '5...',amountInWei: 1n})awaittx.sign(keyringOrAccount)console.log(tx.getRawTx().toJSON())constresult=awaittx.send()

All params have typings which can be imported this way:

import{SubstrateMethodsParams}from'@unique-nft/api'constparams: 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

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages