Skip to content

Commit 33eb470

Browse files
author
Rudolf Nemov
committed
[Fix] Update docstrings
1 parent 5933846 commit 33eb470

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

tronpy/proto/transaction.py

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ class Transaction(typing.TypedDict):
1717

1818

1919
def calculate_txid(transaction: tron_pb2.Transaction) -> str:
20+
"""
21+
Compute the transaction ID by hashing the serialized raw data of a Tron transaction.
22+
23+
Parameters:
24+
transaction (tron_pb2.Transaction): The Tron transaction whose ID is to be calculated.
25+
26+
Returns:
27+
str: The SHA-256 hash of the transaction's raw data, represented as a hexadecimal string.
28+
"""
2029
raw_bytes = transaction.raw_data.SerializeToString()
2130
return hashlib.sha256(raw_bytes).hexdigest()
2231

@@ -27,16 +36,18 @@ def calculate_txid(transaction: tron_pb2.Transaction) -> str:
2736

2837

2938
def _get_tapos_meta(ref_block_id: str) -> typing.Tuple[str, str, int, int]:
30-
"""Extract TAPoS metadata from a block id.
39+
"""
40+
Extracts TAPoS (Transaction as Proof of Stake) metadata from a given block ID.
3141
32-
Parameters
33-
----------
34-
ref_block_id: Hex string of the latest block id
42+
Parameters:
43+
ref_block_id (str): The block ID to extract TAPoS metadata from.
3544
36-
Returns
37-
-------
38-
typing.Tuple[str, str, int, int]
39-
(ref_block_bytes_hex, ref_block_hash_hex, timestamp_ms, expiration_ms)
45+
Returns:
46+
tuple: A tuple containing:
47+
- Reference block bytes as a hex string
48+
- Reference block hash as a hex string
49+
- Current timestamp in milliseconds
50+
- Expiration timestamp in milliseconds (current time plus 60 seconds)
4051
"""
4152
ref_block_bytes_hex = get_ref_block_bytes(ref_block_id)
4253
ref_block_hash_hex = get_ref_block_hash(ref_block_id)
@@ -51,6 +62,23 @@ def create_transaction_offline(
5162
amount: int,
5263
ref_block_id: str,
5364
) -> Transaction:
65+
"""
66+
Construct an unsigned TRX transfer transaction offline.
67+
68+
Builds a Tron transaction dictionary for transferring TRX from one address
69+
to another using the provided reference block ID for TAPoS metadata.
70+
The transaction is not signed and includes all necessary raw data fields
71+
for later signing and broadcasting.
72+
73+
Parameters:
74+
owner_address (str): Base58 or hex-encoded address of the sender.
75+
to_address (str): Base58 or hex-encoded address of the recipient.
76+
amount (int): Amount of TRX to transfer, in SUN (1 TRX = 1_000_000 SUN).
77+
ref_block_id (str): Hex string of the reference block ID for TAPoS.
78+
79+
Returns:
80+
Transaction: Dictionary containing the transaction ID, raw data, empty signature list, and no permission.
81+
"""
5482
to_address_raw = to_raw_address(to_address)
5583
from_address_raw = to_raw_address(owner_address)
5684

@@ -113,17 +141,22 @@ def create_smart_contract_transaction_offline(
113141
fee_limit: int,
114142
contract_address: str,
115143
) -> Transaction:
116-
"""Create and sign a TRC-20 `transfer` transaction.
117-
118-
Parameters
119-
----------
120-
from_address: Base58Check address of the sender
121-
to_address: Recipient address (Base58Check or hex)
122-
amount: Token amount in the token's smallest unit
123-
ref_block_id: Hex string of the latest block id (for TAPoS)
124-
private_key: 32-byte private key of the sender
125-
fee_limit: Maximum energy fee to spend (in SUN)
126-
contract_address: Address of the token (smart-contract) to invoke
144+
"""
145+
Construct an unsigned TRC-20 token transfer transaction offline,
146+
encoding the smart contract call and TAPoS metadata.
147+
148+
Parameters:
149+
from_address (str): Base58Check address of the sender.
150+
to_address (str): Recipient address in Base58Check or hex format.
151+
amount (int): Token amount in the token's smallest unit.
152+
ref_block_id (str): Hex string of the latest block ID for TAPoS.
153+
fee_limit (int): Maximum energy fee to spend (in SUN).
154+
contract_address (str): Address of the TRC-20 token smart contract.
155+
156+
Returns:
157+
Transaction: A dictionary representing the unsigned transaction,
158+
including transaction ID, raw data with encoded contract call,
159+
empty signature list, and no permission.
127160
"""
128161

129162
# ------------------------------------------------------------------ #

tronpy/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,36 @@
22

33

44
def current_timestamp() -> int:
5+
"""
6+
Return the current time in milliseconds since the Unix epoch.
7+
8+
Returns:
9+
int: The current timestamp in milliseconds.
10+
"""
511
return int(time.time() * 1000)
612

713

814
def get_ref_block_bytes(ref_block_id: str) -> str:
15+
"""
16+
Extracts a 4-character substring from positions 12 to 15 of the given reference block ID.
17+
18+
Parameters:
19+
ref_block_id (str): The reference block ID string.
20+
21+
Returns:
22+
str: The 4-character substring representing the reference block bytes.
23+
"""
924
return ref_block_id[12:16]
1025

1126

1227
def get_ref_block_hash(ref_block_id: str) -> str:
28+
"""
29+
Extract the 16-character reference block hash from a block ID string.
30+
31+
Parameters:
32+
ref_block_id (str): The block ID string to extract the hash from.
33+
34+
Returns:
35+
str: The 16-character substring representing the reference block hash.
36+
"""
1337
return ref_block_id[16:32]

0 commit comments

Comments
 (0)