From a9f7adf8b6e898d978dc9d9250a727d4705d0251 Mon Sep 17 00:00:00 2001 From: Szymon Wojtulewicz Date: Mon, 11 Mar 2024 12:04:07 +0000 Subject: [PATCH] working with transaction waiter --- Cargo.toml | 1 - crates/account_sdk/Cargo.toml | 1 - crates/account_sdk/src/transaction_waiter.rs | 36 +++++++++++++++----- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 367b9a16..7afb42fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/crates/account_sdk/Cargo.toml b/crates/account_sdk/Cargo.toml index ba4aa23f..8f315ddd 100644 --- a/crates/account_sdk/Cargo.toml +++ b/crates/account_sdk/Cargo.toml @@ -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 diff --git a/crates/account_sdk/src/transaction_waiter.rs b/crates/account_sdk/src/transaction_waiter.rs index af6fd06a..fce853ef 100644 --- a/crates/account_sdk/src/transaction_waiter.rs +++ b/crates/account_sdk/src/transaction_waiter.rs @@ -1,7 +1,7 @@ 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::{ @@ -9,7 +9,6 @@ use starknet::core::types::{ StarknetError, TransactionFinalityStatus, TransactionReceipt, }; use starknet::providers::{Provider, ProviderError}; -use tokio::time::{Instant, Interval}; type GetReceiptResult = Result; type GetReceiptFuture<'a> = Pin + Send + 'a>>; @@ -77,6 +76,31 @@ pub struct TransactionWaiter<'a, P: Provider> { started_at: Option, } +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 @@ -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 } }