3.0.1 • Published 10 months ago

darwinia-js-sdk v3.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
10 months ago

darwinia.js

Version >= ^3.0.0 for Darwinia 2.0. Version < 3.0.0 is for Darwinia 1.0.

A library to help

  1. fetch internal substrate storages, and
  2. dispatch calls
  3. Easy-to-use derived functions(Derived API List) which are build from 1 and 2.

Nodejs

npm install darwinia.js@^3.0.0

Browser

Collators Amount: <span id="result"></span>

<script type="module">
  import { ethers } from "https://esm.sh/ethers@5.7.2";
  import { clientBuilder } from "https://esm.sh/darwinia.js@3.0.0";
  const provider = new ethers.providers.Web3Provider(window.ethereum);
  const client = clientBuilder.buildPangolinClient(provider);

  // storage
  const result = await client.storages.darwiniaStaking.collatorCount();
  const resultEl = document.getElementById("result");
  resultEl.innerHTML = result;

  // call
  await window.ethereum.request({ method: "eth_requestAccounts" });
  const signer = provider.getSigner();
  const from = await signer.getAddress();
  await client.calls.system.remarkWithEvent(signer, "0x12345678");
</script>

Try it in jsfiddle:

https://jsfiddle.net/wuminzhe/2u80mbfp

Usage

fetch storage

The returns of this lib's functions are all json string or null.

import { ethers } from "ethers";
import { clientBuilder } from "darwinia.js";

async function main(): Promise<void> {
  const provider = new ethers.providers.JsonRpcProvider(
    "https://pangolin-rpc.darwinia.network"
  );
  // or
  // const provider = new ethers.providers.Web3Provider(window.ethereum);

  const pangolin = clientBuilder.buildPangolinClient(provider);

  let result = await pangolin.storages.system.account(
    "0x794BF0B66926D84CB735283D849f454A2A8d9a44"
  );
  console.log(`${result}\n`);
}

main();

dispatch call

import { ethers } from "ethers";
import { clientBuilder } from "darwinia.js";

async function main(): Promise<void> {
  const provider = new ethers.providers.Web3Provider(window.ethereum);

  const signer = provider.getSigner();

  const pangolin = clientBuilder.buildPangolinClient(provider);

  await pangolin.calls.session.setKeys(
    signer,
    {
      aura: "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d",
    }, // keys
    "0x" // proof
  );
}

main();

dispatch multiple calls in a single transaction

import { ethers } from "ethers";
import { clientBuilder } from "darwinia.js";

async function main(): Promise<void> {
  const provider = new ethers.providers.Web3Provider(window.ethereum);

  const signer = provider.getSigner();

  const pangolin = clientBuilder.buildPangolinClient(provider);

  // prepare calls
  const setKeysCall = pangolin.calls.session.buildSetKeysCall(
    // keys
    {
      aura: "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d",
    },
    // proof
    "px"
  );

  const collectCall = pangolin.calls.staking.buildCollectCall(120000000);

  // dispatch
  await pangolin.calls.utility.batchAll(signer, [setKeysCall, collectCall]);
}

main();

dispatch call with encoded input

import { ethers } from "ethers";
import { clientBuilder } from "darwinia.js";

async function main(): Promise<void> {
  const provider = new ethers.providers.Web3Provider(window.ethereum);

  const signer = provider.getSigner();

  const pangolin = clientBuilder.buildPangolinClient(provider);

  // call ended with `D` is the version that accept params encoded in scale codec
  await pangolin.calls.session.setKeysH(
    signer,
    "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d00" // encoded (keys, proof)
  );
}

main();

More examples in examples

Derived API List

nominateAndStake

Nominate a collator and stake tokens to the collator.

setSessionKeysAndCommission

Set session keys and commission percetage. Example

trackSystemEvents

Track specified events. Example

getSystemEvents

Get the latest events. You can use this function with provider.on('block', callback) to implement the tracking functionality like trackSystemEvents. Example

See src/derived.ts.

Update Chains Metadata

npm run gen
3.0.1

10 months ago

2.0.5-1

1 year ago

2.0.5-0

1 year ago

2.0.4-1

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.9

12 months ago

2.0.8

12 months ago

2.0.9-b

12 months ago

2.0.9-a

12 months ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.21

1 year ago

1.2.20

1 year ago

1.2.18

1 year ago

1.2.17

1 year ago

1.2.16

1 year ago

1.2.15

1 year ago

1.2.14

1 year ago

1.2.13

1 year ago

1.2.12

1 year ago

1.2.11

1 year ago

1.2.10

1 year ago

1.2.9

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago