How to use the @polkadot/api.ApiRx.create function in @polkadot/api

To help you get started, we’ve selected a few @polkadot/api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkadot-js / api / docs / examples / rx / 08_system_events / index.js View on Github external
async function main () {
  // Create our API with a default connection to the local node
  ApiRx.create()
    .pipe(
      switchMap((api) =>
        // subscribe to system events via storage
        api.query.system.events()
      )
    )
    // Then we're subscribing to the emitted results
    .subscribe((events) => {
      console.log(`\nReceived ${events.length} events:`);
      // loop through the Vec
      events.forEach((record) => {
      // extract the phase, event and the event types
        const { event, phase } = record;
        const types = event.typeDef;

        // show what we are busy with
github polkadot-js / api / docs / examples / rx / 09_transfer_events / index.js View on Github external
async function main () {
  // Create our API with a connection to the node
  const api = await ApiRx.create().toPromise();

  // Create an instance of our testign keyring
  // If you're using ES6 module imports instead of require, just change this line to:
  // const keyring = testKeyring();
  const keyring = testKeyring.default();

  // Find the actual keypair in the keyring
  const alicePair = keyring.getPair(ALICE);

  // Create a new random recipient
  const recipient = keyring.addFromSeed(randomAsU8a(32)).address;

  console.log('Sending', AMOUNT, 'from', alicePair.address, 'to', recipient);

  // Get the nonce for the admin key
  // Create a extrinsic, transferring 12345 units to Bob.
github polkadot-js / api / docs / examples / rx / 10_upgrade_chain / index.js View on Github external
async function main () {
  // Initialise the provider to connect to the local node
  const provider = new WsProvider('ws://127.0.0.1:9944');

  // Create the API and wait until ready (optional provider passed through)
  const api = await ApiRx.create({ provider }).toPromise();

  // Retrieve the upgrade key from the chain state
  // TODO It seems like this promise doesn't resolve
  const adminId = await api.query.sudo.key().toPromise();

  // Find the actual keypair in the keyring (if this is an changed value, the key
  // needs to be added to the keyring before - this assumes we have defaults, i.e.
  // Alice as the key - and this already exists on the test keyring)
  const keyring = testKeyring.default();
  const adminPair = keyring.getPair(adminId.toString());

  // Retrieve the runtime to upgrade to
  const code = fs.readFileSync('./test.wasm').toString('hex');
  const proposal = api.tx.consensus.setCode(`0x${code}`);

  console.log(`Upgrading chain runtime from ${adminId}`);
github polkadot-js / api / docs / examples / rx / 03_listen_to_balance_change / index.js View on Github external
async function main () {
  // Create an await for the API
  const api = await ApiRx.create().toPromise();

  // Here we subscribe to any balance changes and update the on-screen value.
  // We're using RxJs pairwise() operator to get the previous and current values as an array.
  api.query.balances.freeBalance(Alice)
    .pipe(
      // since pairwise only starts emitting values on the second emission, we prepend an
      // initial value with the startWith() operator to be able to also receive the first value
      startWith('first'),
      pairwise()
    )
    .subscribe((balance) => {
      if (balance[0] === 'first') {
        // Now we know that if the previous value emitted as balance[0] is `first`,
        // then balance[1] is the initial value of Alice account.
        console.log(`Alice ${Alice} has a balance of ${balance[1]}`);
        console.log('You may leave this example running and start the "Make a transfer" example or transfer any value to Alice address');
github polkadot-js / api / docs / examples / rx / 06_make_transfer / index.js View on Github external
async function main () {
  // Instantiate the API
  const api = await ApiRx.create().toPromise();

  // Create an instance of the keyring
  const keyring = new Keyring({ type: 'sr25519' });

  // Add Alice to our keyring (with the known seed for the account)
  const alice = keyring.addFromUri('//Alice');

  // Create a extrinsic, transferring 12345 units to Bob.
  const subscription = api.tx.balances
    // create transfer
    .transfer(BOB, 12345)
    // Sign and send the transcation
    .signAndSend(alice)
    // Subscribe to the status updates of the transfer
    .subscribe(({ status }) => {
      if (status.isFinalized) {

@polkadot/api

Promise and RxJS wrappers around the Polkadot JS RPC

Apache-2.0
Latest version published 5 days ago

Package Health Score

90 / 100
Full package analysis

Similar packages