@@ -18,6 +18,7 @@ use super::model::PairLimits;
1818use crate :: arkln:: Lightning ;
1919use crate :: arkln:: RcvOptions ;
2020use crate :: arkln:: SentOptions ;
21+ use crate :: boltz:: boltz_ws:: SwapUpdate ;
2122use crate :: ldk:: bolt11_invoice as invoice;
2223use crate :: ldk:: offers;
2324use anyhow:: Ok ;
@@ -27,6 +28,7 @@ use std::collections::HashMap;
2728use std:: future:: Future ;
2829use std:: sync:: Arc ;
2930use tokio:: sync:: RwLock ;
31+ use tokio_tungstenite:: tungstenite:: http:: status;
3032
3133#[ derive( Debug , Clone , Copy ) ]
3234pub enum Network {
@@ -52,6 +54,8 @@ pub struct BoltzLightning {
5254 _network : Network ,
5355 api_url : String ,
5456 ws_client : Arc < RwLock < BoltzWebSocketClient > > ,
57+
58+ receiver : lampo_common:: event:: Subscriber < super :: boltz_ws:: SwapUpdate > ,
5559}
5660
5761impl BoltzLightning {
@@ -62,11 +66,13 @@ impl BoltzLightning {
6266 let mut ws_client = BoltzWebSocketClient :: new ( network. clone ( ) ) ;
6367 ws_client. connect ( ) . await ?;
6468
69+ let receiver = ws_client. subscribe ( ) ;
6570 Ok ( Self {
6671 client,
6772 _network : network,
6873 api_url,
6974 ws_client : Arc :: new ( RwLock :: new ( ws_client) ) ,
75+ receiver,
7076 } )
7177 }
7278
@@ -84,6 +90,57 @@ impl BoltzLightning {
8490 Self :: new ( network) . await
8591 }
8692
93+ pub async fn spawn ( self : Arc < Self > ) {
94+ let this = self . clone ( ) ;
95+ tokio:: spawn ( async move {
96+ let mut receiver = this. receiver . subscribe ( ) ;
97+ while let Some ( SwapUpdate { id, status } ) = receiver. recv ( ) . await {
98+ match status {
99+ SwapStatus :: Created => {
100+ // Check if it is inside the storage!
101+ println ! ( "Swap {} created!" , id) ;
102+ }
103+ SwapStatus :: TransactionMempool => {
104+ // Log it
105+ println ! ( "Swap {} transaction in mempool!" , id) ;
106+ }
107+ SwapStatus :: TransactionConfirmed => {
108+ // make a double check with what we see on chain or in the virtual mempool
109+ println ! ( "Swap {} failed!" , id) ;
110+ }
111+ SwapStatus :: InvoiceSet => {
112+ println ! ( "Swap {} invoice settled!" , id) ;
113+ }
114+ SwapStatus :: InvoicePending => {
115+ println ! ( "Swap {} invoice pending!" , id) ;
116+ }
117+ SwapStatus :: InvoicePaid => {
118+ println ! ( "Swap {} invoice paid!" , id) ;
119+ // TODO: we should claim vthlc here!
120+ }
121+ SwapStatus :: InvoiceFailedToPay => {
122+ println ! ( "Swap {} invoice failed to pay!" , id) ;
123+ // We should drop the swap from the storage, and probably keep track
124+ // somehow in the failure
125+ }
126+ SwapStatus :: TransactionClaimed => {
127+ println ! ( "Swap {} transaction claimed!" , id) ;
128+ }
129+ SwapStatus :: SwapExpired => {
130+ println ! ( "Swap {} expired!" , id) ;
131+ // We should drop the swap from the storage, and probably keep track
132+ // somehow in the failure
133+ }
134+ SwapStatus :: Error { error } => {
135+ println ! ( "Swap {} error: {}" , id, error) ;
136+ // We should drop the swap from the storage, and probably keep track
137+ // somehow in the failure
138+ }
139+ }
140+ }
141+ } ) ;
142+ }
143+
87144 pub async fn get_limits ( & self ) -> Result < HashMap < String , PairLimits > > {
88145 let url = format ! ( "{}/v2/swap/submarine" , self . api_url) ;
89146 let response = self . client . get ( & url) . send ( ) . await ?;
0 commit comments