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
$ yarn add @equilab/api
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.