Skip to content

Commit

Permalink
Fix/get cost query (hiero-ledger#1496)
Browse files Browse the repository at this point in the history
  • Loading branch information
ochikov authored Mar 10, 2023
1 parent 7114e2e commit b3a7f0d
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 51 deletions.
9 changes: 2 additions & 7 deletions src/account/AccountInfoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Query, { QUERY_REGISTRY } from "../query/Query.js";
import AccountId from "./AccountId.js";
import AccountInfo from "./AccountInfo.js";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import Hbar from "../Hbar.js";

/**
Expand Down Expand Up @@ -126,13 +127,7 @@ export default class AccountInfoQuery extends Query {
* @returns {Promise<Hbar>}
*/
async getCost(client) {
let cost = await super.getCost(client);

if (cost.toTinybars().greaterThan(25)) {
return cost;
} else {
return Hbar.fromTinybars(25);
}
return super.getCost(client);
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/contract/ContractInfoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Query, { QUERY_REGISTRY } from "../query/Query.js";
import ContractId from "./ContractId.js";
import ContractInfo from "./ContractInfo.js";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import Hbar from "../Hbar.js";

/**
Expand Down Expand Up @@ -127,13 +128,7 @@ export default class ContractInfoQuery extends Query {
* @returns {Promise<Hbar>}
*/
async getCost(client) {
let cost = await super.getCost(client);

if (cost.toTinybars().greaterThan(25)) {
return cost;
} else {
return Hbar.fromTinybars(25);
}
return super.getCost(client);
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/file/FileInfoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Query, { QUERY_REGISTRY } from "../query/Query.js";
import FileId from "./FileId.js";
import FileInfo from "./FileInfo.js";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import Hbar from "../Hbar.js";

/**
Expand Down Expand Up @@ -107,13 +108,7 @@ export default class FileInfoQuery extends Query {
* @returns {Promise<Hbar>}
*/
async getCost(client) {
let cost = await super.getCost(client);

if (cost.toTinybars().greaterThan(25)) {
return cost;
} else {
return Hbar.fromTinybars(25);
}
return super.getCost(client);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/query/CostQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export default class CostQuery extends Executable {
);
}

// operator.accountId
const transactionId = TransactionId.generate(operator.accountId);
if (this._query.paymentTransactionId == null) {
this._query.setPaymentTransactionId(transactionId);
}

this._header = {
payment: await _makePaymentTransaction(
this._getLogId(),
Expand Down
9 changes: 2 additions & 7 deletions src/schedule/ScheduleInfoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Query, { QUERY_REGISTRY } from "../query/Query.js";
import ScheduleId from "./ScheduleId.js";
import ScheduleInfo from "./ScheduleInfo.js";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import Hbar from "../Hbar.js";

/**
Expand Down Expand Up @@ -107,13 +108,7 @@ export default class ScheduleInfoQuery extends Query {
* @returns {Promise<Hbar>}
*/
async getCost(client) {
let cost = await super.getCost(client);

if (cost.toTinybars().greaterThan(25)) {
return cost;
} else {
return Hbar.fromTinybars(25);
}
return super.getCost(client);
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/token/TokenInfoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Query, { QUERY_REGISTRY } from "../query/Query.js";
import TokenId from "./TokenId.js";
import TokenInfo from "./TokenInfo.js";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import Hbar from "../Hbar.js";

/**
Expand Down Expand Up @@ -107,13 +108,7 @@ export default class TokenInfoQuery extends Query {
* @returns {Promise<Hbar>}
*/
async getCost(client) {
let cost = await super.getCost(client);

if (cost.toTinybars().greaterThan(25)) {
return cost;
} else {
return Hbar.fromTinybars(25);
}
return super.getCost(client);
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/token/TokenNftInfoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import NftId from "./NftId.js";
import AccountId from "../account/AccountId.js";
import TokenId from "../token/TokenId.js";
import TokenNftInfo from "./TokenNftInfo.js";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import Hbar from "../Hbar.js";
import Long from "long";

Expand Down Expand Up @@ -298,13 +299,7 @@ export default class TokenNftInfoQuery extends Query {
* @returns {Promise<Hbar>}
*/
async getCost(client) {
let cost = await super.getCost(client);

if (cost.toTinybars().greaterThan(25)) {
return cost;
} else {
return Hbar.fromTinybars(25);
}
return super.getCost(client);
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/topic/TopicInfoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Query, { QUERY_REGISTRY } from "../query/Query.js";
import TopicId from "./TopicId.js";
import TopicInfo from "./TopicInfo.js";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import Hbar from "../Hbar.js";

/**
Expand Down Expand Up @@ -115,13 +116,7 @@ export default class TopicInfoQuery extends Query {
* @returns {Promise<Hbar>}
*/
async getCost(client) {
let cost = await super.getCost(client);

if (cost.toTinybars().greaterThan(25)) {
return cost;
} else {
return Hbar.fromTinybars(25);
}
return super.getCost(client);
}

/**
Expand Down
42 changes: 41 additions & 1 deletion test/integration/AccountInfoIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,47 @@ describe("AccountInfo", function () {
.setAccountId(operatorId)
.getCost(env.client);

expect(cost.toTinybars().toInt()).to.be.at.least(25);
expect(cost.toTinybars().toInt()).to.be.at.least(1);
});

it("should error on query cost on deleted account with ACCOUNT_DELETED", async function () {
this.timeout(120000);

const newKey = PrivateKey.generate();

let createTransaction = await new AccountCreateTransaction()
.setInitialBalance(new Hbar(10)) // 10 h
.setKey(newKey.publicKey)
.execute(env.client);

const receiptCreateTransaction = await createTransaction.getReceipt(
env.client
);

let deleteTransaction = await new AccountDeleteTransaction()
.setAccountId(receiptCreateTransaction.accountId)
.setTransferAccountId(env.operatorId)
.freezeWith(env.client);

newKey.signTransaction(deleteTransaction);
const deleteTransactionSubmitted = await deleteTransaction.execute(
env.client
);

await deleteTransactionSubmitted.getReceipt(env.client);

let err;
try {
await new AccountInfoQuery()
.setAccountId(receiptCreateTransaction.accountId)
.getCost(env.client);
} catch (error) {
err = error.toString().includes(Status.AccountDeleted.toString());
}

if (!err) {
throw new Error("query cost did not error");
}
});

it("should be executable", async function () {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/ContractCallIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("ContractCallIntegration", function () {
.setGas(75000)
.setFunction("getMessage");

const cost = callQuery.getCost(env.client);
const cost = await callQuery.getCost(env.client);

let result = await callQuery
.setMaxQueryPayment(cost)
Expand Down
34 changes: 34 additions & 0 deletions test/integration/ContractInfoIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,40 @@ describe("ContractInfo", function () {
}
});

it("should be able to query cost", async function () {
this.timeout(120000);

const operatorKey = env.operatorKey.publicKey;
let response = await new FileCreateTransaction()
.setKeys([operatorKey])
.setContents(smartContractBytecode)
.execute(env.client);

let receipt = await response.getReceipt(env.client);

const file = receipt.fileId;

response = await new ContractCreateTransaction()
.setAdminKey(operatorKey)
.setGas(100000)
.setConstructorParameters(
new ContractFunctionParameters().addString("Hello from Hedera.")
)
.setBytecodeFileId(file)
.setContractMemo("[e2e::ContractCreateTransaction]")
.execute(env.client);

receipt = await response.getReceipt(env.client);

let contract = receipt.contractId;

const cost = await new ContractInfoQuery()
.setContractId(contract)
.getCost(env.client);

expect(cost.toTinybars().toInt()).to.be.at.least(1);
});

after(async function () {
await env.close();
});
Expand Down
18 changes: 18 additions & 0 deletions test/integration/FileInfoIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ describe("FileInfo", function () {
expect(info.keys.toArray().length).to.be.equal(0);
});

it("should be able to query cost", async function () {
this.timeout(120000);
const operatorKey = env.operatorKey.publicKey;

const response = await new FileCreateTransaction()
.setKeys([operatorKey])
.execute(env.client);

let receipt = await response.getReceipt(env.client);
let file = receipt.fileId;

const cost = await new FileInfoQuery()
.setFileId(file)
.getCost(env.client);

expect(cost.toTinybars().toInt()).to.be.at.least(1);
});

after(async function () {
await env.close();
});
Expand Down
65 changes: 65 additions & 0 deletions test/integration/ScheduleCreateIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ScheduleSignTransaction,
TopicCreateTransaction,
TopicMessageSubmitTransaction,
AccountBalanceQuery,
} from "../../src/exports.js";
import IntegrationTestEnv from "./client/NodeIntegrationTestEnv.js";

Expand Down Expand Up @@ -102,6 +103,70 @@ describe("ScheduleCreate", function () {
.execute(env.client);
});

it("should be able to query cost", async function () {
this.timeout(120000);
const operatorKey = env.operatorKey.publicKey;
const operatorId = env.operatorId;

const key1 = PrivateKey.generateED25519();

// Submit Key
const key2 = PrivateKey.generateED25519();

const key3 = PrivateKey.generateED25519();

const keyList = KeyList.of(
key1.publicKey,
key2.publicKey,
key3.publicKey
);

const balance = await new AccountBalanceQuery()
.setAccountId(operatorId)
.execute(env.client);

console.log(`Balances of the new account: ${balance.toString()}`);

const response = await new AccountCreateTransaction()
.setInitialBalance(new Hbar(10))
.setKey(keyList)
.execute(env.client);

expect((await response.getReceipt(env.client)).accountId).to.be.not
.null;

const topicId = (
await (
await new TopicCreateTransaction()
.setAdminKey(operatorKey)
.setAutoRenewAccountId(operatorId)
.setTopicMemo("HCS Topic_")
.setSubmitKey(key2)
.execute(env.client)
).getReceipt(env.client)
).topicId;

const transaction = new TopicMessageSubmitTransaction()
.setTopicId(topicId)
.setMessage("scheduled hcs message");

const scheduled = transaction
.schedule()
.setPayerAccountId(operatorId)
.setAdminKey(operatorKey)
.freezeWith(env.client);

const scheduleId = (
await (await scheduled.execute(env.client)).getReceipt(env.client)
).scheduleId;

const cost = await new ScheduleInfoQuery()
.setScheduleId(scheduleId)
.getCost(env.client);

expect(cost.toTinybars().toInt()).to.be.at.least(1);
});

after(async function () {
await env.close();
});
Expand Down
Loading

0 comments on commit b3a7f0d

Please sign in to comment.