Skip to content

Commit

Permalink
feat: make callback structure mutexless
Browse files Browse the repository at this point in the history
  • Loading branch information
biryukovmaxim committed May 10, 2024
1 parent 6ed7e83 commit b798e09
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/erc20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ impl ERC20Token {

/// Retrieves the token balance of a specified address
pub async fn get_balance(&self, address: String) -> Result<Uint<256, 4>, Error> {
let IERC20::balanceOfReturn { _0 } = self
let IERC20::balanceOfReturn { _0: balance } = self
.contract
.balanceOf(address.parse().unwrap())
.call()
.await?;
Ok(_0)
Ok(balance)
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -47,7 +46,7 @@ pub type Wei = U256;

// Type alias for the invoice callback function
pub type AsyncCallback =
Arc<Mutex<dyn Fn(Invoice) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>>;
Arc<dyn Fn(Invoice) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;

impl PaymentGateway {
/// Creates a new payment gateway.
Expand Down Expand Up @@ -86,9 +85,9 @@ impl PaymentGateway {

// Wrap the callback in Arc<Mutex<>> 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<Box<dyn Future<Output = ()> + Send>>
}));
});

// TODO: When implementing token transfers allow the user to add their gas wallet here.

Expand Down
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,25 @@ mod tests {
types::{Invoice, PaymentMethod},
};

struct Foo {
bar: std::sync::Mutex<i64>,
}

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(),
Expand Down
3 changes: 1 addition & 2 deletions src/poller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b798e09

Please sign in to comment.