useLazyStorage
⚠️ ink! v5 and v6 only (requires ink! ABI)
Fetches specific lazy storage values from ink! smart contracts with type-safe access to individual fields, mappings, vectors, and objects. Supports automatic refresh on new blocks.
Props
contract
Contract<T> | undefined
The contract instance to fetch lazy storage from.
fn
(lazy: LazyStorage) => any
Function that navigates the lazy storage structure and returns the value to fetch.
watch
boolean?
Whether to watch for block changes and automatically refresh the data. Default: false
.
Return Type
data
T | undefined
The fetched data, fully typed based on the accessor function result.
isLoading
boolean
Whether the initial data fetch is in progress.
isRefreshing
boolean
Whether a manual refresh is in progress.
error
Error | undefined
Any error that occurred during data fetching.
refresh
() => Promise<void>
Function to manually refresh the data.
Basic Usage
Fetch balance from PSP22 mapping:
import { useContract, useLazyStorage } from 'typink';
import { Psp22ContractApi } from './types/psp22';
function UserBalance() {
const { contract } = useContract<Psp22ContractApi>('psp22-token');
const { connectedAccount } = useTypink();
const { data: balance, isLoading } = useLazyStorage(
connectedAccount?.address
? {
contract,
fn: (lazy) => lazy.data.balances.get(connectedAccount.address),
watch: true, // Auto-update on new blocks
}
: undefined
);
if (isLoading) return <div>Loading balance...</div>;
return <div>Balance: {balance?.toString()}</div>;
}
Fetch with manual refresh:
const { data: balance, refresh, error } = useLazyStorage({
contract,
fn: (lazy) => lazy.data.balances.get(targetAddress),
watch: false,
});
// Manually refresh after a transaction
await mintTx.signAndSend({ args: [amount] });
await refresh();
Last updated
Was this helpful?