Browse Source

feat: fake wallet

thesimplekid 5 months ago
parent
commit
065b4e248f
2 changed files with 9 additions and 2 deletions
  1. 1 0
      crates/cdk-fake-wallet/Cargo.toml
  2. 8 2
      crates/cdk-fake-wallet/src/lib.rs

+ 1 - 0
crates/cdk-fake-wallet/Cargo.toml

@@ -15,6 +15,7 @@ bitcoin = { version = "0.32.2", default-features = false }
 cdk = { path = "../cdk", version = "0.4.0", default-features = false, features = ["mint"] }
 futures = { version = "0.3.28", default-features = false }
 tokio = { version = "1", default-features = false }
+tokio-util = { version = "0.7.11", default-features = false }
 tracing = { version = "0.1", default-features = false, features = ["attributes", "log"] }
 thiserror = "1"
 serde = "1"

+ 8 - 2
crates/cdk-fake-wallet/src/lib.rs

@@ -8,6 +8,7 @@
 use std::collections::{HashMap, HashSet};
 use std::pin::Pin;
 use std::str::FromStr;
+use std::sync::atomic::{AtomicBool, Ordering};
 use std::sync::Arc;
 
 use async_trait::async_trait;
@@ -33,6 +34,7 @@ use serde::{Deserialize, Serialize};
 use tokio::sync::Mutex;
 use tokio::time;
 use tokio_stream::wrappers::ReceiverStream;
+use tokio_util::sync::CancellationToken;
 
 pub mod error;
 
@@ -47,6 +49,8 @@ pub struct FakeWallet {
     payment_states: Arc<Mutex<HashMap<String, MeltQuoteState>>>,
     failed_payment_check: Arc<Mutex<HashSet<String>>>,
     payment_delay: u64,
+    wait_invoice_cancel_token: CancellationToken,
+    wait_invoice_is_active: Arc<AtomicBool>,
 }
 
 impl FakeWallet {
@@ -70,6 +74,8 @@ impl FakeWallet {
             payment_states: Arc::new(Mutex::new(payment_states)),
             failed_payment_check: Arc::new(Mutex::new(fail_payment_check)),
             payment_delay,
+            wait_invoice_cancel_token: CancellationToken::new(),
+            wait_invoice_is_active: Arc::new(AtomicBool::new(false)),
         }
     }
 }
@@ -113,11 +119,11 @@ impl MintLightning for FakeWallet {
     }
 
     fn is_wait_invoice_active(&self) -> bool {
-        todo!()
+        self.wait_invoice_is_active.load(Ordering::SeqCst)
     }
 
     fn cancel_wait_invoice(&self) {
-        todo!()
+        self.wait_invoice_cancel_token.cancel()
     }
 
     async fn wait_any_invoice(