forked from nadineloepfe/hedera_sdk_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rough notes token freeze local tests passed test tuple freeze account ids stub naming freezing from recipient
- Loading branch information
1 parent
d2f2ce3
commit d70dc55
Showing
9 changed files
with
353 additions
and
8 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
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,46 @@ | ||
import os | ||
import sys | ||
from dotenv import load_dotenv | ||
|
||
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.tokens.token_delete_transaction import TokenFreezeTransaction | ||
from hedera_sdk_python.client.network import Network | ||
from hedera_sdk_python.tokens.token_id import TokenId | ||
|
||
load_dotenv() | ||
|
||
def freeze_token(): | ||
network = Network(network='testnet') | ||
client = Client(network) | ||
|
||
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID')) | ||
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY')) | ||
freeze_key = PrivateKey.from_string(os.getenv('FREEZE_KEY')) | ||
token_id = TokenId.from_string(os.getenv('TOKEN_ID')) | ||
account_id = AccountId.from_string(os.getenv('FREEZE_ACCOUNT_ID')) | ||
|
||
client.set_operator(operator_id, operator_key) | ||
|
||
transaction = ( | ||
TokenFreezeTransaction() | ||
.set_token_id(token_id) | ||
.set_account(account_id) | ||
.freeze_with(client) | ||
.sign(freeze_key) | ||
) | ||
|
||
try: | ||
receipt = transaction.execute(client) | ||
if receipt is not None and receipt.status == 'SUCCESS': | ||
print(f"Token freeze successful") | ||
else: | ||
print(f"Token freeze failed.") | ||
sys.exit(1) | ||
except Exception as e: | ||
print(f"Token freeze failed: {str(e)}") | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
freeze_token() |
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
126 changes: 126 additions & 0 deletions
126
src/hedera_sdk_python/tokens/token_freeze_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,126 @@ | ||
from hedera_sdk_python.transaction.transaction import Transaction | ||
from hedera_sdk_python.hapi.services import token_freeze_account_pb2 | ||
from hedera_sdk_python.response_code import ResponseCode | ||
|
||
class TokenFreezeTransaction(Transaction): | ||
""" | ||
Represents a token freeze transaction on the Hedera network. | ||
This transaction freezes a specified token for a given account. | ||
Inherits from the base Transaction class and implements the required methods | ||
to build and execute a token freeze transaction. | ||
""" | ||
|
||
def __init__(self, token_id=None, account_id=None): | ||
""" | ||
Initializes a new TokenDeleteTransaction instance with optional token_id. | ||
Args: | ||
token_id (TokenId, optional): The ID of the token to be frozen. | ||
account_id (AccountId, optional): The ID of the account to have their token frozen. | ||
""" | ||
super().__init__() | ||
self.token_id = token_id | ||
self.account_id = account_id | ||
self._default_transaction_fee = 3_000_000_000 | ||
|
||
def set_token_id(self, token_id): | ||
""" | ||
Sets the ID of the token to be frozen. | ||
Args: | ||
token_id (TokenId): The ID of the token to be frozen. | ||
Returns: | ||
TokenFreezeTransaction: Returns self for method chaining. | ||
""" | ||
self._require_not_frozen() | ||
self.token_id = token_id | ||
return self | ||
|
||
def set_account_id(self, account_id): | ||
""" | ||
Sets the ID of the account to be frozen. | ||
Args: | ||
account_id (AccountId): The ID of the account to have their token frozen. | ||
Returns: | ||
TokenFreezeTransaction: Returns self for method chaining. | ||
""" | ||
self._require_not_frozen() | ||
self.account_id = account_id | ||
return self | ||
|
||
def build_transaction_body(self): | ||
""" | ||
Builds and returns the protobuf transaction body for token freeze. | ||
Returns: | ||
TransactionBody: The protobuf transaction body containing the token freeze details. | ||
Raises: | ||
ValueError: If the token ID is missing. | ||
ValueError: If the account ID is missing. | ||
""" | ||
|
||
if not self.token_id: | ||
raise ValueError("Missing required TokenID.") | ||
|
||
if not self.account_id: | ||
raise ValueError("Missing required AccountID.") | ||
|
||
token_freeze_body = token_freeze_account_pb2.TokenFreezeAccountTransactionBody( | ||
token=self.token_id.to_proto(), | ||
account=self.account_id.to_proto() | ||
) | ||
|
||
transaction_body = self.build_base_transaction_body() | ||
transaction_body.tokenFreeze.CopyFrom(token_freeze_body) | ||
|
||
return transaction_body | ||
|
||
def _execute_transaction(self, client, transaction_proto): | ||
""" | ||
Executes the token freeze 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.freezeTokenAccount(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.