-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TokenDissociateTransaction TokenDissociateTransaction TokenDissociateTransaction TokenDissociateTransaction Test token association transfer out operator TokenDissociate readme pythonic excess logs to bytes comments multiple token test path list TokenDissociateTransaction TokenDissociateTransaction Test token association transfer out operator readme pythonic to bytes comments multiple token test path list token_ids not as extra list [] duplicates
- Loading branch information
1 parent
d2f2ce3
commit a2ff10e
Showing
5 changed files
with
353 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
import sys | ||
from dotenv import load_dotenv | ||
|
||
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | ||
sys.path.insert(0, project_root) | ||
|
||
from hedera_sdk_python.client.client import Client | ||
from hedera_sdk_python.account.account_id import AccountId | ||
from hedera_sdk_python.crypto.private_key import PrivateKey | ||
from hedera_sdk_python.client.network import Network | ||
from hedera_sdk_python.tokens.token_id import TokenId | ||
from hedera_sdk_python.tokens.token_dissociate_transaction import TokenDissociateTransaction | ||
|
||
load_dotenv() | ||
|
||
def dissociate_token(): #Single token | ||
network = Network(network='testnet') | ||
client = Client(network) | ||
|
||
recipient_id = AccountId.from_string(os.getenv('OPERATOR_ID')) | ||
recipient_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY')) | ||
token_id = TokenId.from_string('TOKEN_ID') | ||
|
||
client.set_operator(recipient_id, recipient_key) | ||
|
||
transaction = ( | ||
TokenDissociateTransaction() | ||
.set_account_id(recipient_id) | ||
.add_token_id(token_id) | ||
.freeze_with(client) | ||
.sign(recipient_key) | ||
) | ||
|
||
try: | ||
receipt = transaction.execute(client) | ||
print("Token dissociation successful.") | ||
except Exception as e: | ||
print(f"Token dissociation failed: {str(e)}") | ||
sys.exit(1) | ||
|
||
def dissociate_tokens(): # Multiple tokens | ||
network = Network(network='testnet') | ||
client = Client(network) | ||
|
||
recipient_id = AccountId.from_string(os.getenv('OPERATOR_ID')) | ||
recipient_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY')) | ||
token_ids = [TokenId.from_string('TOKEN_ID_1'), TokenId.from_string('TOKEN_ID_2')] | ||
|
||
client.set_operator(recipient_id, recipient_key) | ||
|
||
transaction = ( | ||
TokenDissociateTransaction() | ||
.set_account_id(recipient_id) | ||
) | ||
|
||
for token_id in token_ids: | ||
transaction.add_token_id(token_id) | ||
|
||
transaction = ( | ||
transaction | ||
.freeze_with(client) | ||
.sign(recipient_key) | ||
) | ||
|
||
try: | ||
receipt = transaction.execute(client) | ||
print("Token dissociations successful.") | ||
except Exception as e: | ||
print(f"Token dissociations failed: {str(e)}") | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
dissociate_token() # For single token dissociation | ||
# dissociate_tokens() # For multiple token dissociation |
101 changes: 101 additions & 0 deletions
101
src/hedera_sdk_python/tokens/token_dissociate_transaction.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from hedera_sdk_python.transaction.transaction import Transaction | ||
from hedera_sdk_python.hapi.services import token_dissociate_pb2 | ||
from hedera_sdk_python.response_code import ResponseCode | ||
|
||
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=None, token_ids=None): | ||
""" | ||
Initializes a new TokenDissociateTransaction instance with default values. | ||
""" | ||
super().__init__() | ||
self.account_id = account_id | ||
self.token_ids = token_ids or [] | ||
|
||
self._default_transaction_fee = 500_000_000 | ||
|
||
def set_account_id(self, account_id): | ||
self._require_not_frozen() | ||
self.account_id = account_id | ||
return self | ||
|
||
def add_token_id(self, token_id): | ||
self._require_not_frozen() | ||
self.token_ids.append(token_id) | ||
return self | ||
|
||
def build_transaction_body(self): | ||
""" | ||
Builds and returns the protobuf transaction body for token dissociation. | ||
Returns: | ||
TransactionBody: The protobuf transaction body containing the token dissociation details. | ||
Raises: | ||
ValueError: If account ID or token IDs are not set. | ||
""" | ||
if not self.account_id or not self.token_ids: | ||
raise ValueError("Account ID and token IDs must be set.") | ||
|
||
token_dissociate_body = token_dissociate_pb2.TokenDissociateTransactionBody( | ||
account=self.account_id.to_proto(), | ||
tokens=[token_id.to_proto() for token_id in self.token_ids] | ||
) | ||
|
||
transaction_body = self.build_base_transaction_body() | ||
transaction_body.tokenDissociate.CopyFrom(token_dissociate_body) | ||
|
||
return transaction_body | ||
|
||
def _execute_transaction(self, client, transaction_proto): | ||
""" | ||
Executes the token dissociation transaction using the provided client. | ||
Args: | ||
client (Client): The client instance to use for execution. | ||
transaction_proto (Transaction): The protobuf Transaction message. | ||
Returns: | ||
TransactionReceipt: The receipt from the network after transaction execution. | ||
Raises: | ||
Exception: If the transaction submission fails or receives an error response. | ||
""" | ||
response = client.token_stub.dissociateTokens(transaction_proto) | ||
|
||
if response.nodeTransactionPrecheckCode != ResponseCode.OK: | ||
error_code = response.nodeTransactionPrecheckCode | ||
error_message = ResponseCode.get_name(error_code) | ||
raise Exception(f"Error during transaction submission: {error_code} ({error_message})") | ||
|
||
receipt = self.get_receipt(client) | ||
return receipt | ||
|
||
def get_receipt(self, client, timeout=60): | ||
""" | ||
Retrieves the receipt for the transaction. | ||
Args: | ||
client (Client): The client instance. | ||
timeout (int): Maximum time in seconds to wait for the receipt. | ||
Returns: | ||
TransactionReceipt: The transaction receipt from the network. | ||
Raises: | ||
Exception: If the transaction ID is not set or if receipt retrieval fails. | ||
""" | ||
if self.transaction_id is None: | ||
raise Exception("Transaction ID is not set.") | ||
|
||
receipt = client.get_transaction_receipt(self.transaction_id, timeout) | ||
return receipt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.