Skip to content

Commit 0bd14e8

Browse files
committed
feat(Client): more api features
Signed-off-by: Ricky Saechao <[email protected]>
1 parent f105126 commit 0bd14e8

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Sources/Hedera/Client/Client.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class Client: Sendable {
3434
private let networkUpdateTask: NetworkUpdateTask
3535
private let regenerateTransactionIdInner: ManagedAtomic<Bool>
3636
private let maxTransactionFeeInner: ManagedAtomic<Int64>
37+
private let maxQueryPaymentInner: ManagedAtomic<Int64>
3738
private let networkUpdatePeriodInner: NIOLockedValueBox<UInt64?>
3839
private let backoffInner: NIOLockedValueBox<Backoff>
3940

@@ -50,6 +51,7 @@ public final class Client: Sendable {
5051
self.autoValidateChecksumsInner = .init(false)
5152
self.regenerateTransactionIdInner = .init(true)
5253
self.maxTransactionFeeInner = .init(0)
54+
self.maxQueryPaymentInner = .init(0)
5355
self.networkUpdateTask = NetworkUpdateTask(
5456
eventLoop: eventLoop,
5557
managedNetwork: network,
@@ -80,6 +82,16 @@ public final class Client: Sendable {
8082
return .fromTinybars(value)
8183
}
8284

85+
internal var maxQueryPayment: Hbar? {
86+
let value = maxQueryPaymentInner.load(ordering: .relaxed)
87+
88+
guard value != 0 else {
89+
return nil
90+
}
91+
92+
return .fromTinybars(value)
93+
}
94+
8395
/// The maximum amount of time that will be spent on a request.
8496
public var requestTimeout: TimeInterval? {
8597
get { backoff.requestTimeout }
@@ -214,6 +226,22 @@ public final class Client: Sendable {
214226
return self
215227
}
216228

229+
/// Sets the account that will, by default, be paying for transactions and queries built with
230+
/// this client.
231+
///
232+
/// The operator account ID is used to generate the default transaction ID for all transactions
233+
/// executed with this client.
234+
///
235+
/// The operator signer is used to sign all transactions executed by this client.
236+
@discardableResult
237+
public func setOperatorWith(
238+
_ accountId: AccountId, _ publicKey: PublicKey, using signFunc: @Sendable @escaping (Data) -> Data
239+
) throws -> Self {
240+
operatorInner.withLockedValue { $0 = .init(accountId: accountId, signer: Signer.init(publicKey, signFunc)) }
241+
242+
return self
243+
}
244+
217245
public func ping(_ nodeAccountId: AccountId) async throws {
218246
try await PingQuery(nodeAccountId: nodeAccountId).execute(self)
219247
}
@@ -306,6 +334,46 @@ public final class Client: Sendable {
306334
(self.operator?.accountId).map { .generateFrom($0) }
307335
}
308336

337+
/// Sets the maximum transaction fee to be used when no explicit max transaction fee is set.
338+
///
339+
/// Note: Setting the amount to zero is "unlimited".
340+
/// # Panics
341+
/// - if amount is negative
342+
public func setDefaultMaxTransactionFee(_ amount: Hbar) throws {
343+
assert(amount.toTinybars() >= 0, "Default max transaction fee cannot be set to a negative value.")
344+
345+
self.maxTransactionFeeInner.store(amount.toTinybars(), ordering: .relaxed)
346+
}
347+
348+
/// Gets the maximum transaction fee the paying account is willing to pay.
349+
public func defaultMaxTransactionFee() throws -> Hbar? {
350+
let val = self.maxTransactionFeeInner.load(ordering: .relaxed)
351+
352+
let amount = (val > 0) ? Hbar.fromTinybars(val) : nil
353+
354+
return amount
355+
}
356+
357+
/// Sets the maximum query payment to be used when no explicit max query payment is set.
358+
///
359+
/// Note: Setting the amount to zero is "unlimited".
360+
/// # Panics
361+
/// - if amount is negative
362+
public func setDefaultMaxQueryPayment(_ amount: Hbar) throws {
363+
assert(amount.toTinybars() < 0, "Default max query payment cannot be set to a negative value.")
364+
365+
self.maxQueryPaymentInner.store(amount.toTinybars(), ordering: .relaxed)
366+
}
367+
368+
/// Gets the maximum query payment the paying account is willing to pay.
369+
public func defaultMaxQueryPayment() throws -> Hbar? {
370+
let val = self.maxQueryPaymentInner.load(ordering: .relaxed)
371+
372+
let amount = (val > 0) ? Hbar.fromTinybars(val) : nil
373+
374+
return amount
375+
}
376+
309377
internal var net: Network {
310378
networkInner.primary.load(ordering: .relaxed)
311379
}
@@ -353,6 +421,16 @@ public final class Client: Sendable {
353421
await self.networkUpdateTask.setUpdatePeriod(nanoseconds)
354422
self.networkUpdatePeriodInner.withLockedValue { $0 = nanoseconds }
355423
}
424+
425+
/// Returns the Account ID for the operator.
426+
public var operatorAccountId: AccountId? {
427+
operatorInner.withLockedValue { $0?.accountId }
428+
}
429+
430+
/// Returns the Public Key for the operator.
431+
public var operatorPublicKey: PublicKey? {
432+
operatorInner.withLockedValue { $0?.signer.publicKey }
433+
}
356434
}
357435

358436
extension Client {

0 commit comments

Comments
 (0)