Skip to content

Transaction Lifecycle

Build, sign, submit, and confirm a native D9 transfer from the user’s injected wallet account.

Prerequisites: polkadot-api@3.3.0 with generated d9 descriptors (see RPC Verification) and src/amount.ts from Read the Chain.

import { createClient } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { d9, getMetadata } from "@polkadot-api/descriptors";
import type { D9ConnectorAccount } from "@d9-network/dapp-connector";
import { toChainAddress } from "@d9-network/dapp-connector/protocol";
import { parseAmount } from "./amount";
import { d9Config } from "./config";
export async function executeInjectedTransfer(
account: D9ConnectorAccount,
recipientAddress: string,
d9AmountString: string,
getCurrentAccount: () => Promise<D9ConnectorAccount | undefined>,
) {
if (account.transport !== "papi" || !account.txCreator) {
throw new Error("This action requires an active injected account with txCreator");
}
const txCreator = account.txCreator;
const recipient = toChainAddress(recipientAddress);
const rawValue = parseAmount(d9AmountString, 12);
if (rawValue <= 0n) throw new Error("Transfer amount must be positive");
const assertAuthority = async () => {
const current = await getCurrentAccount();
if (current?.transport !== "papi" || current.address !== account.address ||
current.txCreator !== txCreator) {
throw new Error("Account authorization changed before broadcast");
}
};
const client = createClient(getWsProvider(d9Config.rpcUrl), { getMetadata });
try {
const identity = await client.getChainSpecData();
if (identity.genesisHash !== d9Config.expectedGenesisHash) {
throw new Error("RPC genesis hash does not match configuration");
}
// Build the extrinsic with generated mainnet descriptors
const api = client.getTypedApi(d9);
const tx = api.tx.Balances.transfer_keep_alive({
dest: { type: "Id", value: recipient },
value: rawValue,
});
await assertAuthority();
// Wallet signs and returns the encoded signed extrinsic
const signedTx = await tx.create(txCreator);
// Re-verify account authorization before broadcasting
await assertAuthority();
const result = await client.submit(signedTx);
if (!result.ok) {
throw new Error(`Transaction dispatch failed: ${JSON.stringify(result.dispatchError)}`);
}
return result;
} finally {
client.destroy();
}
}
const result = await executeInjectedTransfer(account, recipientAddress, "1.25", getCurrentAccount);
  • account must be an active injected account (transport: "papi").
  • getCurrentAccount must query the live authorized account state on each invocation—never return a cached copy, or account switches during signing will go undetected.

client.submit waits for finalization. On success, result.ok is true and result.block.hash contains the finalized block hash.

Error Resolution
This action requires an active injected account with txCreator Open the DApp inside D9 Wallet’s in-app browser and connect via the injected provider.
RPC genesis hash does not match configuration Point VITE_D9_RPC_URL at an RPC matching VITE_D9_GENESIS_HASH.
Account authorization changed before broadcast The user switched or disconnected the account during signing; prompt them to retry.
Transaction dispatch failed: … The chain rejected the transfer; inspect dispatchError in the error message.