data-book="docs" Utility (Native API example) | The Root Network
Skip to content

2.47 Utility (Native API example) ​
At a glance
What it isSend extrinsics as a batch, and decode a batch error. Futureverse’s Native API example for The Root Network, with its README and source.
Where it sitsPart II — Building · Chapter 2.47
CoversExamples · Source files
Sourcegithub.com/futureversecom/trn-examples/tree/main/examples/substrate/use-utility

Send extrinsics as a batch, and decode a batch error. Reference: utility pallet.

Copied from Futureverse’s futureversecom/trn-examples, deleted with its GitHub organisation, at commit 44c9e16 of 2024-10-31. Apache-2.0: license. Before you run it, read Running the examples.

Offline destinations

Some places this page points to did not answer when last checked:

  • docs-beta.therootnetwork.com — does not resolve (checked 2026-10-18)
  • stackblitz.com/github/futureversecom — StackBlitz loads these projects from the deleted futureversecom organisation on GitHub (checked 2026-10-18)

Run in StackBlitz (repository deleted, checked 2026-10-18) · Pallet Documentation (offline, checked 2026-10-18)

IMPORTANT

Ensure the following ENV vars are available before running the examples

2.47.1 Examples ​

bash
# change your working directory to this example first
cd examples/substrate/use-utility

# export all required environments
export CALLER_PRIVATE_KEY=

# use `utility.batch` to dispatch a group of extrinsic
pnpm call:batchExtrinsics

# use `registry.findMetaError` to decode batch error
pnpm call:decodeBatchError

2.47.2 Source files ​

src/batchExtrinsics.ts2.47.2.1 ​

ts
import { filterExtrinsicEvents } from "@trne/utils/filterExtrinsicEvents";
import { formatEventData } from "@trne/utils/formatEventData";
import { sendExtrinsic } from "@trne/utils/sendExtrinsic";
import { withChainApi } from "@trne/utils/withChainApi";

/**
 * Use `utility.batch` extrinsic to dispatch a group of extrinsics, potentially saving gas cost.
 *
 * Assumes the caller has XRP to pay for gas.
 */
withChainApi("porcini", async (api, caller, logger) => {
	const calls = new Array(10).fill(1).map((n, i) => api.tx.system.remark(`Call ${n + i}`));

	logger.info(
		{
			parameters: [...calls],
		},
		`create a "utility.batch" extrinsic`
	);
	const extrinsic = api.tx.utility.batch(calls);

	logger.info(`dispatch extrinsic from caller="${caller.address}"`);
	const { result, extrinsicId } = await sendExtrinsic(extrinsic, caller, { log: logger });
	const [batchEvent] = filterExtrinsicEvents(result.events, ["Utility.BatchCompleted"]);

	logger.info(
		{
			result: {
				extrinsicId,
				blockNumber: result.blockNumber,
				batchEvent: formatEventData(batchEvent.event),
			},
		},
		"receive result"
	);
});

src/decodeBatchError.ts2.47.2.2 ​

ts
import { BN, hexToU8a } from "@polkadot/util";
import { filterExtrinsicEvents } from "@trne/utils/filterExtrinsicEvents";
import { formatEventData } from "@trne/utils/formatEventData";
import { sendExtrinsic } from "@trne/utils/sendExtrinsic";
import { withChainApi } from "@trne/utils/withChainApi";

/**
 * Use `utility.batch` extrinsic to dispatch a group of extrinsics with one set to failed
 * deliberately to demostrate how to extract and decode the error.
 *
 * Assumes the caller has XRP to pay for gas.
 */
withChainApi("porcini", async (api, caller, logger) => {
	const calls = new Array(10).fill(1).map((n, i) => {
		// Force error as Asset ID 3 does not exist
		if (i === 6) return api.tx.assets.transfer(3, caller.address, 1);
		return api.tx.system.remark(`Call ${n + i}`);
	});

	logger.info(
		{
			parameters: [...calls],
		},
		`create a "utility.batch" extrinsic`
	);
	const extrinsic = api.tx.utility.batch(calls);

	logger.info(`dispatch extrinsic from caller="${caller.address}"`);
	const { result, extrinsicId } = await sendExtrinsic(extrinsic, caller, { log: logger });
	const [interrupptedEvent] = filterExtrinsicEvents(result.events, ["Utility.BatchInterrupted"]);

	const {
		event: {
			data: [index, err],
		},
	} = interrupptedEvent.toJSON() as {
		event: {
			data: [
				number,
				{
					module: {
						index: number;
						error: `0x${string}`;
					};
				}
			];
		};
	};

	const { section, name, docs } = api.registry.findMetaError({
		index: new BN(err.module.index),
		error: hexToU8a(err.module.error),
	});

	logger.info(
		{
			error: {
				index,
				code: `${section}.${name}`,
				docs,
			},
		},
		`batch interrupted`
	);

	logger.info(
		{
			result: {
				extrinsicId,
				blockNumber: result.blockNumber,
				interrupptedEvent: formatEventData(interrupptedEvent.event),
			},
		},
		"receive result"
	);
});

package.json2.47.2.3 ​

json
{
  "scripts": {
    "call": "tsx --tsconfig ../../../tsconfig.json",
    "call:batchExtrinsics": "pnpm call src/batchExtrinsics.ts",
    "call:decodeBatchError": "pnpm call src/decodeBatchError.ts"
  },
  "dependencies": {
    "@polkadot/util": "^12.3.2"
  }
}
Source: futureversecom/trn-examples, examples/substrate/use-utility (deleted) · commit 44c9e16, 2024-10-31 · Apache-2.0, license · part of Code examples

Associated docs

Where else you might go from here. Following one is optional — this chapter stands without them.

Back matter — read in order
See also — The whole of Back matter as one continuous page, reprinted from its chapters without change.
Code examples
See also — Futureverse’s code examples for The Root Network: scripts for the native API and the EVM, and two contract projects, each with its README and source.
Contents
See also — Every chapter in this book, each with the one line that tells you what you get by reading it.
Front matter — read in order
See also — The whole of Front matter as one continuous page, reprinted from its chapters without change.
Guides for the code examples
See also — How Futureverse’s TRN code examples get a test account: create one in MetaMask and fund it from the Porcini faucet.
Index · A
See also — 609 index terms that start with A, each linked to the pages that define or use it.
Index · B
See also — 302 index terms that start with B, each linked to the pages that define or use it.
Index · F
See also — 319 index terms that start with F, each linked to the pages that define or use it.

Curated independently and source-attributed. Not affiliated with Futureverse or TRN Labs. Sources

Curated independently and source-attributed. Not affiliated with Futureverse or TRN Labs.