Skip to main content
Version: 0.13

Read Storage Values

Let's explore how to interact with public accounts and retrieve their storage data.

Understanding Account Storage​

Miden accounts contain several types of data you can read.

Account Components:

  • Vault: Contains the account's assets (tokens)
  • Storage: Key-value data store with up to 255 slots
  • Code: The account's smart contract logic (MAST root)
  • Nonce: Nonce that increments with each state change to prevent double spend

Storage Visibility:

  • Public accounts: All data is publicly accessible and can be read by anyone
  • Private accounts: Only commitments are public; full data is held privately

Set Up Development Environment​

To run the code examples in this guide, you'll need to set up a development environment. If you haven't already, follow the setup instructions in the Accounts guide.

Reading from a Public Smart Contract​

Let's interact with a counter contract deployed on the Miden testnet. This contract maintains a simple counter value in a named storage map slot.

Reading the Count of a Counter contract​

src/lib/read-count.ts
import { WebClient, AccountId, Word } from "@miden-sdk/miden-sdk";

export async function demo() {
// Initialize client to connect with the Miden Testnet.
// NOTE: The client is our entry point to the Miden network.
// All interactions with the network go through the client.
const nodeEndpoint = "https://rpc.testnet.miden.io:443";

// Initialize client
const client = await WebClient.createClient(nodeEndpoint);
await client.syncState();

const accountId = AccountId.fromHex("0x224a96d294e10d006aef3d4f1b0876");

// Import the account into the client's database
await client.importAccountById(accountId);
const counter = await client.getAccount(accountId);

// Get the count from the counter account by querying its storage map
// using the named storage slot and counter key.
const slotName = "miden::component::miden_counter_account::count_map";
const counterKey = new Word(BigUint64Array.from([0n, 0n, 0n, 1n]));
const count = counter?.storage().getMapItem(slotName, counterKey);

// The count value is a WORD (array of 4 u64 values).
// The 4th value is the counter number.
console.log("Count:", Number(count?.toU64s()[3]));
}
Expected output
Count: 1

Reading Account Token Balances​

You can also query the assets (tokens) held by an account:

src/lib/token-balance.ts
import { WebClient, AccountId } from "@miden-sdk/miden-sdk";

export async function demo() {
// Initialize client to connect with the Miden Testnet.
// NOTE: The client is our entry point to the Miden network.
// All interactions with the network go through the client.
const nodeEndpoint = "https://rpc.testnet.miden.io:443";

// Initialize client
const client = await WebClient.createClient(nodeEndpoint);
await client.syncState();

const aliceId = AccountId.fromHex("0x5b2840a923dedc102ea67e0c1eba3c");
const faucetId = AccountId.fromHex("0x29dd1dc628d2842032e751ed1b5da7");

// Import the account into the client's database
await client.importAccountById(aliceId);
const aliceAccount = await client.getAccount(aliceId);

const balance = aliceAccount?.vault().getBalance(faucetId);
console.log("Alice's TEST token balance:", Number(balance));
}
Expected output
Alice's TEST token balance: 900