ArchwayClient
Class
A client to interact with the Archway chain.
export default class ArchwayClient extends BaseChainClient {
public signingClient?: SigningArchwayClient;
public queryClient?: CosmWasmClient;
public 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;
this.queryClient = await CosmWasmClient.connect(endpoint);
if (signer) {
this.signingClient = await SigningArchwayClient.connectWithSigner(
endpoint,
signer,
{
gasAdjustment: 1.4,
}
);
const [account] = await signer.getAccounts();
this.signer = account.address;
}
}
endpoint
string
The endpoint of the chain to connect to.
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);
}
messages
EncodeObject[]
The messages to sign.
memo
string
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);
}
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
);
}
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);
}
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);
}
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,
"auto",
memo,
funds
);
}
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);
}
upload
Uploads given contract code (Uint8Array) to the chain.
async upload(
code: Uint8Array,
fee: Fee = "auto",
memo?: string | undefined
): Promise<UploadResult> {
this.preMessage();
const result = await this.signingClient!.upload(
this.signer,
code,
fee,
memo
);
return { ...result };
}
code
Uint8Array
The wasm binary code to upload to the connected client.
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);
}
code
Uint8Array
The wasm binary code to simulate 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
);
}
codeId
number
The code Id of the contract to instantiate.
label
string
A label for the instantiation. Can be any string such as " instantiating a CW721" or "my_label" ect...
simulateInstantiate
Simulates an instantiation message and returns a gas fee estimate.
async simulateInstantiate(
codeId: number,
msg: Msg,
label: string,
fee?: StdFee,
options?: InstantiateOptions
): Promise<number | undefined> {
const message = this.encodeInstantiateMsg(codeId, msg, label);
return this.simulate(message, fee, options?.memo);
}
codeId
number
The code Id of the contract to simulate instantiating.
label
string
A label for the instantiation. Can be any string such as " instantiating a CW721" or "my_label" ect...
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
);
}
simulateMigrate
Simulates a migrate message for a given contract address, code id and migrate message and returns a gas estimate.
async simulateMigrate(
contractAddress: string,
codeId: number,
msg: Msg,
fee?: Fee | undefined,
memo?: string | undefined
): Promise<number | undefined> {
const message = this.encodeMigrateMessage(contractAddress, codeId, msg);
return this.simulate(message, fee, memo);
}
contractAddress
string
The contract address that you want to migrate.
codeId
number
The code Id to migrate to.
msg
Msg
The migrate message.
memo
string
Optional memo to attach to the transaction.
sendTokens
Sends tokens from the signing address to the provided receiving address.
async sendTokens(
receivingAddress: string,
amount: readonly Coin[],
fee: Fee = "auto",
memo?: string | undefined
): Promise<any> {
return this.signingClient?.sendTokens(
this.signer,
receivingAddress,
amount,
fee,
memo
);
}
}
Last updated