RPC Verification
Confirm that your RPC endpoint serves D9 Mainnet before reading state or signing transactions, then generate typed PAPI 3 descriptors.
Prerequisites: src/config.ts from Environment Configuration.
1. Verify the genesis hash
Section titled “1. Verify the genesis hash”Run this check before using an RPC endpoint:
import { d9Config } from "./config";
const socket = new WebSocket(d9Config.rpcUrl);
socket.onopen = () => { // Genesis hash socket.send(JSON.stringify({ jsonrpc: "2.0", id: 1, method: "chain_getBlockHash", params: [0] })); // Token symbol and decimals socket.send(JSON.stringify({ jsonrpc: "2.0", id: 2, method: "system_properties", params: [] }));};
socket.onmessage = ({ data }) => { const response = JSON.parse(String(data)); if (response.id === 1) { if (response.result !== d9Config.expectedGenesisHash) { console.error("Genesis mismatch, rejecting RPC:", response.result); socket.close(); return; } console.log("Verified genesis hash:", response.result); } else if (response.id === 2) { console.log("Chain properties:", response.result); }};Verify: The console prints Verified genesis hash: followed by your configured hash.
Re-verify the genesis hash before every transaction signature request.
2. Read from one finalized block
Section titled “2. Read from one finalized block”- Call
chain_getFinalizedHead. - Pass the returned block hash as
atto every relatedstate_getStorageandstate_callrequest.
Without pinning to a single finalized block, quotes, reserves, and fee estimates may reflect different chain states.
3. Generate PAPI 3 descriptors
Section titled “3. Generate PAPI 3 descriptors”-
Export the verified RPC URL in your shell. The PAPI CLI does not read Vite’s
.envfiles.Terminal window export VITE_D9_RPC_URL=wss://rpc-hermes.d9network.com # Verified in step 1 -
In your DApp project directory, run:
Terminal window npm install --save-exact polkadot-api@3.3.0 @d9-network/dapp-connector@0.2.0 effect@4.0.5: "${VITE_D9_RPC_URL:?Export the verified VITE_D9_RPC_URL first}"npx papi add d9 -w "$VITE_D9_RPC_URL"npx papi generate -
Commit the generated
.papiconfiguration and metadata files to version control. -
After any reviewed runtime upgrade, re-run
npx papi generate. Keep the samepolkadot-apiversion for both code generation and runtime execution.
Verify: A local @polkadot-api/descriptors package is created and exports d9 and getMetadata.
Common errors
Section titled “Common errors”| Error | Resolution |
|---|---|
Genesis mismatch, rejecting RPC |
Switch to another RPC from Mainnet Endpoints and verify VITE_D9_GENESIS_HASH. |
Export the verified VITE_D9_RPC_URL first |
Run the export command from step 3.1 in the same shell session. |
