InjectiveClient
Helper function to convert Uint8Array to JSON.
function arrayToJson(array: Uint8Array) {
const jsonString = Buffer.from(array).toString("utf8");
const parsedData = JSON.parse(jsonString);
return parsedData;
}
Class
A client to interact with the Injective chain.
export default class InjectiveClient
extends BaseChainClient
implements ChainClient {
declare public signingClient?: SigningStargateClient;
declare public queryClient?: CosmWasmClient;
public gasPrice?: GasPrice;
private directSigner?: OfflineDirectSigner;
protected preMessage(signed = true) {
super.preMessage(signed);
if (signed && !this.directSigner) {
throw new Error("No signer assigned");
}
}
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?: OfflineDirectSigner,
options?: SigningCosmWasmClientOptions
): Promise<void> {
delete this.signingClient;
delete this.queryClient;
this.queryClient = await CosmWasmClient.connect(endpoint);
this.gasPrice = options?.gasPrice || GasPrice.fromString(DEFAULT_GAS_PRICE + DEFAULT_FEE_DENOM);
if (signer) {
const tmClient = await Tendermint37Client.connect(endpoint);
this.signingClient = await InjectiveStargate.InjectiveSigningStargateClient.createWithSigner(tmClient, signer, {
registry: options?.registry || new Registry([...defaultRegistryTypes, ...wasmTypes]),
aminoTypes: options?.aminoTypes || new AminoTypes({ ...createDefaultAminoConverters() }),
gasPrice: this.gasPrice
});
this.directSigner = signer;
const [account] = await signer.getAccounts();
this.signer = account.address;
}
}
endpoint
string
The endpoint of the chain to connect to.
disconnect
Disconnects the from the connected client.
async disconnect(): Promise<void> {
delete this.signingClient;
this.queryClient?.disconnect();
delete this.queryClient;
this.signer = "";
delete this.directSigner;
delete this.gasPrice;
}
sign
Signs a given message with the connected signer.
async sign(messages: EncodeObject[], fee?: StdFee, memo?: string): Promise<TxRaw> {
this.preMessage(true);
return await this.signingClient!.sign(this.signer, messages, fee || DEFAULT_STD_FEE, memo || "");
}
memo
string
Optional memo to attach. Defaults to an empty string.
broadcast
Broadcasts a given transaction to the connected client.
async broadcast(
tx: TxRaw,
): Promise<DeliverTxResponse> {
this.preMessage(true);
const txBytes = TxRaw.encode(tx).finish();
return await this.signingClient!.broadcastTx(txBytes)
}
signAndBroadcast
Signs a given message before broadcasting it to the connected chain.
async signAndBroadcast(
messages: EncodeObject[],
fee?: StdFee,
memo?: string
): Promise<DeliverTxResponse & { logs: readonly Log[] }> {
const signed = await this.signInj(messages, fee, memo);
const resp = await this.broadcast(signed);
return {
...resp,
logs: parseRawLog(resp.rawLog),
};
}
fee
The fee to pay for signing and broadcasting the message. Defaults to auto if not specified.
memo
string
An optional memo to attach to the transaction.
EncodeObject
export interface EncodeObject {
readonly typeUrl: string;
readonly value: any;
}
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
Optional memo to attach to the transaction. Defaults to empty.
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?: StdFee | undefined,
memo?: string | undefined,
funds?: readonly Coin[] | undefined
): Promise<DeliverTxResponse & { logs: any }> {
this.preMessage(true);
const encoded = this.encodeExecuteMsg(contractAddress, msg, [...funds || []]);
const res = await this.signAndBroadcast([encoded], fee, memo);
return {
...res,
logs: []
}
}
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: StdFee | "auto" = "auto", memo?: string | undefined): Promise<UploadResult> {
this.preMessage();
const compressed = gzip(code, { level: 9 });
const encoded = this.encodeUploadMessage(compressed);
const res = await this.signAndBroadcast([encoded], fee, memo);
const compressedChecksum = toHex(sha256(compressed));
const codeIdAttr = findAttribute([{ msg_index: 0, 'log': '', events: res.events }], "cosmwasm.wasm.v1.EventCodeStored", "code_id");
let codeId = -1;
try {
codeId = parseInt(JSON.parse(codeIdAttr.value));
} catch (err) { }
return {
...res,
originalSize: code.length,
compressedSize: compressed.length,
checksum: compressedChecksum,
codeId: codeId,
logs: []
}
}
code
Uint8Array
The wasm binary code to upload to the connected client.
memo
string
Optional memo to add to the transaction. Defaults to an empty string if not specified.
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: StdFee | "auto" = "auto",
options?: InstantiateOptions
): Promise<InstantiateResult> {
this.preMessage(true);
const encoded = this.encodeInstantiateMsg(codeId, msg, label, options);
const res = await this.signAndBroadcast([encoded], fee, "Instantiate");
const contractAddressAttr = findAttribute(
res.logs,
"wasm",
"_contract_address"
);
return {
...res,
contractAddress: contractAddressAttr.value,
}
}
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 instantiate.
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: StdFee | "auto" = "auto",
memo?: string | undefined
): Promise<MigrateResult> {
this.preMessage(true);
const encoded = this.encodeMigrateMessage(contractAddress, codeId, msg);
return this.signAndBroadcast([encoded], 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 or undefined
Optional memo to attach to the transaction.
sendTokens
Sends tokens from the signing address to the provided receiving address.
You can only send one amount at a time.
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