@cyberk/hyperion-sdk
Version:
Hyperion SDK from Cyberk
81 lines • 3.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSimulateTxQueryOptions = exports.getSimulateTxQueryKey = void 0;
const signing_1 = require("@titanlabjs/cosmos-types/cosmos/tx/signing/v1beta1/signing");
const tx_1 = require("@titanlabjs/cosmos-types/cosmos/tx/v1beta1/tx");
const encoding_1 = require("@titanlabjs/encoding");
const keys_1 = require("@titanlabjs/titan-types/cosmos/crypto/secp256k1/keys");
const helpers_1 = require("@titanlabjs/titan-types/helpers");
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const getSimulateTxQueryKey = (options) => {
return [
"simulateTx",
options.signerAddress,
options.signingClient,
(0, encoding_1.toBase64)((0, encoding_1.toUtf8)(JSON.stringify(options.messages))),
];
};
exports.getSimulateTxQueryKey = getSimulateTxQueryKey;
const getSimulateTxQueryOptions = (options) => {
return {
queryFn: async () => {
const { signingClient, messages, signerAddress } = options;
if (!signerAddress) {
throw new Error("Signer address is required");
}
if (signingClient) {
return await signingClient.simulate(signerAddress, messages, "");
}
const lcdUrl = constants_1.LCDs[options.chainId];
const accountInfo = await (0, utils_1.getAccountInfoFromLcd)({
address: signerAddress,
lcdUrl,
});
console.log("accountInfo", accountInfo);
const txBodyBytes = tx_1.TxBody.encode(tx_1.TxBody.fromPartial({
messages: messages,
memo: "",
})).finish();
const authInfoBytes = tx_1.AuthInfo.encode(tx_1.AuthInfo.fromPartial({
signerInfos: [
{
publicKey: {
typeUrl: accountInfo.pubKey?.["@type"],
value: keys_1.PubKey.encode({
key: (0, helpers_1.bytesFromBase64)(accountInfo.pubKey?.key ?? ""),
}).finish(),
},
sequence: BigInt(accountInfo.sequence ?? 0),
modeInfo: {
single: {
mode: signing_1.SignMode.SIGN_MODE_DIRECT,
},
},
},
],
fee: tx_1.Fee.fromPartial({
amount: [{ denom: "atkx", amount: "1000000000000000000" }],
gasLimit: 0n,
}),
})).finish();
const txRawBytes = tx_1.TxRaw.encode({
bodyBytes: txBodyBytes,
authInfoBytes: authInfoBytes,
signatures: [(0, encoding_1.toUtf8)("AQ==")], // Mảng các chữ ký, ở đây là 1 chữ ký
}).finish();
const result = await fetch(`${lcdUrl}/cosmos/tx/v1beta1/simulate`, {
method: "POST",
body: JSON.stringify({
tx_bytes: (0, encoding_1.toBase64)(txRawBytes),
}),
});
const data = (await result.json());
console.log("data", data);
return data.gas_info.gas_used;
},
queryKey: (0, exports.getSimulateTxQueryKey)(options),
};
};
exports.getSimulateTxQueryOptions = getSimulateTxQueryOptions;
//# sourceMappingURL=simulateTx.js.map