Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/hiero_sdk_python/tokens/supply_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from enum import Enum

from hiero_sdk_python.hapi.services import basic_types_pb2


class SupplyType(Enum):
"""
Expand All @@ -21,3 +23,13 @@ class SupplyType(Enum):

INFINITE = 0
FINITE = 1

@staticmethod
def _from_proto(proto: basic_types_pb2.TokenSupplyType) -> SupplyType:
"""Create token supply type from prorobuf message."""
if proto == basic_types_pb2.TokenSupplyType.FINITE:
return SupplyType.FINITE
if proto == basic_types_pb2.TokenSupplyType.INFINITE:
return SupplyType.INFINITE

raise ValueError(f"Invalid SupplyType: {proto}")
55 changes: 55 additions & 0 deletions src/hiero_sdk_python/tokens/token_create_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,58 @@ def build_scheduled_body(self) -> SchedulableTransactionBody:

def _get_method(self, channel: _Channel) -> _Method:
return _Method(transaction_func=channel.token.createToken, query_func=None)

@classmethod
def _from_protobuf(cls, transaction_body: transaction_pb2.TransactionBody) -> TokenCreateTransaction:
"""Create token create transaction transaction from protobuf message."""
transaction = super()._from_protobuf(transaction_body)

if transaction_body.HasField("tokenCreation"):
proto_body = transaction_body.tokenCreation
token_params = TokenParams(
token_name=proto_body.name,
token_symbol=proto_body.symbol,
decimals=proto_body.decimals,
initial_supply=proto_body.initialSupply,
treasury_account_id=(
AccountId._from_proto(proto_body.treasury)
if proto_body.HasField("treasury")
else AccountId(0, 0, 1)
Comment thread
manishdait marked this conversation as resolved.
Resolved
),
freeze_default=proto_body.freezeDefault,
expiration_time=Timestamp._from_protobuf(proto_body.expiry) if proto_body.HasField("expiry") else None,
auto_renew_account_id=(
AccountId._from_proto(proto_body.autoRenewAccount)
if proto_body.HasField("autoRenewAccount")
else None
),
auto_renew_period=(
Duration._from_proto(proto_body.autoRenewPeriod) if proto_body.HasField("autoRenewPeriod") else None
),
memo=proto_body.memo,
token_type=TokenType._from_proto(proto_body.tokenType),
supply_type=SupplyType._from_proto(proto_body.supplyType),
max_supply=proto_body.maxSupply,
metadata=proto_body.metadata,
Comment thread
manishdait marked this conversation as resolved.
Resolved
custom_fees=[CustomFee._from_proto(fee) for fee in proto_body.custom_fees],
)

keys = TokenKeys(
admin_key=Key.from_proto_key(proto_body.adminKey) if proto_body.HasField("adminKey") else None,
kyc_key=Key.from_proto_key(proto_body.kycKey) if proto_body.HasField("kycKey") else None,
freeze_key=Key.from_proto_key(proto_body.freezeKey) if proto_body.HasField("freezeKey") else None,
wipe_key=Key.from_proto_key(proto_body.wipeKey) if proto_body.HasField("wipeKey") else None,
supply_key=Key.from_proto_key(proto_body.supplyKey) if proto_body.HasField("supplyKey") else None,
fee_schedule_key=(
Key.from_proto_key(proto_body.fee_schedule_key) if proto_body.HasField("fee_schedule_key") else None
),
metadata_key=(
Key.from_proto_key(proto_body.metadata_key) if proto_body.HasField("metadata_key") else None
),
pause_key=Key.from_proto_key(proto_body.pause_key) if proto_body.HasField("pause_key") else None,
)

transaction._token_params = token_params
transaction._keys = keys

return transaction
12 changes: 12 additions & 0 deletions src/hiero_sdk_python/tokens/token_delete_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,15 @@ def build_scheduled_body(self) -> SchedulableTransactionBody:

def _get_method(self, channel: _Channel) -> _Method:
return _Method(transaction_func=channel.token.deleteToken, query_func=None)

@classmethod
def _from_protobuf(cls, transaction_body: transaction_pb2.TransactionBody) -> TokenDeleteTransaction:
"""Create token delete transaction from protobuf message."""
transaction = super()._from_protobuf(transaction_body)

if transaction_body.HasField("tokenDeletion"):
proto_body = transaction_body.tokenDeletion
if proto_body.HasField("token"):
transaction.token_id = TokenId._from_proto(proto_body.token)

return transaction
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,18 @@ def _get_method(self, channel: _Channel) -> _Method:
def __repr__(self):
"""Readable representation for debugging."""
return f"<TokenFeeScheduleUpdateTransaction token_id={self.token_id} fees={len(self.custom_fees)}>"

@classmethod
def _from_protobuf(cls, transaction_body: transaction_pb2.TransactionBody) -> TokenFeeScheduleUpdateTransaction:
"""Create token fee schedule update transaction transaction from protobuf message."""
transaction = super()._from_protobuf(transaction_body)

if transaction_body.HasField("token_fee_schedule_update"):
proto_body = transaction_body.token_fee_schedule_update

if proto_body.HasField("token_id"):
transaction.token_id = TokenId._from_proto(proto_body.token_id)

transaction.custom_fees = [CustomFee._from_proto(fee) for fee in proto_body.custom_fees]

return transaction
12 changes: 12 additions & 0 deletions src/hiero_sdk_python/tokens/token_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from enum import Enum

from hiero_sdk_python.hapi.services import basic_types_pb2


class TokenType(Enum):
"""
Expand Down Expand Up @@ -44,3 +46,13 @@ class TokenType(Enum):

FUNGIBLE_COMMON = 0
NON_FUNGIBLE_UNIQUE = 1

@staticmethod
def _from_proto(proto: basic_types_pb2.TokenType) -> TokenType:
"""Create TokenType from protobuf message."""
if proto == basic_types_pb2.TokenType.FUNGIBLE_COMMON:
return TokenType.FUNGIBLE_COMMON
if proto == basic_types_pb2.TokenType.NON_FUNGIBLE_UNIQUE:
return TokenType.NON_FUNGIBLE_UNIQUE

raise ValueError(f"Invalid TokenType: {proto}")
18 changes: 18 additions & 0 deletions src/hiero_sdk_python/tokens/token_update_nfts_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,21 @@ def _from_proto(self, proto: token_update_nfts_pb2.TokenUpdateNftsTransactionBod
self.serial_numbers = list(proto.serial_numbers)
self.metadata = proto.metadata.value
return self

@classmethod
def _from_protobuf(cls, transaction_body: transaction_pb2.TransactionBody) -> TokenUpdateNftsTransactionBody:
"""Create token update nfts transaction transaction from protobuf message."""
transaction = super()._from_protobuf(transaction_body)

if transaction_body.HasField("token_update_nfts"):
proto_body = transaction_body.token_update_nfts

if proto_body.HasField("token"):
transaction.token_id = TokenId._from_proto(proto_body.token)

if proto_body.HasField("metadata"):
transaction.metadata = proto_body.metadata.value

transaction.serial_numbers = [serial for serial in proto_body.serial_numbers]

return transaction
47 changes: 47 additions & 0 deletions src/hiero_sdk_python/tokens/token_update_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,50 @@ def _set_keys_to_proto(self, token_update_body: token_update_pb2.TokenUpdateTran
token_update_body.kycKey.CopyFrom(key_to_proto(self.kyc_key))
if self.fee_schedule_key:
token_update_body.fee_schedule_key.CopyFrom(key_to_proto(self.fee_schedule_key))

@classmethod
def _from_protobuf(cls, transaction_body: transaction_pb2.TransactionBody) -> TokenUpdateTransaction:
"""Create token create transaction transaction from protobuf message."""
transaction = super()._from_protobuf(transaction_body)

if transaction_body.HasField("tokenUpdate"):
proto_body = transaction_body.tokenUpdate

transaction.token_id = TokenId._from_proto(proto_body.token) if proto_body.HasField("token") else None
transaction.token_symbol = proto_body.symbol
transaction.token_name = proto_body.name
transaction.treasury_account_id = (
AccountId._from_proto(proto_body.treasury) if proto_body.HasField("treasury") else None
)
transaction.admin_key = Key.from_proto_key(proto_body.adminKey) if proto_body.HasField("adminKey") else None
transaction.kyc_key = Key.from_proto_key(proto_body.kycKey) if proto_body.HasField("kycKey") else None
transaction.freeze_key = (
Key.from_proto_key(proto_body.freezeKey) if proto_body.HasField("freezeKey") else None
)
transaction.wipe_key = Key.from_proto_key(proto_body.wipeKey) if proto_body.HasField("wipeKey") else None
transaction.supply_key = (
Key.from_proto_key(proto_body.supplyKey) if proto_body.HasField("supplyKey") else None
)
transaction.auto_renew_account_id = (
AccountId._from_proto(proto_body.autoRenewAccount) if proto_body.HasField("autoRenewAccount") else None
)
transaction.auto_renew_period = (
Duration._from_proto(proto_body.autoRenewPeriod) if proto_body.HasField("autoRenewPeriod") else None
)
transaction.expiration_time = (
Timestamp._from_protobuf(proto_body.expiry) if proto_body.HasField("expiry") else None
)
transaction.token_memo = proto_body.memo.value if proto_body.HasField("memo") else None
transaction.metadata = proto_body.metadata.value if proto_body.HasField("metadata") else None
transaction.fee_schedule_key = (
Key.from_proto_key(proto_body.fee_schedule_key) if proto_body.HasField("fee_schedule_key") else None
)
transaction.metadata_key = (
Key.from_proto_key(proto_body.metadata_key) if proto_body.HasField("metadata_key") else None
)
transaction.pause_key = (
Key.from_proto_key(proto_body.pause_key) if proto_body.HasField("pause_key") else None
)
transaction.token_key_verification_mode = TokenKeyValidation._from_proto(proto_body.key_verification_mode)

return transaction
58 changes: 57 additions & 1 deletion tests/integration/token_create_transaction_e2e_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from hiero_sdk_python.response_code import ResponseCode
from hiero_sdk_python.timestamp import Timestamp
from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee
from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction, TokenParams
from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction, TokenKeys, TokenParams
from hiero_sdk_python.tokens.token_fee_schedule_update_transaction import TokenFeeScheduleUpdateTransaction
from hiero_sdk_python.tokens.token_type import TokenType
from hiero_sdk_python.transaction.transaction import Transaction
Expand Down Expand Up @@ -243,3 +243,59 @@ def test_fungible_token_create_with_metadata(env):
token_info = TokenInfoQuery(token_id=token_id).execute(env.client)

assert token_info.metadata == metadata, "Token metadata mismatch"


@pytest.mark.integration
def test_create_fungible_token_from_bytes(env):
"""Test create fungible token using a transaction deserialized from bytes."""
params = TokenParams(
token_name="Test FT",
token_symbol="TFT",
initial_supply=1,
treasury_account_id=env.client.operator_account_id,
Comment thread
manishdait marked this conversation as resolved.
Resolved
token_type=TokenType.FUNGIBLE_COMMON,
)

original_tx = TokenCreateTransaction(params)
copy_tx = TokenCreateTransaction.from_bytes(original_tx.to_bytes())

receipt = copy_tx.execute(env.client)
assert receipt.token_id is not None, "TokenID not found in receipt. Token may not have been created."

token_id = receipt.token_id

token_info = TokenInfoQuery(token_id=token_id).execute(env.client)

assert token_info.token_type == TokenType.FUNGIBLE_COMMON, "Token type mismatch"
assert token_info.total_supply == 1, "Token total supply must be equal to 1"
assert token_info.name == "Test FT", "Token name mismatch"
assert token_info.symbol == "TFT", "Token symbol mismatch"


@pytest.mark.integration
def test_create_nft_from_bytes(env):
"""Test create nft using a transaction deserialized from bytes."""
params = TokenParams(
token_name="Test NFT",
token_symbol="TNFT",
treasury_account_id=env.client.operator_account_id,
initial_supply=0,
token_type=TokenType.NON_FUNGIBLE_UNIQUE,
)

keys = TokenKeys(supply_key=env.operator_key)

original_tx = TokenCreateTransaction(params, keys)
copy_tx = TokenCreateTransaction.from_bytes(original_tx.to_bytes())

receipt = copy_tx.execute(env.client)
assert receipt.token_id is not None, "TokenID not found in receipt. Token may not have been created."

token_id = receipt.token_id

token_info = TokenInfoQuery(token_id=token_id).execute(env.client)

assert token_info.token_type == TokenType.NON_FUNGIBLE_UNIQUE, "Token type mismatch"
assert token_info.total_supply == 0, "Token total supply must be equal to 0"
assert token_info.name == "Test NFT", "Token name mismatch"
assert token_info.symbol == "TNFT", "Token symbol mismatch"
19 changes: 19 additions & 0 deletions tests/integration/token_delete_transaction_e2e_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from hiero_sdk_python.query.token_info_query import TokenInfoQuery
from hiero_sdk_python.response_code import ResponseCode
from hiero_sdk_python.tokens.token_delete_transaction import TokenDeleteTransaction
from tests.integration.utils import create_fungible_token
Expand All @@ -20,3 +21,21 @@ def test_integration_token_delete_transaction_can_execute(env):
assert receipt.status == ResponseCode.SUCCESS, (
f"Token deletion failed with status: {ResponseCode(receipt.status).name}"
)


@pytest.mark.integration
def test_delete_token_from_bytes(env):
"""Test delete token transaction can be executed from bytes."""
token_id = create_fungible_token(env)
assert token_id is not None, "TokenID not found in receipt. Token may not have been created."

original_tx = TokenDeleteTransaction(token_id=token_id)
copy_tx = TokenDeleteTransaction.from_bytes(original_tx.to_bytes())
receipt = copy_tx.execute(env.client)

assert receipt.status == ResponseCode.SUCCESS, (
f"Token deletion failed with status: {ResponseCode(receipt.status).name}"
)

token_info = TokenInfoQuery(token_id=token_id).execute(env.client)
assert token_info.is_deleted is True, "Token must have been deleted"
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,35 @@ def test_token_fee_schedule_update_clears_fees(env):

token_info_cleared = TokenInfoQuery().set_token_id(token_id).execute(env.client)
assert len(token_info_cleared.custom_fees) == 0


@pytest.mark.integration
def test_update_token_fee_schedule_from_bytes(env):
"""Test token update fee schedule transaction can be executed from bytes."""
fee_schedule_key = env.operator_key
token_id = create_fungible_token(env, opts=[lambda tx: tx.set_fee_schedule_key(fee_schedule_key)])
assert token_id is not None

new_fee = CustomFixedFee(
amount=25,
fee_collector_account_id=env.operator_id,
)
original_tx = (
TokenFeeScheduleUpdateTransaction()
.set_token_id(token_id)
.set_custom_fees([new_fee])
.freeze_with(env.client)
.sign(fee_schedule_key)
)

copy_tx = TokenFeeScheduleUpdateTransaction.from_bytes(original_tx.to_bytes())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial · Maintainability & Code Quality · Quick win

Assert the reconstructed fields before execution.

execute() can submit restored _transaction_body_bytes without rebuilding the body from copy_tx. The network assertions can therefore pass even if _from_protobuf fails to restore token_id or custom_fees.

Before execution, assert copy_tx.token_id, the fee count, and the restored fee's type, amount, and collector. Keep the network assertions to verify execution separately.

As per path instructions, “Suggest additional assertions that would strengthen correctness.” This also targets the PR's field-for-field restoration objective.

Source: Path instructions


receipt = copy_tx.execute(env.client)
assert receipt.status == ResponseCode.SUCCESS, f"Fee schedule update failed: {ResponseCode(receipt.status).name}"

token_info = TokenInfoQuery().set_token_id(token_id).execute(env.client)
assert token_info.custom_fees and len(token_info.custom_fees) == 1, (
"Custom fee must be present and should have length 1"
)
retrieved_fee = token_info.custom_fees[0]
assert isinstance(retrieved_fee, CustomFixedFee) and retrieved_fee.amount == 25, "Custom fee amount must be 25"
36 changes: 36 additions & 0 deletions tests/integration/token_update_nfts_transaction_e2e_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,39 @@ def test_token_update_nfts_transaction_fails_for_nonexistent_serial(env):
assert receipt.status == ResponseCode.INVALID_NFT_ID, (
f"NFT update should fail with INVALID_NFT_ID, got status: {ResponseCode(receipt.status).name}"
)


@pytest.mark.integration
def test_update_nfts_from_bytes(env):
"""Test token update nfts transaction can be executed from bytes."""
supply_private_key = PrivateKey.generate_ed25519()
metadata_private_key = PrivateKey.generate_ed25519()

nft_id = create_nft_token(
env, [lambda tx: tx.set_supply_key(supply_private_key), lambda tx: tx.set_metadata_key(metadata_private_key)]
)

mint_metadata = b"Hello, Hedera"
mint_tx = TokenMintTransaction(token_id=nft_id, metadata=mint_metadata)
receipt = mint_tx.freeze_with(env.client).sign(supply_private_key).execute(env.client)

assert receipt.status == ResponseCode.SUCCESS, (
f"NFT minting failed with status: {ResponseCode(receipt.status).name}"
)

serials = receipt.serial_numbers
nft_info = TokenNftInfoQuery(NftId(nft_id, serials[0])).execute(env.client)
assert nft_info.metadata == mint_metadata, f"Initial metadata mismatch for serial {serials[0]}"

updated_metadata = b"Hello, Hiero"
original_update_tx = TokenUpdateNftsTransaction(token_id=nft_id, serial_numbers=serials, metadata=updated_metadata)
original_update_tx.transaction_fee = Hbar(3).to_tinybars()
original_update_tx.freeze_with(env.client).sign(metadata_private_key)

copy_update_tx = TokenUpdateNftsTransaction.from_bytes(original_update_tx.to_bytes())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial · Maintainability & Code Quality · Quick win

Assert the restored NFT update fields.

The restored transaction retains the original signed body bytes. Execution can submit those bytes even if _from_protobuf leaves token_id, serial_numbers, or metadata unset. The final metadata assertion does not detect that omission. (raw.githubusercontent.com)

After Line 295, compare those three attributes with nft_id, list(serials), and updated_metadata. Keep the signed execution and network assertions.

As per path instructions, “Suggest additional assertions that would strengthen correctness.”

Source: Path instructions

receipt = copy_update_tx.execute(env.client)

assert receipt.status == ResponseCode.SUCCESS, f"NFT update failed with status: {ResponseCode(receipt.status).name}"

nft_info = TokenNftInfoQuery(NftId(nft_id, serials[0])).execute(env.client)
assert nft_info.metadata == updated_metadata, f"Updated metadata mismatch for serial {serials[0]}"
Loading
Loading