@cyberk/hyperion-sdk
Version:
Hyperion SDK from Cyberk
76 lines • 3.18 kB
JavaScript
import { SignMode } from "@titanlabjs/cosmos-types/cosmos/tx/signing/v1beta1/signing";
import { AuthInfo, Fee, TxBody, TxRaw, } from "@titanlabjs/cosmos-types/cosmos/tx/v1beta1/tx";
import { toBase64, toUtf8 } from "@titanlabjs/encoding";
import { PubKey } from "@titanlabjs/titan-types/cosmos/crypto/secp256k1/keys";
import { bytesFromBase64 } from "@titanlabjs/titan-types/helpers";
import { LCDs } from "../constants";
import { getAccountInfoFromLcd } from "../utils";
export const getSimulateTxQueryKey = (options) => {
return [
"simulateTx",
options.signerAddress,
options.signingClient,
toBase64(toUtf8(JSON.stringify(options.messages))),
];
};
export 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 = LCDs[options.chainId];
const accountInfo = await getAccountInfoFromLcd({
address: signerAddress,
lcdUrl,
});
console.log("accountInfo", accountInfo);
const txBodyBytes = TxBody.encode(TxBody.fromPartial({
messages: messages,
memo: "",
})).finish();
const authInfoBytes = AuthInfo.encode(AuthInfo.fromPartial({
signerInfos: [
{
publicKey: {
typeUrl: accountInfo.pubKey?.["@type"],
value: PubKey.encode({
key: bytesFromBase64(accountInfo.pubKey?.key ?? ""),
}).finish(),
},
sequence: BigInt(accountInfo.sequence ?? 0),
modeInfo: {
single: {
mode: SignMode.SIGN_MODE_DIRECT,
},
},
},
],
fee: Fee.fromPartial({
amount: [{ denom: "atkx", amount: "1000000000000000000" }],
gasLimit: 0n,
}),
})).finish();
const txRawBytes = TxRaw.encode({
bodyBytes: txBodyBytes,
authInfoBytes: authInfoBytes,
signatures: [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: toBase64(txRawBytes),
}),
});
const data = (await result.json());
console.log("data", data);
return data.gas_info.gas_used;
},
queryKey: getSimulateTxQueryKey(options),
};
};
//# sourceMappingURL=simulateTx.js.map