Skip to content

Commit

Permalink
working with transaction waiter
Browse files Browse the repository at this point in the history
  • Loading branch information
piniom committed Mar 11, 2024
1 parent a22e7f6 commit a9f7adf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ toml = "0.8"
u256-literal = "1"
url = "2"
wasm-bindgen = "0.2"
webauthn-authenticator-rs = { version = "0.4" }
webauthn-rs-proto = "0.4"
account-sdk = { path = "crates/account_sdk" }
1 change: 0 additions & 1 deletion crates/account_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ toml.workspace = true
u256-literal.workspace = true
url.workspace = true
wasm-bindgen.workspace = true
webauthn-authenticator-rs.workspace = true
webauthn-rs-proto.workspace = true
36 changes: 28 additions & 8 deletions crates/account_sdk/src/transaction_waiter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use std::time::{Duration, Instant};

use futures::FutureExt;
use starknet::core::types::{
ExecutionResult, FieldElement, MaybePendingTransactionReceipt, PendingTransactionReceipt,
StarknetError, TransactionFinalityStatus, TransactionReceipt,
};
use starknet::providers::{Provider, ProviderError};
use tokio::time::{Instant, Interval};

type GetReceiptResult = Result<MaybePendingTransactionReceipt, ProviderError>;
type GetReceiptFuture<'a> = Pin<Box<dyn Future<Output = GetReceiptResult> + Send + 'a>>;
Expand Down Expand Up @@ -77,6 +76,31 @@ pub struct TransactionWaiter<'a, P: Provider> {
started_at: Option<Instant>,
}

struct Interval {
last: Instant,
interval: Duration,
}

impl Interval {
fn new(interval: Duration) -> Self {
Self {
last: Instant::now(),
interval,
}
}

fn poll_tick(&mut self, _cx: &mut Context<'_>) -> Poll<()> {
if self.last.elapsed() > self.interval {
self.last = Instant::now();
Poll::Ready(())
} else {
std::thread::sleep(self.interval);
Poll::Pending
}
}

}

#[allow(dead_code)]
impl<'a, P> TransactionWaiter<'a, P>
where
Expand All @@ -94,17 +118,13 @@ where
finality_status: None,
receipt_request_fut: None,
timeout: Self::DEFAULT_TIMEOUT,
interval: tokio::time::interval_at(
Instant::now() + Self::DEFAULT_INTERVAL,
Self::DEFAULT_INTERVAL,
),
interval: Interval::new(Self::DEFAULT_INTERVAL),
}
}

pub fn with_interval(self, milisecond: u64) -> Self {
let interval = Duration::from_millis(milisecond);
Self {
interval: tokio::time::interval_at(Instant::now() + interval, interval),
interval: Interval::new(Duration::from_millis(milisecond)),
..self
}
}
Expand Down

0 comments on commit a9f7adf

Please sign in to comment.