Skip to content

Commit

Permalink
feat(Client): more api features
Browse files Browse the repository at this point in the history
Signed-off-by: Ricky Saechao <[email protected]>
  • Loading branch information
RickyLB committed Dec 18, 2023
1 parent f105126 commit a5b7f84
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>AccountAliasExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>AccountAllowanceExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
</dict>
<key>ConsensusPubSubChunkedExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>4</integer>
</dict>
<key>ConsensusPubSubExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>5</integer>
</dict>
<key>ConsensusPubSubWithSubmitKeyExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>6</integer>
</dict>
<key>CreateAccountExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>7</integer>
</dict>
<key>CreateAccountThresholdKeyExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>8</integer>
</dict>
<key>CreateFileExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>9</integer>
</dict>
<key>CreateSimpleContractExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>10</integer>
</dict>
<key>CreateStatefulContractExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>11</integer>
</dict>
<key>CreateTopicExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>12</integer>
</dict>
<key>DeleteAccountExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>13</integer>
</dict>
<key>DeleteFileExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>14</integer>
</dict>
<key>FileAppendChunkedExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>15</integer>
</dict>
<key>GenerateKeyExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>16</integer>
</dict>
<key>GenerateKeyWithMnemonicExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>17</integer>
</dict>
<key>GetAccountBalanceExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>18</integer>
</dict>
<key>GetAccountInfoExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>19</integer>
</dict>
<key>GetAddressBookExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>20</integer>
</dict>
<key>GetExchangeRatesExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>21</integer>
</dict>
<key>GetFileContentsExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>22</integer>
</dict>
<key>Hedera-Package.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
<key>Hedera.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
<key>MultiAppTransferExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>23</integer>
</dict>
<key>MultiSigOfflineExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>24</integer>
</dict>
<key>PrngExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>25</integer>
</dict>
<key>ScheduleExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>26</integer>
</dict>
<key>ScheduleIdenticalTransactionExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>27</integer>
</dict>
<key>ScheduleMultiSigTransactionExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>28</integer>
</dict>
<key>ScheduledTransactionMultiSigThresholdExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>29</integer>
</dict>
<key>ScheduledTransferExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>30</integer>
</dict>
<key>StakingExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>31</integer>
</dict>
<key>StakingWithUpdateExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>32</integer>
</dict>
<key>TopicWithAdminKeyExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>33</integer>
</dict>
<key>TransferCryptoExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>34</integer>
</dict>
<key>TransferTokensExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>35</integer>
</dict>
<key>UpdateAccountPublicKeyExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>36</integer>
</dict>
<key>ValidateChecksumExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>37</integer>
</dict>
</dict>
</dict>
</plist>
78 changes: 78 additions & 0 deletions Sources/Hedera/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class Client: Sendable {
private let networkUpdateTask: NetworkUpdateTask
private let regenerateTransactionIdInner: ManagedAtomic<Bool>
private let maxTransactionFeeInner: ManagedAtomic<Int64>
private let maxQueryPaymentInner: ManagedAtomic<Int64>
private let networkUpdatePeriodInner: NIOLockedValueBox<UInt64?>
private let backoffInner: NIOLockedValueBox<Backoff>

Expand All @@ -50,6 +51,7 @@ public final class Client: Sendable {
self.autoValidateChecksumsInner = .init(false)
self.regenerateTransactionIdInner = .init(true)
self.maxTransactionFeeInner = .init(0)
self.maxQueryPaymentInner = .init(0)
self.networkUpdateTask = NetworkUpdateTask(
eventLoop: eventLoop,
managedNetwork: network,
Expand Down Expand Up @@ -80,6 +82,16 @@ public final class Client: Sendable {
return .fromTinybars(value)
}

internal var maxQueryPayment: Hbar? {
let value = maxQueryPaymentInner.load(ordering: .relaxed)

guard value != 0 else {
return nil
}

return .fromTinybars(value)
}

/// The maximum amount of time that will be spent on a request.
public var requestTimeout: TimeInterval? {
get { backoff.requestTimeout }
Expand Down Expand Up @@ -214,6 +226,22 @@ public final class Client: Sendable {
return self
}

/// Sets the account that will, by default, be paying for transactions and queries built with
/// this client.
///
/// The operator account ID is used to generate the default transaction ID for all transactions
/// executed with this client.
///
/// The operator signer is used to sign all transactions executed by this client.
@discardableResult
public func setOperatorWith(
_ accountId: AccountId, _ publicKey: PublicKey, using signFunc: @Sendable @escaping (Data) -> Data
) throws -> Self {
operatorInner.withLockedValue { $0 = .init(accountId: accountId, signer: Signer.init(publicKey, signFunc)) }

return self
}

public func ping(_ nodeAccountId: AccountId) async throws {
try await PingQuery(nodeAccountId: nodeAccountId).execute(self)
}
Expand Down Expand Up @@ -306,6 +334,46 @@ public final class Client: Sendable {
(self.operator?.accountId).map { .generateFrom($0) }
}

/// Sets the maximum transaction fee to be used when no explicit max transaction fee is set.
///
/// Note: Setting the amount to zero is "unlimited".
/// # Panics
/// - if amount is negative
public func setDefaultMaxTransactionFee(_ amount: Hbar) throws {
assert(amount.toTinybars() < 0, "Default max transaction fee cannot be set to a negative value.")

self.maxTransactionFeeInner.store(amount.toTinybars(), ordering: .relaxed)
}

/// Gets the maximum transaction fee the paying account is willing to pay.
public func defaultMaxTransactionFee() throws -> Hbar? {
let val = self.maxTransactionFeeInner.load(ordering: .relaxed)

let amount = (val > 0) ? Hbar.fromTinybars(val) : nil

return amount
}

/// Sets the maximum query payment to be used when no explicit max query payment is set.
///
/// Note: Setting the amount to zero is "unlimited".
/// # Panics
/// - if amount is negative
public func setDefaultMaxQueryPayment(_ amount: Hbar) throws {
assert(amount.toTinybars() < 0, "Default max query payment cannot be set to a negative value.")

self.maxQueryPaymentInner.store(amount.toTinybars(), ordering: .relaxed)
}

/// Gets the maximum query payment the paying account is willing to pay.
public func defaultMaxQueryPayment() throws -> Hbar? {
let val = self.maxQueryPaymentInner.load(ordering: .relaxed)

let amount = (val > 0) ? Hbar.fromTinybars(val) : nil

return amount
}

internal var net: Network {
networkInner.primary.load(ordering: .relaxed)
}
Expand Down Expand Up @@ -353,6 +421,16 @@ public final class Client: Sendable {
await self.networkUpdateTask.setUpdatePeriod(nanoseconds)
self.networkUpdatePeriodInner.withLockedValue { $0 = nanoseconds }
}

/// Returns the Account ID for the operator.
public var operatorAccountId: AccountId? {
operatorInner.withLockedValue { $0?.accountId }
}

/// Returns the Public Key for the operator.
public var operatorPublicKey: PublicKey? {
operatorInner.withLockedValue { $0?.signer.publicKey }
}
}

extension Client {
Expand Down

0 comments on commit a5b7f84

Please sign in to comment.