Parallel FInance provides easy to use js sdk to communicate with our chain. You can download it via:
yarn add @parallel-finance/api @parallel-finance/type-definitions @parallel-finance/types
Create API
This api object will be used for all our future code snippet. There are two ways to communicate with the node
via websocket for subscribing storage changes
via http for not maintaining a websocket connection to fasten the DAPP
By default we use websocket
, if you need to use HTTP, you can replace the WsProvider
by HttpProvider
and change to http endpoint.
import { options } from '@parallel-finance/api';
import { ApiPromise } from '@polkadot/api';
import { WsProvider } from '@polkadot/rpc-provider';
const provider = new WsProvider('wss://heiko-rpc.parallel.fi');
const api = new ApiPromise(options({ provider }));
await api.isReady;
Extrinsic
Native Token Transfer
Transferring about the native token, include PARA/HKO.
Transfer
const currentAccount = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
const amount = 1000000000000;
const hash = await api.tx.balances.transfer(BOB, amount).signAndSend(currentAccount);
console.log('hash: ' + hash);
TransferAll
const currentAccount = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
const hash = await api.tx.balances.transferAll(BOB, true).signAndSend(currentAccount);
console.log('hash: ' + hash);
TransferKeepAlive
const currentAccount = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
const amount = 1000000000000;
const hash = await api.tx.balances.transferKeepAlive(BOB, amount).signAndSend(currentAccount);
console.log('hash: ' + hash);
Non-Native Token Transfer
Transfer
const currentAccount = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
const assetId = 100;
const amount = 1000000000000;
const hash = await api.tx.assets.transfer(assetId, BOB, amount).signAndSend(currentAccount);
console.log('hash: ' + hash);
Storage
Native Token TotalIssuance
const totalIssuance = await api.query.balances.totalIssuance();
console.log('totalIssuance: ' + totalIssuance);
Native Token Balance
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
const balance = await api.query.system.account(BOB);
console.log('balance: ' + balance);
Non-Native Token TotalIssuance
const assetId = 100;
const asset = await api.query.assets.asset(assetId);
console.log('asset: ' + asset);
Non-Native Token Balance
const assetId = 100;
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
const balance = await api.query.assets.account(assetId, BOB);
console.log('balance: ' + balance);