Skip to content

Commit d7f9a3c

Browse files
RickyLBrwalworth
andauthored
HIP-551: Atomic Batch Transactions (#452)
* chore: replace remaining file license headers Signed-off-by: Ricky Saechao <ricky@launchbadge.com> * feat(hip-551): add BatchTransaction Signed-off-by: Ricky Saechao <ricky@launchbadge.com> * feat: add tests and example for batch transaction Signed-off-by: Rob Walworth <robert.walworth@swirldslabs.com> * fix: tests Signed-off-by: Rob Walworth <robert.walworth@swirldslabs.com> * fix: tests Signed-off-by: Rob Walworth <robert.walworth@swirldslabs.com> * fix: batch transaction init from protobuf Signed-off-by: Rob Walworth <robert.walworth@swirldslabs.com> * fix: snapshot Signed-off-by: Rob Walworth <robert.walworth@swirldslabs.com> --------- Signed-off-by: Ricky Saechao <ricky@launchbadge.com> Signed-off-by: Rob Walworth <robert.walworth@swirldslabs.com> Co-authored-by: Rob Walworth <robert.walworth@swirldslabs.com>
1 parent 7208527 commit d7f9a3c

File tree

78 files changed

+842
-1306
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+842
-1306
lines changed

Examples/AddNftAllowance/main.swift

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
1-
/*
2-
* Hedera Swift SDK
3-
*
4-
* Copyright (C) 2022 - 2024 Hedera Hashgraph, LLC
5-
*
6-
* Licensed under the Apache License, Version 2.0 (the "License");
7-
* you may not use this file except in compliance with the License.
8-
* You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
12-
* Unless required by applicable law or agreed to in writing, software
13-
* distributed under the License is distributed on an "AS IS" BASIS,
14-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
* See the License for the specific language governing permissions and
16-
* limitations under the License.
17-
*/
1+
// SPDX-License-Identifier: Apache-2.0
182

193
import Foundation
204
import Hiero
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
import Hiero
4+
import HieroExampleUtilities
5+
import SwiftDotenv
6+
7+
@main
8+
internal enum Program {
9+
internal static func main() async throws {
10+
/// Grab the environment variables.
11+
let env = try Dotenv.load()
12+
13+
/// Initialize the client based on the provided environment.
14+
let client = try Client.forName(env.networkName)
15+
client.setOperator(env.operatorAccountId, env.operatorKey)
16+
17+
/// Step 1: Create batch keys.
18+
let batchKey1 = PrivateKey.generateEcdsa()
19+
let batchKey2 = PrivateKey.generateEcdsa()
20+
let batchKey3 = PrivateKey.generateEcdsa()
21+
22+
/// Step 2: Create accounts.
23+
let aliceKey = PrivateKey.generateEcdsa()
24+
let aliceAccountId = try await AccountCreateTransaction()
25+
.keyWithoutAlias(.single(aliceKey.publicKey))
26+
.initialBalance(5)
27+
.execute(client)
28+
.getReceipt(client).accountId!
29+
print("Alice account ID: \(aliceAccountId)")
30+
31+
let bobKey = PrivateKey.generateEcdsa()
32+
let bobAccountId = try await AccountCreateTransaction()
33+
.keyWithoutAlias(.single(bobKey.publicKey))
34+
.initialBalance(5)
35+
.execute(client)
36+
.getReceipt(client).accountId!
37+
print("Bob account ID: \(bobAccountId)")
38+
39+
let carolKey = PrivateKey.generateEcdsa()
40+
let carolAccountId = try await AccountCreateTransaction()
41+
.keyWithoutAlias(.single(carolKey.publicKey))
42+
.initialBalance(5)
43+
.execute(client)
44+
.getReceipt(client).accountId!
45+
print("Carol account ID: \(carolAccountId)")
46+
47+
/// Step 3: Prepare transfers for batching.
48+
let aliceTransferTx = try TransferTransaction()
49+
.hbarTransfer(env.operatorAccountId, Hbar(1))
50+
.hbarTransfer(aliceAccountId, Hbar(-1))
51+
.transactionId(TransactionId.generateFrom(aliceAccountId))
52+
.batchKey(.single(batchKey1.publicKey))
53+
.freezeWith(client)
54+
.sign(aliceKey)
55+
56+
let bobTransferTx = try TransferTransaction()
57+
.hbarTransfer(env.operatorAccountId, Hbar(1))
58+
.hbarTransfer(bobAccountId, Hbar(-1))
59+
.transactionId(TransactionId.generateFrom(bobAccountId))
60+
.batchKey(.single(batchKey2.publicKey))
61+
.freezeWith(client)
62+
.sign(bobKey)
63+
64+
let carolTransferTx = try TransferTransaction()
65+
.hbarTransfer(env.operatorAccountId, Hbar(1))
66+
.hbarTransfer(carolAccountId, Hbar(-1))
67+
.transactionId(TransactionId.generateFrom(carolAccountId))
68+
.batchKey(.single(batchKey3.publicKey))
69+
.freezeWith(client)
70+
.sign(carolKey)
71+
72+
/// Step 4: Get initial balances.
73+
let aliceBalance = try await AccountBalanceQuery().accountId(aliceAccountId).execute(client)
74+
let bobBalance = try await AccountBalanceQuery().accountId(bobAccountId).execute(client)
75+
let carolBalance = try await AccountBalanceQuery().accountId(carolAccountId).execute(client)
76+
print("Alice balance: \(aliceBalance.hbars)")
77+
print("Bob balance: \(bobBalance.hbars)")
78+
print("Carol balance: \(carolBalance.hbars)")
79+
80+
/// Step 5: Prepare and send the batch transaction.
81+
_ = try await BatchTransaction()
82+
.addInnerTransaction(aliceTransferTx)
83+
.addInnerTransaction(bobTransferTx)
84+
.addInnerTransaction(carolTransferTx)
85+
.freezeWith(client)
86+
.sign(batchKey1)
87+
.sign(batchKey2)
88+
.sign(batchKey3)
89+
.execute(client)
90+
.getReceipt(client)
91+
92+
/// Step 6: Get and compare balances.
93+
let aliceNewBalance = try await AccountBalanceQuery().accountId(aliceAccountId).execute(client)
94+
let bobNewBalance = try await AccountBalanceQuery().accountId(bobAccountId).execute(client)
95+
let carolNewBalance = try await AccountBalanceQuery().accountId(carolAccountId).execute(client)
96+
print("Alice balance (should be 1 less): \(aliceNewBalance.hbars)")
97+
print("Bob balance (should be 1 less): \(bobNewBalance.hbars)")
98+
print("Carol balance (should be 1 less): \(carolNewBalance.hbars)")
99+
100+
}
101+
}
102+
103+
extension Environment {
104+
/// Account ID for the operator to use in this example.
105+
internal var operatorAccountId: AccountId {
106+
AccountId(self["OPERATOR_ID"]!.stringValue)!
107+
}
108+
109+
/// Private key for the operator to use in this example.
110+
internal var operatorKey: PrivateKey {
111+
PrivateKey(self["OPERATOR_KEY"]!.stringValue)!
112+
}
113+
114+
/// The name of the hedera network this example should be ran against.
115+
///
116+
/// Testnet by default.
117+
internal var networkName: String {
118+
self["HEDERA_NETWORK"]?.stringValue ?? "testnet"
119+
}
120+
}

Examples/CreateAccountWithAlias/main.swift

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
1-
/*
2-
* ‌
3-
* Hedera Swift SDK
4-
* ​
5-
* Copyright (C) 2022 - 2025 Hedera Hashgraph, LLC
6-
* ​
7-
* Licensed under the Apache License, Version 2.0 (the "License");
8-
* you may not use this file except in compliance with the License.
9-
* You may obtain a copy of the License at
10-
*
11-
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
13-
* Unless required by applicable law or agreed to in writing, software
14-
* distributed under the License is distributed on an "AS IS" BASIS,
15-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16-
* See the License for the specific language governing permissions and
17-
* limitations under the License.
18-
* ‍
19-
*/
1+
// SPDX-License-Identifier: Apache-2.0
202

213
import Foundation
224
import Hiero

Examples/ModifyTokenKeys/main.swift

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
1-
/*
2-
* ‌
3-
* Hedera Swift SDK
4-
*
5-
* Copyright (C) 2022 - 2024 Hedera Hashgraph, LLC
6-
*
7-
* Licensed under the Apache License, Version 2.0 (the "License");
8-
* you may not use this file except in compliance with the License.
9-
* You may obtain a copy of the License at
10-
*
11-
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
13-
* Unless required by applicable law or agreed to in writing, software
14-
* distributed under the License is distributed on an "AS IS" BASIS,
15-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16-
* See the License for the specific language governing permissions and
17-
* limitations under the License.
18-
*
19-
*/
1+
// SPDX-License-Identifier: Apache-2.0
202

213
import Foundation
224
import Hiero

Examples/NftUpdateMetadata/main.swift

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
1-
/*
2-
* ‌
3-
* Hedera Swift SDK
4-
*
5-
* Copyright (C) 2022 - 2024 Hedera Hashgraph, LLC
6-
*
7-
* Licensed under the Apache License, Version 2.0 (the "License");
8-
* you may not use this file except in compliance with the License.
9-
* You may obtain a copy of the License at
10-
*
11-
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
13-
* Unless required by applicable law or agreed to in writing, software
14-
* distributed under the License is distributed on an "AS IS" BASIS,
15-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16-
* See the License for the specific language governing permissions and
17-
* limitations under the License.
18-
*
19-
*/
1+
// SPDX-License-Identifier: Apache-2.0
202

213
import Foundation
224
import Hiero

Examples/TokenUpdateMetadata/main.swift

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
1-
/*
2-
* ‌
3-
* Hedera Swift SDK
4-
*
5-
* Copyright (C) 2022 - 2024 Hedera Hashgraph, LLC
6-
*
7-
* Licensed under the Apache License, Version 2.0 (the "License");
8-
* you may not use this file except in compliance with the License.
9-
* You may obtain a copy of the License at
10-
*
11-
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
13-
* Unless required by applicable law or agreed to in writing, software
14-
* distributed under the License is distributed on an "AS IS" BASIS,
15-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16-
* See the License for the specific language governing permissions and
17-
* limitations under the License.
18-
*
19-
*/
1+
// SPDX-License-Identifier: Apache-2.0
202

213
import Foundation
224
import Hiero

0 commit comments

Comments
 (0)