Skip to main content
Extends @cosmjs/stargate with CosmWasm-specific functionality for interacting with smart contracts on CosmWasm-enabled chains.
npm install @cosmjs/cosmwasm
The previous package name @cosmjs/cosmwasm-stargate is deprecated. Update your imports to use @cosmjs/cosmwasm.

CosmWasmClient

Read-only client for querying blockchain state and smart contracts. Provides common query methods plus contract-specific operations.

Static Methods

MethodParametersReturns
connectendpoint: string | HttpEndpoint, options?: CosmWasmClientOptionsPromise<CosmWasmClient>
createcometClient: CometClient, options?: CosmWasmClientOptionsCosmWasmClient

Instance Methods — Chain Queries

MethodParametersReturns
getChainIdPromise<string>
getHeightPromise<number>
getAccountsearchAddress: stringPromise<Account | null>
getSequenceaddress: stringPromise<SequenceResponse>
getBlockheight?: numberPromise<Block>
getBalanceaddress: string, searchDenom: stringPromise<Coin>
getTxid: stringPromise<IndexedTx | null>
searchTxquery: SearchTxQueryPromise<IndexedTx[]>
broadcastTxtx: Uint8Array, timeoutMs?: number, pollIntervalMs?: numberPromise<DeliverTxResponse>
broadcastTxSynctx: Uint8ArrayPromise<string>
disconnectvoid

Instance Methods — Contract Queries

MethodParametersReturns
getCodesPromise<readonly Code[]>
getCodeDetailscodeId: numberPromise<CodeDetails>
getContractscodeId: numberPromise<readonly string[]>
getContractsByCreatorcreator: stringPromise<string[]>
getContractaddress: stringPromise<Contract>
getContractCodeHistoryaddress: stringPromise<readonly ContractCodeHistoryEntry[]>
queryContractRawaddress: string, key: Uint8ArrayPromise<Uint8Array | null>
queryContractSmartaddress: string, queryMsg: JsonObjectPromise<JsonObject>

Usage

import { CosmWasmClient } from "@cosmjs/cosmwasm";

const client = await CosmWasmClient.connect("https://rpc.my-chain.network");

const info = await client.getContract("osmo1contractaddr...");
const state = await client.queryContractSmart("osmo1contractaddr...", {
  get_count: {},
});
const codes = await client.getCodes();
const contracts = await client.getContracts(1);

client.disconnect();

SigningCosmWasmClient

Extends CosmWasmClient with transaction signing and smart contract management. Inherits all read-only methods above.

Static Methods

MethodParametersReturns
connectWithSignerendpoint: string | HttpEndpoint, signer: OfflineSigner, options?: SigningCosmWasmClientOptionsPromise<SigningCosmWasmClient>
createWithSignercometClient: CometClient, signer: OfflineSigner, options?: SigningCosmWasmClientOptionsSigningCosmWasmClient
offlinesigner: OfflineSigner, options?: SigningCosmWasmClientOptionsPromise<SigningCosmWasmClient>

Instance Methods — Contract Operations

MethodParametersReturns
uploadsenderAddress: string, wasmCode: Uint8Array, fee: StdFee | "auto" | number, memo?: string, instantiatePermission?: AccessConfigPromise<UploadResult>
instantiatesenderAddress: string, codeId: number, msg: JsonObject, label: string, fee: StdFee | "auto" | number, options?: InstantiateOptionsPromise<InstantiateResult>
instantiate2senderAddress: string, codeId: number, salt: Uint8Array, msg: JsonObject, label: string, fee: StdFee | "auto" | number, options?: InstantiateOptionsPromise<InstantiateResult>
executesenderAddress: string, contractAddress: string, msg: JsonObject, fee: StdFee | "auto" | number, memo?: string, funds?: readonly Coin[]Promise<ExecuteResult>
executeMultiplesenderAddress: string, instructions: readonly ExecuteInstruction[], fee: StdFee | "auto" | number, memo?: stringPromise<ExecuteResult>
migratesenderAddress: string, contractAddress: string, codeId: number, migrateMsg: JsonObject, fee: StdFee | "auto" | number, memo?: stringPromise<MigrateResult>
updateAdminsenderAddress: string, contractAddress: string, newAdmin: string, fee: StdFee | "auto" | number, memo?: stringPromise<ChangeAdminResult>
clearAdminsenderAddress: string, contractAddress: string, fee: StdFee | "auto" | number, memo?: stringPromise<ChangeAdminResult>

Instance Methods — Token Operations

MethodParametersReturns
sendTokenssenderAddress: string, recipientAddress: string, amount: readonly Coin[], fee: StdFee | "auto" | number, memo?: stringPromise<DeliverTxResponse>
delegateTokensdelegatorAddress: string, validatorAddress: string, amount: Coin, fee: StdFee | "auto" | number, memo?: stringPromise<DeliverTxResponse>
undelegateTokensdelegatorAddress: string, validatorAddress: string, amount: Coin, fee: StdFee | "auto" | number, memo?: stringPromise<DeliverTxResponse>
withdrawRewardsdelegatorAddress: string, validatorAddress: string, fee: StdFee | "auto" | number, memo?: stringPromise<DeliverTxResponse>

Instance Methods — Signing

MethodParametersReturns
simulatesignerAddress: string, messages: readonly EncodeObject[], memo: string | undefinedPromise<number>
signAndBroadcastsignerAddress: string, messages: readonly EncodeObject[], fee: StdFee | "auto" | number, memo?: string, timeoutHeight?: bigintPromise<DeliverTxResponse>
signAndBroadcastSyncsignerAddress: string, messages: readonly EncodeObject[], fee: StdFee | "auto" | number, memo?: string, timeoutHeight?: bigintPromise<string>
signsignerAddress: string, messages: readonly EncodeObject[], fee: StdFee, memo: string, explicitSignerData?: SignerData, timeoutHeight?: bigintPromise<TxRaw>

Usage

import fs from "node:fs";
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { GasPrice } from "@cosmjs/stargate";

const wallet = await DirectSecp256k1HdWallet.fromMnemonic("your mnemonic ...", {
  prefix: "osmo",
});
const [{ address }] = await wallet.getAccounts();

const client = await SigningCosmWasmClient.connectWithSigner(
  "https://rpc.my-chain.network",
  wallet,
  { gasPrice: GasPrice.fromString("0.025uosmo") },
);

// Upload
const wasmCode = fs.readFileSync("contract.wasm");
const { codeId } = await client.upload(address, wasmCode, "auto");

// Instantiate
const { contractAddress } = await client.instantiate(
  address, codeId, { count: 0 }, "My Contract", "auto",
);

// Execute
const result = await client.execute(
  address, contractAddress, { increment: {} }, "auto",
);

// Query
const state = await client.queryContractSmart(contractAddress, { get_count: {} });

Key Types

Code

interface Code {
  readonly id: number;
  readonly creator: string;
  readonly checksum: string;
}

CodeDetails

interface CodeDetails extends Code {
  readonly data: Uint8Array;
}

Contract

interface Contract {
  readonly address: string;
  readonly codeId: number;
  readonly creator: string;
  readonly admin: string | undefined;
  readonly label: string;
  readonly ibcPortId: string | undefined;
}

ContractCodeHistoryEntry

interface ContractCodeHistoryEntry {
  readonly operation: "Genesis" | "Init" | "Migrate";
  readonly codeId: number;
  readonly msg: JsonObject;
}
In the interfaces below, logs refers to the namespace re-exported from @cosmjs/stargate (import { logs } from "@cosmjs/stargate"), so logs.Log[] is the type logs.Log array, not a reference to the field of the same name.

UploadResult

interface UploadResult {
  readonly checksum: string;
  readonly originalSize: number;
  readonly compressedSize: number;
  readonly codeId: number;
  readonly height: number;
  readonly transactionHash: string;
  readonly events: readonly Event[];
  /** @deprecated Not filled in Cosmos SDK >= 0.50. Use `events` instead. */
  readonly logs: readonly logs.Log[];
  readonly gasWanted: bigint;
  readonly gasUsed: bigint;
}

InstantiateResult

interface InstantiateResult {
  readonly contractAddress: string;
  readonly height: number;
  readonly transactionHash: string;
  readonly events: readonly Event[];
  /** @deprecated Not filled in Cosmos SDK >= 0.50. Use `events` instead. */
  readonly logs: readonly logs.Log[];
  readonly gasWanted: bigint;
  readonly gasUsed: bigint;
}

InstantiateOptions

interface InstantiateOptions {
  readonly memo?: string;
  readonly funds?: readonly Coin[];
  readonly admin?: string;
}

ExecuteResult

interface ExecuteResult {
  readonly height: number;
  readonly transactionHash: string;
  readonly events: readonly Event[];
  /** @deprecated Not filled in Cosmos SDK >= 0.50. Use `events` instead. */
  readonly logs: readonly logs.Log[];
  readonly gasWanted: bigint;
  readonly gasUsed: bigint;
}

ExecuteInstruction

interface ExecuteInstruction {
  contractAddress: string;
  msg: JsonObject;
  funds?: readonly Coin[];
}

MigrateResult

interface MigrateResult {
  readonly height: number;
  readonly transactionHash: string;
  readonly events: readonly Event[];
  /** @deprecated Not filled in Cosmos SDK >= 0.50. Use `events` instead. */
  readonly logs: readonly logs.Log[];
  readonly gasWanted: bigint;
  readonly gasUsed: bigint;
}

ChangeAdminResult

interface ChangeAdminResult {
  readonly height: number;
  readonly transactionHash: string;
  readonly events: readonly Event[];
  /** @deprecated Not filled in Cosmos SDK >= 0.50. Use `events` instead. */
  readonly logs: readonly logs.Log[];
  readonly gasWanted: bigint;
  readonly gasUsed: bigint;
}

SigningCosmWasmClientOptions

interface SigningCosmWasmClientOptions extends CosmWasmClientOptions {
  readonly registry?: Registry;
  readonly aminoTypes?: AminoTypes;
  readonly broadcastTimeoutMs?: number;
  readonly broadcastPollIntervalMs?: number;
  readonly gasPrice?: GasPrice | DynamicGasPriceConfig;
}

Helper Functions

FunctionParametersReturns
setupWasmExtensionbase: QueryClientWasmExtension
createWasmAminoConvertersAminoConverters
instantiate2Addresschecksum: Uint8Array, creator: string, salt: Uint8Array, prefix: stringstring
toBinaryobj: anystring
fromBinarybinary: stringany

Encode Objects

TypetypeUrl
MsgStoreCodeEncodeObject/cosmwasm.wasm.v1.MsgStoreCode
MsgInstantiateContractEncodeObject/cosmwasm.wasm.v1.MsgInstantiateContract
MsgInstantiateContract2EncodeObject/cosmwasm.wasm.v1.MsgInstantiateContract2
MsgExecuteContractEncodeObject/cosmwasm.wasm.v1.MsgExecuteContract
MsgMigrateContractEncodeObject/cosmwasm.wasm.v1.MsgMigrateContract
MsgUpdateAdminEncodeObject/cosmwasm.wasm.v1.MsgUpdateAdmin
MsgClearAdminEncodeObject/cosmwasm.wasm.v1.MsgClearAdmin