Skip to content

Commit 256661c

Browse files
feat: file append tx (#206)
* feat: file append tx Signed-off-by: Ivaylo Nikolov <[email protected]> * feat: file append example Signed-off-by: Ivaylo Nikolov <[email protected]> * test: file append tx Signed-off-by: Ivaylo Nikolov <[email protected]> * fix: import try catch Signed-off-by: Ivaylo Nikolov <[email protected]> * fix: remove chunk interval Signed-off-by: Ivaylo Nikolov <[email protected]> * fix: accept only fileid Signed-off-by: Ivaylo Nikolov <[email protected]> * chore: lint errors Signed-off-by: Ivaylo Nikolov <[email protected]> * test: remove mocked tests Signed-off-by: Ivaylo Nikolov <[email protected]> * fix: have to use protected member Signed-off-by: Ivaylo Nikolov <[email protected]> * test: check content test Signed-off-by: Ivaylo Nikolov <[email protected]> * chore: pr comments Signed-off-by: Ivaylo Nikolov <[email protected]> * fix: add check if file id is set Signed-off-by: Ivaylo Nikolov <[email protected]> * fix: file_id instead of fileId Signed-off-by: Ivaylo Nikolov <[email protected]> * test: add missing test Signed-off-by: Ivaylo Nikolov <[email protected]> * test: add missing unit test Signed-off-by: Ivaylo Nikolov <[email protected]> * fix: from_proto to _from_proto Signed-off-by: Ivaylo Nikolov <[email protected]> --------- Signed-off-by: Ivaylo Nikolov <[email protected]> Signed-off-by: ivaylonikolov7 <[email protected]>
1 parent 079e7e8 commit 256661c

File tree

6 files changed

+912
-1
lines changed

6 files changed

+912
-1
lines changed

examples/file_append.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
File Append Example
5+
6+
This example demonstrates how to append content to an existing file on the network.
7+
It shows both single-chunk and multi-chunk append operations.
8+
"""
9+
10+
import sys
11+
import os
12+
from dotenv import load_dotenv
13+
14+
# Load environment variables from .env file
15+
load_dotenv()
16+
17+
from hiero_sdk_python import (
18+
Client, Network, PrivateKey, FileCreateTransaction, AccountId,
19+
FileAppendTransaction, ResponseCode
20+
)
21+
22+
def setup_client():
23+
"""Initialize and set up the client with operator account"""
24+
network = Network(network='testnet')
25+
client = Client(network)
26+
27+
print(os.getenv('OPERATOR_ID'))
28+
print(os.getenv('OPERATOR_KEY'))
29+
30+
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
31+
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
32+
client.set_operator(operator_id, operator_key)
33+
34+
35+
return client
36+
37+
def file_append():
38+
"""
39+
Demonstrates appending content to a file on the network by:
40+
1. Setting up client with operator account
41+
2. Creating a file with initial content
42+
3. Appending additional content to the file
43+
"""
44+
client = setup_client()
45+
46+
file_private_key = PrivateKey.generate_ed25519()
47+
48+
# Step 1: Create a file with initial content
49+
print("Creating file with initial content...")
50+
create_receipt = (
51+
FileCreateTransaction()
52+
.set_keys(file_private_key.public_key())
53+
.set_contents(b"Initial file content")
54+
.set_file_memo("File for append example")
55+
.freeze_with(client)
56+
.sign(file_private_key)
57+
.execute(client)
58+
)
59+
60+
if create_receipt.status != ResponseCode.SUCCESS:
61+
print(f"File creation failed with status: {ResponseCode(create_receipt.status).name}")
62+
sys.exit(1)
63+
64+
file_id = create_receipt.file_id
65+
print(f"File created successfully with ID: {file_id}")
66+
67+
# Step 2: Append content to the file (single chunk)
68+
print("\nAppending content to file (single chunk)...")
69+
append_receipt = (
70+
FileAppendTransaction()
71+
.set_file_id(file_id)
72+
.set_contents(b"\nThis is appended content!")
73+
.freeze_with(client)
74+
.sign(file_private_key)
75+
.execute(client)
76+
)
77+
78+
if append_receipt.status != ResponseCode.SUCCESS:
79+
print(f"File append failed with status: {ResponseCode(append_receipt.status).name}")
80+
sys.exit(1)
81+
82+
print("Content appended successfully!")
83+
84+
# Step 3: Append large content (multi-chunk)
85+
print("\nAppending large content (multi-chunk)...")
86+
large_content = b"Large content that will be split into multiple chunks. " * 100
87+
88+
large_append_receipt = (
89+
FileAppendTransaction()
90+
.set_file_id(file_id)
91+
.set_contents(large_content)
92+
.set_chunk_size(1024) # 1KB chunks
93+
.set_max_chunks(50) # Allow up to 50 chunks
94+
.freeze_with(client)
95+
.sign(file_private_key)
96+
.execute(client)
97+
)
98+
99+
if large_append_receipt.status != ResponseCode.SUCCESS:
100+
print(f"Large file append failed with status: {ResponseCode(large_append_receipt.status).name}")
101+
sys.exit(1)
102+
103+
print("Large content appended successfully!")
104+
print(f"Total chunks used: {FileAppendTransaction().set_contents(large_content).get_required_chunks()}")
105+
106+
if __name__ == "__main__":
107+
file_append()

src/hiero_sdk_python/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181

8282
# File
8383
from .file.file_create_transaction import FileCreateTransaction
84+
from .file.file_append_transaction import FileAppendTransaction
8485
from .file.file_info_query import FileInfoQuery
8586
from .file.file_info import FileInfo
8687
from .file.file_contents_query import FileContentsQuery
@@ -166,6 +167,7 @@
166167

167168
# File
168169
"FileCreateTransaction",
170+
"FileAppendTransaction",
169171
"FileInfoQuery",
170172
"FileInfo",
171173
"FileContentsQuery",

0 commit comments

Comments
 (0)