diff --git a/src/erc20/mod.rs b/src/erc20/mod.rs index 3c961ae..1f1fb6c 100644 --- a/src/erc20/mod.rs +++ b/src/erc20/mod.rs @@ -27,12 +27,12 @@ impl ERC20Token { /// Retrieves the token balance of a specified address pub async fn get_balance(&self, address: String) -> Result, Error> { - let IERC20::balanceOfReturn { _0 } = self + let IERC20::balanceOfReturn { _0: balance } = self .contract .balanceOf(address.parse().unwrap()) .call() .await?; - Ok(_0) + Ok(balance) } } diff --git a/src/gateway/mod.rs b/src/gateway/mod.rs index 5101af0..460b3a5 100644 --- a/src/gateway/mod.rs +++ b/src/gateway/mod.rs @@ -9,7 +9,6 @@ use alloy::{ }; use reqwest::{Client, Url}; use sled::Tree; -use tokio::sync::Mutex; use crate::{ common::{get_unix_time_millis, get_unix_time_seconds, DatabaseError}, @@ -47,7 +46,7 @@ pub type Wei = U256; // Type alias for the invoice callback function pub type AsyncCallback = - Arc Pin + Send>> + Send + Sync>>; + Arc Pin + Send>> + Send + Sync>; impl PaymentGateway { /// Creates a new payment gateway. @@ -86,9 +85,9 @@ impl PaymentGateway { // Wrap the callback in Arc> to allow sharing across threads and state mutation // We have to create a pinned box to prevent the future from being moved around in heap memory. - let callback = Arc::new(Mutex::new(move |invoice: Invoice| { + let callback = Arc::new(move |invoice: Invoice| { Box::pin(callback(invoice)) as Pin + Send>> - })); + }); // TODO: When implementing token transfers allow the user to add their gas wallet here. diff --git a/src/lib.rs b/src/lib.rs index e44f9a2..5b0a24b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,8 +19,25 @@ mod tests { types::{Invoice, PaymentMethod}, }; + struct Foo { + bar: std::sync::Mutex, + } + + impl Foo { + async fn increase(&self) { + *self.bar.lock().unwrap() += 1; + } + } + fn setup_test_gateway(db_path: &str) -> PaymentGateway { - async fn callback(_invoice: Invoice) {} + let foo = std::sync::Arc::new(Foo { + bar: Default::default(), + }); + let foo_clone = foo.clone(); + let callback = move |_| { + let foo = foo_clone.clone(); + async move { foo.increase().await } + }; PaymentGateway::new( "https://123.com", "0xdac17f958d2ee523a2206206994597c13d831ec7".to_string(), diff --git a/src/poller/mod.rs b/src/poller/mod.rs index e33e33d..1f2644f 100644 --- a/src/poller/mod.rs +++ b/src/poller/mod.rs @@ -133,8 +133,7 @@ pub async fn poll_payments(gateway: PaymentGateway) { delete_invoice(&gateway.tree, entry.0).await; let mut invoice = entry.1; invoice.paid_at_timestamp = get_unix_time_seconds(); - let lock = gateway.config.callback.lock().await; - (*lock)(invoice).await; // Execute callback function + (gateway.config.callback)(invoice).await;// Execute callback function } // To prevent rate limitations on certain Web3 RPC's we sleep here for the specified amount. tokio::time::sleep(std::time::Duration::from_millis(