Skip to content
Termbook
App integrations

Read account state

Return a complete account snapshot anchored to one canonical block.

On this page

Read context

ReadContext
{
  client,
  credit_id,
  account,
  block: { number: 123456n, hash: block_hash },
}

Termbook supplies the public client, Credit ID, bound Credit Account and block identity. Every RPC read must use context.block.number. Return the same number and hash. Do not combine balances from latest with orders from a different block.

Read actual balances

read.ts · atomic spot holdings
import { check_state, type ReadContext, type PluginStates } from "@termbook/sdk";
import { parseAbi, zeroAddress, type Address } from "viem";

const erc20 = parseAbi(["function balanceOf(address) view returns (uint256)"]);

export async function read_spot(context: ReadContext,
  assets: Address[],
): Promise<PluginStates["spot"]> {
  const holdings = await Promise.all(assets.map(async (asset) => {
    const amount = asset === zeroAddress
      ? await context.client.getBalance({
          address: context.account, blockNumber: context.block.number,
        })
      : await context.client.readContract({
          address: asset, abi: erc20, functionName: "balanceOf",
          args: [context.account], blockNumber: context.block.number,
        });
    return {
      holding_id: asset.toLowerCase(), asset, amount,
      held_at: context.account, controlled_by: context.account,
      withdrawable_now: amount,
    };
  }));

  return check_state("spot", {
    block: context.block, holdings, orders: [], liabilities: [],
  }, context);
}

Uniform records

RecordRequired fields
Holdingholding_id, asset, amount, held_at, controlled_by, withdrawable_now
Liabilityliability_id, asset, amount
Orderorder_id, market_id, owner, direction, input_asset, remaining_input, filled_input, output_asset, filled_output, exposure, status
Positionposition_id, market_id, owner, assets, liability, recoverable_value + use-case fields
Requestrequest_id, owner, market_id, assets, liability, recoverable_value, claimable_at, optional replaces_position_id

amount and value fields are bigint. Record IDs must be unique. All owners and controlled_by values must match the Credit Account. withdrawable_now cannot exceed amount. Request claim times are Unix seconds.

What gets checked

assess.ts
import { assess_state } from "@termbook/sdk";

const state = await exchange.account.read(context);
const assessment = await assess_state(exchange.definition, state, context);
if (!assessment.verified) throw new Error("unverified-state");

Assessment compares holdings with native or ERC-20 balances, compares positions and requests with shared onchain readers, reconciles orders and liabilities, rejects omitted known holdings and rechecks the canonical block hash. Reported recoverable value cannot exceed independently computed value.

Documentation
8 results

Let’s talk

Tell us what you have in mind.

Supply liquidity