express_relay_api_types/
ws.rs

1use {
2    crate::{
3        bid::{
4            BidCancel,
5            BidCreate,
6            BidResult,
7            BidStatusWithId,
8        },
9        opportunity::{
10            Opportunity,
11            OpportunityDelete,
12        },
13        ChainId,
14        Routable,
15        SvmChainUpdate,
16    },
17    http::Method,
18    serde::{
19        Deserialize,
20        Serialize,
21    },
22    strum::AsRefStr,
23    utoipa::ToSchema,
24};
25
26
27#[derive(Deserialize, Clone, ToSchema, Serialize)]
28#[serde(tag = "method", content = "params")]
29pub enum ClientMessage {
30    #[serde(rename = "subscribe")]
31    Subscribe {
32        #[schema(value_type = Vec<String>)]
33        chain_ids: Vec<ChainId>,
34    },
35    #[serde(rename = "unsubscribe")]
36    Unsubscribe {
37        #[schema(value_type = Vec<String>)]
38        chain_ids: Vec<ChainId>,
39    },
40    #[serde(rename = "post_bid")]
41    PostBid { bid: BidCreate },
42
43    #[serde(rename = "cancel_bid")]
44    CancelBid { data: BidCancel },
45}
46
47#[derive(Deserialize, Clone, ToSchema, Serialize)]
48pub struct ClientRequest {
49    pub id:  String,
50    #[serde(flatten)]
51    pub msg: ClientMessage,
52}
53
54/// This enum is used to send an update to the client for any subscriptions made.
55#[derive(Serialize, Clone, ToSchema, Deserialize, Debug)]
56#[serde(tag = "type")]
57#[allow(clippy::large_enum_variant)]
58pub enum ServerUpdateResponse {
59    #[serde(rename = "new_opportunity")]
60    NewOpportunity { opportunity: Opportunity },
61    #[serde(rename = "bid_status_update")]
62    BidStatusUpdate { status: BidStatusWithId },
63    #[serde(rename = "svm_chain_update")]
64    SvmChainUpdate { update: SvmChainUpdate },
65    #[serde(rename = "remove_opportunities")]
66    RemoveOpportunities {
67        opportunity_delete: OpportunityDelete,
68    },
69}
70
71#[derive(Serialize, Clone, ToSchema, Deserialize, Debug)]
72#[serde(untagged)]
73pub enum APIResponse {
74    BidResult(BidResult),
75}
76#[derive(Serialize, Clone, ToSchema, Deserialize, Debug)]
77#[serde(tag = "status", content = "result")]
78pub enum ServerResultMessage {
79    #[serde(rename = "success")]
80    Success(Option<APIResponse>),
81    #[serde(rename = "error")]
82    Err(String),
83}
84
85/// This enum is used to send the result for a specific client request with the same id.
86/// Id is only None when the client message is invalid.
87#[derive(Serialize, ToSchema, Deserialize, Clone, Debug)]
88pub struct ServerResultResponse {
89    pub id:     Option<String>,
90    #[serde(flatten)]
91    pub result: ServerResultMessage,
92}
93
94#[derive(AsRefStr, Clone)]
95#[strum(prefix = "/")]
96pub enum Route {
97    #[strum(serialize = "ws")]
98    Ws,
99}
100
101impl Routable for Route {
102    fn properties(&self) -> crate::RouteProperties {
103        let full_path = format!("{}{}", crate::Route::V1.as_ref(), self.as_ref())
104            .trim_end_matches('/')
105            .to_string();
106        match self {
107            Route::Ws => crate::RouteProperties {
108                access_level: crate::AccessLevel::Public,
109                method: Method::GET,
110                full_path,
111            },
112        }
113    }
114}