Skip to content

Commit 4c8ba94

Browse files
authored
feat: add FileUpdateTransaction (#220)
* feat: add FileUpdateTransaction Signed-off-by: dosi <[email protected]> * test: add FileUpdateTransaction integration tests Signed-off-by: dosi <[email protected]> * test: add FileUpdateTransaction unit tests Signed-off-by: dosi <[email protected]> * docs: add token update example Signed-off-by: dosi <[email protected]> * docs: update README with FileUpdateTransaction Signed-off-by: dosi <[email protected]> * chore: add FileUpdateTransaction to __init__.py Signed-off-by: dosi <[email protected]> --------- Signed-off-by: dosi <[email protected]>
1 parent 256661c commit 4c8ba94

File tree

6 files changed

+820
-19
lines changed

6 files changed

+820
-19
lines changed

examples/README.md

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ You can choose either syntax or even mix both styles in your projects.
5050
- [Creating a File](#creating-a-file)
5151
- [Querying File Info](#querying-file-info)
5252
- [Querying File Contents](#querying-file-contents)
53+
- [Updating a File](#updating-a-file)
5354
- [Deleting a File](#deleting-a-file)
5455
- [Miscellaneous Queries](#miscellaneous-queries)
5556
- [Querying Transaction Record](#querying-transaction-record)
@@ -186,8 +187,8 @@ transaction = TokenMintTransaction(
186187
amount=amount, # lowest denomination, must be positive and not zero
187188
).freeze_with(client)
188189
189-
transaction.sign(operator_key)
190-
transaction.sign(supply_key)
190+
transaction.sign(operator_key)
191+
transaction.sign(supply_key)
191192
transaction.execute(client)
192193
```
193194
#### Method Chaining:
@@ -198,8 +199,8 @@ transaction = (
198199
.set_amount(amount) # lowest denomination, must be positive and not zero
199200
.freeze_with(client)
200201
)
201-
transaction.sign(operator_key)
202-
transaction.sign(admin_key)
202+
transaction.sign(operator_key)
203+
transaction.sign(admin_key)
203204
transaction.execute(client)
204205
```
205206

@@ -212,8 +213,8 @@ transaction = TokenMintTransaction(
212213
metadata=metadata # Bytes for non-fungible tokens (NFTs)
213214
).freeze_with(client)
214215
215-
transaction.sign(operator_key)
216-
transaction.sign(supply_key)
216+
transaction.sign(operator_key)
217+
transaction.sign(supply_key)
217218
transaction.execute(client)
218219
```
219220
#### Method Chaining:
@@ -224,8 +225,8 @@ transaction = (
224225
.set_metadata(metadata) # Bytes for non-fungible tokens (NFTs)
225226
.freeze_with(client)
226227
)
227-
transaction.sign(operator_key)
228-
transaction.sign(admin_key)
228+
transaction.sign(operator_key)
229+
transaction.sign(admin_key)
229230
transaction.execute(client)
230231
```
231232

@@ -286,8 +287,8 @@ transaction = (
286287
transaction = TransferTransaction(
287288
token_transfers={
288289
token_id: {
289-
operator_id: -amount,
290-
recipient_id: amount
290+
operator_id: -amount,
291+
recipient_id: amount
291292
}
292293
}
293294
).freeze_with(client)
@@ -560,7 +561,7 @@ transaction.execute(client)
560561
transaction = (
561562
TokenUpdateNftsTransaction()
562563
.set_token_id(nft_token_id)
563-
.set_serial_numbers(serial_numbers)
564+
.set_serial_numbers(serial_numbers)
564565
.set_metadata(new_metadata)
565566
.freeze_with(client)
566567
.sign(metadata_key)
@@ -628,7 +629,7 @@ transaction = TokenUpdateTransaction(
628629
token_id=token_id,
629630
token_params=TokenUpdateParams(
630631
token_name="UpdateToken",
631-
token_symbol="UPD",
632+
token_symbol="UPD",
632633
token_memo="Updated memo",
633634
metadata="Updated metadata",
634635
treasury_account_id=new_account_id
@@ -751,7 +752,7 @@ transaction.execute(client)
751752
transaction = (
752753
TransferTransaction()
753754
.add_hbar_transfer(operator_id, -100000000) # send 1 HBAR (in tinybars)
754-
.add_hbar_transfer(recipient_id, 100000000)
755+
.add_hbar_transfer(recipient_id, 100000000)
755756
.freeze_with(client)
756757
)
757758
@@ -776,7 +777,7 @@ transaction.execute(client)
776777
transaction.execute(client)
777778
```
778779
#### Method Chaining:
779-
```
780+
```
780781
transaction = (
781782
TopicCreateTransaction()
782783
.set_memo("My Super Topic Memo")
@@ -904,10 +905,10 @@ query.subscribe(client)
904905
```
905906
query = (
906907
TopicMessageQuery()
907-
.set_topic_id(topic_id)
908-
.set_start_time(datetime.now(timezone.utc))
909-
.set_chunking_enabled(True)
910-
.set_limit(0)
908+
.set_topic_id(topic_id)
909+
.set_start_time(datetime.now(timezone.utc))
910+
.set_chunking_enabled(True)
911+
.set_limit(0)
911912
)
912913
913914
query.subscribe(client)
@@ -984,6 +985,39 @@ print(str(file_contents)) # decode bytes to string
984985
985986
```
986987

988+
### Updating a File
989+
990+
#### Pythonic Syntax:
991+
```
992+
transaction = FileUpdateTransaction(
993+
file_id=file_id,
994+
keys=[new_file_public_key],
995+
contents=b"New File Contents",
996+
file_memo="Updated file memo"
997+
).freeze_with(client)
998+
999+
transaction.sign(current_file_private_key)
1000+
transaction.sign(new_file_private_key)
1001+
transaction.execute(client)
1002+
```
1003+
1004+
#### Method Chaining:
1005+
```
1006+
transaction = (
1007+
FileUpdateTransaction()
1008+
.set_file_id(file_id)
1009+
.set_keys([new_file_public_key])
1010+
.set_contents(b"New File Contents")
1011+
.set_file_memo("Updated file memo")
1012+
.freeze_with(client)
1013+
.sign(current_file_private_key)
1014+
.sign(new_file_private_key)
1015+
)
1016+
1017+
transaction.execute(client)
1018+
1019+
```
1020+
9871021
### Deleting a File
9881022

9891023
#### Pythonic Syntax:
@@ -1040,4 +1074,4 @@ print(f"Transaction Fee: {record.transaction_fee}")
10401074
print(f"Transaction Hash: {record.transaction_hash}")
10411075
print(f"Transaction Memo: {record.transaction_memo}")
10421076
print(f"Transaction Account ID: {record.receipt.account_id}")
1043-
```
1077+
```

examples/file_update.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os
2+
import sys
3+
4+
from dotenv import load_dotenv
5+
6+
from hiero_sdk_python import AccountId, Client, Network, PrivateKey
7+
from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction
8+
from hiero_sdk_python.file.file_info_query import FileInfoQuery
9+
from hiero_sdk_python.file.file_update_transaction import FileUpdateTransaction
10+
from hiero_sdk_python.response_code import ResponseCode
11+
12+
load_dotenv()
13+
14+
15+
def setup_client():
16+
"""Initialize and set up the client with operator account"""
17+
network = Network(network="testnet")
18+
client = Client(network)
19+
20+
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID"))
21+
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY"))
22+
client.set_operator(operator_id, operator_key)
23+
24+
return client
25+
26+
27+
def create_file(client):
28+
"""Create a test file"""
29+
file_private_key = PrivateKey.generate_ed25519()
30+
31+
receipt = (
32+
FileCreateTransaction()
33+
.set_keys(file_private_key.public_key())
34+
.set_contents(b"Hello, this is a test file for querying!")
35+
.set_file_memo("Test file for query")
36+
.freeze_with(client)
37+
.sign(file_private_key)
38+
.execute(client)
39+
)
40+
41+
if receipt.status != ResponseCode.SUCCESS:
42+
print(f"File creation failed with status: {ResponseCode(receipt.status).name}")
43+
sys.exit(1)
44+
45+
file_id = receipt.file_id
46+
print(f"\nFile created with ID: {file_id}")
47+
48+
return file_id, file_private_key
49+
50+
51+
def query_file_info(client, file_id):
52+
info = FileInfoQuery().set_file_id(file_id).execute(client)
53+
54+
print(info)
55+
56+
57+
def file_update():
58+
"""
59+
Demonstrates querying file info by:
60+
1. Setting up client with operator account
61+
2. Creating a test file
62+
3. Querying the file info
63+
4. Updating the file info
64+
5. Querying the file info again
65+
"""
66+
client = setup_client()
67+
68+
# Create a test file first
69+
file_id, file_private_key = create_file(client)
70+
71+
print("File info before update:")
72+
# Query the file info
73+
query_file_info(client, file_id)
74+
75+
# Generate a new private key that will add to the existing key
76+
new_private_key = PrivateKey.generate_ed25519()
77+
78+
# Update the file
79+
receipt = (
80+
FileUpdateTransaction()
81+
.set_file_id(file_id)
82+
.set_keys([file_private_key.public_key(), new_private_key.public_key()])
83+
.set_contents(b"Updated contents!")
84+
.set_file_memo("Updated memo!")
85+
.freeze_with(client)
86+
.sign(new_private_key)
87+
.sign(file_private_key)
88+
.execute(client)
89+
)
90+
91+
if receipt.status != ResponseCode.SUCCESS:
92+
print(f"File update failed with status: {ResponseCode(receipt.status).name}")
93+
sys.exit(1)
94+
95+
print("File info after update:")
96+
# Query the file info again
97+
query_file_info(client, file_id)
98+
99+
100+
if __name__ == "__main__":
101+
file_update()

src/hiero_sdk_python/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
from .file.file_info_query import FileInfoQuery
8686
from .file.file_info import FileInfo
8787
from .file.file_contents_query import FileContentsQuery
88+
from .file.file_update_transaction import FileUpdateTransaction
8889
from .file.file_delete_transaction import FileDeleteTransaction
8990

9091
__all__ = [
@@ -171,5 +172,6 @@
171172
"FileInfoQuery",
172173
"FileInfo",
173174
"FileContentsQuery",
175+
"FileUpdateTransaction",
174176
"FileDeleteTransaction",
175177
]

0 commit comments

Comments
 (0)