Repository navigation
Add guide to simulate contract calls - #2891
leighmcculloch wants to merge 8 commits into
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
A missing generated route and several documentation inaccuracies must be corrected before approval.
Review effort: Lite
Findings: 1
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 | ||
| ``` | ||
|
|
||
| ``` |
| 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. |
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The guide contains a failing authorization example and several unresolved documentation and SDK example issues.
Review effort: Lite
Findings: 2
Open (7)
Sign authorization entries in transfer simulation · New Regenerate routes.txt with the new documentation route Document numeric SDK state-change type values · New Clarify automatic versus manual archival restoration Correct the CLI guidance for enforce authorization mode Clarify stateChanges versus the complete transaction footprint Label the CLI output fences as text
| 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 }); |
| - `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 |
| @@ -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. | |||
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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".
|
where do I pick up
…On Tue, Oct 6, 2026, 11:54 AM Nando Vieira ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In docs/build/guides/transactions/simulate-contract-calls.mdx
<#2891 (comment)>
:
> + }
+ ],
+ "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.
+
+The full response also contains the fields used to submit the transaction: `transactionDataJson` (the ledger footprint and resources), `results[0].authJson` (the authorization entries to sign), and `minResourceFee`.
+
+## Using the JavaScript SDK
+
+Install the SDK:
+
+```bash
+npm install @stellar/stellar-sdk
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".
—
Reply to this email directly, view it on GitHub
<#2891?email_source=notifications&email_token=A5LQRFJ3KVGI2RRUAU3EWOD5SU5VRA5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTKNBTGMYTSOJUGYYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#pullrequestreview-5433199460>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A5LQRFKWB3DABH6DBIC3TFL5SU5VRAVCNFSNUABFKJSXA33TNF2G64TZHM2TEMJTGA3TOMRZHNEXG43VMU5TKNRXG4ZTAMJZGM32C5QC>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
|
bash I can switch
…On Tue, Oct 6, 2026, 12:09 PM gre3n3yes34 ***@***.***> wrote:
where do I pick up
On Tue, Oct 6, 2026, 11:54 AM Nando Vieira ***@***.***>
wrote:
> ***@***.**** commented on this pull request.
> ------------------------------
>
> In docs/build/guides/transactions/simulate-contract-calls.mdx
> <#2891 (comment)>
> :
>
> > + }
> + ],
> + "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.
> +
> +The full response also contains the fields used to submit the transaction: `transactionDataJson` (the ledger footprint and resources), `results[0].authJson` (the authorization entries to sign), and `minResourceFee`.
> +
> +## Using the JavaScript SDK
> +
> +Install the SDK:
> +
> +```bash
> +npm install @stellar/stellar-sdk
>
> 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".
>
> —
> Reply to this email directly, view it on GitHub
> <#2891?email_source=notifications&email_token=A5LQRFJ3KVGI2RRUAU3EWOD5SU5VRA5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTKNBTGMYTSOJUGYYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#pullrequestreview-5433199460>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/A5LQRFKWB3DABH6DBIC3TFL5SU5VRAVCNFSNUABFKJSXA33TNF2G64TZHM2TEMJTGA3TOMRZHNEXG43VMU5TKNRXG4ZTAMJZGM32C5QC>
> .
> You are receiving this because you are subscribed to this thread.Message
> ID: ***@***.***>
>
|



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.