anyswap-sdk 1.2.0

Rust client SDK for AnySwap: swaps between Lightning and on-chain Bitcoin or Liquid, with a driver that runs them to completion
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! A driver that runs a swap to completion against the chain, taking from the
//! server only what the chain can verify: its cancel report, its preimage and
//! its key. The loop is in `tick.rs`, what fixes a swap's terms in
//! `accept.rs`, what survives a crash in `record.rs`, the knobs in
//! `config.rs`, and what an integrator implements for the chain in `chain.rs`
//! and for the server in `server.rs`. See `docs/proposals/driver.md`.

mod accept;
mod chain;
mod config;
mod error;
mod record;
mod rt;
mod server;
mod tick;

use std::{
    collections::HashMap,
    future::Future,
    ops::{Deref, DerefMut},
    sync::{Arc, Mutex, OnceLock, Weak},
    time::Duration,
};

use anyswap_core::{
    api::{CancelRequest, CreateSwapRequest},
    invoice,
    types::{Chain, SwapType},
    utils::{now, validate_user_address},
};
use tokio::sync::broadcast::{self, error::RecvError};
use uuid::Uuid;

use self::tick::ServerStatus as Reported;
pub use self::{
    accept::{Quote, RestoreReport, TargetAmount},
    chain::{ChainClient, ChainError, HistoryTx},
    config::{ChainConfig, Config},
    error::{DriverError, Refusal},
    record::{
        Endpoint, Height, MemoryStore, Sent, Status, StoreError, SwapRecord, SwapStore, Timestamp,
    },
    rt::{MaybeSend, MaybeSync, spawn},
    server::{ServerClient, Signals},
    tick::{
        Action, Decision, Message, Observed, Output, Path, Server, ServerStatus, Spend, Spender,
        decide,
    },
};
use crate::{
    context::SwapContext,
    swap_in::SwapInStatus,
    swap_out::SwapOutStatus,
    swap_script::Side,
    wallet::{SwapWallet, SwapWalletExt},
};

pub(crate) fn wallet_error(e: impl std::fmt::Display) -> DriverError {
    DriverError::Wallet(e.to_string())
}

#[derive(Clone)]
pub(crate) struct Registered {
    client: Arc<dyn ChainClient>,
    config: ChainConfig,
}

pub(crate) struct Core<W: SwapWallet> {
    ctx: SwapContext<W>,
    client: Arc<dyn ServerClient>,
    store: Arc<dyn SwapStore>,
    locks: Mutex<HashMap<Uuid, Arc<tokio::sync::Mutex<()>>>>,
    errors: Mutex<HashMap<Uuid, Arc<DriverError>>>,
    signals: broadcast::Sender<Uuid>,
    updates: broadcast::Sender<Swap<W>>,
    /// The push channel, until the listener takes it.
    source: Mutex<Option<Box<dyn Signals>>>,
    listener: OnceLock<()>,
}

/// Drives every swap in its store. Cheap to clone; every clone shares the
/// store, the server client and the loop's channels.
pub struct Driver<W: SwapWallet> {
    core: Arc<Core<W>>,
    chains: HashMap<Chain, Registered>,
    config: Config,
    destinations: Option<Arc<dyn Destinations>>,
}

/// Where funds land when a swap is created without an address.
pub trait Destinations: MaybeSend + MaybeSync {
    /// The address a SwapOut on `chain` claims to; asked at creation.
    fn claim_address(&self, chain: Chain) -> String;
    /// The address a SwapIn on `chain` refunds to; asked once per broadcast,
    /// only with something spendable under the script.
    fn refund_address(&self, chain: Chain) -> String;
}

impl<W: SwapWallet> Clone for Driver<W> {
    fn clone(&self) -> Self {
        Self {
            core: self.core.clone(),
            chains: self.chains.clone(),
            config: self.config.clone(),
            destinations: self.destinations.clone(),
        }
    }
}

impl<W: SwapWallet + MaybeSend + MaybeSync + 'static> Driver<W> {
    pub fn new(
        ctx: SwapContext<W>,
        client: impl ServerClient + 'static,
        store: impl SwapStore + 'static,
    ) -> Self {
        Self {
            core: Arc::new(Core {
                ctx,
                client: Arc::new(client),
                store: Arc::new(store),
                locks: Mutex::default(),
                errors: Mutex::default(),
                signals: broadcast::channel(64).0,
                updates: broadcast::channel(256).0,
                source: Mutex::new(None),
                listener: OnceLock::new(),
            }),
            chains: HashMap::new(),
            config: Config::default(),
            destinations: None,
        }
    }

    /// Registers a chain with its policy; a chain without one cannot be swapped on.
    pub fn with_chain(
        mut self,
        chain: Chain,
        client: impl ChainClient + 'static,
        config: ChainConfig,
    ) -> Self {
        self.chains.insert(
            chain,
            Registered {
                client: Arc::new(client),
                config,
            },
        );
        self
    }

    pub fn with_config(mut self, config: Config) -> Self {
        self.config = config;
        self
    }

    pub fn with_destinations(mut self, destinations: impl Destinations + 'static) -> Self {
        self.destinations = Some(Arc::new(destinations));
        self
    }

    /// Takes a push channel to pace the loops by; without one they pace on
    /// `poll_interval` alone. `HttpClient::signals` is the server's WebSocket.
    pub fn with_signals(self, signals: impl Signals + 'static) -> Self {
        *self.core.source.lock().unwrap() = Some(Box::new(signals));
        self
    }

    /// Wakes any loop waiting on `swap_id`, as a signal from the channel
    /// would. For a push source the driver cannot pump itself, a webhook
    /// receiver or a browser's socket.
    pub fn signal(&self, swap_id: Uuid) {
        let _ = self.core.signals.send(swap_id);
    }

    /// A quote for a swap from `source` to `dest`, an end being Lightning
    /// when its chain is `None`, pinned by the amount the user receives or
    /// the amount they spend. Pinned by what they spend, the terms are
    /// inverted against the one read of `/v1/info` the quote comes from.
    pub async fn quote(
        &self,
        source: Option<Chain>,
        dest: Option<Chain>,
        target: TargetAmount,
    ) -> Result<Quote, DriverError> {
        for chain in [source, dest].into_iter().flatten() {
            self.chain(chain)?;
        }
        let info = self.core.client.get_info().await?;
        let policy_asset = self.core.ctx.liquid().map(|liquid| liquid.policy_asset);
        accept::quote(
            info,
            self.core.ctx.network(),
            policy_asset,
            source,
            dest,
            target,
            now(),
        )
    }

    /// Creates a SwapIn against `quote` with the invoice minted for its
    /// `receive_amount`. The handle carries where and how much to deposit.
    /// A `refund_address` is signed with the request and paid by any refund;
    /// without one the destinations hook is asked when there is something to
    /// take, or `refund` is called with one.
    pub async fn swap_in(
        &self,
        quote: Quote,
        claim_invoice: String,
        refund_address: Option<String>,
    ) -> Result<SwapInHandle<W>, DriverError> {
        let chain = self.check_quote(&quote, SwapType::SwapIn)?;
        let msat =
            invoice::amount_msat(&claim_invoice).map_err(|e| Refusal::Invoice(e.to_string()))?;
        if Some(msat) != quote.receive_amount.checked_mul(1000) {
            return Err(Refusal::Invoice("amount is not the quoted one".into()).into());
        }
        if let Some(refund_address) = &refund_address {
            validate_user_address(
                "user_refund_address",
                chain,
                refund_address,
                self.core.ctx.network(),
            )
            .map_err(|e| Refusal::Address(e.to_string()))?;
        }
        let wallet = self.core.ctx.wallet();
        let swap_id = Uuid::new_v4();
        let liquid = chain == Chain::Liquid;
        let req = CreateSwapRequest {
            protocol_version: quote.protocol_version,
            swap_id,
            user_id: wallet.user_id().map_err(wallet_error)?,
            source_chain: quote.source_chain,
            source_asset_id: quote.source_asset_id,
            dest_chain: quote.dest_chain,
            dest_asset_id: quote.dest_asset_id,
            receive_amount: quote.receive_amount,
            send_amount: quote.send_amount,
            quote_id: Some(quote.quote_id),
            claim_invoice: Some(claim_invoice),
            source_refund_pubkey: Some(wallet.pubkey(&swap_id).map_err(wallet_error)?.to_string()),
            source_blinding_pubkey: liquid
                .then(|| {
                    wallet
                        .source_blinding_pubkey(&swap_id)
                        .map(|k| k.to_string())
                })
                .transpose()
                .map_err(wallet_error)?,
            dest_claim_pubkey: None,
            dest_blinding_pubkey: None,
            user_claim_address: None,
            user_refund_address: refund_address,
            payment_hash: None,
            webhook: None,
        };
        let started_at = now();
        self.core.client.create_swap(&req).await?;
        let (record, status) = self.adopt(&req, started_at).await?;
        Ok(SwapInHandle {
            handle: Handle::new(self.clone(), record, Some(status)),
        })
    }

    /// Creates a SwapOut against `quote` paying `claim_address`, or the
    /// destinations hook's address without one. The handle carries the hold
    /// invoice to pay.
    pub async fn swap_out(
        &self,
        quote: Quote,
        claim_address: Option<String>,
    ) -> Result<SwapOutHandle<W>, DriverError> {
        let chain = self.check_quote(&quote, SwapType::SwapOut)?;
        let claim_address = claim_address
            .or_else(|| self.destinations.as_ref().map(|d| d.claim_address(chain)))
            .ok_or_else(|| Refusal::Address("none given".into()))?;
        validate_user_address(
            "user_claim_address",
            chain,
            &claim_address,
            self.core.ctx.network(),
        )
        .map_err(|e| Refusal::Address(e.to_string()))?;
        let wallet = self.core.ctx.wallet();
        let swap_id = Uuid::new_v4();
        let liquid = chain == Chain::Liquid;
        let req = CreateSwapRequest {
            protocol_version: quote.protocol_version,
            swap_id,
            user_id: wallet.user_id().map_err(wallet_error)?,
            source_chain: quote.source_chain,
            source_asset_id: quote.source_asset_id,
            dest_chain: quote.dest_chain,
            dest_asset_id: quote.dest_asset_id,
            receive_amount: quote.receive_amount,
            send_amount: quote.send_amount,
            quote_id: Some(quote.quote_id),
            claim_invoice: None,
            source_refund_pubkey: None,
            source_blinding_pubkey: None,
            dest_claim_pubkey: Some(wallet.pubkey(&swap_id).map_err(wallet_error)?.to_string()),
            dest_blinding_pubkey: liquid
                .then(|| wallet.dest_blinding_pubkey(&swap_id).map(|k| k.to_string()))
                .transpose()
                .map_err(wallet_error)?,
            user_claim_address: Some(claim_address),
            user_refund_address: None,
            payment_hash: Some(hex::encode(
                wallet.payment_hash(&swap_id).map_err(wallet_error)?,
            )),
            webhook: None,
        };
        let started_at = now();
        self.core.client.create_swap(&req).await?;
        let (record, status) = self.adopt(&req, started_at).await?;
        Ok(SwapOutHandle {
            handle: Handle::new(self.clone(), record, Some(status)),
        })
    }

    /// The stored swap, as a fresh handle.
    pub async fn get(&self, swap_id: Uuid) -> Result<Option<Swap<W>>, DriverError> {
        self.core
            .store
            .load(&swap_id)
            .await?
            .map(|record| Swap::new(self.clone(), record, None))
            .transpose()
    }

    /// Drives every swap in the store that is not quiet, forever. Spawn it.
    pub fn run(&self) -> impl Future<Output = ()> + MaybeSend + 'static {
        let driver = self.clone();
        async move {
            driver.ensure_listener();
            let mut signals = driver.core.signals.subscribe();
            loop {
                driver.sweep().await;
                tokio::select! {
                    _ = rt::sleep(driver.config.poll_interval) => {}
                    _ = signals.recv() => {}
                }
            }
        }
    }

    /// Every swap a pass changed, from `run` and from `step`.
    pub fn subscribe(&self) -> Updates<W> {
        Updates(self.core.updates.subscribe())
    }

    async fn sweep(&self) {
        let Ok(records) = self.core.store.list().await else {
            return;
        };
        let now = now();
        for record in records {
            if record.status.is_quiet(now) {
                continue;
            }
            let id = record.swap_id;
            let _guard = self.lock(id).await;
            let Ok(Some(mut record)) = self.core.store.load(&id).await else {
                continue;
            };
            if let Err(error) = self.tick(&mut record).await {
                self.core.errors.lock().unwrap().insert(id, Arc::new(error));
                self.emit(record, None);
            }
        }
    }

    async fn adopt(
        &self,
        req: &CreateSwapRequest,
        started_at: Timestamp,
    ) -> Result<(SwapRecord, Reported), DriverError> {
        let state = self.core.client.get_swap(req.swap_id).await?;
        match self.accept(req, started_at, state) {
            Ok((record, status)) => {
                self.core.store.save(&record).await?;
                Ok((record, status))
            }
            Err(error) => {
                let cancel = CancelRequest {
                    cancel_message: error.to_string(),
                };
                let _ = self.core.client.cancel(req.swap_id, &cancel).await;
                Err(error)
            }
        }
    }

    /// The quote's on-chain end, once the quote is for `swap_type` and fresh.
    fn check_quote(&self, quote: &Quote, swap_type: SwapType) -> Result<Chain, DriverError> {
        let chain = match (swap_type, quote.source_chain, quote.dest_chain) {
            (SwapType::SwapIn, Some(chain), None) | (SwapType::SwapOut, None, Some(chain)) => chain,
            _ => return Err(DriverError::WrongSwapType),
        };
        self.chain(chain)?;
        if now() >= quote.expires_at {
            return Err(DriverError::QuoteExpired);
        }
        Ok(chain)
    }

    fn chain(&self, chain: Chain) -> Result<&Registered, DriverError> {
        self.chains
            .get(&chain)
            .ok_or(DriverError::ChainNotConfigured(chain))
    }

    async fn lock(&self, swap_id: Uuid) -> tokio::sync::OwnedMutexGuard<()> {
        let lock = self
            .core
            .locks
            .lock()
            .unwrap()
            .entry(swap_id)
            .or_default()
            .clone();
        lock.lock_owned().await
    }

    fn emit(&self, record: SwapRecord, server: Option<Reported>) {
        if let Ok(swap) = Swap::new(self.clone(), record, server) {
            let _ = self.core.updates.send(swap);
        }
    }

    /// Starts pumping the push channel once per driver. Its signals only say
    /// that reading sooner is worthwhile; the loops are paced by them and
    /// never fed by them.
    fn ensure_listener(&self) {
        self.core.listener.get_or_init(|| {
            if let Some(source) = self.core.source.lock().unwrap().take() {
                rt::spawn(listen(
                    Arc::downgrade(&self.core),
                    source,
                    self.config.poll_interval,
                ));
            }
        });
    }

    /// A signal for `swap_id` or `poll_interval`, whichever comes first.
    async fn wait_for(&self, swap_id: Uuid) {
        self.ensure_listener();
        let mut signals = self.core.signals.subscribe();
        let sleep = rt::sleep(self.config.poll_interval);
        tokio::pin!(sleep);
        loop {
            tokio::select! {
                _ = &mut sleep => return,
                signal = signals.recv() => match signal {
                    Ok(id) if id == swap_id => return,
                    Ok(_) | Err(RecvError::Lagged(_)) => continue,
                    Err(RecvError::Closed) => return,
                },
            }
        }
    }
}

/// Forwards each id the channel yields to the loops, waiting `retry` while
/// it is down, until the driver is dropped.
async fn listen<W: SwapWallet + MaybeSend + MaybeSync + 'static>(
    core: Weak<Core<W>>,
    mut source: Box<dyn Signals>,
    retry: Duration,
) {
    loop {
        match source.next().await {
            Some(id) => {
                let Some(core) = core.upgrade() else {
                    return;
                };
                let _ = core.signals.send(id);
            }
            None => {
                if core.strong_count() == 0 {
                    return;
                }
                rt::sleep(retry).await;
            }
        }
    }
}

/// The answer to `cancel`: the swap closes when the server confirms it, and
/// `TooLate` means the payment landed first and the swap continues.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cancellation {
    Requested,
    TooLate,
}

/// A swap bound to its driver: the record as this handle last saw it, and
/// the calls that act on it.
pub struct Handle<W: SwapWallet> {
    driver: Driver<W>,
    record: SwapRecord,
    server: Option<ServerStatus>,
    error: Option<Arc<DriverError>>,
}

impl<W: SwapWallet> Clone for Handle<W> {
    fn clone(&self) -> Self {
        Self {
            driver: self.driver.clone(),
            record: self.record.clone(),
            server: self.server.clone(),
            error: self.error.clone(),
        }
    }
}

impl<W: SwapWallet + MaybeSend + MaybeSync + 'static> Handle<W> {
    fn new(driver: Driver<W>, record: SwapRecord, server: Option<ServerStatus>) -> Self {
        let error = driver
            .core
            .errors
            .lock()
            .unwrap()
            .get(&record.swap_id)
            .cloned();
        Self {
            driver,
            record,
            server,
            error,
        }
    }

    pub fn swap_id(&self) -> Uuid {
        self.record.swap_id
    }

    pub fn record(&self) -> &SwapRecord {
        &self.record
    }

    pub fn status(&self) -> &Status {
        &self.record.status
    }

    /// The server's status as the last pass read it. Shown, never decided on.
    pub fn server_status(&self) -> Option<&ServerStatus> {
        self.server.as_ref()
    }

    /// The error of this swap's last failing tick under `run`, `None` after a
    /// working one; read when the handle was made.
    pub fn error(&self) -> Option<&DriverError> {
        self.error.as_deref()
    }

    /// One pass, now.
    pub async fn step(&mut self) -> Result<Status, DriverError> {
        let id = self.record.swap_id;
        let _guard = self.driver.lock(id).await;
        self.record = self
            .driver
            .core
            .store
            .load(&id)
            .await?
            .ok_or(DriverError::Unknown(id))?;
        self.server = self.driver.tick(&mut self.record).await?;
        self.error = None;
        Ok(self.record.status.clone())
    }

    /// Waits for a server notification or `poll_interval`, whichever comes
    /// first, then makes one pass.
    pub async fn advance(&mut self) -> Result<Status, DriverError> {
        self.driver.wait_for(self.record.swap_id).await;
        self.step().await
    }

    pub async fn cancel(&mut self) -> Result<Cancellation, DriverError> {
        let id = self.record.swap_id;
        let _guard = self.driver.lock(id).await;
        if let Some(record) = self.driver.core.store.load(&id).await? {
            self.record = record;
        }
        let open = matches!(
            self.record.status,
            Status::Funding { .. } | Status::Confirming { .. }
        );
        if !open || self.record.committed_at.is_some() {
            return Ok(Cancellation::TooLate);
        }
        let req = CancelRequest {
            cancel_message: "canceled by the user".into(),
        };
        match self.driver.core.client.cancel(id, &req).await {
            Ok(()) => {
                self.record.sent.cancel = true;
                self.driver.core.store.save(&self.record).await?;
                Ok(Cancellation::Requested)
            }
            Err(e) if e.transition_rejected() => Ok(Cancellation::TooLate),
            Err(e) => Err(e.into()),
        }
    }
}

/// A SwapIn: what the user must deposit, and where.
pub struct SwapInHandle<W: SwapWallet> {
    handle: Handle<W>,
}

impl<W: SwapWallet + MaybeSend + MaybeSync + 'static> SwapInHandle<W> {
    /// The deposit address; `record().send_amount` goes here.
    pub fn address(&self) -> Result<String, DriverError> {
        let terms = self.handle.record.terms(Side::Source)?;
        let script = self.handle.driver.core.ctx.script(&terms)?;
        Ok(script.address())
    }

    pub fn server_status(&self) -> Option<&SwapInStatus> {
        match self.handle.server.as_ref()? {
            ServerStatus::In(status) => Some(status),
            ServerStatus::Out(_) => None,
        }
    }

    /// Takes every output that is ours, spendable and worth its fee to
    /// `destination`. `None` when there is nothing to spend yet.
    pub async fn refund(&self, destination: &str) -> Result<Option<String>, DriverError> {
        let id = self.record.swap_id;
        let _guard = self.driver.lock(id).await;
        let record = self
            .driver
            .core
            .store
            .load(&id)
            .await?
            .ok_or(DriverError::Unknown(id))?;
        self.driver.refund(&record, destination).await
    }
}

/// A SwapOut: the hold invoice the user must pay.
pub struct SwapOutHandle<W: SwapWallet> {
    handle: Handle<W>,
}

impl<W: SwapWallet + MaybeSend + MaybeSync + 'static> SwapOutHandle<W> {
    /// The hold invoice to pay; `record().send_amount` is its amount.
    pub fn claim_invoice(&self) -> &str {
        self.handle
            .record
            .source
            .invoice
            .as_deref()
            .unwrap_or_default()
    }

    pub fn server_status(&self) -> Option<&SwapOutStatus> {
        match self.handle.server.as_ref()? {
            ServerStatus::Out(status) => Some(status),
            ServerStatus::In(_) => None,
        }
    }
}

pub enum Swap<W: SwapWallet> {
    In(SwapInHandle<W>),
    Out(SwapOutHandle<W>),
}

impl<W: SwapWallet + MaybeSend + MaybeSync + 'static> Swap<W> {
    fn new(
        driver: Driver<W>,
        record: SwapRecord,
        server: Option<ServerStatus>,
    ) -> Result<Self, DriverError> {
        let swap_type = record.swap_type()?;
        let handle = Handle::new(driver, record, server);
        match swap_type {
            SwapType::SwapIn => Ok(Self::In(SwapInHandle { handle })),
            SwapType::SwapOut => Ok(Self::Out(SwapOutHandle { handle })),
            other => Err(DriverError::Unsupported(other)),
        }
    }
}

macro_rules! deref_handle {
    ($type:ident, |$self:ident| $handle:expr, |$self_mut:ident| $handle_mut:expr) => {
        impl<W: SwapWallet> Deref for $type<W> {
            type Target = Handle<W>;

            fn deref(&$self) -> &Handle<W> {
                $handle
            }
        }

        impl<W: SwapWallet> DerefMut for $type<W> {
            fn deref_mut(&mut $self_mut) -> &mut Handle<W> {
                $handle_mut
            }
        }

        impl<W: SwapWallet> Clone for $type<W> {
            fn clone(&self) -> Self {
                self.clone_handle()
            }
        }
    };
}

deref_handle!(SwapInHandle, |self| &self.handle, |self| &mut self.handle);
deref_handle!(SwapOutHandle, |self| &self.handle, |self| &mut self.handle);
deref_handle!(
    Swap,
    |self| match self {
        Swap::In(swap) => &swap.handle,
        Swap::Out(swap) => &swap.handle,
    },
    |self| match self {
        Swap::In(swap) => &mut swap.handle,
        Swap::Out(swap) => &mut swap.handle,
    }
);

impl<W: SwapWallet> SwapInHandle<W> {
    fn clone_handle(&self) -> Self {
        Self {
            handle: self.handle.clone(),
        }
    }
}

impl<W: SwapWallet> SwapOutHandle<W> {
    fn clone_handle(&self) -> Self {
        Self {
            handle: self.handle.clone(),
        }
    }
}

impl<W: SwapWallet> Swap<W> {
    fn clone_handle(&self) -> Self {
        match self {
            Self::In(swap) => Self::In(swap.clone()),
            Self::Out(swap) => Self::Out(swap.clone()),
        }
    }
}

/// Every swap a pass changed, as `run` and `step` produce them, plus a
/// handle for every failing tick under `run`.
pub struct Updates<W: SwapWallet>(broadcast::Receiver<Swap<W>>);

impl<W: SwapWallet> Updates<W> {
    pub async fn next(&mut self) -> Option<Swap<W>> {
        loop {
            match self.0.recv().await {
                Ok(swap) => return Some(swap),
                Err(RecvError::Lagged(_)) => continue,
                Err(RecvError::Closed) => return None,
            }
        }
    }
}