---
title: Batch writes
description: Create many atoms and triples in dependency order, quote deposits after creation, and keep each confirmed transaction for recovery.
url: https://docs.intuition.systems/docs/build/batch-writes
markdown_url: https://docs.intuition.systems/docs/build/batch-writes.md
version: V2
llms_index: https://docs.intuition.systems/llms.txt
kind: how-to
reader:
  - app
packages:
  - '@0xintuition/sdk@4.1.0'
  - '@0xintuition/protocol@4.1.0'
---

Create several terms and seed their vaults in dependency order, checking the result of each transaction before pricing the next one.

**Prompt for your agent:** Run batch writes in dependency order

```text
Goal: create many atoms and triples from my dataset in dependency order, one reviewed stage at a time.
Read https://docs.intuition.systems/docs/build/batch-writes.md, https://docs.intuition.systems/docs/build/create-atoms.md and https://docs.intuition.systems/docs/build/create-triples.md.
Use exact package versions from https://docs.intuition.systems/intuition-release.json and public entry points only.
Input: [my CSV or JSON of entities and relationships].
Derive every ID offline first and skip terms that already exist. Put atoms before the triples that use them, and keep each stage within the MultiVault batch limit.
For each stage, prepare the call and stop. Show me the stage contents, value and expected IDs, and wait for my approval. Never sign or send anything yourself.
Save every confirmed receipt so a failed run can resume after the last confirmed stage. Quote deposits only after their terms exist.
Report the checks you actually ran and anything you could not verify.
```

## Before you start

Follow [Install and pin the packages](https://docs.intuition.systems/docs/build/install), [Create atoms](https://docs.intuition.systems/docs/build/create-atoms) and [Create triples](https://docs.intuition.systems/docs/build/create-triples). Use a compatible MultiVault, a funded signer and the same interactive session for each stage. Set aside gas for **each** transaction. The example’s signer creates the terms and receives the seed shares. [Complete stage helper](https://github.com/0xIntuition/intuition-docs/blob/d928b7692bdee7f3c9e24f65bf0c38e8c26bc152/examples/v2/first-write/src/batch-writes-guide.ts)

## Steps

### 1. Split inputs at their dependencies

For example, to create Alice, “follows” and Bob, then create “Alice follows Bob” and fund its vault, prepare three stages. Batch the missing atoms first. Use their primary bytes to derive the IDs. After their original successful receipt, prepare the triple with those IDs. After the triple’s receipt, preview and seed its vault. Create any nested triples in dependency order, waiting for each prerequisite transaction.

The read-backed `multivaultCreateAndSeedTerm` action can return `create-atoms`, `create-triples` and `deposit-batch` steps in that order, with per-leg previews and a `totalValue`. Planning reads current state. A triple preview can run before its input terms exist and use fee assumptions that change once they do. Triple execution checks all three terms. A deposit preview for a term that has yet to be created reverts. Prepare each dependent stage against confirmed state. [SDK planner](https://github.com/0xIntuition/packages/blob/ccae0f0d0277cddd8490b72c49d3771866e7ffea/packages/sdk/src/actions/multivault.ts) · [MultiVault previews](https://github.com/0xIntuition/intuition-contracts-v2/blob/82cfcdb832c100fd2e53ee3f474289ae14cc6020/src/protocol/MultiVault.sol)

### 2. Build one bounded stage

The helper accepts a `kind` and its `rows`. For `atoms`, pass the primary byte strings. For `triples`, pass ordered subject, predicate and object IDs. For `seeds`, pass the target term, curve, gross native wei and slippage in basis points for each leg. Each nonempty stage must fit the deployment’s `MAX_BATCH_SIZE`. Align inputs by index and keep each term/curve pair unique in a seed batch. If one leg could materially change the next leg’s quote, submit them separately.

```ts
  if (stage.kind === 'atoms') {
    unique(stage.rows);
    const assetsIn = await multiVaultGetAtomCost(s);
    const atomsToCreate = await Promise.all(
      stage.rows.map(async (data) => ({
        data,
        assetsIn,
        termId: await checkAtomData(s, data),
      })),
    );
    return multivaultCreateAndSeedTerm(s, { receiver, atomsToCreate });
  }
  if (stage.kind === 'triples') {
    const assetsIn = await multiVaultGetTripleCost(s);
    const triplesToCreate = await Promise.all(
      stage.rows.map(async (roles) => ({
        ...roles,
        assetsIn,
        termId: (await checkTriple(s, roles)).tripleId,
      })),
    );
    unique(triplesToCreate.map((triple) => triple.termId));
    return multivaultCreateAndSeedTerm(s, { receiver, triplesToCreate });
  }
  unique(stage.rows.map((seed) => seed.termId + '/' + seed.curveId));
  const exists = await Promise.all(
    stage.rows.map(({ termId }) =>
      multiVaultIsTermCreated(s, { args: [termId] }),
    ),
  );
  if (!exists.every(Boolean))
    throw new Error('Wait for creation before quoting this deposit.');
  const plan = await multivaultCreateAndSeedTerm(s, {
    receiver,
    seedDeposits: stage.rows,
  });
```

At creation the helper pays the current per-item creation cost. For seeds, choose the actual gross assets to deposit, check the encoded `minShares` on every leg and reserve the sum as payment. Output estimates are per leg. The contract decides whether the batch can execute at those minima.

### 3. Review and send this stage

Call `submitStage(session, stage)` once for the stage you intend to send. Read the chain, MultiVault, signer, receiver, term bytes or ordered IDs, decoded arguments, calldata and exact native value in the terminal. The helper accepts exactly one planned step, simulates its complete calldata, prompts for that operation and records its submitted hash.

```ts
  const tx = { to: s.address, data: step.calldata, value: plan.totalValue };
  const call = decodeFunctionData({ abi: MultiVaultAbi, data: tx.data });
  await s.publicClient.call({ ...tx, account: s.signer });
  await review(s, step.type.toUpperCase(), {
    receiver: plan.receiver,
    stage,
    call,
    plan,
    ...tx,
  });
  const hash = await s.walletClient.sendTransaction({
    ...tx,
    account: s.walletClient.account!,
    chain: s.walletClient.chain,
  });
  const receipt = await waitForOriginal(s, hash);
```

The complete helper matches creation events to the expected IDs and seed events to the receiver, term, curve, payment and minimum shares. Use its returned receipt and plan as your record before preparing the next stage.

## Check the result

An individual batch call reverts as a unit. Earlier transactions persist if a later transaction fails. For the example above, you should have a successful atom receipt, a successful triple receipt, and then a separate successful deposit receipt. Verify each against the original hash and selected MultiVault address. Read the final receiver’s share balance for the seeded term and curve. [Deposit and redeem](https://docs.intuition.systems/docs/build/deposit-redeem)

## Troubleshooting

If confirmation times out or the wallet replaces a transaction, resolve its original hash and any replacement first. If the atom stage succeeded and triple creation reverted, keep the atom receipt. Check whether the triple now exists, derive only the outstanding work, read costs again and review a new transaction. For deposits, inspect the original receipt’s deposit events before retrying: a second successful submission would deposit a second time.

## Related

[Build action plans and envelopes](https://docs.intuition.systems/docs/build/action-plans) · [Route writes through the FeeProxy](https://docs.intuition.systems/docs/build/fee-proxy) · [Protocol write lifecycle](https://docs.intuition.systems/docs/concepts/protocol-writes)

Applies to V2 · `@0xintuition/sdk` 4.1.0 · `@0xintuition/protocol` 4.1.0
