Skip to content

Commit 0ddef58

Browse files
chore: add TokenId example (#2565)
Signed-off-by: Emily Richard <emilyrichard415@gmail.com> Signed-off-by: emilyrich4rd <emilyrichard415@gmail.com> Co-authored-by: Daniel Ntege <danientege785@gmail.com>
1 parent ce57d3d commit 0ddef58

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

examples/tokens/token_id.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
3+
Example: Token Id.
4+
5+
uv run examples/tokens/token_id.py
6+
python examples/tokens/token_id.py
7+
"""
8+
9+
from hiero_sdk_python import Client, Network
10+
from hiero_sdk_python.tokens.token_id import TokenId
11+
12+
13+
def create_token_id() -> TokenId:
14+
"""Create a TokenId manually."""
15+
token_id = TokenId(shard=0, realm=0, num=1234)
16+
17+
print(f" Shard: {token_id.shard}")
18+
print(f" Realm: {token_id.realm}")
19+
print(f" Num: {token_id.num}")
20+
print(f" TokenId: {token_id}")
21+
22+
return token_id
23+
24+
25+
def parse_token_id() -> TokenId:
26+
"""Parse a TokenId from string representation."""
27+
token_id_str = "0.0.1234"
28+
token_id = TokenId.from_string(token_id_str)
29+
manually_constructed_token_id = TokenId(shard=0, realm=0, num=1234)
30+
31+
print(f" Shard: {token_id.shard}")
32+
print(f" Realm: {token_id.realm}")
33+
print(f" Num: {token_id.num}")
34+
print(f" Parsed TokenId equals manually constructed TokenId: {manually_constructed_token_id == token_id}")
35+
36+
return token_id
37+
38+
39+
def parse_bad_token_id() -> None:
40+
"""Parse an invalid token and handle resulting error."""
41+
try:
42+
TokenId.from_string("not-a-token-id")
43+
except ValueError as e:
44+
print(f" Error: {e}")
45+
46+
47+
def convert_to_proto_and_back(token_id: TokenId) -> TokenId:
48+
"""Convert a TokenId to protobuf and back."""
49+
# Leading underscore marks _to_proto and _from_proto as internal methods that users normally never call
50+
# This example shows how the SDK communicates with the network
51+
proto = token_id._to_proto()
52+
token_id = TokenId._from_proto(proto)
53+
54+
print(f" Shard: {token_id.shard}")
55+
print(f" Realm: {token_id.realm}")
56+
print(f" Num: {token_id.num}")
57+
58+
return token_id
59+
60+
61+
def show_with_checksum(client: Client, token_id: TokenId) -> None:
62+
"""Display TokenId with checksum."""
63+
# Checksum is network-specific so a Client is required
64+
# Checksum generated for testnet will not validate on mainnet
65+
token_id_with_checksum = token_id.to_string_with_checksum(client)
66+
67+
print(f" TokenId with checksum: {token_id_with_checksum}")
68+
69+
70+
def main() -> None:
71+
"""Demonstrate TokenId functionality."""
72+
token_id = TokenId(shard=0, realm=0, num=1234)
73+
network = Network("testnet")
74+
client = Client(network)
75+
76+
# Create a TokenId
77+
create_token_id()
78+
79+
# Parse a TokenId from string representation
80+
parse_token_id()
81+
82+
# Parse an invalid token and handle error
83+
parse_bad_token_id()
84+
85+
# Convert a TokenId to protobuf and back
86+
convert_to_proto_and_back(token_id)
87+
88+
# Display TokenId with checksum
89+
show_with_checksum(client, token_id)
90+
91+
92+
if __name__ == "__main__":
93+
main()

0 commit comments

Comments
 (0)