Skip to content

Commit 112822f

Browse files
fix: remove circular dependancy in dependencies (#2985)
* fix: remove circular dependancy Signed-off-by: Ivaylo Nikolov <[email protected]> * refactor: remove dead code Signed-off-by: Ivaylo Nikolov <[email protected]> --------- Signed-off-by: Ivaylo Nikolov <[email protected]>
1 parent fe6e3d8 commit 112822f

File tree

3 files changed

+89
-16
lines changed

3 files changed

+89
-16
lines changed

src/exports.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,3 @@ export const NetworkName = {
210210
Testnet: "testnet",
211211
Previewnet: "previewnet",
212212
};
213-
214-
import "./query/CostQuery.js";

src/query/CostQuery.js

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import TransactionId from "../transaction/TransactionId.js";
44
import Hbar from "../Hbar.js";
55
import Executable from "../Executable.js";
66
import AccountId from "../account/AccountId.js";
7-
import { _makePaymentTransaction, COST_QUERY } from "./Query.js";
87
import * as HieroProto from "@hashgraph/proto";
8+
import Long from "long";
99

1010
/**
1111
* @typedef {import("../channel/Channel.js").default} Channel
@@ -187,4 +187,89 @@ export default class CostQuery extends Executable {
187187
}
188188
}
189189

190-
COST_QUERY.push((query) => new CostQuery(query));
190+
/**
191+
* Generate a payment transaction given, aka. `TransferTransaction`
192+
*
193+
* @param {TransactionId} paymentTransactionId
194+
* @param {AccountId} nodeId
195+
* @param {?import("../Executable.js").ClientOperator} operator
196+
* @param {Hbar} paymentAmount
197+
* @returns {Promise<HieroProto.proto.ITransaction>}
198+
*/
199+
export async function _makePaymentTransaction(
200+
paymentTransactionId,
201+
nodeId,
202+
operator,
203+
paymentAmount,
204+
) {
205+
const accountAmounts = [];
206+
207+
// If an operator is provided then we should make sure we transfer
208+
// from the operator to the node.
209+
// If an operator is not provided we simply create an effectively
210+
// empty account amounts
211+
if (operator != null) {
212+
accountAmounts.push({
213+
accountID: operator.accountId._toProtobuf(),
214+
amount: paymentAmount.negated().toTinybars(),
215+
});
216+
accountAmounts.push({
217+
accountID: nodeId._toProtobuf(),
218+
amount: paymentAmount.toTinybars(),
219+
});
220+
} else {
221+
accountAmounts.push({
222+
accountID: new AccountId(0)._toProtobuf(),
223+
// If the account ID is 0, shouldn't we just hard
224+
// code this value to 0? Same for the latter.
225+
amount: paymentAmount.negated().toTinybars(),
226+
});
227+
accountAmounts.push({
228+
accountID: nodeId._toProtobuf(),
229+
amount: paymentAmount.toTinybars(),
230+
});
231+
}
232+
/**
233+
* @type {HieroProto.proto.ITransactionBody}
234+
*/
235+
const body = {
236+
transactionID: paymentTransactionId._toProtobuf(),
237+
nodeAccountID: nodeId._toProtobuf(),
238+
transactionFee: new Hbar(1).toTinybars(),
239+
transactionValidDuration: {
240+
seconds: Long.fromNumber(120),
241+
},
242+
cryptoTransfer: {
243+
transfers: {
244+
accountAmounts,
245+
},
246+
},
247+
};
248+
249+
/** @type {HieroProto.proto.ISignedTransaction} */
250+
const signedTransaction = {
251+
bodyBytes: HieroProto.proto.TransactionBody.encode(body).finish(),
252+
};
253+
254+
// Sign the transaction if an operator is provided
255+
//
256+
// We have _several_ places where we build the transactions, maybe this is
257+
// something we can deduplicate?
258+
if (operator != null) {
259+
const signature = await operator.transactionSigner(
260+
/** @type {Uint8Array} */ (signedTransaction.bodyBytes),
261+
);
262+
263+
signedTransaction.sigMap = {
264+
sigPair: [operator.publicKey._toProtobufSignature(signature)],
265+
};
266+
}
267+
268+
// Create and return a `proto.Transaction`
269+
return {
270+
signedTransactionBytes:
271+
HieroProto.proto.SignedTransaction.encode(
272+
signedTransaction,
273+
).finish(),
274+
};
275+
}

src/query/Query.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import TransactionId from "../transaction/TransactionId.js";
88
import * as HieroProto from "@hashgraph/proto";
99
import PrecheckStatusError from "../PrecheckStatusError.js";
1010
import MaxQueryPaymentExceeded from "../MaxQueryPaymentExceeded.js";
11+
import CostQuery from "./CostQuery.js";
1112
import Long from "long";
1213

1314
/**
@@ -161,13 +162,9 @@ export default class Query extends Executable {
161162
);
162163
}
163164

164-
if (COST_QUERY.length != 1) {
165-
throw new Error("CostQuery has not been loaded yet");
166-
}
167-
168165
// Change the timestamp. Should we be doing this?
169166
this._timestamp = Date.now();
170-
const cost = await COST_QUERY[0](this).execute(client);
167+
const cost = await new CostQuery(this).execute(client);
171168
return Hbar.fromTinybars(
172169
cost._valueInTinybar.multipliedBy(1.1).toFixed(0),
173170
);
@@ -615,10 +612,3 @@ export async function _makePaymentTransaction(
615612
).finish(),
616613
};
617614
}
618-
619-
/**
620-
* Cache for the cost query constructor. This prevents cyclic dependencies.
621-
*
622-
* @type {((query: Query<*>) => import("./CostQuery.js").default<*>)[]}
623-
*/
624-
export const COST_QUERY = [];

0 commit comments

Comments
 (0)