Skip to content

Commit

Permalink
hopefully fix fee estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakuhito committed Aug 17, 2024
1 parent 10fde96 commit 86e3e57
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export declare class Peer {
* Retrieves the fee estimate for a given target time.
*
* @param {Peer} peer - The peer connection to the Chia node.
* @param {BigInt} targetTimeSeconds - The target time in seconds from the current time for the fee estimate.
* @param {BigInt} targetTimeSeconds - Time delta: The target time in seconds from the current time for the fee estimate.
* @returns {Promise<BigInt>} The estimated fee in mojos per CLVM cost.
*/
getFeeEstimate(targetTimeSeconds: bigint): Promise<bigint>
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ impl Peer {
/// Retrieves the fee estimate for a given target time.
///
/// @param {Peer} peer - The peer connection to the Chia node.
/// @param {BigInt} targetTimeSeconds - The target time in seconds from the current time for the fee estimate.
/// @param {BigInt} targetTimeSeconds - Time delta: The target time in seconds from the current time for the fee estimate.
/// @returns {Promise<BigInt>} The estimated fee in mojos per CLVM cost.
pub async fn get_fee_estimate(&self, target_time_seconds: BigInt) -> napi::Result<BigInt> {
wallet::get_fee_estimate(&self.inner.clone(), u64::from_js(target_time_seconds))
Expand Down
11 changes: 11 additions & 0 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use clvm_traits::ToClvmError;
use clvm_utils::tree_hash;
use clvmr::Allocator;
use std::io::Error as IoError;
use std::time::{SystemTime, UNIX_EPOCH};
use thiserror::Error;

use crate::datastore_spend;
Expand Down Expand Up @@ -796,6 +797,12 @@ pub async fn get_fee_estimate(
peer: &Peer,
target_time_seconds: u64,
) -> Result<u64, ClientError<String>> {
let target_time_seconds = target_time_seconds
+ SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs();

let fee_estimate_group = peer
.request_fee_estimates(vec![target_time_seconds])
.await
Expand All @@ -806,6 +813,10 @@ pub async fn get_fee_estimate(
}

if let Some(first_estimate) = fee_estimate_group.estimates.first() {
if let Some(error_message) = &first_estimate.error {
return Err(ClientError::Rejection(error_message.clone()));
}

return Ok(first_estimate.estimated_fee_rate.mojos_per_clvm_cost);
}

Expand Down

0 comments on commit 86e3e57

Please sign in to comment.