From midnight-dapp
Use when reading public ledger state, implementing reactive UI that updates with chain state, caching state for performance, or understanding public vs private state in Midnight DApps.
How this skill is triggered — by the user, by Claude, or both
Slash command
/midnight-dapp:skills/state-managementThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Read, sync, and cache contract state in Midnight DApps with proper handling of the dual-state model.
examples/cache-manager/CacheManager.tsexamples/cache-manager/cacheConfig.tsexamples/state-sync-provider/StateSyncProvider.tsxexamples/state-sync-provider/useStateSync.tsexamples/use-contract-state/types.tsexamples/use-contract-state/useContractState.tsreferences/chain-sync.mdreferences/contract-state.mdreferences/privacy-aware-caching.mdreferences/web3-comparison.mdRead, sync, and cache contract state in Midnight DApps with proper handling of the dual-state model.
Midnight contracts have two types of state:
| State Type | Storage Location | Access Method | Visibility |
|---|---|---|---|
| Public Ledger State | On-chain (indexer) | contract.state.* | Anyone can read |
| Private Local State | Browser (LevelDB) | WitnessContext.privateState | Only local user |
This is fundamentally different from Ethereum where all state is public on-chain.
// Public state - anyone can read
const totalSupply = await contract.state.total_supply();
const balance = await contract.state.balances.get(address);
// Private state - accessed only in witnesses
const witnesses = {
get_secret: ({ privateState }) => privateState.secretKey,
};
State on-chain can change from other transactions. DApps must handle:
| Document | Description |
|---|---|
| contract-state.md | Reading public and private contract state |
| chain-sync.md | Synchronization patterns and subscriptions |
| privacy-aware-caching.md | Safe caching strategies for Midnight |
| web3-comparison.md | Ethereum state patterns vs Midnight |
| Example | Description |
|---|---|
| use-contract-state/ | React hook for reading contract state |
| state-sync-provider/ | Context provider for state synchronization |
| cache-manager/ | Privacy-aware caching utilities |
// After contract deployment, read public state
const contract = await deployedContract();
// Simple value
const totalSupply: bigint = await contract.state.total_supply();
// Map lookup
const balance: bigint | undefined = await contract.state.balances.get(userAddress);
// Set membership
const isMember: boolean = await contract.state.members.has(userAddress);
function useContractState<T>(
contract: Contract,
accessor: (state: ContractState) => Promise<T>
) {
const [value, setValue] = useState<T | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
let cancelled = false;
async function fetchState() {
setLoading(true);
const result = await accessor(contract.state);
if (!cancelled) {
setValue(result);
setLoading(false);
}
}
fetchState();
return () => { cancelled = true; };
}, [contract, accessor]);
return { value, loading };
}
// Poll for state changes
useEffect(() => {
const interval = setInterval(async () => {
const newBalance = await contract.state.balances.get(address);
setBalance(newBalance);
}, 5000); // Poll every 5 seconds
return () => clearInterval(interval);
}, [contract, address]);
interface ContractState {
total_supply(): Promise<bigint>;
balances: {
get(address: string): Promise<bigint | undefined>;
};
members: {
has(address: string): Promise<boolean>;
};
}
// The compiler generates these types from your Compact contract
async function safeStateRead<T>(
accessor: () => Promise<T>,
fallback: T
): Promise<T> {
try {
return await accessor();
} catch (error) {
console.error('State read failed:', error);
return fallback;
}
}
const balance = await safeStateRead(
() => contract.state.balances.get(address),
0n
);
// Update UI immediately, then sync with chain
const [balance, setBalance] = useState<bigint>(0n);
async function transfer(to: string, amount: bigint) {
// Optimistic update
setBalance(prev => prev - amount);
try {
await contract.callTx.transfer(to, amount, witnesses);
// State will sync on next poll
} catch (error) {
// Revert optimistic update
setBalance(prev => prev + amount);
throw error;
}
}
wallet-integration - Required for setting up providersproof-handling - Witness implementation that accesses private statetransaction-flows - Submitting state-changing transactionserror-handling - Handling state read errors/dapp-check - Validates provider configuration/dapp-debug state - Debug state synchronization issuesnpx claudepluginhub aaronbassett/midnight-knowledgebase --plugin midnight-dappThis skill should be used when the user asks about the Midnight.js SDK, midnight-js packages, @midnight-ntwrk npm packages, setting up SDK providers, deploying or finding contracts with deployContract or findDeployedContract, calling circuits with callTx or submitCallTx, the transaction lifecycle, SDK provider types (WalletProvider, MidnightProvider, PublicDataProvider, ProofProvider, ZkConfigProvider, PrivateStateProvider), testkit-js testing, observable state subscriptions, contract maintenance and verifier keys, or connecting to the indexer or proof server.
This skill should be used when the user asks about Midnight network architecture, transaction structure, guaranteed vs fallible sections, Zswap/Kachina integration, ledger and state management, cryptographic binding, balance verification, nullifiers, address derivation, transaction merging, atomic swaps, fee handling, or the privacy model separating private and public domains.
This skill should be used when the user asks about Midnight transaction execution, guaranteed vs fallible phases, kernel.checkpoint(), transaction composition, state conflicts, DUST fees, gas limits, proof verification, partial transaction success, transaction merging, atomic swaps, or how Compact circuits map to on-chain execution. Also triggered by mentions of "transaction semantics", "fallible phase", "guaranteed phase", "checkpoint", "well-formedness", "Impact VM", or "Zswap offers".