interchainjs
Version:
InterchainJS is a JavaScript library for interacting with Cosmos SDK based blockchains.
57 lines (56 loc) • 2.03 kB
JavaScript
;
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.12.19
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildQuery = buildQuery;
exports.buildTx = buildTx;
const extern_1 = require("./extern");
const helpers_1 = require("./helpers");
const utils_1 = require("@interchainjs/cosmos/utils");
function buildQuery(opts) {
registerDependencies(opts.deps ?? []);
return async (client, request) => {
let rpc;
if ((0, helpers_1.isRpc)(client)) {
rpc = client;
}
else {
rpc = client ? await (0, extern_1.getRpcClient)(client) : undefined;
}
if (!rpc)
throw new Error("Query Rpc is not initialized");
const data = opts.encode(request).finish();
const response = await rpc.request(opts.service, opts.method, data);
return opts.decode(response);
};
}
function buildTx(opts) {
if (opts.msg) {
registerDependencies([opts.msg]);
}
return async (client, signerAddress, message, fee, memo) => {
if (!client)
throw new Error("SigningClient is not initialized");
//register all related encoders and converters
client.addEncoders?.((0, utils_1.toEncoders)(opts.msg));
client.addConverters?.((0, utils_1.toConverters)(opts.msg));
const data = Array.isArray(message)
? message.map(msg => ({
typeUrl: opts.msg.typeUrl,
value: msg,
}))
: [{
typeUrl: opts.msg.typeUrl,
value: message,
}];
return client.signAndBroadcast(signerAddress, data, fee, memo);
};
}
function registerDependencies(deps) {
for (const dep of deps) {
dep.registerTypeUrl?.();
}
}