# new BindingGenerator(spec)
Private constructor - use static factory methods instead.
Parameters:
| Name | Type | Description |
|---|---|---|
spec |
The parsed contract specification |
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
Members
# private validateOptions
Validates that required generation options are provided.
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 functiontypes.ts: TypeScript interfaces for all contract types (structs, enums, unions)index.ts: Barrel export filepackage.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) |
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 |
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 |
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 |
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 |
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 |
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
);