Skip to main content

Use React hooks

Version: V2
View Markdown

Use the same MultiVault deployment for a wallet-free atom lookup and a wallet-approved creation in your React application.

Prompt for your agentAdd an atom read and a reviewed creation to a React app
Goal: add Intuition to my existing React app: read an atom by term ID, and create one with the connected wallet.
Read https://docs.intuition.systems/docs/build/react.md, https://docs.intuition.systems/docs/examples/react-app.md and https://docs.intuition.systems/docs/reference/packages/react.md.
Use exact package versions and peers from https://docs.intuition.systems/intuition-release.json and public entry points only.
Configure the providers for Intuition testnet, chain 13579, with the read and the write using the same MultiVault deployment.
Show loading, empty and error states for the read.
Before the wallet prompt, show the exact bytes, cost and chain. The user approves in their own wallet. Never sign for them.
After the receipt arrives, read the new atom back by term ID and confirm it matches what was reviewed.
Report the checks you actually ran and anything you could not verify.

Paste this into your coding agent. It points the agent at the Markdown version of each page and leaves every signature to you.

Before you start​

Install the pinned packages and peers in a React host. The worked React app includes the dependencies, connector, mount and browser bundle instructions. Have a testnet RPC URL and an existing 32-byte atom term ID for the read. Creation also requires an injected wallet on Intuition testnet and native funds for the creation cost and gas. Install the packages · Atoms · Protocol writes

Steps​

1. Match the providers to your deployment​

Give wagmi the chain and the reader's RPC endpoint. Keep one query client for the lifetime of the provider. The testnet preset resolves the MultiVault address from the deployments package. For a custom deployment, supply a verified chain, chain ID and MultiVault address through the provider’s config prop and configure the same chain in wagmi. Provider implementation

function Providers({
endpoint,
children,
}: {
endpoint: string;
children: ReactNode;
}) {
const [config] = useState(() =>
createConfig({
chains: [intuitionTestnet],
connectors: [injected()],
transports: { [intuitionTestnet.id]: http(endpoint) },
}),
);
const [client] = useState(createWalletQueryClient);
return (
<WagmiProvider config={config} reconnectOnMount={false}>
<QueryClientProvider client={client}>
<IntuitionProvider network="testnet">{children}</IntuitionProvider>
</QueryClientProvider>
</WagmiProvider>
);
}

The example’s createWalletQueryClient configures wagmi’s writeContract mutation to start immediately and never retry. Submission checks connectivity first. If the app is offline, reconnect and review the creation again. Wallet write configuration

2. Read the atom by term ID​

Mount the reader after validating the term ID. useAtom(termId, { enabled, curveId, chainId }) reads the contract through wagmi. The provider supplies the chain and the contract supplies the default curve when you omit those overrides. The result includes the atom bytes and vault totals when all component reads succeed. Treat a missing result as an incomplete read. Only an explicit false isAtom establishes the negative result returned by this hook. Read hook implementation

function AtomResult({ termId }: { termId: Hex }) {
const query = useAtom(termId, { enabled: true });
const cache = useQueryClient();
if (query.isLoading) return <p role="status">Reading the atom…</p>;
if (query.isError || !query.data)
return (
<>
<p role="status">
Could not read all atom fields. Check the term ID and RPC.
</p>
<button onClick={() => void cache.refetchQueries({ type: 'active' })}>
Retry reads
</button>
</>
);
if (!query.data.isAtom)
return <p role="status">No atom exists for this term ID.</p>;
return (
<dl>
<dt>Atom bytes</dt>
<dd>
<code>{query.data.atomData}</code>
</dd>
<dt>Curve ID</dt>
<dd>{String(query.data.curveId)}</dd>
<dt>Total assets (wei)</dt>
<dd>{String(query.data.totalAssets)}</dd>
<dt>Total shares</dt>
<dd>{String(query.data.totalShares)}</dd>
</dl>
);
}

3. Review and submit one creation​

Call useCreateAtomFlow() inside the component. Enable review only after the wallet connects on the selected chain and the hook returns an atom cost. Prepare the UTF-8 data, one asset allocation in native wei and the encoded call. Display the sender, target, chain, arguments and value before enabling submission. An account, chain, payload or cost change requires a new review.

Only the submit button calls createAtom({ data, assets }). It invokes ordinary MultiVault createAtoms with value equal to the sum of allocations. The example assigns the exact creation cost to its single atom. Additional deposits change the amount the user must review. URI context registration uses a separate protocol operation. Creation flow · Create atoms

Check the result​

While the wallet is open, show that approval is pending. After broadcast, display the submitted hash from the hook. Keep the wallet on the selected chain while the flow polls. Compare that attempt’s submitted hash with receipt.transactionHash once createAtom resolves. A wallet replacement can produce a successful receipt with a different hash. Show both hashes and ask the reader to inspect the replacement before retrying.

For the original hash, check receipt success, sender and MultiVault target. This one-atom example also requires one AtomCreated event from that transaction and MultiVault. Check its creator, exact atom bytes and term ID derived from those bytes. Only then select the term for a contract read on the same chain. Creation flow tests

Troubleshooting​

If the cost is unavailable, retry the contract read and keep submission disabled. If the user declines the wallet request, let them start a fresh review. A reverted receipt rejects the flow. If you already have a hash and receipt polling fails, inspect that hash on the selected chain before retrying. Transaction flow implementation

Build the React app · React package reference · Query indexed data on your Core node

Applies to V2 · @0xintuition/react 0.1.1 · @0xintuition/protocol 4.1.0 · @0xintuition/deployments 0.1.1