@@ -4,8 +4,8 @@ import TransactionId from "../transaction/TransactionId.js";
4
4
import Hbar from "../Hbar.js" ;
5
5
import Executable from "../Executable.js" ;
6
6
import AccountId from "../account/AccountId.js" ;
7
- import { _makePaymentTransaction , COST_QUERY } from "./Query.js" ;
8
7
import * as HieroProto from "@hashgraph/proto" ;
8
+ import Long from "long" ;
9
9
10
10
/**
11
11
* @typedef {import("../channel/Channel.js").default } Channel
@@ -187,4 +187,89 @@ export default class CostQuery extends Executable {
187
187
}
188
188
}
189
189
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
+ }
0 commit comments