Class

BindingGenerator

BindingGenerator(spec)

Generates TypeScript bindings for Stellar smart contracts.

This class creates fully-typed TypeScript client code from a contract's specification, allowing developers to interact with Stellar smart contracts with full IDE support and compile-time type checking.

Constructor

# new BindingGenerator(spec)

Private constructor - use static factory methods instead.

Parameters:
Name Type Description
spec

The parsed contract specification

View Source lib/bindings/generator.js, line 64

Examples
// Create from a local WASM file
const wasmBuffer = fs.readFileSync("./my_contract.wasm");
const generator = await BindingGenerator.fromWasm(wasmBuffer);
const bindings = generator.generate({ contractName: "my-contract" });
// Create from a contract deployed on the network
const generator = await BindingGenerator.fromContractId(
  "CABC...XYZ",
  "https://soroban-testnet.stellar.org",
  Networks.TESTNET
);
const bindings = generator.generate({ contractName: "my-contract" });
// Create from a Spec object directly
const spec = new Spec(specEntries);
const generator = BindingGenerator.fromSpec(spec);
const bindings = generator.generate({ contractName: "my-contract" });

Classes

BindingGenerator

Members

# private validateOptions

Validates that required generation options are provided.

View Source lib/bindings/generator.d.ts, line 156

Methods

# generate(options)

Generates TypeScript bindings for the contract.

Produces all the files needed for a standalone npm package:

  • client.ts: A typed Client class with methods for each contract function
  • types.ts: TypeScript interfaces for all contract types (structs, enums, unions)
  • index.ts: Barrel export file
  • package.json, tsconfig.json, README.md, .gitignore: Package configuration

The generated code does not write to disk - use the returned strings to write files as needed.

Parameters:
Name Type Description
options

Configuration options for generation

contractName

Required. The name for the generated package (kebab-case recommended)

View Source lib/bindings/generator.js, line 194

If contractName is missing or empty

Error

An object containing all generated file contents as strings

Example
const bindings = generator.generate({
  contractName: "my-token",
  contractAddress: "CABC...XYZ",
  rpcUrl: "https://soroban-testnet.stellar.org",
  networkPassphrase: Networks.TESTNET
});

// Write files to disk
fs.writeFileSync("./src/client.ts", bindings.client);
fs.writeFileSync("./src/types.ts", bindings.types);

# validateOptions(options)

Validates that required generation options are provided.

Parameters:
Name Type Description
options

The options to validate

View Source lib/bindings/generator.js, line 230

If contractName is missing or empty

Error

# async static fromContractId(contractId, rpcServer)

Creates a BindingGenerator by fetching contract info from a deployed contract ID.

Retrieves the contract's WASM from the network using the contract ID, then parses the specification. If the contract is a Stellar Asset Contract (SAC), returns a generator with the standard SAC specification.

Parameters:
Name Type Description
contractId

The contract ID (C... address) of the deployed contract

rpcServer

The Stellar RPC server instance

View Source lib/bindings/generator.js, line 155

If the contract cannot be found or fetched

Error

A Promise resolving to a new BindingGenerator instance

Example
const generator = await BindingGenerator.fromContractId(
  "CABC123...XYZ",
  rpcServer
);

# static fromSpec(spec)

Creates a BindingGenerator from an existing Spec object.

Use this when you already have a parsed contract specification, such as from manually constructed spec entries or from another source.

Parameters:
Name Type Description
spec

The contract specification containing function and type definitions

View Source lib/bindings/generator.js, line 87

A new BindingGenerator instance

Example
const spec = new Spec(specEntries);
const generator = BindingGenerator.fromSpec(spec);

# static fromWasm(wasmBuffer)

Creates a BindingGenerator from a WASM binary buffer.

Parses the contract specification directly from the WASM file's custom section. This is the most common method when working with locally compiled contracts.

Parameters:
Name Type Description
wasmBuffer

The raw WASM binary as a Buffer

View Source lib/bindings/generator.js, line 105

If the WASM file doesn't contain a valid contract spec

Error

A Promise resolving to a new BindingGenerator instance

Example
const wasmBuffer = fs.readFileSync("./target/wasm32-unknown-unknown/release/my_contract.wasm");
const generator = await BindingGenerator.fromWasm(wasmBuffer);

# async static fromWasmHash(wasmHash, rpcServer)

Creates a BindingGenerator by fetching WASM from the network using its hash.

Retrieves the WASM bytecode from Stellar RPC using the WASM hash, then parses the contract specification from it. Useful when you know the hash of an installed WASM but don't have the binary locally.

Parameters:
Name Type Description
wasmHash

The hex-encoded hash of the installed WASM blob

rpcServer

The Stellar RPC server instance

View Source lib/bindings/generator.js, line 129

If the WASM cannot be fetched or doesn't contain a valid spec

Error

A Promise resolving to a new BindingGenerator instance

Example
const generator = await BindingGenerator.fromWasmHash(
  "a1b2c3...xyz",
  "https://soroban-testnet.stellar.org",
  Networks.TESTNET
);