Skip to content

Commit 4899036

Browse files
authored
Merge pull request #340 from hashgraph/sr/more-examples
Add more examples
2 parents a519177 + b4fc561 commit 4899036

File tree

6 files changed

+153
-18
lines changed

6 files changed

+153
-18
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Hedera
2+
import SwiftDotenv
3+
4+
@main
5+
enum Program {
6+
static func main() async throws {
7+
let env = try Dotenv.load()
8+
9+
// todo: network from name
10+
let client = Client.forTestnet()
11+
12+
// Defaults the operator account ID and key such that all generated transactions will be paid for
13+
// by this account and be signed by this key
14+
client.setOperator(env.operatorAccountId, env.operatorKey)
15+
16+
// The file is required to be a byte array,
17+
// you can easily use the bytes of a file instead.
18+
let fileContents = "Hedera hashgraph is great!"
19+
20+
let response = try await FileCreateTransaction()
21+
.keys([.single(env.operatorKey.getPublicKey())])
22+
.contents(fileContents.data(using: .utf8)!)
23+
.maxTransactionFee(2)
24+
.execute(client)
25+
26+
let receipt = try await response.getReceipt(client)
27+
28+
print("file: \(String(describing: receipt.fileId))")
29+
}
30+
}
31+
extension Environment {
32+
public var operatorAccountId: AccountId {
33+
AccountId(self["OPERATOR_ACCOUNT_ID"]!.stringValue)!
34+
}
35+
36+
public var operatorKey: PrivateKey {
37+
PrivateKey(self["OPERATOR_KEY"]!.stringValue)!
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Hedera
2+
import SwiftDotenv
3+
4+
@main
5+
enum Program {
6+
static func main() async throws {
7+
let env = try Dotenv.load()
8+
9+
// todo: network from name
10+
let client = Client.forTestnet()
11+
12+
// Defaults the operator account ID and key such that all generated transactions will be paid for
13+
// by this account and be signed by this key
14+
client.setOperator(env.operatorAccountId, env.operatorKey)
15+
16+
let createResponse = try await TopicCreateTransaction().execute(client)
17+
let createReceipt = try await createResponse.getReceipt(client)
18+
19+
print("topic id = \(createReceipt.topicId!)")
20+
21+
let sendResponse = try await TopicMessageSubmitTransaction()
22+
.topicId(createReceipt.topicId!)
23+
.message("hello world".data(using: .utf8)!)
24+
.execute(client)
25+
26+
let sendReceipt = try await sendResponse.getReceipt(client)
27+
28+
print("sequence number = \(sendReceipt.topicSequenceNumber)")
29+
}
30+
}
31+
extension Environment {
32+
public var operatorAccountId: AccountId {
33+
AccountId(self["OPERATOR_ACCOUNT_ID"]!.stringValue)!
34+
}
35+
36+
public var operatorKey: PrivateKey {
37+
PrivateKey(self["OPERATOR_KEY"]!.stringValue)!
38+
}
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* ‌
3+
* Hedera Swift SDK
4+
* ​
5+
* Copyright (C) 2022 - 2023 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+
*/
20+
21+
import Foundation
22+
import Hedera
23+
24+
@main
25+
public enum Program {
26+
public static func main() async throws {
27+
do {
28+
let mnemonic = Mnemonic.generate24()
29+
let privateKey = try mnemonic.toPrivateKey()
30+
let publicKey = privateKey.getPublicKey()
31+
32+
print("24 word mnemonic: \(mnemonic)")
33+
print("private key = \(privateKey)")
34+
print("public key = \(publicKey)")
35+
}
36+
37+
do {
38+
let mnemonic = Mnemonic.generate12()
39+
let privateKey = try mnemonic.toPrivateKey()
40+
let publicKey = privateKey.getPublicKey()
41+
42+
print("12 word mnemonic: \(mnemonic)")
43+
print("private key = \(privateKey)")
44+
print("public key = \(publicKey)")
45+
}
46+
}
47+
}

sdk/swift/Package.swift

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,25 @@
2222

2323
import PackageDescription
2424

25-
// collect example targets
26-
var exampleTargets: [PackageDescription.Target] = []
27-
for name in [
28-
"GetAccountBalance",
25+
let exampleTargets = [
26+
"CreateAccount",
27+
"CreateFile",
28+
"CreateTopic",
29+
"DeleteAccount",
2930
"GenerateKey",
31+
"GenerateKeyWithMnemonic",
32+
"GetAccountBalance",
3033
"GetAccountInfo",
3134
"TransferHbar",
32-
"CreateAccount",
33-
"DeleteAccount",
3435
"GetAddressBook",
3536
"GetFileContents",
36-
] {
37-
exampleTargets.append(
38-
.executableTarget(
39-
name: "\(name)Example",
40-
dependencies: ["Hedera", .product(name: "SwiftDotenv", package: "swift-dotenv")],
41-
path: "Examples/\(name)",
42-
swiftSettings: [
43-
.unsafeFlags([
44-
"-parse-as-library"
45-
])
46-
]))
37+
].map { name in
38+
Target.executableTarget(
39+
name: "\(name)Example",
40+
dependencies: ["Hedera", .product(name: "SwiftDotenv", package: "swift-dotenv")],
41+
path: "Examples/\(name)",
42+
swiftSettings: [.unsafeFlags(["-parse-as-library"])]
43+
)
4744
}
4845

4946
let package = Package(

sdk/swift/Sources/Hedera/Mnemonic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public final class Mnemonic: LosslessStringConvertible, ExpressibleByStringLiter
6767
}
6868

6969
public static func generate12() -> Self {
70-
let ptr = hedera_mnemonic_generate_24()
70+
let ptr = hedera_mnemonic_generate_12()
7171
return Self(ptr!)
7272
}
7373

sdk/swift/Sources/Hedera/Transaction.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,21 @@ public class Transaction: Request {
2727
public typealias Response = TransactionResponse
2828

2929
private enum CodingKeys: String, CodingKey {
30+
case maxTransactionFee
3031
case type = "$type"
3132
}
3233

34+
/// The maximum allowed transaction fee for this transaction.
35+
public var maxTransactionFee: Hbar? = 1
36+
37+
/// Sets the maximum allowed transaction fee for this transaction.
38+
@discardableResult
39+
public func maxTransactionFee(_ maxTransactionFee: Hbar) -> Self {
40+
self.maxTransactionFee = maxTransactionFee
41+
42+
return self
43+
}
44+
3345
@discardableResult
3446
public func sign(_ privateKey: PrivateKey) -> Self {
3547
self.signWith(privateKey.getPublicKey()) { privateKey.sign($0) }
@@ -58,5 +70,6 @@ public class Transaction: Request {
5870
let requestName = typeName.prefix(1).lowercased() + typeName.dropFirst().dropLast(11)
5971

6072
try container.encode(requestName, forKey: .type)
73+
try container.encode(maxTransactionFee, forKey: .maxTransactionFee)
6174
}
6275
}

0 commit comments

Comments
 (0)