-
Notifications
You must be signed in to change notification settings - Fork 131
/
TransactionRecord.js
636 lines (583 loc) · 22.7 KB
/
TransactionRecord.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*-
*
* Hedera JavaScript SDK
*
* Copyright (C) 2020 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import TransactionReceipt from "./TransactionReceipt.js";
import TransactionId from "./TransactionId.js";
import Timestamp from "../Timestamp.js";
import Hbar from "../Hbar.js";
import Transfer from "../Transfer.js";
import ContractFunctionResult from "../contract/ContractFunctionResult.js";
import TokenTransferMap from "../account/TokenTransferMap.js";
import TokenNftTransferMap from "../account/TokenNftTransferMap.js";
import * as HashgraphProto from "@hashgraph/proto";
import ScheduleId from "../schedule/ScheduleId.js";
import AssessedCustomFee from "../token/AssessedCustomFee.js";
import TokenAssocation from "../token/TokenAssociation.js";
import Key from "../Key.js";
import PublicKey from "../PublicKey.js";
import TokenTransfer from "../token/TokenTransfer.js";
import EvmAddress from "../EvmAddress.js";
import * as hex from "../encoding/hex.js";
/**
* @typedef {import("../token/TokenId.js").default} TokenId
* @typedef {import("../token/TokenTransfer.js").TokenTransferJSON} TokenTransferJSON
* @typedef {import("../account/HbarAllowance.js").default} HbarAllowance
* @typedef {import("../account/TokenAllowance.js").default} TokenAllowance
* @typedef {import("../account/TokenNftAllowance.js").default} TokenNftAllowance
* @typedef {import("./TransactionReceipt.js").TransactionReceiptJSON} TransactionReceiptJSON
* @typedef {import("../Transfer.js").TransferJSON} TransferJSON
*/
/**
* @typedef {object} TransactionRecordJSON
* @property {TransactionReceiptJSON} receipt
* @property {?string} transactionHash
* @property {Date} consensusTimestamp
* @property {string} transactionId
* @property {string} transactionMemo
* @property {string} transactionFee
* @property {TransferJSON[]} transfers
* @property {TokenTransferMap} tokenTransfers
* @property {TokenTransferJSON[]} tokenTransfersList
* @property {?string} scheduleRef
* @property {AssessedCustomFee[]} assessedCustomFees
* @property {TokenNftTransferMap} nftTransfers
* @property {TokenAssocation[]} automaticTokenAssociations
* @property {Date | null} parentConsensusTimestamp
* @property {?string} aliasKey
* @property {TransactionRecord[]} duplicates
* @property {TransactionRecord[]} children
* @property {?string} ethereumHash
* @property {Transfer[]} paidStakingRewards
* @property {?string} prngBytes
* @property {?number} prngNumber
* @property {?string} evmAddress
*/
/**
* Either the record of processing the first consensus transaction with the given id whose
* status was neither <tt>INVALID_NODE_ACCOUNT</tt> nor <tt>INVALID_PAYER_SIGNATURE</tt>;
* <b>or</b>, if no such record exists, the record of processing the first transaction to reach
* consensus with the given transaction id.
*/
export default class TransactionRecord {
/**
* @private
* @param {object} props
* @param {ContractFunctionResult} [props.contractFunctionResult]
* @param {TransactionReceipt} props.receipt
* @param {Uint8Array} props.transactionHash
* @param {Timestamp} props.consensusTimestamp
* @param {TransactionId} props.transactionId
* @param {string} props.transactionMemo
* @param {Hbar} props.transactionFee
* @param {Transfer[]} props.transfers
* @param {TokenTransferMap} props.tokenTransfers
* @param {TokenTransfer[]} props.tokenTransfersList
* @param {?ScheduleId} props.scheduleRef
* @param {AssessedCustomFee[]} props.assessedCustomFees
* @param {TokenNftTransferMap} props.nftTransfers
* @param {TokenAssocation[]} props.automaticTokenAssociations
* @param {Timestamp | null} props.parentConsensusTimestamp
* @param {PublicKey | null} props.aliasKey
* @param {TransactionRecord[]} props.duplicates
* @param {TransactionRecord[]} props.children
* @param {HbarAllowance[]} props.hbarAllowanceAdjustments
* @param {TokenAllowance[]} props.tokenAllowanceAdjustments
* @param {TokenNftAllowance[]} props.nftAllowanceAdjustments
* @param {?Uint8Array} props.ethereumHash
* @param {Transfer[]} props.paidStakingRewards
* @param {?Uint8Array} props.prngBytes
* @param {?number} props.prngNumber
* @param {?EvmAddress} props.evmAddress
*/
constructor(props) {
/**
* The status (reach consensus, or failed, or is unknown) and the ID of
* any new account/file/instance created.
*
* @readonly
*/
this.receipt = props.receipt;
/**
* The hash of the Transaction that executed (not the hash of any Transaction that failed
* for having a duplicate TransactionID).
*
* @readonly
*/
this.transactionHash = props.transactionHash;
/**
* The consensus timestamp (or null if didn't reach consensus yet).
*
* @readonly
*/
this.consensusTimestamp = props.consensusTimestamp;
/**
* The ID of the transaction this record represents.
*
* @readonly
*/
this.transactionId = props.transactionId;
/**
* The memo that was submitted as part of the transaction (max 100 bytes).
*
* @readonly
*/
this.transactionMemo = props.transactionMemo;
/**
* The actual transaction fee charged,
* not the original transactionFee value from TransactionBody.
*
* @readonly
*/
this.transactionFee = props.transactionFee;
/**
* All hbar transfers as a result of this transaction, such as fees, or transfers performed
* by the transaction, or by a smart contract it calls, or by the creation of threshold
* records that it triggers.
*
* @readonly
*/
this.transfers = props.transfers;
/**
* Record of the value returned by the smart contract function or constructor.
*
* @readonly
*/
this.contractFunctionResult =
props.contractFunctionResult != null
? props.contractFunctionResult
: null;
/**
* All the token transfers from this account
*
* @readonly
*/
this.tokenTransfers = props.tokenTransfers;
/**
* All the token transfers from this account
*
* @readonly
*/
this.tokenTransfersList = props.tokenTransfersList;
/**
* Reference to the scheduled transaction ID that this transaction record represent
*
* @readonly
*/
this.scheduleRef = props.scheduleRef;
/**
* All custom fees that were assessed during a CryptoTransfer, and must be paid if the
* transaction status resolved to SUCCESS
*
* @readonly
*/
this.assessedCustomFees = props.assessedCustomFees;
/** @readonly */
this.nftTransfers = props.nftTransfers;
/**
* All token associations implicitly created while handling this transaction
*
* @readonly
*/
this.automaticTokenAssociations = props.automaticTokenAssociations;
/**
* In the record of an internal transaction, the consensus timestamp of the user
* transaction that spawned it.
*
* @readonly
*/
this.parentConsensusTimestamp = props.parentConsensusTimestamp;
/**
* In the record of an internal CryptoCreate transaction triggered by a user
* transaction with a (previously unused) alias, the new account's alias.
*
* @readonly
*/
this.aliasKey = props.aliasKey;
/**
* The records of processing all consensus transaction with the same id as the distinguished
* record above, in chronological order.
*
* @readonly
*/
this.duplicates = props.duplicates;
/**
* The records of processing all child transaction spawned by the transaction with the given
* top-level id, in consensus order. Always empty if the top-level status is UNKNOWN.
*
* @readonly
*/
this.children = props.children;
/**
* @deprecated
* @readonly
*/
// eslint-disable-next-line deprecation/deprecation
this.hbarAllowanceAdjustments = props.hbarAllowanceAdjustments;
/**
* @deprecated
* @readonly
*/
// eslint-disable-next-line deprecation/deprecation
this.tokenAllowanceAdjustments = props.tokenAllowanceAdjustments;
/**
* @deprecated
* @readonly
*/
// eslint-disable-next-line deprecation/deprecation
this.nftAllowanceAdjustments = props.nftAllowanceAdjustments;
/**
* The keccak256 hash of the ethereumData. This field will only be populated for
* EthereumTransaction.
*
* @readonly
*/
this.ethereumHash = props.ethereumHash;
/**
* List of accounts with the corresponding staking rewards paid as a result of a transaction.
*
* @readonly
*/
this.paidStakingRewards = props.paidStakingRewards;
/**
* In the record of a PRNG transaction with no output range, a pseudorandom 384-bit string.
*
* @readonly
*/
this.prngBytes = props.prngBytes;
/**
* In the record of a PRNG transaction with an output range, the output of a PRNG whose input was a 384-bit string.
*
* @readonly
*/
this.prngNumber = props.prngNumber;
/**
* The new default EVM address of the account created by this transaction.
* This field is populated only when the EVM address is not specified in the related transaction body.
*
* @readonly
*/
this.evmAddress = props.evmAddress;
Object.freeze(this);
}
/**
* @internal
* @returns {HashgraphProto.proto.ITransactionGetRecordResponse}
*/
_toProtobuf() {
const tokenTransfers = this.tokenTransfers._toProtobuf();
const nftTransfers = this.nftTransfers._toProtobuf();
const tokenTransferLists = [];
for (const tokenTransfer of tokenTransfers) {
for (const nftTransfer of nftTransfers) {
if (
tokenTransfer.token != null &&
nftTransfer.token != null &&
tokenTransfer.token.shardNum ===
nftTransfer.token.shardNum &&
tokenTransfer.token.realmNum ===
nftTransfer.token.realmNum &&
tokenTransfer.token.tokenNum === nftTransfer.token.tokenNum
) {
tokenTransferLists.push({
token: tokenTransfer.token,
transfers: tokenTransfer.transfers,
nftTransfers: tokenTransfer.nftTransfers,
});
} else {
tokenTransferLists.push(tokenTransfer);
tokenTransferLists.push(nftTransfer);
}
}
}
const duplicates = this.duplicates.map(
(record) =>
/** @type {HashgraphProto.proto.ITransactionRecord} */ (
record._toProtobuf().transactionRecord
)
);
const children = this.children.map(
(record) =>
/** @type {HashgraphProto.proto.ITransactionRecord} */ (
record._toProtobuf().transactionRecord
)
);
return {
duplicateTransactionRecords: duplicates,
childTransactionRecords: children,
transactionRecord: {
receipt: this.receipt._toProtobuf().receipt,
transactionHash:
this.transactionHash != null ? this.transactionHash : null,
consensusTimestamp:
this.consensusTimestamp != null
? this.consensusTimestamp._toProtobuf()
: null,
transactionID:
this.transactionId != null
? this.transactionId._toProtobuf()
: null,
memo:
this.transactionMemo != null ? this.transactionMemo : null,
transactionFee:
this.transactionFee != null
? this.transactionFee.toTinybars()
: null,
contractCallResult:
this.contractFunctionResult != null &&
!this.contractFunctionResult._createResult
? this.contractFunctionResult._toProtobuf()
: null,
contractCreateResult:
this.contractFunctionResult != null &&
this.contractFunctionResult._createResult
? this.contractFunctionResult._toProtobuf()
: null,
transferList:
this.transfers != null
? {
accountAmounts: this.transfers.map((transfer) =>
transfer._toProtobuf()
),
}
: null,
tokenTransferLists,
scheduleRef:
this.scheduleRef != null
? this.scheduleRef._toProtobuf()
: null,
assessedCustomFees: this.assessedCustomFees.map((fee) =>
fee._toProtobuf()
),
automaticTokenAssociations: this.automaticTokenAssociations.map(
(association) => association._toProtobuf()
),
parentConsensusTimestamp:
this.parentConsensusTimestamp != null
? this.parentConsensusTimestamp._toProtobuf()
: null,
alias:
this.aliasKey != null
? HashgraphProto.proto.Key.encode(
this.aliasKey._toProtobufKey()
).finish()
: null,
ethereumHash: this.ethereumHash,
paidStakingRewards: this.paidStakingRewards.map((transfer) =>
transfer._toProtobuf()
),
prngBytes: this.prngBytes,
prngNumber: this.prngNumber != null ? this.prngNumber : null,
evmAddress:
this.evmAddress != null ? this.evmAddress.toBytes() : null,
},
};
}
/**
* @internal
* @param {HashgraphProto.proto.ITransactionGetRecordResponse} response
* @returns {TransactionRecord}
*/
static _fromProtobuf(response) {
const record = /** @type {HashgraphProto.proto.ITransactionRecord} */ (
response.transactionRecord
);
let aliasKey =
record.alias != null && record.alias.length > 0
? Key._fromProtobufKey(
HashgraphProto.proto.Key.decode(record.alias)
)
: null;
if (!(aliasKey instanceof PublicKey)) {
aliasKey = null;
}
const children =
response.childTransactionRecords != null
? response.childTransactionRecords.map((child) =>
TransactionRecord._fromProtobuf({
transactionRecord: child,
})
)
: [];
const duplicates =
response.duplicateTransactionRecords != null
? response.duplicateTransactionRecords.map((duplicate) =>
TransactionRecord._fromProtobuf({
transactionRecord: duplicate,
})
)
: [];
const contractFunctionResult =
record.contractCallResult != null
? ContractFunctionResult._fromProtobuf(
record.contractCallResult,
false
)
: record.contractCreateResult != null
? ContractFunctionResult._fromProtobuf(
record.contractCreateResult,
true
)
: undefined;
return new TransactionRecord({
receipt: TransactionReceipt._fromProtobuf({
receipt:
/** @type {HashgraphProto.proto.ITransactionReceipt} */ (
record.receipt
),
}),
transactionHash:
record.transactionHash != null
? record.transactionHash
: new Uint8Array(),
consensusTimestamp: Timestamp._fromProtobuf(
/** @type {HashgraphProto.proto.ITimestamp} */
(record.consensusTimestamp)
),
transactionId: TransactionId._fromProtobuf(
/** @type {HashgraphProto.proto.ITransactionID} */ (
record.transactionID
)
),
transactionMemo: record.memo != null ? record.memo : "",
transactionFee: Hbar.fromTinybars(
record.transactionFee != null ? record.transactionFee : 0
),
transfers: Transfer._fromProtobuf(
record.transferList != null
? record.transferList.accountAmounts != null
? record.transferList.accountAmounts
: []
: []
),
contractFunctionResult,
tokenTransfers: TokenTransferMap._fromProtobuf(
record.tokenTransferLists != null
? record.tokenTransferLists
: []
),
tokenTransfersList: TokenTransfer._fromProtobuf(
record.tokenTransferLists != null
? record.tokenTransferLists
: []
),
scheduleRef:
record.scheduleRef != null
? ScheduleId._fromProtobuf(record.scheduleRef)
: null,
assessedCustomFees:
record.assessedCustomFees != null
? record.assessedCustomFees.map((fee) =>
AssessedCustomFee._fromProtobuf(fee)
)
: [],
nftTransfers: TokenNftTransferMap._fromProtobuf(
record.tokenTransferLists != null
? record.tokenTransferLists
: []
),
automaticTokenAssociations:
record.automaticTokenAssociations != null
? record.automaticTokenAssociations.map((association) =>
TokenAssocation._fromProtobuf(association)
)
: [],
parentConsensusTimestamp:
record.parentConsensusTimestamp != null
? Timestamp._fromProtobuf(record.parentConsensusTimestamp)
: null,
aliasKey,
duplicates,
children,
hbarAllowanceAdjustments: [],
tokenAllowanceAdjustments: [],
nftAllowanceAdjustments: [],
ethereumHash:
record.ethereumHash != null ? record.ethereumHash : null,
paidStakingRewards:
record.paidStakingRewards != null
? Transfer._fromProtobuf(record.paidStakingRewards)
: [],
prngBytes: record.prngBytes != null ? record.prngBytes : null,
prngNumber: record.prngNumber != null ? record.prngNumber : null,
evmAddress:
record.evmAddress != null
? EvmAddress.fromBytes(record.evmAddress)
: null,
});
}
/**
* @param {Uint8Array} bytes
* @returns {TransactionRecord}
*/
static fromBytes(bytes) {
return TransactionRecord._fromProtobuf(
HashgraphProto.proto.TransactionGetRecordResponse.decode(bytes)
);
}
/**
* @returns {Uint8Array}
*/
toBytes() {
return HashgraphProto.proto.TransactionGetRecordResponse.encode(
this._toProtobuf()
).finish();
}
/**
* @returns {TransactionRecordJSON}
*/
toJSON() {
return {
receipt: this.receipt.toJSON(),
transactionHash: hex.encode(this.transactionHash),
consensusTimestamp: this.consensusTimestamp.toDate(),
transactionId: this.transactionId.toString(),
transactionMemo: this.transactionMemo,
transactionFee: this.transactionFee.toTinybars().toString(),
transfers: this.transfers.map((transfer) => transfer.toJSON()),
tokenTransfers: this.tokenTransfers,
tokenTransfersList: this.tokenTransfersList.map((transfer) =>
transfer.toJSON()
),
scheduleRef: this.scheduleRef?.toString() || null,
assessedCustomFees: this.assessedCustomFees,
nftTransfers: this.nftTransfers,
automaticTokenAssociations: this.automaticTokenAssociations,
parentConsensusTimestamp:
this.parentConsensusTimestamp?.toDate() || null,
aliasKey: this.aliasKey?.toString() || null,
duplicates: this.duplicates,
children: this.children,
ethereumHash:
this.ethereumHash != null
? hex.encode(this.ethereumHash)
: null,
paidStakingRewards: this.paidStakingRewards,
prngBytes:
this.prngBytes != null ? hex.encode(this.prngBytes) : null,
prngNumber: this.prngNumber,
evmAddress: this.evmAddress?.toString() || null,
};
}
/**
* @returns {string}
*/
toString() {
return JSON.stringify(this.toJSON());
}
}