1+ """
2+ This module implements the TransactionRecord class which represents a complete record
3+ of a transaction executed on the Hiero network. It serves as a comprehensive data
4+ structure that captures all aspects of a transaction's execution and its effects.
5+
6+ The module provides functionality to:
7+ - Store and access detailed transaction metadata (ID, hash, memo, fees)
8+ - Track various types of asset transfers:
9+ * HBAR cryptocurrency transfers between accounts
10+ * Fungible token transfers with amounts
11+ * Non-fungible token (NFT) transfers with serial numbers
12+ - Handle smart contract execution results
13+ - Process airdrop records for token distributions
14+ - Manage pseudo-random number generation (PRNG) outputs
15+ - Convert between Hiero's internal representation and protobuf messages
16+
17+ """
18+
119from collections import defaultdict
220from dataclasses import dataclass , field
321from typing import Optional
1533@dataclass
1634class TransactionRecord :
1735 """
18- Represents a transaction record on the network.
36+ Represents a record of a completed transaction on the Hiero network.
37+ This class combines detailed information about the a transaction including the
38+ transaction ID, receipt, token and NFT transfers, fees & other
39+ metadata such as pseudo-random number generation(PRNG) results and
40+ pending airdrop records.
41+
42+ Attributes:
43+ transaction_id (Optional[TransactionId]): The unique identifier of the transaction.
44+ transaction_hash (Optional[bytes]): The raw hash of the transaction as recorded on-chain.
45+ transaction_memo (Optional[str]): A text memo associated with the transaction.
46+ transaction_fee (Optional[int]): The total network fee (in tinybars) charged for the transaction.
47+ receipt (Optional[TransactionReceipt]): The receipt summarizing the outcome and status of the transaction.
48+ call_result (Optional[ContractFunctionResult]): The result of a contract call if the transaction was a smart contract execution.
49+ token_transfers (defaultdict[TokenId, defaultdict[AccountId, int]]):
50+ A mapping of token IDs to account-level transfer amounts.
51+ Represents fungible token movements within the transaction.
52+ nft_transfers (defaultdict[TokenId, list[TokenNftTransfer]]):
53+ A mapping of token IDs to lists of NFT transfers for that token.
54+ transfers (defaultdict[AccountId, int]): A mapping of account IDs to hbar transfer amounts (positive for credit, negative for debit).
55+ new_pending_airdrops (list[PendingAirdropRecord]):A list of new airdrop records created by this transaction.
56+
57+ prng_number (Optional[int]): A pseudo-random integer generated by the network (if applicable).
58+ prng_bytes (Optional[bytes]): A pseudo-random byte array generated by the network (if applicable).
59+
1960 """
2061
2162 transaction_id : Optional [TransactionId ] = None
@@ -34,6 +75,16 @@ class TransactionRecord:
3475 prng_bytes : Optional [bytes ] = None
3576
3677 def __repr__ (self ) -> str :
78+ """Returns a human-readable string representation of the TransactionRecord.
79+
80+ This method constructs a detailed string containing all significant fields of the
81+ transaction record including transaction ID, hash, memo, fees, status, transfers,
82+ and PRNG results. For the receipt status, it attempts to resolve the numeric status
83+ to a human-readable ResponseCode name.
84+
85+ Returns:
86+ str: A string representation showing all significant fields of the TransactionRecord.
87+ """
3788 status = None
3889 if self .receipt :
3990 try :
@@ -57,12 +108,31 @@ def __repr__(self) -> str:
57108
58109 @classmethod
59110 def _from_proto (cls , proto : transaction_record_pb2 .TransactionRecord , transaction_id : Optional [TransactionId ] = None ) -> 'TransactionRecord' :
60- """
61- Creates a TransactionRecord from a protobuf record.
111+ """Creates a TransactionRecord instance from a protobuf transaction record.
112+
113+ This method performs complex data aggregation from the protobuf message,
114+ including:
115+ - Basic transaction metadata (hash, memo, fees)
116+ - Token transfers (both fungible and non-fungible)
117+ - Account balance transfers
118+ - Contract execution results
119+ - Airdrop records
120+ - PRNG (pseudo-random number generation) results
121+
122+ The method maps all nested transfer data from the raw protobuf message into
123+ the structured format used by the TransactionRecord class & organizing them
124+ into appropriate defaultdict collections for efficient access.
62125
63126 Args:
64- proto: The protobuf transaction record
65- transaction_id: Optional transaction ID to associate with the record
127+ proto (transaction_record_pb2.TransactionRecord): The raw protobuf
128+ transaction record containing all transaction data.
129+ transaction_id (Optional[TransactionId]): Optional transaction ID to
130+ associate with the record. If not provided, will be extracted from
131+ the protobuf message if available.
132+
133+ Returns:
134+ TransactionRecord: A new TransactionRecord instance containing all the
135+ processed and structured data from the protobuf message.
66136 """
67137 token_transfers = defaultdict (lambda : defaultdict (int ))
68138 for token_transfer_list in proto .tokenTransferLists :
@@ -106,8 +176,25 @@ def _from_proto(cls, proto: transaction_record_pb2.TransactionRecord, transactio
106176 )
107177
108178 def _to_proto (self ) -> transaction_record_pb2 .TransactionRecord :
109- """
110- Returns the underlying protobuf transaction record.
179+ """Converts the TransactionRecord instance to its protobuf representation.
180+
181+ This method serializes all components of the transaction record into a protobuf message,
182+ including:
183+ - Basic transaction metadata (hash, memo, fees)
184+ - Token transfers (both fungible and non-fungible)
185+ - Account balance transfers
186+ - Contract execution results
187+ - Airdrop records
188+ - PRNG (pseudo-random number generation) results
189+
190+ The method performs a deep conversion of all nested objects (token transfers,
191+ NFT transfers, account transfers, and pending airdrops) to their respective
192+ protobuf representations.
193+
194+ Returns:
195+ transaction_record_pb2.TransactionRecord: A protobuf message containing
196+ all the transaction record data in a format suitable for network transmission
197+ or storage.
111198 """
112199 record_proto = transaction_record_pb2 .TransactionRecord (
113200 transactionHash = self .transaction_hash ,
0 commit comments