// SPDX-License-Identifier: Apache-2.0 import TransactionId from "../transaction/TransactionId.js"; import Hbar from "../Hbar.js"; import AccountId from "../account/AccountId.js"; import * as HieroProto from "@hiero-ledger/proto"; import QueryBase from "./QueryBase.js"; /** * @typedef {import("../channel/Channel.js").default} Channel * @typedef {import("../channel/MirrorChannel.js").default} MirrorChannel * @typedef {import("../Status.js").default} Status * @typedef {import("../Executable.js").ExecutionState} ExecutionState */ /** * @template OutputT * @augments {QueryBase} */ export default class CostQuery extends QueryBase { /** * @param {import("./Query.js").default} query */ constructor(query) { super(); this._query = query; // The cost lookup is part of the parent query's execution, so it runs // with the parent's execution settings. A setting the parent left // `null` falls through to the client's value in `_setupExecution`, // exactly as it does for the parent itself. this._grpcDeadline = query._grpcDeadline; this._requestTimeout = query._requestTimeout; this._nodeAccountIds = query._nodeAccountIds.clone(); this._operator = query._operator; this._maxAttempts = query._maxAttempts; this._minBackoff = query._minBackoff; this._maxBackoff = query._maxBackoff; this._logger = query.logger; /** * @type {HieroProto.proto.IQueryHeader | null} */ this._header = null; } /** * @returns {TransactionId} */ _getTransactionId() { return this._query._getTransactionId(); } /** * @returns {string} */ _getLogId() { return `CostQuery:${this._query._getLogId()}`; } /** * @abstract * @protected * @param {import("../client/Client.js").default} client * @returns {Promise} */ async _beforeExecute(client) { if (client == null) { throw new Error("Cannot do CostQuery without Client"); } const operator = this._operator != null ? this._operator : client._operator; if (this._query._nodeAccountIds.isEmpty) { this._query._nodeAccountIds.setList( client._network.getNodeAccountIdsForExecute(), ); } const transactionId = TransactionId.generate( operator != null ? operator.accountId : new AccountId(0), ); if (this._query.paymentTransactionId == null) { this._query.setPaymentTransactionId(transactionId); } const logId = this._getLogId(); const nodeId = new AccountId(0); const paymentTransactionId = /** @type {import("../transaction/TransactionId.js").default} */ (TransactionId.generate(new AccountId(0))); const paymentAmount = new Hbar(0); if (this._logger) { this._logger.debug( `[${logId}] making a payment transaction for node ${nodeId.toString()} and transaction ID ${paymentTransactionId.toString()} with amount ${paymentAmount.toString()}`, ); } // A COST_ANSWER query is answered free of charge, but the node still // expects a payment transaction in the query header to pass // validation. Without an operator the payment is left unsigned — // the node checks its presence, not its signatures. this._header = { payment: await this._makePaymentTransaction( paymentTransactionId, new AccountId(0), operator, paymentAmount, ), responseType: HieroProto.proto.ResponseType.COST_ANSWER, }; } /** * @abstract * @internal * @returns {Promise} */ _makeRequestAsync() { return Promise.resolve( this._query._onMakeRequest( /** @type {HieroProto.proto.IQueryHeader} */ (this._header), ), ); } /** * @abstract * @internal * @param {HieroProto.proto.IQuery} request * @param {HieroProto.proto.IResponse} response * @returns {[Status, ExecutionState]} */ _getStatusAndExecutionState(request, response) { return this._query._getStatusAndExecutionState(request, response); } /** * @abstract * @internal * @param {HieroProto.proto.IQuery} request * @param {HieroProto.proto.IResponse} response * @param {AccountId} nodeId * @returns {Error} */ _mapStatusError(request, response, nodeId) { return this._query._mapStatusError(request, response, nodeId); } /** * @override * @internal * @param {HieroProto.proto.IResponse} response * @param {AccountId} nodeAccountId * @param {HieroProto.proto.IQuery} request * @returns {Promise} */ // eslint-disable-next-line @typescript-eslint/no-unused-vars _mapResponse(response, nodeAccountId, request) { const cost = this._query._mapResponseHeader(response).cost; return Promise.resolve( Hbar.fromTinybars(/** @type {Long | number} */ (cost)), ); } /** * @override * @internal * @param {Channel} channel * @param {HieroProto.proto.IQuery} request * @returns {Promise} */ _execute(channel, request) { return this._query._execute(channel, request); } /** * @param {HieroProto.proto.Query} request * @returns {Uint8Array} */ _requestToBytes(request) { return this._query._requestToBytes(request); } /** * @param {HieroProto.proto.Response} response * @returns {Uint8Array} */ _responseToBytes(response) { return this._query._responseToBytes(response); } }