Skip to content

Feature rich and user friendly typescript package to interact with the Polymesh blockchain

License

Notifications You must be signed in to change notification settings

PolymeshAssociation/polymesh-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

semantic-release js-semistandard-style Types npm License Coverage Github Actions Workflow Sonar Status Issues

@polymeshassociation/polymesh-sdk

Polymesh version

This release is compatible with Polymesh v7

Getting Started

Purpose

The Polymesh SDK's main goal is to provide external developers with a set of tools that will allow them to build powerful applications that interact with the Polymesh protocol. It focuses on abstracting away all the complexities of the Polymesh blockchain and expose a simple but complete interface. The result is a feature-rich, user-friendly JS library.

Technical Pre-requisites

In order to use the Polymesh SDK, you must install node (version 18+ recommended) and npm. The library is written in typescript, but can also be used in plain javascript. This document will assume you are using typescript, but the translation to javascript is very simple.

Documentation

Polymesh SDK API Reference:

https://developers.polymesh.network/sdk-docs/

How to use

Installation

npm i @polymeshassociation/polymesh-sdk --save

Or, if you're using yarn

yarn add @polymeshassociation/polymesh-sdk

Or, if using pnpm

pnpm add @polymeshassociation/polymesh-sdk

NOTE it is highly recommended that you use one of these three package managers. This project uses package resolutions/overrides to pin certain problematic dependencies, and these are only supported by the aforementioned package managers. Using a different package manager may result in unexpected behavior

NOTE if using TypeScript the compiler option "skipLibCheck" should be set to true in your tsconfig.json file

Initializing the client

Before you can start registering Tickers and creating Assets, you have to connect the Polymesh SDK client to a Polymesh node. This is a pretty straightforward process:

import { Polymesh } from '@polymeshassociation/polymesh-sdk';
import { LocalSigningManager } from '@polymeshassociation/local-signing-manager';

async function run() {
  const signingManager = await LocalSigningManager.create({
    accounts: [
      {
        mnemonic: '//Alice', //A "well known" mnemonic, often with sudo privileges on development chains
      },
      {
        mnemonic: 'forest end mail art wish leave truth else ignore royal knife river', // most mnemonics are 12 words
      },
    ],
  });
  const polyClient = await Polymesh.connect({
    nodeUrl: 'wss://some-node-url.com',
    signingManager,
  });

  // do stuff with the client
}

Here is an overview of the parameters passed to the connect function:

  • nodeUrl is a URL that points to a running Polymesh node
  • signingManager is an object that complies with the SigningManager interface. It holds the Accounts capable of signing transactions, and the signing logic itself. In this example, LocalSigningManager is a simple signing manager that holds private keys in memory and signs with them

NOTE: if using the SDK on a browser environment (i.e. with the Polymesh wallet browser extension), you would use the BrowserExtensionSigningManager provided by @polymeshassociation/browser-extension-signing-manager

import { Polymesh } from '@polymeshassociation/polymesh-sdk';
import { BrowserExtensionSigningManager } from '@polymeshassociation/browser-extension-signing-manager';

async function run() {
  const signingManager = await BrowserExtensionSigningManager.create('MY_APP_NAME'); // The Polymesh wallet extension will ask the user to authorize MY_APP_NAME for access

  const polyClient = await Polymesh.connect({
    nodeUrl: 'wss://some-node-url.com',
    signingManager,
  });

  // do stuff with the client
}

Creating Transactions

Creating transactions is a two-step process. First a procedure is created, which validates the chain is likely to accept the transaction and returns a Procedure object. This procedure is then executed. This includes having the signing manager generate a signature and waiting for block finalization. Some procedures resolve to a relevant entity, such as createAsset resolving to the created asset.

  /**
   * This step performs validations, and will throw an error if the transaction isn't expected to proceed, e.g., if the `ticker` is already in use
   */
  const createAssetProc = await polyClient.assets.createAsset({
    name: 'My new asset'
    ticker: 'TICKER',
    // ... (args omitted for brevity)
  })

  /**
   * The promise will resolve when the transaction is in a finalized block which takes on average 15 seconds. It will throw an error if the transaction fails to finalize.
   * For example, if the `ticker` was claimed after the procedure was created, but before it was executed, or the signing manager didn't generate a correct signature.
   */
  const newAsset = await createAssetProc.run()

Creating MultiSig Proposals

If the signingAccount is a MultiSig signer, then the transaction will need to be ran with .runAsProposal() instead of the usual .run(). The underlying transaction will be wrapped with multiSig.createProposalAsKey extrinsic and will resolve to the MultiSig proposal created.

Approving and rejecting existing proposals are an exception and should be submitted with .run(). If your application supports MultiSig signers, then the procedure's multiSig param can be checked to ensure the correct method is called.

const createAssetProc = await polyClient.assets.createAsset(args, {
  signingAccount: multiSigSigner,
});
createAssetProc.multiSig; // indicates the acting MultiSig. If set `runAsProposal` must be used
const proposal = await createAssetProc.runAsProposal();

const rejectProc = await proposal.reject({ signingAccount: multiSigSigner });
rejectProc.multiSig; // returns `null`. Rejecting a proposal does not get wrapped
await rejectProc.run();

Reading Data

The SDK exposes getter functions that will return entities, which may have their own functions:

const assetsPage = await polyClient.assets.get({ size: new BigNumber(20) });
const asset = assetsPage.data[0];

const assetDetails = await asset.details();
console.log('asset details:', assetDetails);

Note: Some getters require "middleware" to be configured, which is a chain indexer that aids in historical queries. All such methods will have a comment indicating this requirement.

Terminology

The SDK uses the class Account as an abstraction for a public/private key pair that is used to sign transactions. Although consistent with Substrate (the chain's framework) naming conventions, it can be a source of confusion considering the domain. What the SDK calls an account is often referred to as a key. Public keys are often represented in SS58 format which is a special encoding that indicates if the key is intended for mainnet or not. In this form, it is referred to as an address and looks like: 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY (non-mainnet keys begin with 5, mainnet addresses will instead begin with 2).

The only thing an Account holds is the POLYX utility token. Ownership of any asset on the Polymesh chain requires an Identity. This process involves a trusted provider writing a claim to the chain, stating that this person has completed a "customer due diligence" (CDD) process. For development chains, the mnemonic //Alice can create CDD claims by default.

Polymesh uses an Identity to provide flexibility in managing permissions. Portfolios can be created, and secondary keys can be granted permission to provide fine grained authorization.

About

Feature rich and user friendly typescript package to interact with the Polymesh blockchain

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages