2

I am curious if there is a way to access some internal runtime pallet functions from javascript. Is there such ability in the polkadot npm package?

3 Answers 3

2

I think you are asking how to submit a transaction to your chain using Polkadot JS, which is outlined in their documentation here:

https://polkadot.js.org/docs/api/start/api.tx

https://polkadot.js.org/docs/api/examples/promise/make-transfer

Here is a copy of the example from the page above:

// Import the API, Keyring and some utility functions
const { ApiPromise } = require('@polkadot/api');
const { Keyring } = require('@polkadot/keyring');

const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';

async function main () {
  // Instantiate the API
  const api = await ApiPromise.create();

  // Constuct the keyring after the API (crypto has an async init)
  const keyring = new Keyring({ type: 'sr25519' });

  // Add Alice to our keyring with a hard-deived path (empty phrase, so uses dev)
  const alice = keyring.addFromUri('//Alice');

  // Create a extrinsic, transferring 12345 units to Bob
  const transfer = api.tx.balances.transfer(BOB, 12345);

  // Sign and send the transaction using our account
  const hash = await transfer.signAndSend(alice);

  console.log('Transfer sent with hash', hash.toHex());
}

main().catch(console.error).finally(() => process.exit());

These transactions are ultimately generated by:

  1. Using metadata to understand how to compose a transaction message
  2. Submitting the transaction to the node's RPC endpoint

If you want to call other internal functionalities of your runtime, you may want to expose custom RPCs.

See: How do I include a new rpc that uses a NetworkWorker in the node-template?

1
  • I was asking for calling other internal functionalities, thanks for the clarification. I will rearrange my question.
    – Szegoo
    Commented Jun 2, 2022 at 15:54
1

This is documented here https://polkadot.js.org/docs/api/cookbook/tx#how-do-i-get-the-decoded-enum-for-an-extrinsicfailed-event

You can also find an example here https://github.com/danforbes/parachain-demo/blob/main/start-collator.mjs#L165

0

The only runtime functions you can access from Javascript are "dispatchable" functions, which are usually accessed by way of signed transactions. By definition, you cannot access "internal" runtime functions from outside of the runtime, e.g. Javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.