Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inaccurate cost calculation #1430

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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