Skip to content

React provider and hooks

Connect React applications to the SDK: the provider and the read and workflow hooks.

One AnvilProvider near the root of the tree makes an SDK instance available to every component beneath it, and useAnvilSDK returns it.

Read hooks such as useCollateralTokens, useTokenPrice, and useRiskFactor wrap the SDK’s reads and carry their own loading and error state. Workflow hooks such as useCreateLOCWorkflow validate a form, report issues per field, and hand back a workflow once the input is valid. Every workflow hook returns the same UseWorkflowPreparationResult.

After a workflow runs, WorkflowSettlement describes how it ended, and getUnconfirmedSend picks out the case that needs its own screen: a send the wallet couldn’t confirm.

useIncrementalValidation runs a workflow’s validation rules field by field for a form you build yourself. It is the one beta hook here: it takes an SDK context argument rather than reading the provider, and that signature may change.

The hooks and the provider are the supported React surface, and Use the React hooks walks through them. The beta components built on them are in React components.

Provider at the root, hook in a component:

export function App() {
// Build the SDK once (module scope or useMemo); hooks key on it.
return (
<AnvilProvider sdk={sdk}>
<LOCBeneficiary id={1n} />
</AnvilProvider>
);
}
function LOCBeneficiary({ id }: { id: bigint }) {
// Throws if this component renders outside <AnvilProvider>.
const sdk = useAnvilSDK();
const [loc, setLoc] = useState<OutstandingLetterOfCredit>();
useEffect(() => {
void sdk.loc.getOutstandingLetterOfCredit({ id }).then(setLoc);
}, [sdk, id]);
return <p>Beneficiary: {loc?.beneficiary ?? 'loading'}</p>;
}

Primary APIs

Provider

  • AnvilProvider — React context provider that makes one configured AnvilSDK available to every hook and component beneath it.
  • useAnvilSDK — Returns the AnvilSDK instance from context — the one passed to <AnvilProvider sdk={...}> — so sdk.loc, sdk.vault, sdk.pricing,…

Read hooks

  • useCollateralTokens — Fetches every token the Collateral Vault knows as collateral, each with its enabled flag, one page at a time and with React state for…
  • useTokenPrice — Fetches a token’s USD Price from the oracle.
  • useRiskFactor — Scores how close a Dynamic LOC is to its pair’s collateral factor threshold, for an open LOC or a creation preview.

Workflow hooks

  • useCreateLOCWorkflow — Validates and builds the workflow that creates a Static or Dynamic LOC, with React state for the latest request.
  • UseWorkflowPreparationResult — What every workflow hook returns, such as useCreateLOCWorkflow: methods to check and build a WalletOperationWorkflow from the operation’s…

Workflow settlement

Validation (beta)