Skip to content

Commit 0a77e85

Browse files
committed
feat: add default_max_{transaction_fee,query_payment
Signed-off-by: Skyler Ross <[email protected]>
1 parent 65fcdf8 commit 0a77e85

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

src/client/mod.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use crate::{
4747
AccountId,
4848
ArcSwapOption,
4949
Error,
50+
Hbar,
5051
LedgerId,
5152
NodeAddressBook,
5253
PrivateKey,
@@ -86,6 +87,7 @@ struct ClientBuilder {
8687
network: ManagedNetwork,
8788
operator: Option<Operator>,
8889
max_transaction_fee: Option<NonZeroU64>,
90+
max_query_payment: Option<NonZeroU64>,
8991
ledger_id: Option<LedgerId>,
9092
auto_validate_checksums: bool,
9193
regenerate_transaction_ids: bool,
@@ -100,6 +102,7 @@ impl ClientBuilder {
100102
network,
101103
operator: None,
102104
max_transaction_fee: None,
105+
max_query_payment: None,
103106
ledger_id: None,
104107
auto_validate_checksums: false,
105108
regenerate_transaction_ids: true,
@@ -121,6 +124,7 @@ impl ClientBuilder {
121124
network,
122125
operator,
123126
max_transaction_fee,
127+
max_query_payment,
124128
ledger_id,
125129
auto_validate_checksums,
126130
regenerate_transaction_ids,
@@ -143,6 +147,9 @@ impl ClientBuilder {
143147
max_transaction_fee_tinybar: AtomicU64::new(
144148
max_transaction_fee.map(NonZeroU64::get).unwrap_or(0),
145149
),
150+
max_query_payment_tinybar: AtomicU64::new(
151+
max_query_payment.map(NonZeroU64::get).unwrap_or(0),
152+
),
146153
ledger_id: ArcSwapOption::new(ledger_id.map(Arc::new)),
147154
auto_validate_checksums: AtomicBool::new(auto_validate_checksums),
148155
regenerate_transaction_ids: AtomicBool::new(regenerate_transaction_ids),
@@ -156,6 +163,7 @@ struct ClientInner {
156163
network: ManagedNetwork,
157164
operator: ArcSwapOption<Operator>,
158165
max_transaction_fee_tinybar: AtomicU64,
166+
max_query_payment_tinybar: AtomicU64,
159167
ledger_id: ArcSwapOption<LedgerId>,
160168
auto_validate_checksums: AtomicBool,
161169
regenerate_transaction_ids: AtomicBool,
@@ -410,9 +418,38 @@ impl Client {
410418
&self.0.network.mirror
411419
}
412420

421+
/// Sets the maximum transaction fee to be used when no explicit max transaction fee is set.
422+
///
423+
/// Note: Setting `amount` to zero is "unlimited"
424+
/// # Panics
425+
/// - if amount is negative
426+
pub fn set_default_max_transaction_fee(&self, amount: Hbar) {
427+
assert!(amount >= Hbar::ZERO);
428+
self.0.max_transaction_fee_tinybar.store(amount.to_tinybars() as u64, Ordering::Relaxed)
429+
}
430+
413431
/// Gets the maximum transaction fee the paying account is willing to pay.
414-
pub(crate) fn max_transaction_fee(&self) -> &AtomicU64 {
415-
&self.0.max_transaction_fee_tinybar
432+
pub fn default_max_transaction_fee(&self) -> Option<Hbar> {
433+
let val = self.0.max_transaction_fee_tinybar.load(Ordering::Relaxed);
434+
435+
(val > 0).then(|| Hbar::from_tinybars(val as i64))
436+
}
437+
438+
/// Gets the maximum query fee the paying account is willing to pay.
439+
pub fn default_max_query_payment(&self) -> Option<Hbar> {
440+
let val = self.0.max_query_payment_tinybar.load(Ordering::Relaxed);
441+
442+
(val > 0).then(|| Hbar::from_tinybars(val as i64))
443+
}
444+
445+
/// Sets the maximum query payment to be used when no explicit max query payment is set.
446+
///
447+
/// Note: Setting `amount` to zero is "unlimited"
448+
/// # Panics
449+
/// - if amount is negative
450+
pub fn set_default_max_query_payment(&self, amount: Hbar) {
451+
assert!(amount >= Hbar::ZERO);
452+
self.0.max_query_payment_tinybar.store(amount.to_tinybars() as u64, Ordering::Relaxed);
416453
}
417454

418455
/// Returns the maximum amount of time that will be spent on a request.

src/query/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ where
266266
// payment is required but none was specified, query the cost
267267
let cost = QueryCost::new(self).execute(client, None).await?;
268268

269+
if self.payment.get_max_amount().is_none() {
270+
// N.B. This can still be `None`.
271+
self.payment.max_amount(client.default_max_query_payment());
272+
}
273+
269274
if let Some(max_amount) = self.payment.get_max_amount() {
270275
if cost > max_amount {
271276
return Err(Error::MaxQueryPaymentExceeded {

src/transaction/mod.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,7 @@ impl<D: ValidateChecksums> Transaction<D> {
394394
let max_transaction_fee = self.body.max_transaction_fee.or_else(|| {
395395
// no max has been set on the *transaction*
396396
// check if there is a global max set on the client
397-
let client_max_transaction_fee = client
398-
.map(|it| it.max_transaction_fee().load(std::sync::atomic::Ordering::Relaxed));
399-
400-
match client_max_transaction_fee {
401-
Some(max) if max > 1 => Some(Hbar::from_tinybars(max as i64)),
402-
// no max has been set on the client either
403-
// fallback to the hard-coded default for this transaction type
404-
_ => None,
405-
}
397+
client.and_then(|it| it.default_max_transaction_fee())
406398
});
407399

408400
let operator = client.and_then(Client::full_load_operator);

0 commit comments

Comments
 (0)