-
Notifications
You must be signed in to change notification settings - Fork 20
TokenDissociateTransaction #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nadineloepfe
merged 2 commits into
hiero-ledger:main
from
exploreriii:TokenDissociateTransaction
Jan 27, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.