|
1 | 1 | """Transaction to delete a file on the network.""" |
2 | | - |
3 | 2 | from __future__ import annotations |
4 | 3 |
|
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
5 | 6 | from hiero_sdk_python.channels import _Channel |
6 | 7 | from hiero_sdk_python.executable import _Method |
7 | 8 | from hiero_sdk_python.file.file_id import FileId |
|
13 | 14 | from hiero_sdk_python.transaction.transaction import Transaction |
14 | 15 |
|
15 | 16 |
|
16 | | -DEFAULT_TRANSACTION_FEE = Hbar(2).to_tinybars() |
17 | | - |
| 17 | +if TYPE_CHECKING: |
| 18 | + from hiero_sdk_python.hapi.services.transaction_pb2 import TransactionBody |
18 | 19 |
|
| 20 | +DEFAULT_TRANSACTION_FEE = Hbar(2).to_tinybars() |
19 | 21 | class FileDeleteTransaction(Transaction): |
20 | 22 | """ |
21 | 23 | Represents a file deletion transaction on the network. |
22 | | -
|
23 | 24 | This transaction deletes a specified file, rendering it inactive. |
24 | | -
|
25 | 25 | Inherits from the base Transaction class and implements the required methods |
26 | 26 | to build and execute a file deletion transaction. |
27 | 27 | """ |
28 | | - |
29 | 28 | def __init__(self, file_id: FileId | None = None): |
30 | 29 | """ |
31 | 30 | Initializes a new FileDeleteTransaction instance with optional file_id. |
32 | | -
|
33 | 31 | Args: |
34 | 32 | file_id (FileId, optional): The ID of the file to be deleted. |
35 | 33 | """ |
36 | 34 | super().__init__() |
37 | 35 | self.file_id = file_id |
38 | 36 | self._default_transaction_fee = DEFAULT_TRANSACTION_FEE |
39 | | - |
40 | 37 | def set_file_id(self, file_id: FileId) -> FileDeleteTransaction: |
41 | 38 | """ |
42 | 39 | Sets the ID of the file to be deleted. |
43 | | -
|
44 | 40 | Args: |
45 | 41 | file_id (FileId): The ID of the file to be deleted. |
46 | | -
|
47 | 42 | Returns: |
48 | 43 | FileDeleteTransaction: Returns self for method chaining. |
49 | 44 | """ |
50 | 45 | self._require_not_frozen() |
51 | 46 | self.file_id = file_id |
52 | 47 | return self |
53 | | - |
54 | | - def _build_proto_body(self): |
| 48 | + def _build_proto_body(self) -> FileDeleteTransactionBody: |
55 | 49 | """ |
56 | 50 | Returns the protobuf body for the file delete transaction. |
57 | 51 |
|
| 52 | + If file_id is not set, the fileID field is left unset so the network |
| 53 | + responds with INVALID_FILE_ID rather than failing locally. |
| 54 | +
|
58 | 55 | Returns: |
59 | 56 | FileDeleteTransactionBody: The protobuf body for this transaction. |
60 | | -
|
61 | | - Raises: |
62 | | - ValueError: If file_id is not set. |
63 | 57 | """ |
64 | | - if self.file_id is None: |
65 | | - raise ValueError("Missing required FileID") |
66 | | - |
67 | | - return FileDeleteTransactionBody(fileID=self.file_id._to_proto()) |
68 | | - |
69 | | - def build_transaction_body(self): |
| 58 | + return FileDeleteTransactionBody( |
| 59 | + fileID=self.file_id._to_proto() if self.file_id is not None else None |
| 60 | + ) |
| 61 | + def build_transaction_body(self) -> TransactionBody: |
70 | 62 | """ |
71 | 63 | Builds and returns the protobuf transaction body for file deletion. |
72 | | -
|
73 | 64 | Returns: |
74 | 65 | TransactionBody: The protobuf transaction body containing the file deletion details. |
75 | 66 | """ |
76 | 67 | file_delete_body = self._build_proto_body() |
77 | 68 | transaction_body = self.build_base_transaction_body() |
78 | 69 | transaction_body.fileDelete.CopyFrom(file_delete_body) |
79 | 70 | return transaction_body |
80 | | - |
81 | 71 | def build_scheduled_body(self) -> SchedulableTransactionBody: |
82 | 72 | """ |
83 | 73 | Builds the scheduled transaction body for this file delete transaction. |
84 | | -
|
85 | 74 | Returns: |
86 | 75 | SchedulableTransactionBody: The built scheduled transaction body. |
87 | 76 | """ |
88 | 77 | file_delete_body = self._build_proto_body() |
89 | 78 | schedulable_body = self.build_base_scheduled_body() |
90 | 79 | schedulable_body.fileDelete.CopyFrom(file_delete_body) |
91 | 80 | return schedulable_body |
92 | | - |
93 | 81 | def _get_method(self, channel: _Channel) -> _Method: |
94 | 82 | """ |
95 | 83 | Gets the method to execute the file delete transaction. |
96 | | -
|
97 | 84 | This internal method returns a _Method object containing the appropriate gRPC |
98 | 85 | function to call when executing this transaction on the network. |
99 | | -
|
100 | 86 | Args: |
101 | 87 | channel (_Channel): The channel containing service stubs |
102 | | -
|
103 | 88 | Returns: |
104 | 89 | _Method: An object containing the transaction function to delete a file. |
105 | 90 | """ |
|
0 commit comments