Skip to content

fix(soroban): make the smart-contract cost guide's examples compile on SDK 28 - #2903

Merged
ElliotFriend merged 1 commit into
stellar:mainfrom
water-k-max:soroban-cost-guide-examples
Oct 12, 2026
Merged

ElliotFriend merged 1 commit into
stellar:mainfrom
water-k-max:soroban-cost-guide-examples

Conversation

@water-k-max

Copy link
Copy Markdown
Contributor

Closes #1845. Part of #1837.

Every Rust block in this guide was extracted with a script and compiled in an
isolated crate against soroban-sdk 28.0.0 / stellar-cli 28.1.0
(stellar contract build --optimize=false). Before this change, 7 of the 8
full-contract examples failed to build; only the "Optimized Using Events"
example compiled. The failures include three of the four blocks the guide
presents as the correct pattern: both "Optimal Contract ✅" blocks and the
"Alternative: using keyed vecs ✅" block it recommends.

What was broken, and why

Symptom in the guide Compiler response Fix
persistent().get("total_count") Val: TryFromVal<Env, str> not satisfied; str has no known size keys are passed by reference and are Symbol / #[contracttype] values
get(...) result used as the value cannot add _ to Option<_>, += on Option<_>, no method push for Option unwrap_or(...) / unwrap_or_else(...)
moves.push(x) on a Soroban Vec no method named push found for soroban_sdk::Vec<T> push_back
.unwrap_or_default() on Vec<Symbol> soroban_sdk::Vec<T>: Default not satisfied `unwrap_or_else(
.iter().filter().map().collect() Vec<Symbol>: FromIterator<Symbol> not implemented explicit loop building Vec::new(&env)
plain struct/enum as a stored value or key Val: TryFromVal<Env, DataKey> not satisfied #[contracttype]
Vec<u32> / Vec<Address> in signatures cannot find type Vec in this scope Vec added to the use soroban_sdk::{...} list
token_client.transfer(&from, recipient, &amt) SEP-41 transfer takes to: MuxedAddress (soroban-sdk-28.0.0/src/token.rs:154) Vec<MuxedAddress> recipients, MuxedAddress::from(...) for the contract itself

CLI wording

  • stellar contract build --help on 28.1.0 describes --optimize as
    "Enabled by default; pass --optimize=false to disable." Measured on this
    guide's own kind of contract: 10,067 bytes with --optimize=false, 8,575
    bytes with the default build. The guide told readers to add the flag to get
    optimization, which is no longer the step that matters.
  • --cost was described as producing a breakdown for any invoke. It reports
    what the network charged, so it prints the fee table only when a transaction
    is sent; with --send=no the call is simulated and --cost prints nothing.

Structure

"### 5. Contract size" duplicated "### 5. Use of appropriate
env.storage mechanisms", and the temporary()/instance() headings were at
the same level as their parent while persistent() was one level below it.

Verification

Re-extracting the blocks from the edited file and rebuilding each one: all
eight full-contract examples pass. The two remaining ```rust fences are inline
expression fragments (let mut v1 = alloc::vec![]; / `let mut v2 = [0; 100];`)
that are not standalone crates, so they were not compiled.

Formatting: pnpm exec prettier --config .prettierrc.js -c <file> reports "All
matched files use Prettier code style!" — re-run after the prose edits, exit 0. I did
not run a full pnpm build, so I am claiming the file parses and is format-clean, not
that I inspected the rendered page. The three prose changes are a 3/3-line delta that
touches no code fence, so the compile results above still describe the shipped
examples.

Storage prose (§3 bullet, §5)

Three prose lines were changed alongside the code, because stellar network settings --network testnet and the SDK source contradict them:

  • Two places state that temporary storage is cleared at the end of the invocation /
    after each transaction. The SDK's own temporary() doc says entries are "removed
    from the ledger after their lifetime ends… such as oracle data, claimable
    balances, offer", and testnet's min_temporary_ttl is 720 ledgers (~1 hour at the
    5 s target close), so they outlive the invocation. §5 also contradicted itself: the
    same paragraph said "cleared at the end of the contract invocation" while its
    Lifetime bullet said "exists for a predefined period and is then removed".
  • The instance-storage cap was given as "typically in the order of 100 KB
    serialized". It is a validator-set network parameter, and today it is 65,536 bytes
    (contract_data_entry_size_bytes; keys are capped by contract_data_key_size_bytes
    = 250). I replaced the number with a link to the live value, which is the pattern
    docs: correct storage cost ranking and entry size limit claim #2880 used in choosing-the-right-storage.mdx.

Two nearby claims I left deliberately: "Likely cheaper than storing data separately
in persistent storage" and "does not appear in the ledger footprint" are the SDK's own
wording for instance(), and "Highest cost compared to others" for persistent
matches the SDK's "the more expensive storage type". They read oddly next to #2880's
persistent/instance "same rent class" table, but that table is about durability while
those sentences are about access pattern, so I did not want to force a rewrite. If
you would rather §5 just defer to choosing-the-right-storage.mdx, say so and I will
swap in a link instead.

Still untouched: the events-vs-storage and batch-vs-loop pricing narrative, and the
"use of built-in tools" section's fee claims. I did not measure those, so I have not
asserted anything about them either way. Happy to open a follow-up issue if useful.

Seven of the guide's eight full contract examples did not build against
soroban-sdk 28.0.0 / stellar-cli 28.1.0, including three of the four blocks the
guide presents as the correct pattern (both "Optimal Contract" blocks and the
keyed-vecs alternative it recommends).

Storage examples: pass keys by reference (`get(&KEY)`); a bare `&str` key fails
the `TryFromVal<Env, str>` bound on `Val`; use `Symbol`/`#[contracttype]` keys;
unwrap the `Option` that `get` returns; `push_back`, not `push`, on
`soroban_sdk::Vec`; `unwrap_or_else(|| Vec::new(&env))`, because
`soroban_sdk::Vec<T>` implements neither `Default` nor `FromIterator`, so
`unwrap_or_default()` and iterator-chain `.collect()` are both unavailable; add
the missing `Vec` import and the `#[contracttype]` attribute on types used as
storage values and keys.

Token examples: SEP-41 `transfer` takes `to: MuxedAddress`, so the recipient
vectors become `Vec<MuxedAddress>` and the contract's own address is converted
with `MuxedAddress::from`.

CLI guidance: `stellar contract build` optimizes by default in current
stellar-cli, so document `--optimize=false` and how to tell which behaviour a
version has; `--cost` prints only for a sent transaction, and prints nothing
with `--send=no`.

Storage prose: temporary entries are not cleared at the end of the invocation —
they live until their TTL (testnet `min_temporary_ttl` is 720 ledgers), so the
§5 description and one §3 trade-off bullet are wrong, and §5 also contradicted
itself. The instance-storage size cap is a validator-set network parameter
(testnet `contract_data_entry_size_bytes` is 65,536, not "on the order of
100 KB"), so link the live network limits instead of asserting a number. Both
points match how stellar#2880 corrected the sibling storage guide.

Also renumber the duplicated "### 5" section and level the two storage-API
headings that sat at the same depth as their parent.

Closes stellar#1845
@stellar-doxie

stellar-doxie Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

🤖 Automated message from the Stellar Triage Bot.

Thanks for the PR. I saw it, but I review only when someone calls me in.

How to reach me:

  • Anyone with write permission can call me in with a /triage comment or the bot:take label.
  • The PR author can send me away with /triage stop.
  • People with write permission can also use the bot:hands-off label.
  • A review request to a maintainer does not call me in. It asks for their judgment.

@kaankacar

Copy link
Copy Markdown
Contributor

/triage

@stellar-doxie

stellar-doxie Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

🤖 Automated message from the Stellar Triage Bot.

Reviewing because @kaankacar asked with /triage.

Verified against the soroban-sdk 28.0.0 and stellar-cli 28.1.0 source. The SDK calls, the --optimize default and the --cost behavior match. Ready to merge.

  • The "Build, format MDX and Lint" run waits for first-time contributor approval. Approve it and let it go green before you merge. No live preview was available.
  • Closes #1845 closes the guide issue, but the PR leaves the pricing prose unreviewed. Change it to Refs #1845 if the issue must stay open.

External-origin change, so a maintainer merges this.

@stellar-doxie stellar-doxie Bot added the bot:ready-to-merge Bot verified and approved; waiting for a maintainer to merge label Oct 7, 2026
@ElliotFriend
ElliotFriend merged commit 902971c into stellar:main Oct 12, 2026
6 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

PR Preview: torn down

oceans404 added a commit that referenced this pull request Oct 21, 2026
Four conflicts resolved: two storage headings moved down a level with
the export's anchor ids kept, and the --optimize and --cost passages
taken from upstream with root-relative links.

Checks: scope 1 file, all 10 Rust samples identical to main, parity
all upstream lines present (6 differ only by heading ids and link
format), guard clean, mint validate passed, broken-links at baseline
8, page 200.
ElliotFriend pushed a commit that referenced this pull request Oct 21, 2026
Port of 902971c to the Mintlify tree.

Refs #2903

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ElliotFriend added a commit that referenced this pull request Oct 22, 2026
)

Replaces the Docusaurus site with Mintlify's converted tree (base bcec501),
with tooling trimmed to prettier, husky, redocly, the OpenRPC build, and the
mint CLI. Commits that landed on main after bcec501 were ported by hand.

Ported:
Refs #2448
Refs #2671
Refs #2672
Refs #2727
Refs #2827
Refs #2828
Refs #2837
Refs #2856
Refs #2857
Refs #2869
Refs #2871
Refs #2878
Refs #2880
Refs #2881
Refs #2885
Refs #2886
Refs #2890
Refs #2892
Refs #2893
Refs #2895
Refs #2900
Refs #2901
Refs #2902
Refs #2903
Refs #2905
Refs #2912
Refs #2913
Refs #2915
Refs #2918
Refs #2922

Merged into the branch:
Refs #2936
Refs #2937
Refs #2938
Refs #2940

Co-authored-by: alexistabak-cpu <[email protected]>
Co-authored-by: Emir Ayral <[email protected]>
Co-authored-by: John Wooten <[email protected]>
Co-authored-by: Kaan Kacar <[email protected]>
Co-authored-by: Leigh <[email protected]>
Co-authored-by: maz <[email protected]>
Co-authored-by: Michael Lip <[email protected]>
Co-authored-by: Scopuly <[email protected]>
Co-authored-by: stellar-triage-bot[bot] <333411864+stellar-triage-bot[bot]@users.noreply.github.com>
Co-authored-by: Steph <[email protected]>
Co-authored-by: urvisavla <[email protected]>
Co-authored-by: water-k-max <[email protected]>
Co-authored-by: Wlademyr Mendes <[email protected]>
Co-Authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:ready-to-merge Bot verified and approved; waiting for a maintainer to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update guide: Analyzing smart contract cost & efficiency

3 participants