Skip to content

Commit

Permalink
Fix inaccurate cost calculation (#1430)
Browse files Browse the repository at this point in the history
* fix inaccurate cost calculation

Signed-off-by: Petar Tonev <petar.tonev@limechain.tech>

* rename misleading comment

Signed-off-by: Petar Tonev <petar.tonev@limechain.tech>

---------

Signed-off-by: Petar Tonev <petar.tonev@limechain.tech>
  • Loading branch information
petreze authored Feb 1, 2023
1 parent b585214 commit 8122867
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
3 changes: 1 addition & 2 deletions examples/create-stateful-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ async function main() {
.setGas(75000)
// Set the function to call on the contract
.setFunction("get_message")
// Set the query payment explicitly since sometimes automatic payment calculated
// is too low
// Set query payment explicitly
.setQueryPayment(new Hbar(3))
.executeWithSigner(wallet);

Expand Down
11 changes: 5 additions & 6 deletions src/query/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export default class Query extends Executable {
* @param {import("../client/Client.js").default<Channel, *>} client
* @returns {Promise<Hbar>}
*/
getCost(client) {
async getCost(client) {
// The node account IDs must be set to execute a cost query
if (this._nodeAccountIds.isEmpty) {
this._nodeAccountIds.setList(
Expand All @@ -187,8 +187,9 @@ export default class Query extends Executable {

// Change the timestamp. Should we be doing this?
this._timestamp = Date.now();
const cost = await COST_QUERY[0](this).execute(client);

return COST_QUERY[0](this).execute(client);
return Hbar.fromString(cost.toBigNumber().plus(0.001).toString());
}

/**
Expand Down Expand Up @@ -343,9 +344,7 @@ export default class Query extends Executable {
// Not sure if we should be overwritting this field tbh.
this._timestamp = Date.now();

if (!this._nodeAccountIds.locked) {
return;
}
this._nodeAccountIds.setLocked();

// Generate the payment transactions
for (const node of this._nodeAccountIds.list) {
Expand All @@ -357,7 +356,7 @@ export default class Query extends Executable {
),
node,
this._isPaymentRequired() ? this._operator : null,
/** @type {Hbar} */ (cost)
/** @type {Hbar} */ (this._queryPayment)
)
);
}
Expand Down

0 comments on commit 8122867

Please sign in to comment.