useRootStorage
⚠️ ink! v5 and v6 only (requires ink! ABI)
Fetches and manages the entire root storage structure of an ink! smart contract with type-safe access to all storage fields. Supports automatic refresh on new blocks.
Props
contract
Contract<T> | undefined
The contract instance to fetch root storage from.
watch
boolean?
Whether to watch for block changes and automatically refresh the storage. Default: false
.
Return Type
storage
RootStorage | undefined
The root storage data, fully typed based on the contract's storage structure.
isLoading
boolean
Whether the initial storage fetch is in progress.
isRefreshing
boolean
Whether a manual refresh is in progress.
error
Error | undefined
Any error that occurred during storage fetching.
refresh
() => Promise<void>
Function to manually refresh the storage data.
Basic Usage
Access token metadata:
import { useContract, useRootStorage } from 'typink';
import { Psp22ContractApi } from './types/psp22';
function TokenInfo() {
const { contract } = useContract<Psp22ContractApi>('psp22-token');
const { storage, isLoading, refresh } = useRootStorage({ contract });
if (isLoading) return <div>Loading token info...</div>;
return (
<div>
<p>Name: {storage?.name}</p>
<p>Symbol: {storage?.symbol}</p>
<p>Decimals: {storage?.decimals}</p>
<p>Total Supply: {storage?.data?.totalSupply?.toString()}</p>
<button onClick={refresh}>Refresh</button>
</div>
);
}
With watch mode:
const { storage, isLoading } = useRootStorage({
contract,
watch: true, // Auto-update on new blocks
});
// Access nested storage fields
const totalSupply = storage?.data?.totalSupply;
const tokenName = storage?.name;
Last updated
Was this helpful?