JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 90
  • Score
    100M100P100Q46570F
  • License MIT

XCM SDK is a tool that provides an interface to send XCM messages for Substrate based blockchains.

Package Exports

  • xcm-sdk
  • xcm-sdk/lib.esm/index.js
  • xcm-sdk/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (xcm-sdk) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

XCM SDK

npm CircleCI Coverage Status Codacy Badge License CodeQL

About

XCM SDK is a tool that provides an interface to send XCM messages for Substrate based blockchains. This library is written in Typescript so it can be imported in a whole new set of applications or dApps that use Javascript/Typescript engines such as Node.js.

Get Started

Install

npm i xcm-sdk

Usage

// JavaScript
const { Provider } = require("xcm-sdk")

// TypeScript
import { Provider } from "xcm-sdk"

Provider

const provider = new Provider(rpc, sender)
Param Description
rpc rpc endpoint
sender signer of the transaction

Examples

If you want to sign with Alice in a local node:

import { Keyring } from '@polkadot/keyring'
import { cryptoWaitReady } from '@polkadot/util-crypto'

const rpc = "ws://127.0.0.1:37345" // local node ws
await cryptoWaitReady();

const keyring = new Keyring({ type: "sr25519" });
const sender = keyring.addFromUri("//Alice");

const provider = new Provider(rpc, sender);

If you want to sign with mnemonic

import { Keyring } from '@polkadot/keyring'

const sender = keyring.addFromMnemonic(
"<your mnemonic seed here>"
);

If you want to sign with polkadotjs extension

import { web3FromAddress, web3Accounts, web3Enable } from "@polkadot/extension-dapp";

const extensions = await web3Enable("<your app name>");
const accounts = await web3Accounts();
const accountId = accounts[0].address;

const injector = await web3FromAddress(accountId);

const provider = new Provider(rpc, accountId);
provider.setInjectorSigner(injector.signer);

Supported Methods

Reserve Asset Transfer with reserveTransferAsset and LimitedReserveTransferAsset methods and Asset teleportation with teleportAsset and LimitedTeleportAsset methods.

provider.limitedReserveTransferAssets(params)

provider.reserveTransferAssets(params)

provider.limitedReserveTransferAssets(params)

provider.reserveTransferAssets(params)

Methods parameters

Param Description
destination The destination to transfer the asset. If you want to transfer asset from relaychain to a parachain set 'Parachain'. Default 'Here'.
destinationParents 0 is default, 1 when you want to transfer from parachain to relaychain or parachain to parachain
destinationValue The destination value, for example a parachain id
beneficiary beneficary target, an accountId32
beneficiaryParents 0 is default
beneficiaryValue The beneficiary value, account address to send the asset
amount token amount to transfer
assetId AssetId to transfer from parachain, make sure the parchain support the asset and the sender account have enough asset to transfer
weightLimit Optional, only for limited methods. Set the maximum weight for the extrinsic

Disclaimer

Depends on the parachain or relay chain configuration you have to use Asset teleportation or reserve asset transfer. Make sure you know what method use before execute any transfer. You can search in any scan to know, for example rococo scan

Rococo examples

If you want to tests in Testnet, you have Rococo.
Get some assets: Rococo faucet

Config

The examples are in ./examples/rococo/, you can put your configuration in ./examples/rococo/rococo-examples-util.ts. Then you can run a command for each example. If you want to run them manually, you must create your own script (.js or ts) and import the dependencies.

export const rococoExampleUtils = {
  rococoRpc: 'wss://rococo-rpc.polkadot.io',
  rockMineRpc: 'wss://rococo-rockmine-rpc.polkadot.io',
  rockMineParachainId: 1000,
  daliParachainId: 2087,
  senderMnemonic: '<your account mnemonic>',
  rockmineDestinationAccount: '<rockmine address account>',
  daliDestinationAccount: '<dali destination account>',
  rococoDestinationAccount: '<rococo address account>',
  rocAmount: <amount to transfer>,
}

Send Asset from Rococo to Rockmine

command:

npx ts-node src/examples/rococo/rococo-to-rockmine.ts

manually:

  const destination = "Parachain"
  const destinationValue = 2000 // Rockmine parchain id
  const beneficiary = "AccountId32"
  const beneficiaryValue = "<rockmine account address>" // account address
  const amount = 1000000000000000

  const res = await provider.limitedTeleportAssets({
    destination,
    destinationValue,
    beneficiary,
    beneficiaryValue,
    amount,
  });

Send Asset from RockMine to Rococo

command:

npx ts-node src/examples/rococo/rockmine-to-rococo.ts

manualy:

  const destinationParents = 1; // Destination to Rococo
  const beneficiary = "AccountId32"
  const beneficiaryValue = "<rococo account address>" // account address
  const amount = 1000000000000000


  const res = await provider.limitedTeleportAssets({
    destination,
    destinationValue,
    beneficiary,
    beneficiaryValue,
    amount,
  });

Send native Asset (ROC) from RockMine to Dali

command:

npx ts-node src/examples/rococo/rockmine-to-dali-roc.ts

manualy:

  const destination = "Parachain";
  const destinationValue = 2087; // dali parachain id
  const destinationParents = 1;
  const beneficiary = "AccountId32";
  const beneficiaryValue = "<dali account address>";
  const assetParents = 1; // native asset (ROC)
  const amount = 1000000000000000;

  const res = await provider.limitedReserveTransferAssets({
    destination,
    destinationValue,
    destinationParents,
    beneficiary,
    beneficiaryValue,
    assetParents,
    amount,
  });

Send Asset from Rococo to dali

Make sure you have assets to transfer. It is also necessary to change the asset id in ./examples/rococo/rockmine-to-dali-asset.ts, default is 1984 (Rockmine USD).

npx ts-node src/examples/rococo/rockmine-to-dali-asset.ts

manualy

  const destination = "Parachain"
  const destinationValue = 2087
  const destinationParents = 1
  const beneficiary = 'AccountId32'
  const beneficiaryValue = "<dali account address>"
  const assetId = 1984   // asset id in rockmine
  const amount = 50000000000


  const res = await provider.limitedReserveTransferAssets({
    destination,
    destinationValue,
    beneficiary,
    beneficiaryValue,
    amount,
  });

Testing

Running the unit tests.

npm run test

Running the test coverage.

npm run test:cov

Change Log

See Changelog for more information.

Contributing

Contributions welcome! See Contributing.

Collaborators

License

Licensed under the MIT - see the LICENSE file for details.