Telescope
Cosmwasm

CosmWasm Integration

This document provides a reference for generating TypeScript SDKs for CosmWasm smart contracts using Telescope.

Overview

Telescope integrates with @cosmwasm/ts-codegen (opens in a new tab) to generate TypeScript client libraries for CosmWasm smart contracts. This enables you to work with your smart contracts using strongly-typed interfaces in your frontend applications.

Configuration

To generate TypeScript SDKs for your CosmWasm contracts, add the cosmwasm option to your Telescope configuration:

import { TelescopeOptions } from "@cosmology/types";
 
const options: TelescopeOptions = {
  cosmwasm: {
    contracts: [
      {
        name: "MyContract",
        dir: "./path/to/schema"
      }
    ],
    outPath: "./src/contracts"
  }
};

The cosmwasm option is a direct reference to the TSBuilderInput object from the @cosmwasm/ts-codegen package.

Contract Configuration

Basic Contract Setup

At minimum, each contract configuration requires:

PropertyTypeDescription
namestringName of the contract, used in generated class names
dirstringPath to the contract schema directory

Example:

{
  name: "SG721",
  dir: "./schema/sg721"
}

Advanced Contract Configuration

For more control, you can use additional configuration options:

{
  name: "Marketplace",
  dir: "./schema/marketplace",
  // Additional options
  camelCase: true,
  customTypes: {
    "cosmos.base.v1beta1.Coin": {
      module: "@cosmjs/stargate",
      type: "Coin"
    }
  },
  messageComposer: {
    enabled: true
  }
}

Output Configuration

The outPath property specifies where the generated files should be placed:

{
  outPath: "./src/generated/contracts"
}

Full Configuration Options

PropertyTypeDescriptionDefault
contractsarrayList of contract configurationsRequired
contracts[].namestringContract nameRequired
contracts[].dirstringPath to contract schemaRequired
contracts[].camelCasebooleanConvert snake_case to camelCasetrue
contracts[].customTypesobjectCustom type mappings{}
contracts[].messageComposerobjectMessage composer options{ enabled: true }
contracts[].typesobjectCustom types generation options{ enabled: true }
contracts[].clientobjectClient generation options{ enabled: true }
contracts[].reactQueryobjectReact Query hooks options{ enabled: false, version: 'v4' }
contracts[].recoilobjectRecoil state management options{ enabled: false }
contracts[].bundleobjectBundle generation options{ enabled: false }
outPathstringOutput directory for generated filesCurrent directory

Generated Files

For each contract, the following files are generated:

FileDescription
<ContractName>.types.tsTypeScript interfaces for contract interactions
<ContractName>.client.tsClient classes for interacting with the contract
<ContractName>.message-composer.tsHelper for composing contract messages
<ContractName>.react-query.tsReact Query hooks (if enabled)
<ContractName>.recoil.tsRecoil state atoms and selectors (if enabled)

Using Generated Clients

The generated clients provide a type-safe way to interact with your CosmWasm contracts:

import { MyContractClient } from "./generated/contracts/MyContract.client";
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
 
// Create a client instance
const client = new MyContractClient(
  signingCosmWasmClient,
  "cosmos1...",  // Contract address
  {}  // Default options
);
 
// Query contract state
const { balance } = await client.getBalance({ address: "cosmos1..." });
 
// Execute contract functions
const result = await client.mint({
  tokenId: "123",
  owner: "cosmos1..."
});

Message Composer

The generated message composer helps create contract messages in the correct format:

import { MyContractMessageComposer } from "./generated/contracts/MyContract.message-composer";
 
const msgComposer = new MyContractMessageComposer("cosmos1...");  // Contract address
 
// Create a message for broadcasting
const mintMsg = msgComposer.mint({
  tokenId: "123",
  owner: "cosmos1..."
});
 
// Use with a SigningCosmWasmClient
const result = await signingClient.signAndBroadcast(
  senderAddress,
  [mintMsg],
  fee
);

React Query Integration

If you enable React Query integration, you'll get custom hooks for each query and mutation:

import { useMyContractGetBalance } from "./generated/contracts/MyContract.react-query";
 
function BalanceDisplay({ address }) {
  const { data, isLoading, error } = useMyContractGetBalance({
    address
  });
 
  if (isLoading) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;
 
  return <div>Balance: {data.balance}</div>;
}

Customizing Type Imports

You can customize how external types are imported using the customTypes configuration:

customTypes: {
  "cosmos.base.v1beta1.Coin": {
    module: "@cosmjs/stargate",
    type: "Coin"
  },
  "SomeCustomType": {
    module: "../types",
    type: "MyType"
  }
}

Dependencies

To use the generated code, you'll need to install the following dependencies:

npm install @cosmjs/cosmwasm-stargate @cosmjs/stargate

For React Query integration:

npm install @tanstack/react-query

For Recoil integration:

npm install recoil

Limitations

  • Schema files must follow the CosmWasm JSON Schema format
  • Custom integration might be needed for very complex contract architectures
  • Generated code relies on the runtime libraries specified in dependencies