forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoken_dissociate_transaction.py
More file actions
136 lines (109 loc) · 5.58 KB
/
Copy pathtoken_dissociate_transaction.py
File metadata and controls
136 lines (109 loc) · 5.58 KB
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
"""
hiero_sdk_python.tokens.token_dissociate_transaction.py.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This module provides the `TokenDissociateTransaction` class, which models
a Hedera network transaction to dissociate one or more tokens from an account.
Classes:
TokenDissociateTransaction
Builds, signs, and executes a token dissociate transaction. Inherits
from the base `Transaction` class and encapsulates all necessary
fields and methods to perform a token dissociation on Hedera.
"""
from __future__ import annotations
from hiero_sdk_python.account.account_id import AccountId
from hiero_sdk_python.channels import _Channel
from hiero_sdk_python.executable import _Method
from hiero_sdk_python.hapi.services import token_dissociate_pb2, transaction_pb2
from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import (
SchedulableTransactionBody,
)
from hiero_sdk_python.hbar import Hbar
from hiero_sdk_python.tokens.token_id import TokenId
from hiero_sdk_python.transaction.transaction import Transaction
class TokenDissociateTransaction(Transaction):
"""
Represents a token dissociate transaction on the Hedera network.
This transaction dissociates the specified tokens with an account,
meaning the account can no longer hold or transact with those tokens.
Inherits from the base Transaction class and implements the required methods
to build and execute a token dissociate transaction.
"""
def __init__(self, account_id: AccountId | None = None, token_ids: list[TokenId] | None = None) -> None:
"""
Initializes a new TokenDissociateTransaction instance with default values.
Args:
account_id (AccountId, optional): The ID of the account to dissociate tokens from
token_ids (list[TokenId], optional): A list of token IDs to dissociate from the account
"""
super().__init__()
self.account_id: AccountId | None = account_id
self.token_ids: list[TokenId] = token_ids or []
self._default_transaction_fee = Hbar(2)
def set_account_id(self, account_id: AccountId) -> TokenDissociateTransaction:
"""Sets the account ID for the token dissociation transaction."""
self._require_not_frozen()
self.account_id = account_id
return self
def add_token_id(self, token_id: TokenId) -> TokenDissociateTransaction:
"""Adds a token ID to the list of tokens to dissociate from the account."""
self._require_not_frozen()
self.token_ids.append(token_id)
return self
def set_token_ids(self, token_ids: list[TokenId]) -> TokenDissociateTransaction:
"""Sets the list of token IDs to dissociate from the account."""
self._require_not_frozen()
self.token_ids = token_ids
return self
def _validate_check_sum(self, client) -> None:
"""Validates the checksums of the account ID and token IDs against the provided client."""
if self.account_id is not None:
self.account_id.validate_checksum(client)
for token_id in self.token_ids or []:
if token_id is not None:
token_id.validate_checksum(client)
@classmethod
def _from_proto(cls, proto: token_dissociate_pb2.TokenDissociateTransactionBody) -> TokenDissociateTransaction:
"""
Creates a TokenDissociateTransaction instance from a protobuf
TokenDissociateTransactionBody object.
Args:
proto (TokenDissociateTransactionBody): The protobuf
representation of the token dissociate transaction.
"""
account_id = AccountId._from_proto(proto.account)
token_ids = [TokenId._from_proto(token_proto) for token_proto in proto.tokens]
return cls(account_id=account_id, token_ids=token_ids)
def _build_proto_body(self) -> token_dissociate_pb2.TokenDissociateTransactionBody:
"""
Returns the protobuf body for the token dissociate transaction.
Returns:
TokenDissociateTransactionBody: The protobuf body for this transaction.
"""
transaction_body = token_dissociate_pb2.TokenDissociateTransactionBody()
if self.account_id is not None:
transaction_body.account.CopyFrom(self.account_id._to_proto())
if self.token_ids is not None:
transaction_body.tokens.extend(token_id._to_proto() for token_id in self.token_ids if token_id is not None)
return transaction_body
def build_transaction_body(self) -> transaction_pb2.TransactionBody:
"""
Builds and returns the protobuf transaction body for token dissociation.
Returns:
TransactionBody: The protobuf transaction body with token dissociate details.
"""
token_dissociate_body = self._build_proto_body()
transaction_body: transaction_pb2.TransactionBody = self.build_base_transaction_body()
transaction_body.tokenDissociate.CopyFrom(token_dissociate_body)
return transaction_body
def build_scheduled_body(self) -> SchedulableTransactionBody:
"""
Builds the scheduled transaction body for this token dissociate transaction.
Returns:
SchedulableTransactionBody: The built scheduled transaction body.
"""
token_dissociate_body = self._build_proto_body()
schedulable_body = self.build_base_scheduled_body()
schedulable_body.tokenDissociate.CopyFrom(token_dissociate_body)
return schedulable_body
def _get_method(self, channel: _Channel) -> _Method:
return _Method(transaction_func=channel.token.dissociateTokens, query_func=None)