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
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:
- Using metadata to understand how to compose a transaction message
- 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?
-
I was asking for calling other internal functionalities, thanks for the clarification. I will rearrange my question.– SzegooCommented Jun 2, 2022 at 15:54
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
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.