Skip to main content

anyswap_sdk/driver/
server.rs

1//! The server as the driver sees it: the calls it makes over the API, and an
2//! optional push channel that only says which swap to read sooner.
3
4use std::{sync::Arc, time::Duration};
5
6use anyswap_core::api::{
7    CancelRequest, CreateSwapRequest, InfoResponse, RevealClaimRequest, RevealPreimageRequest,
8    SwapListQuery, SwapListResponse, SwapState,
9};
10use async_trait::async_trait;
11use uuid::Uuid;
12
13use super::rt::{MaybeSend, MaybeSync, Timeout};
14use crate::client::{ClientError, HttpClient};
15
16impl Timeout for ClientError {
17    fn timeout(limit: Duration) -> Self {
18        Self::Transport(format!("no answer within {limit:?}"))
19    }
20}
21
22/// The server API as the driver uses it. Every decision is taken on what
23/// `get_swap` returns and the chain confirms, so an implementation carries
24/// requests and answers and never interprets them.
25#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
26#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
27pub trait ServerClient: MaybeSend + MaybeSync {
28    async fn get_info(&self) -> Result<InfoResponse, ClientError>;
29    async fn create_swap(&self, req: &CreateSwapRequest) -> Result<(), ClientError>;
30    async fn get_swap(&self, swap_id: Uuid) -> Result<SwapState, ClientError>;
31    async fn list_swaps(&self, query: &SwapListQuery) -> Result<SwapListResponse, ClientError>;
32    async fn cancel(&self, swap_id: Uuid, req: &CancelRequest) -> Result<(), ClientError>;
33    async fn reveal_claim(
34        &self,
35        swap_id: Uuid,
36        req: &RevealClaimRequest,
37    ) -> Result<(), ClientError>;
38    async fn reveal_preimage(
39        &self,
40        swap_id: Uuid,
41        req: &RevealPreimageRequest,
42    ) -> Result<(), ClientError>;
43}
44
45#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
46#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
47impl<C: ServerClient + ?Sized> ServerClient for Arc<C> {
48    async fn get_info(&self) -> Result<InfoResponse, ClientError> {
49        (**self).get_info().await
50    }
51
52    async fn create_swap(&self, req: &CreateSwapRequest) -> Result<(), ClientError> {
53        (**self).create_swap(req).await
54    }
55
56    async fn get_swap(&self, swap_id: Uuid) -> Result<SwapState, ClientError> {
57        (**self).get_swap(swap_id).await
58    }
59
60    async fn list_swaps(&self, query: &SwapListQuery) -> Result<SwapListResponse, ClientError> {
61        (**self).list_swaps(query).await
62    }
63
64    async fn cancel(&self, swap_id: Uuid, req: &CancelRequest) -> Result<(), ClientError> {
65        (**self).cancel(swap_id, req).await
66    }
67
68    async fn reveal_claim(
69        &self,
70        swap_id: Uuid,
71        req: &RevealClaimRequest,
72    ) -> Result<(), ClientError> {
73        (**self).reveal_claim(swap_id, req).await
74    }
75
76    async fn reveal_preimage(
77        &self,
78        swap_id: Uuid,
79        req: &RevealPreimageRequest,
80    ) -> Result<(), ClientError> {
81        (**self).reveal_preimage(swap_id, req).await
82    }
83}
84
85#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
86#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
87impl ServerClient for HttpClient {
88    async fn get_info(&self) -> Result<InfoResponse, ClientError> {
89        HttpClient::get_info(self).await
90    }
91
92    async fn create_swap(&self, req: &CreateSwapRequest) -> Result<(), ClientError> {
93        HttpClient::create_swap(self, req).await
94    }
95
96    async fn get_swap(&self, swap_id: Uuid) -> Result<SwapState, ClientError> {
97        HttpClient::get_swap(self, swap_id).await
98    }
99
100    async fn list_swaps(&self, query: &SwapListQuery) -> Result<SwapListResponse, ClientError> {
101        HttpClient::list_swaps(self, query).await
102    }
103
104    async fn cancel(&self, swap_id: Uuid, req: &CancelRequest) -> Result<(), ClientError> {
105        HttpClient::cancel(self, swap_id, req).await
106    }
107
108    async fn reveal_claim(
109        &self,
110        swap_id: Uuid,
111        req: &RevealClaimRequest,
112    ) -> Result<(), ClientError> {
113        HttpClient::reveal_claim(self, swap_id, req).await
114    }
115
116    async fn reveal_preimage(
117        &self,
118        swap_id: Uuid,
119        req: &RevealPreimageRequest,
120    ) -> Result<(), ClientError> {
121        HttpClient::reveal_preimage(self, swap_id, req).await
122    }
123}
124
125/// A push channel naming swaps worth reading sooner. `None` means the channel
126/// is down; the driver waits `poll_interval` and asks again. It paces the loops
127/// and never feeds them, so an implementation may drop, repeat or invent ids
128/// and change nothing but timing.
129#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
130#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
131pub trait Signals: MaybeSend {
132    async fn next(&mut self) -> Option<Uuid>;
133}