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

@equilab/api

v1.15.2

Published

JS API for Equilibrium and Genshiro parachains.

Downloads

320

Readme

EQUILIBRIUM API

Equilibrium node list

  • Mainnet node: wss://node.equilibrium.io
  • Testnet node: wss://testnet.equilibrium.io/eq/collator/api/wss (this node implements latest features)

Getting started

$ npm i --save @equilab/api     # if you are using npm or
$ yarn add @equilab/api         # for yarn package manager

Usage

API Init

Use getApiCreator(nodeSpec: "Eq" | "EqNext" | "Genshiro") factory from @equilab/api package

Import modules from equilibrium interfaces to access types from @polkadot/types/lookup.

Example: import { EqPrimitivesSignedBalance } from "@polkadot/types/lookup";

import "@equilab/api/equilibrium/interfaces/augment-api";
import "@equilab/api/equilibrium/interfaces/types-lookup";

import { getApiCreator } from "@equilab/api";
import { cryptoWaitReady } from "@polkadot/util-crypto";
import Keyring from "@polkadot/keyring";

async function main() {
  await cryptoWaitReady();

  const keyring = new Keyring({ ss58Format: 68 });

  SEED_PHRASES.forEach((seed) => keyring!.addFromMnemonic(seed, {}, "sr25519"));

  const api = await getApiCreator("Eq")(TESTNET_NODE);

  const balances = await getBalances(api)(
    "cg73qE7Zgu8i8CYEPcFK65iMZ7EAYrV1jJi5dTSiEuHSA9nN7",
  );

  console.log("balances", balances);

  await transfer(
    api,
    keyring,
  )({
    token: "eq",
    from: "cg7ENkxLYjpXV2QWjw5murokf8ZxUNkjjhCkqqxxAP7svN49A",
    to: "cg73qE7Zgu8i8CYEPcFK65iMZ7EAYrV1jJi5dTSiEuHSA9nN7",
    amount: "12000000000",
  });

  process.exit();
}

Query storage

Storage queries are compliant with Polkadot.JS storage interfaces.

Get balances from storage method is using currencyFromU64 to decode asset u64 id into token name (eg 'eq')

import { currencyFromU64, u64FromCurrency } from "@equilab/api/equilibrium";

function getBalances(api: Api) {
  return async function (account: string): Promise<{
    ok: boolean;
    lock?: string;
    balances?: Map<string, string>;
  }> {
    const accountInfo = await api._api.query.system.account(account);
    if (!accountInfo.data.isV0) return { ok: false };

    const lock = accountInfo.data.asV0.lock.toString(10);

    const balances = accountInfo.data.asV0.balance
      .toArray()
      .reduce(
        (acc, [id, balance]) =>
          acc.set(
            currencyFromU64(id),
            balance.isPositive
              ? balance.asPositive.toString(10)
              : `-${balance.asNegative.toString(10)}`,
          ),
        new Map<string, string>(),
      );

    return { ok: true, lock, balances };
  };
}

Successfull output looks like this:

{ ok: true, lock: '22000000000', balances: { 'eq' => '72000000000', 'dot' => '597056440465' } }

Balance has 1e9 decimals places and thus '72000000000' equals 72 eq.

Sign and send transactions

Transactions are compliant with Polkadot.JS transactions.

Equilibrium palletes and methods can be accessed using _api ApiPromise interface.

function transfer(api: Api, keyring: Keyring) {
  return async function ({
    token,
    from,
    to,
    amount,
  }: {
    token: string;
    from: string;
    to: string;
    amount: string;
  }) {
    try {
      const tx = api._api.tx.eqBalances.transfer(
        u64FromCurrency(token),
        to,
        amount,
      );
      const keyPair = keyring.getPair(from);

      const res = await tx.signAndSend(keyPair, { nonce: -1 });
      console.log("tx result: ", res.toJSON());
    } catch (e) {
      console.error(e);
    }
  };
}

This example is using keyring to create key pairs from seed phrases. When using wallet on frontend account address can be used instead of key pair.

API examples

Our public DEX API contains various examples of rxjs api usage:

Equilibrium DEX API.