Skip to content

Add guide to simulate contract calls - #2891

Draft
leighmcculloch wants to merge 8 commits into
mainfrom
simulation-read-write-guide
Draft

leighmcculloch wants to merge 8 commits into
mainfrom
simulation-read-write-guide

Conversation

@leighmcculloch

@leighmcculloch leighmcculloch commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

What

Add a guide showing how to use simulateTransaction, via the Stellar CLI and the JavaScript SDK, to call read-only contract functions and to preview the return value, state changes, events, and auth of a write before submitting it.

Why

Existing simulation docs explain the RPC method, or simulation in the context of something else, but don't show developers how to read contract data for free or inspect what a write will change before signing it.

@leighmcculloch
leighmcculloch requested review from a team and ElliotFriend and a lite review from Copilot October 2, 2026 12:22
@github-actions github-actions Bot added the preview Preview builds for PRs by SDF employees. label Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

A missing generated route and several documentation inaccuracies must be corrected before approval.

Review effort: Lite
Findings: 1 High severity · 3 Medium severity · 1 Low severity

Open (5)
What changed in this PR

Adds a guide for simulating Stellar contract calls with the CLI and JavaScript SDK.

Changes:

  • Demonstrates read-only calls and write previews.
  • Covers results, events, state changes, authorization, fees, and archival behavior.
  • Adds SDK, raw RPC, and transaction-building examples.
File Review summary
docs/​build/​guides/​transactions/​simulate-contract-calls.mdx Add the missing routes.txt entry, label all code fences, and correct the CLI, footprint, state-change, archival, and build-only guidance.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@@ -0,0 +1,429 @@
---
title: Simulate contract calls to read data and preview changes

##### State changes

`sim.stateChanges` lists every ledger entry the call would touch. Each change has:

- **Simulation reflects the ledger at the time you simulate.** If other transactions change the same entries before yours is applied, the real result can differ. The `latestLedger` field in the response tells you which ledger the simulation ran against.
- **Fees aren't included in state changes.** The balances in `stateChanges` reflect only what the contract did. The transaction fee is charged to the source account separately when the transaction is applied.
- **Simulation doesn't check signatures.** When the transaction has no authorization entries, simulation runs in recording mode: it records which addresses must authorize the call, but doesn't verify any signatures or run custom account `__check_auth` logic. A successful simulation doesn't guarantee the signed transaction will pass authorization. To check authorization entries you've already signed, simulate again with the `enforce` auth mode (`--auth-mode enforce` in the CLI, or the `authMode` argument to `server.simulateTransaction()`).
- **Simulation reflects the ledger at the time you simulate.** If other transactions change the same entries before yours is applied, the real result can differ. The `latestLedger` field in the response tells you which ledger the simulation ran against.
- **Fees aren't included in state changes.** The balances in `stateChanges` reflect only what the contract did. The transaction fee is charged to the source account separately when the transaction is applied.
- **Simulation doesn't check signatures.** When the transaction has no authorization entries, simulation runs in recording mode: it records which addresses must authorize the call, but doesn't verify any signatures or run custom account `__check_auth` logic. A successful simulation doesn't guarantee the signed transaction will pass authorization. To check authorization entries you've already signed, simulate again with the `enforce` auth mode (`--auth-mode enforce` in the CLI, or the `authMode` argument to `server.simulateTransaction()`).
- **Archived entries need restoring first.** If the call touches archived entries, the response includes a `restorePreamble` that describes the restore transaction to submit first. See [Restore archived contract data using the JavaScript SDK](../archival/restore-data-js.mdx).
stellar contract id asset --asset native
```

```
Comment on lines +105 to +167
To get the response in a readable form, build the transaction with the CLI and send it to `simulateTransaction` yourself. `--build-only` writes the unsigned transaction to stdout. The RPC's `xdrFormat: "json"` parameter returns every XDR field decoded as JSON:

```bash
TX=$(stellar contract invoke \
--id native \
--build-only \
-- \
transfer --from alice --to bob --amount 10000000)

curl -s https://soroban-testnet.stellar.org \
-H 'Content-Type: application/json' \
-d "{
\"jsonrpc\": \"2.0\",
\"id\": 1,
\"method\": \"simulateTransaction\",
\"params\": { \"transaction\": \"$TX\", \"xdrFormat\": \"json\" }
}" \
| jq '.result | {
returnValue: .results[0].returnValueJson,
events: [.eventsJson[] | select(.event.type == "contract") | .event.body.v0],
stateChanges: [.stateChanges[] | {
type,
key: .keyJson,
balanceBefore: .beforeJson.data.account.balance,
balanceAfter: .afterJson.data.account.balance
}],
minResourceFee
}'
```

```json
{
"returnValue": "void",
"events": [
{
"topics": [
{ "symbol": "transfer" },
{ "address": "GC6N…VHCV" },
{ "address": "GASO…EJNSB" },
{ "string": "native" }
],
"data": { "i128": "10000000" }
}
],
"stateChanges": [
{
"type": "updated",
"key": { "account": { "account_id": "GASO…EJNSB" } },
"balanceBefore": "100000000000",
"balanceAfter": "100010000000"
},
{
"type": "updated",
"key": { "account": { "account_id": "GC6N…VHCV" } },
"balanceBefore": "100000000000",
"balanceAfter": "99990000000"
}
],
"minResourceFee": "23460"
}
```

The response shows that the transfer would move 1 XLM from `alice` to `bob`, emit one `transfer` event, and update two account entries. The `jq` filter picks out account balances because the native asset contract stores G-address balances on the account entries. For other contracts, look at the whole `beforeJson` and `afterJson` objects. Contract storage appears as `contract_data` entries.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's pretty hard to get at additional info about the simulation like the state changes with the stellar-cli. We should improve this, so I opened:

Copilot AI lite review requested due to automatic review settings October 2, 2026 15:41
@github-actions github-actions Bot removed the preview Preview builds for PRs by SDF employees. label Oct 2, 2026
@github-actions github-actions Bot added the preview Preview builds for PRs by SDF employees. label Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +341 to +344
If the preview looks right, sign and submit the transaction, passing a `signTransaction` function from a wallet or from `contract.basicNodeSigner`:

```js
await transfer.signAndSend({ signTransaction });
Comment on lines +288 to +291
- `type`: `"created"`, `"updated"`, or `"deleted"`
- `key`: the `LedgerKey` of the entry
- `before`: the `LedgerEntry` before the call, or `null` if it would be created
- `after`: the `LedgerEntry` after the call, or `null` if it would be deleted
@leighmcculloch
leighmcculloch marked this pull request as draft October 6, 2026 02:09
@@ -0,0 +1,429 @@
---
title: Simulate contract calls to read data and preview changes
description: Use the Stellar CLI and the JavaScript SDK to call read-only contract functions for free, and to preview the return value, state changes, and events of a write before submitting it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a sidebar_position to the frontmatter. Most sibling guides in this folder set one (create-account 5, signing-soroban-invocations 15, fee-sponsorship 35, etc.); without it this guide sorts by the default rather than being placed intentionally alongside the related invoke/simulate guides.

Install the SDK:

```bash
npm install @stellar/stellar-sdk

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth pinning a minimum SDK version here. The JS examples rely on the ergonomic XDR API introduced in @stellar/stellar-sdk v17 — property-style access (change.key.type, change.before?.data.account.balance), array footprints (footprint.readWrite.map(...)), and .toJson(). I verified these all work against 17.2.1, but on an older SDK (v13–16) XDR objects use method accessors (.switch(), .data(), .readWrite()) and every one of those snippets throws. Since the rest of the docs still use the old method style, the dependency is easy to miss — suggest something like "requires @stellar/stellar-sdk 17 or later".

@gre3n3yes34

gre3n3yes34 commented Oct 6, 2026 via email

Copy link
Copy Markdown

@gre3n3yes34

gre3n3yes34 commented Oct 6, 2026 via email

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Preview builds for PRs by SDF employees.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants