Skip to content

Fix broken JS SDK code snippets (invalid syntax, missing imports, undefined variables) #2562

Open

Description

@oceans404

Context

An audit of JS SDK content across the docs found snippets that fail if copied as written. Each item below is a concrete fix with an exact location. These are copy-paste bugs, not API staleness; the surrounding pages are otherwise current.

Checklist

docs/data/apis/horizon/api-reference/errors/error-handling.mdx

  • Line 69: let server = sdk.Server("https://horizon-testnet.stellar.org"); uses the pre-v10 constructor without new and without the Horizon namespace. Change to let server = new sdk.Horizon.Server("https://horizon-testnet.stellar.org");
  • Line 97: time.sleep(5 * time.Second); is Go code inside a JS block. Replace with await new Promise((resolve) => setTimeout(resolve, 5000));
  • Line 138: let server = Horizon.Server(...) is missing new. Also normalize the inconsistent sdk. vs StellarSdk. naming across the page's snippets.
  • While in the file, cross-link the SDK error handling guide: https://stellar.github.io/js-stellar-sdk/guides/05-handle-errors/

docs/learn/fundamentals/contract-development/contract-interactions/stellar-transaction.mdx

  • Line 48: rpc as StellarRpc appears inside a CommonJS require() destructure. as aliasing is import-statement syntax; in a destructure it must be rpc: StellarRpc (or convert the block to an ESM import).

docs/learn/fundamentals/contract-development/contract-interactions/transaction-simulation.mdx

  • Line 75 and line 129: const s = Server("https://soroban-testnet.stellar.org"); is missing new.
  • The snippets use BASE_FEE, nativeToScVal, and Address without importing them. Add them to the import statements.

docs/tokens/how-to-issue-an-asset.mdx

  • Line 118: the secret key is wrapped in curly quotes (‘...’), which is a syntax error if copied. Replace with straight quotes.
  • Early snippets on the page use a global StellarSdk with no import shown; add the import * as StellarSdk from "@stellar/stellar-sdk"; line (the full sample at the bottom already has it).

docs/build/guides/transactions/path-payments.mdx

  • Line 77: await SERVER.submitTransaction(transaction) references an undefined SERVER. Use the Horizon server variable defined earlier in the snippet.

docs/data/apis/rpc/api-reference/methods/getLedgerEntries.mdx

  • Line 387: const s = new Server(...) with no import shown and no namespace. Show import { Server } from "@stellar/stellar-sdk/rpc"; (and the AssetType import used nearby).

Secondary (undefined variables, verify while in the file)

Acceptance criteria

  • Every JS/TS snippet touched parses (paste into a TS playground or run npx tsc --noEmit on extracted snippets).
  • No JS block contains non-JS code.

Activity

  1. added theissue type on Jul 7, 2026
  2. kalepail commented on Nov 2, 2026

    @kalepail
    Contributor

    Raven rechecked the contract-restoration example on 2026-11-02 and found more defects than this issue lists. The empty return type and the missing xdr import on restore-contract-js line 69 are still present. The same function is also on the state-archival page line 365.

    Additional defects in the same example:

    • getWasmLedgerKey(instance) receives the LedgerKey from c.getFootprint(), but reads it as a ContractDataEntry. The instance ledger key holds no WASM hash. The hash is in the instance ledger entry. With @stellar/stellar-sdk 17.4.0 the path is response.entries[0].val.value.val.value.executable.value, after a check that the executable is contractExecutableWasm.
    • The snippet puts the getLedgerEntries response into setReadWrite([instance, wasmEntry]). The footprint needs two LedgerKey values.
    • The restoration transaction sets no time bounds.
    • In restore-data-js, line 43 declares const { Api, assembleTransaction } = StellarRpc;. The guide tells readers to reuse submitTx from submit-transaction-wait-js, which imports Api from @stellar/stellar-sdk/rpc. The combined code fails TypeScript with TS2440: Import declaration conflicts with local declaration of 'Api'.

    A local check with SDK 17.4.0 read the instance entry through that path, built the contract-code key, put both keys in the read-write footprint, set time bounds, and signed the transaction. No transaction was submitted.

    Source record (immutable): https://github.com/stellar-experimental/stellar-raven/blob/9e3ec615e64d6223d3d5f090b36e1ef46a9c7ea8/improvements/stellar-docs/sd-081-contract-restoration-example-obsolete-xdr.md

  3. kalepail commented on Nov 6, 2026

    @kalepail
    Contributor

    Verified a partial fix on 2026-11-06: both contract-restoration examples now call .setTimeout(30) before .build().
    The missing-time-bounds item in my earlier comment is therefore complete.

    The remaining problems still reproduce on restore-contract-js and state-archival:

    • getWasmLedgerKey has an empty return type, and xdr is not imported.
    • It receives an instance LedgerKey but reads it as an instance entry containing the WASM hash.
    • The restoration footprint receives a getLedgerEntries response instead of a second LedgerKey.

    The separate Api redeclaration also remains when combining the data-restoration example with its linked polling helper.
    Raven retains sd-081 for these residuals and has removed the completed time-bounds recommendation.

  4. kalepail commented on Nov 17, 2026

    @kalepail
    Contributor

    Raven rechecked the contract-restoration examples on 2026-11-17.

    PR #3083 fixed most of the defects listed here. The -js page paths now redirect to
    restore-contract and
    restore-data.

    Fixed:

    • Both JavaScript restoration fences parse. The state-archival page has the same fence.
    • getWasmLedgerKey returns xdr.LedgerKey, and xdr is imported.
    • The caller reads the instance entry from getLedgerEntries(instance). The footprint holds two LedgerKey values.
    • The restore-contract fence has 0 type errors with @stellar/stellar-sdk 17.4.0.

    Still failing: restore-data declares const { Api, assembleTransaction } = StellarRpc; on line 42.
    The submitTx helper that the guide reuses imports Api from @stellar/stellar-sdk/rpc.
    The two blocks combined report TS2440: Import declaration conflicts with local declaration of 'Api'.
    with SDK 17.4.0.

    Smallest fix: change line 42 of restore-data to const { assembleTransaction } = StellarRpc;.

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

Metadata

Metadata

Assignees

Type

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions