CosmClient

Class

A client to interact with the cosmos hub chain.

export default class CosmClient extends BaseChainClient implements ChainClient {
  public signingClient?: SigningCosmWasmClient;
  public queryClient?: CosmWasmClient;
  private gasPrice?: GasPrice;

Methods

connect

Connects to the given chain. Assigns all clients used within the chain client, if a signer is provided a signing client is assigned.

async connect(
    endpoint: string,
    signer?: OfflineSigner,
    options?: SigningCosmWasmClientOptions
  ): Promise<void> {
    delete this.signingClient;
    delete this.queryClient;
    this.gasPrice = options?.gasPrice;

    if (signer) {
      this.signingClient = await SigningCosmWasmClient.connectWithSigner(
        endpoint,
        signer,
        { broadcastTimeoutMs: 30000, ...options }
      );
      this.queryClient = this.signingClient;

      const [account] = await signer.getAccounts();
      this.signer = account.address;
    }
  }
NameTypeDescription

endpoint

string

The endpoint of the chain to connect to.

signer

Optional signer used to sign the messages.

options

Optional field to set the client options.

disconnect

Disconnects the assigned clients.

async disconnect(): Promise<void> {
    if (this.signingClient) this.signingClient.disconnect();
    if (this.queryClient) this.queryClient.disconnect();

    delete this.signingClient;
    delete this.queryClient;
    this.signer = "";
    delete this.gasPrice;
  }

sign

Signs a given message with the connected signer.

async sign(messages: EncodeObject[], fee: StdFee, memo = ""): Promise<TxRaw> {
    this.preMessage(true);
    return await this.signingClient!.sign(this.signer, messages, fee, memo);
  }
Name TypeDescription

messages

EncodeObject[]

The messages to sign.

fee

The fee to pay for signing.

memo

string

Optional memo to attach. Defaults to an empty string.

broadcast

Broadcasts a given transaction to the connected client.

async broadcast(tx: TxRaw): ReturnType<ChainClient["broadcast"]> {
    this.preMessage(true);
    const txBytes = TxRaw.encode(tx).finish();
    return await this.signingClient!.broadcastTx(txBytes);
  }
NameTypeDescription

tx

the transaction to broadcast.

TxRaw

TxRaw is a variant of Tx that pins the signer's exact binary representationof body and auth_info. This is used for signing, broadcasting and verification. The binary serialize(tx: TxRaw) is stored in Tendermint and the hash sha256(serialize(tx: TxRaw)) becomes the "txhash", commonly used as the transaction ID.

export interface TxRaw {
    /**
     * body_bytes is a protobuf serialization of a TxBody that matches the
     * representation in SignDoc.
     */
    bodyBytes: Uint8Array;
    /**
     * auth_info_bytes is a protobuf serialization of an AuthInfo that matches the
     * representation in SignDoc.
     */
    authInfoBytes: Uint8Array;
    /**
     * signatures is a list of signatures that matches the length and order of
     * AuthInfo's signer_infos to allow connecting signature meta information like
     * public key and signing mode by position.
     */
    signatures: Uint8Array[];
}

signAndBroadcast

Signs a given message before broadcasting it to the connected chain.

async signAndBroadcast(
    messages: EncodeObject[],
    fee: Fee = "auto",
    memo?: string | undefined
  ): Promise<DeliverTxResponse> {
    this.preMessage(true);

    return await this.signingClient!.signAndBroadcast(
      this.signer,
      messages,
      fee,
      memo
    );
  }
NameTypeDescription

messages

The messages to sign and broadcast.

fee

The fee to pay for signing and broadcasting the message. Defaults to auto if not specified.

memo

string or undefined

An optional memo to attach to the transaction.

EncodeObject

export interface EncodeObject {
    readonly typeUrl: string;
    readonly value: any;
}

Fee

export type Fee = number | StdFee | "auto";

simulateMulti

Simulates all given messages and returns a gas fee estimate.

async simulateMulti(
    messages: EncodeObject[],
    _fee: Fee = "auto",
    memo?: string | undefined
  ): Promise<number> {
    this.preMessage();
    return this.signingClient!.simulate(this.signer, messages, memo);
    }
NameType Description

messages

The messages to simulate.

_fee

The fee to pay. Defaults to auto if not specified.

memo

string or undefined

Optional memo to attach to the transaction.

simulate

Simulates the given message and returns a gas fee estimate.

async simulate(
    message: EncodeObject,
    _fee?: Fee | undefined,
    memo?: string | undefined
  ): Promise<number> {
    this.preMessage();
    return this.signingClient!.simulate(this.signer, [message], memo);
  }
NameType Description

message

The message to simulate.

_fee

Optional fee attached to the message.

memo

string or undefined

Optional memo to attach to the transaction.

execute

Executes a message on the specified contract.

async execute(
    contractAddress: string,
    msg: Msg,
    fee?: Fee | undefined,
    memo?: string | undefined,
    funds?: readonly Coin[] | undefined
  ): Promise<ExecuteResult> {
    this.preMessage(true);

    return await this.signingClient!.execute(
      this.signer,
      contractAddress,
      msg,
      fee ?? "auto",
      memo,
      funds
    );
  }
Name Type Description

contractAddress

string

The contract address to execute on.

msg

The execute message.

memo

string or undefined

Optional memo to attach to the transaction.

funds

Coin[] or udefined

Optional funds to attach along with the message.

Msg

export type Msg = Record<string, unknown>;

simulateExecute

Simulates an execute message and returns a gas fee estimate.

async simulateExecute(
    address: string,
    msg: Msg,
    funds: Coin[],
    _fee?: Fee,
    memo?: string | undefined
  ) {
    const message = this.encodeExecuteMsg(address, msg, funds);
    return this.simulate(message, undefined, memo);
  }
NameTypeDescription

address

string

The contract address you are executing on.

msg

The execute message to simulate.

funds

The funds attached along with the message.

_fee

Optional fee for executing the message.

memo

string or undefined

Optional memo to add to the transaction.

upload

Uploads given contract code (Uint8Array) to the chain.

async upload(code: Uint8Array, fee: Fee = "auto", memo?: string | undefined) {
    this.preMessage();
    return this.signingClient!.upload(this.signer, code, fee, memo);
  }
NameTypeDescription

code

Uint8Array

The wasm binary code to upload to the connected client.

fee

Fee

Fee to pay for uploading.

memo

string or undefined

Optional memo to add to the transaction.

simulateUpload

Simulate an upload message and returns a gas fee estimate.

async simulateUpload(
    code: Uint8Array,
    _fee?: Fee | undefined,
    memo?: string | undefined
  ): Promise<number | undefined> {
    const message = this.encodeUploadMessage(code);
    return this.simulate(message, undefined, memo);
  }
NameTypeDescription

code

Uint8Array

The wasm binary code to simulate uploading.

_fee

Fee to pay for uploading.

memo

string or undefined

Optional memo to add to the transaction.

instantiate

Instantiates a contract with the given code id using the provided instantiate message.

async instantiate(
    codeId: number,
    msg: Msg,
    label: string,
    fee: Fee = "auto",
    options?: InstantiateOptions
  ): Promise<InstantiateResult> {
    this.preMessage(true);
    return this.signingClient!.instantiate(
      this.signer,
      codeId,
      msg,
      label,
      fee,
      options
    );
  }
NameTypeDescription

codeId

number

The code Id of the contract to instantiate.

msg

The instantiation message.

label

string

A label for the instantiation. Can be any string such as " instantiating a CW721" or "my_label" ect...

fee

Fee to pay for instantiation. Defaults to auto.

options

The options of an .instantiate() call. All properties are optional.

simulateInstantiate

Simulates an instantiation message and returns a gas fee estimate.

simulateInstantiate(
    codeId: number,
    msg: Msg,
    label: string,
    fee?: Fee,
    options?: InstantiateOptions
  ): Promise<number | undefined>;
NameTypeDescription

codeId

number

The code Id of the contract to simulate instantiating.

msg

The instantiation message.

label

string

A label for the instantiation. Can be any string such as " instantiating a CW721" or "my_label" ect...

fee

Fee to pay for instantiation. Defaults to auto.

options

The options of an .instantiate() call. All properties are optional.

migrate

Migrates a contract to a given code id.

async migrate(
    contractAddress: string,
    codeId: number,
    msg: Msg,
    fee: Fee = "auto",
    memo?: string | undefined
  ): Promise<MigrateResult> {
    this.preMessage(true);
    return this.signingClient!.migrate(
      this.signer,
      contractAddress,
      codeId,
      msg,
      fee,
      memo
    );
  }
NameType Description

contractAddress

string

The address of the contract that you want to migrate.

codeId

number

The code Id to migrate to.

msg

The migrate message.

fee

The fee to pay for the migration.

memo

string or undefined

Optional memo to attach to the transaction.

simulateMigrate

Simulates a migrate message for a given contract address, code id and migrate message and returns a gas estimate.

simulateMigrate(
    contractAddress: string,
    codeId: number,
    msg: Msg,
    fee?: Fee,
    memo?: string
  ): Promise<number | undefined>;
Name Type Description

contractAddress

string

The contract address that you want to migrate.

codeId

number

The code Id to migrate to.

msg

Msg

The migrate message.

fee

Optional fee to pay to send the funds.

memo

string

Optional memo to attach to the transaction.

sendTokens

Sends tokens from the signing address to the provided receiving address.

sendTokens(
    receivingAddress: string,
    amount: readonly Coin[],
    fee?: Fee,
    memo?: string
  ): Promise<DeliverTxResponse>;
NameTypeDescription

receivingAddress

string

The address to receive the tokens.

amount

Coin []

The funds to send.

fee

Optional fee to pay to send the funds.

memo

string

Optional memo to attach to the transaction.

Last updated