Skip to main content
Version: 0.11 (stable)

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 its storage at storage slot 0.

Reading the Count of a Counter contract

src/lib/read-count.ts
import { WebClient, AccountId } from "@demox-labs/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";
const client = await WebClient.createClient(nodeEndpoint);
await client.syncState();

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

// Import the account into the client's database
let account = await client.getAccount(accountId);
if (account === undefined) {
account = await client.getAccount(accountId);
}

// Define counter account instance
const counter = await client.getAccount(accountId);

// Get the count from the counter account by querying the first
// storage slot.
const count = counter?.storage().getItem(0);

// Convert the 4th value of the WORD Storage value to a number.
// NOTE: The WORD Storage value is an array of 4 values, each of which is a 64-bit unsigned integer.
// NOTE: The 4th value is the u64 number of the counter.
console.log("Count:", Number(count?.toU64s()[3]));
}
Expected output
Count: 43

Reading Account Token Balances

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

src/lib/token-balance.ts
import { WebClient, AccountId } from "@demox-labs/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";
const client = await WebClient.createClient(nodeEndpoint);
await client.syncState();

const aliceId = AccountId.fromHex("0x49e27aa5fa5686102fde8e81b89999");
const faucetId = AccountId.fromHex("0x9796be9c72f137206676f7821a9968");

// Import the account into the client's database
let aliceAccount = await client.getAccount(aliceId);
if (aliceAccount === undefined) {
await client.importAccountById(aliceId);
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