Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.

### Added
- docs: Add Google-style docstrings to `TokenId` class and its methods in `token_id.py`.
- added Google-style docstrings to the `TransactionRecord` class including all dataclass fields, `__repr__`, `_from_proto()` & `_to_proto()` methods.
- Standardized docstrings, improved error handling, and updated type hinting (`str | None` to `Optional[str]`) for the `FileId` class (#652).

- Add Google-style docstrings to `AccountInfo` class and its methods in `account_info.py`.
Expand Down
101 changes: 94 additions & 7 deletions src/hiero_sdk_python/transaction/transaction_record.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
"""
This module implements the TransactionRecord class which represents a complete record
of a transaction executed on the Hiero network. It serves as a comprehensive data
structure that captures all aspects of a transaction's execution and its effects.

The module provides functionality to:
- Store and access detailed transaction metadata (ID, hash, memo, fees)
- Track various types of asset transfers:
* HBAR cryptocurrency transfers between accounts
* Fungible token transfers with amounts
* Non-fungible token (NFT) transfers with serial numbers
- Handle smart contract execution results
- Process airdrop records for token distributions
- Manage pseudo-random number generation (PRNG) outputs
- Convert between Hiero's internal representation and protobuf messages

"""

from collections import defaultdict
from dataclasses import dataclass, field
from typing import Optional
Expand All @@ -15,7 +33,30 @@
@dataclass
class TransactionRecord:
"""
Represents a transaction record on the network.
Represents a record of a completed transaction on the Hiero network.
This class combines detailed information about the a transaction including the
transaction ID, receipt, token and NFT transfers, fees & other
metadata such as pseudo-random number generation(PRNG) results and
pending airdrop records.

Attributes:
transaction_id (Optional[TransactionId]): The unique identifier of the transaction.
transaction_hash (Optional[bytes]): The raw hash of the transaction as recorded on-chain.
transaction_memo (Optional[str]): A text memo associated with the transaction.
transaction_fee (Optional[int]): The total network fee (in tinybars) charged for the transaction.
receipt (Optional[TransactionReceipt]): The receipt summarizing the outcome and status of the transaction.
call_result (Optional[ContractFunctionResult]): The result of a contract call if the transaction was a smart contract execution.
token_transfers (defaultdict[TokenId, defaultdict[AccountId, int]]):
A mapping of token IDs to account-level transfer amounts.
Represents fungible token movements within the transaction.
nft_transfers (defaultdict[TokenId, list[TokenNftTransfer]]):
A mapping of token IDs to lists of NFT transfers for that token.
transfers (defaultdict[AccountId, int]): A mapping of account IDs to hbar transfer amounts (positive for credit, negative for debit).
new_pending_airdrops (list[PendingAirdropRecord]):A list of new airdrop records created by this transaction.

prng_number (Optional[int]): A pseudo-random integer generated by the network (if applicable).
prng_bytes (Optional[bytes]): A pseudo-random byte array generated by the network (if applicable).

"""

transaction_id: Optional[TransactionId] = None
Expand All @@ -34,6 +75,16 @@ class TransactionRecord:
prng_bytes: Optional[bytes] = None

def __repr__(self) -> str:
"""Returns a human-readable string representation of the TransactionRecord.

This method constructs a detailed string containing all significant fields of the
transaction record including transaction ID, hash, memo, fees, status, transfers,
and PRNG results. For the receipt status, it attempts to resolve the numeric status
to a human-readable ResponseCode name.

Returns:
str: A string representation showing all significant fields of the TransactionRecord.
"""
status = None
if self.receipt:
try:
Expand All @@ -57,12 +108,31 @@ def __repr__(self) -> str:

@classmethod
def _from_proto(cls, proto: transaction_record_pb2.TransactionRecord, transaction_id: Optional[TransactionId] = None) -> 'TransactionRecord':
"""
Creates a TransactionRecord from a protobuf record.
"""Creates a TransactionRecord instance from a protobuf transaction record.

This method performs complex data aggregation from the protobuf message,
including:
- Basic transaction metadata (hash, memo, fees)
- Token transfers (both fungible and non-fungible)
- Account balance transfers
- Contract execution results
- Airdrop records
- PRNG (pseudo-random number generation) results

The method maps all nested transfer data from the raw protobuf message into
the structured format used by the TransactionRecord class & organizing them
into appropriate defaultdict collections for efficient access.

Args:
proto: The protobuf transaction record
transaction_id: Optional transaction ID to associate with the record
proto (transaction_record_pb2.TransactionRecord): The raw protobuf
transaction record containing all transaction data.
transaction_id (Optional[TransactionId]): Optional transaction ID to
associate with the record. If not provided, will be extracted from
the protobuf message if available.

Returns:
TransactionRecord: A new TransactionRecord instance containing all the
processed and structured data from the protobuf message.
"""
token_transfers = defaultdict(lambda: defaultdict(int))
for token_transfer_list in proto.tokenTransferLists:
Expand Down Expand Up @@ -106,8 +176,25 @@ def _from_proto(cls, proto: transaction_record_pb2.TransactionRecord, transactio
)

def _to_proto(self) -> transaction_record_pb2.TransactionRecord:
"""
Returns the underlying protobuf transaction record.
"""Converts the TransactionRecord instance to its protobuf representation.

This method serializes all components of the transaction record into a protobuf message,
including:
- Basic transaction metadata (hash, memo, fees)
- Token transfers (both fungible and non-fungible)
- Account balance transfers
- Contract execution results
- Airdrop records
- PRNG (pseudo-random number generation) results

The method performs a deep conversion of all nested objects (token transfers,
NFT transfers, account transfers, and pending airdrops) to their respective
protobuf representations.

Returns:
transaction_record_pb2.TransactionRecord: A protobuf message containing
all the transaction record data in a format suitable for network transmission
or storage.
"""
record_proto = transaction_record_pb2.TransactionRecord(
transactionHash=self.transaction_hash,
Expand Down