Skip to main content

ave_common/bridge/
request.rs

1//! Request types for Ave API
2//!
3//! These types are used for communication with the Ave HTTP API
4
5use std::fmt::Display;
6
7use crate::{
8    request::EventRequest, response::TimeRange, signature::BridgeSignature,
9};
10use borsh::{BorshDeserialize, BorshSerialize};
11use serde::{Deserialize, Serialize};
12use serde_json::Value;
13
14#[cfg(feature = "openapi")]
15use utoipa::{IntoParams, ToSchema};
16#[cfg(feature = "typescript")]
17use ts_rs::TS;
18
19#[derive(Debug, Clone, Deserialize, Serialize)]
20#[cfg_attr(feature = "openapi", derive(ToSchema, IntoParams))]
21#[cfg_attr(feature = "openapi", into_params(parameter_in = Query))]
22#[cfg_attr(feature = "typescript", derive(TS))]
23#[cfg_attr(feature = "typescript", ts(export))]
24pub struct SubjectQuery {
25    pub active: Option<bool>,
26    pub schema_id: Option<String>,
27}
28
29#[derive(Debug, Clone, Deserialize, Serialize)]
30#[cfg_attr(feature = "openapi", derive(ToSchema, IntoParams))]
31#[cfg_attr(feature = "openapi", into_params(parameter_in = Query))]
32#[cfg_attr(feature = "typescript", derive(TS))]
33#[cfg_attr(feature = "typescript", ts(export))]
34pub struct GovQuery {
35    pub active: Option<bool>,
36}
37
38#[derive(Debug, Clone, Deserialize, Serialize)]
39#[cfg_attr(feature = "openapi", derive(ToSchema, IntoParams))]
40#[cfg_attr(feature = "openapi", into_params(parameter_in = Query))]
41#[cfg_attr(feature = "typescript", derive(TS))]
42#[cfg_attr(feature = "typescript", ts(export))]
43pub struct ApprovalQuery {
44    pub state: Option<ApprovalState>,
45}
46
47#[derive(Debug, Clone, Deserialize, Serialize)]
48#[cfg_attr(feature = "openapi", derive(ToSchema, IntoParams))]
49#[cfg_attr(feature = "openapi", into_params(parameter_in = Query))]
50#[cfg_attr(feature = "typescript", derive(TS))]
51#[cfg_attr(feature = "typescript", ts(export))]
52pub struct EventsQuery {
53    pub quantity: Option<u64>,
54    pub page: Option<u64>,
55    pub reverse: Option<bool>,
56    #[cfg_attr(feature = "openapi", param(style = DeepObject, explode))]
57    pub event_request_ts: Option<TimeRange>,
58    #[cfg_attr(feature = "openapi", param(style = DeepObject, explode))]
59    pub event_ledger_ts: Option<TimeRange>,
60    #[cfg_attr(feature = "openapi", param(style = DeepObject, explode))]
61    pub sink_ts: Option<TimeRange>,
62    pub event_type: Option<EventRequestType>,
63}
64
65#[derive(Debug, Clone, Deserialize, Serialize)]
66#[cfg_attr(feature = "openapi", derive(ToSchema, IntoParams))]
67#[cfg_attr(feature = "openapi", into_params(parameter_in = Query))]
68#[cfg_attr(feature = "typescript", derive(TS))]
69#[cfg_attr(feature = "typescript", ts(export))]
70pub struct AbortsQuery {
71    pub request_id: Option<String>,
72    pub sn: Option<u64>,
73    pub quantity: Option<u64>,
74    pub page: Option<u64>,
75    pub reverse: Option<bool>,
76}
77
78#[derive(Debug, Clone, Deserialize, Serialize)]
79#[cfg_attr(feature = "openapi", derive(ToSchema, IntoParams))]
80#[cfg_attr(feature = "openapi", into_params(parameter_in = Query))]
81#[cfg_attr(feature = "typescript", derive(TS))]
82#[cfg_attr(feature = "typescript", ts(export))]
83pub struct FirstEndEvents {
84    pub quantity: Option<u64>,
85    pub reverse: Option<bool>,
86    pub event_type: Option<EventRequestType>,
87}
88
89#[derive(Serialize, Deserialize, Debug, Clone)]
90#[cfg_attr(feature = "openapi", derive(ToSchema))]
91#[cfg_attr(feature = "typescript", derive(TS))]
92#[cfg_attr(feature = "typescript", ts(export))]
93#[serde(rename_all = "snake_case")]
94pub enum EventRequestType {
95    Create,
96    Fact,
97    Transfer,
98    Confirm,
99    Reject,
100    Eol,
101}
102
103impl From<&EventRequest> for EventRequestType {
104    fn from(value: &EventRequest) -> Self {
105        match value {
106            EventRequest::Create(..) => Self::Create,
107            EventRequest::Fact(..) => Self::Fact,
108            EventRequest::Transfer(..) => Self::Transfer,
109            EventRequest::Confirm(..) => Self::Confirm,
110            EventRequest::EOL(..) => Self::Eol,
111            EventRequest::Reject(..) => Self::Reject,
112        }
113    }
114}
115
116impl Display for EventRequestType {
117    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118        match self {
119            EventRequestType::Create => write!(f, "create"),
120            EventRequestType::Fact => write!(f, "fact"),
121            EventRequestType::Transfer => write!(f, "transfer"),
122            EventRequestType::Confirm => write!(f, "confirm"),
123            EventRequestType::Reject => write!(f, "reject"),
124            EventRequestType::Eol => write!(f, "eol"),
125        }
126    }
127}
128
129#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
130#[cfg_attr(feature = "openapi", derive(ToSchema))]
131#[cfg_attr(feature = "typescript", derive(TS))]
132#[cfg_attr(feature = "typescript", ts(export))]
133#[serde(rename_all = "snake_case")]
134pub enum ApprovalStateRes {
135    /// Request for approval which is in responded status and accepted
136    Accepted,
137    /// Request for approval which is in responded status and rejected
138    Rejected,
139    /// The approval entity is obsolete.
140    Obsolete,
141}
142
143impl Display for ApprovalStateRes {
144    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
145        let string = match self {
146            ApprovalStateRes::Accepted => "accepted".to_owned(),
147            ApprovalStateRes::Rejected => "rejected".to_owned(),
148            ApprovalStateRes::Obsolete => "obsolete".to_owned(),
149        };
150        write!(f, "{}", string,)
151    }
152}
153
154#[derive(
155    Default,
156    Debug,
157    Clone,
158    Serialize,
159    Deserialize,
160    PartialEq,
161    Eq,
162    BorshDeserialize,
163    BorshSerialize,
164)]
165#[cfg_attr(feature = "openapi", derive(ToSchema))]
166#[cfg_attr(feature = "typescript", derive(TS))]
167#[cfg_attr(feature = "typescript", ts(export))]
168#[serde(rename_all = "snake_case")]
169pub enum ApprovalState {
170    /// The approval entity is pending a response.
171    #[default]
172    Pending,
173    /// Request for approval which is in responded status and accepted
174    Accepted,
175    /// Request for approval which is in responded status and rejected
176    Rejected,
177    /// The approval entity is obsolete.
178    Obsolete,
179}
180
181impl Display for ApprovalState {
182    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183        let string = match self {
184            ApprovalState::Accepted => "accepted".to_owned(),
185            ApprovalState::Rejected => "rejected".to_owned(),
186            ApprovalState::Obsolete => "obsolete".to_owned(),
187            ApprovalState::Pending => "pending".to_owned(),
188        };
189        write!(f, "{}", string,)
190    }
191}
192
193/// Signed event request
194#[derive(Serialize, Deserialize, Debug, Clone)]
195#[cfg_attr(feature = "openapi", derive(ToSchema))]
196#[cfg_attr(feature = "typescript", derive(TS))]
197#[cfg_attr(feature = "typescript", ts(export))]
198pub struct BridgeSignedEventRequest {
199    /// Event request
200    pub request: BridgeEventRequest,
201    /// Signature
202    pub signature: Option<BridgeSignature>,
203}
204
205/// Event request
206#[derive(Serialize, Deserialize, Debug, Clone)]
207#[cfg_attr(feature = "openapi", derive(ToSchema))]
208#[cfg_attr(feature = "typescript", derive(TS))]
209#[cfg_attr(feature = "typescript", ts(export))]
210#[serde(tag = "event", content = "data", rename_all = "snake_case")]
211pub enum BridgeEventRequest {
212    Create(BridgeCreateRequest),
213    Fact(BridgeFactRequest),
214    Transfer(BridgeTransferRequest),
215    Eol(BridgeEOLRequest),
216    Confirm(BridgeConfirmRequest),
217    Reject(BridgeRejectRequest),
218}
219
220#[derive(Serialize, Deserialize, Debug, Clone)]
221#[cfg_attr(feature = "openapi", derive(ToSchema))]
222#[cfg_attr(feature = "typescript", derive(TS))]
223#[cfg_attr(feature = "typescript", ts(export))]
224pub struct BridgeRejectRequest {
225    /// Subject identifier
226    pub subject_id: String,
227}
228
229#[derive(Serialize, Deserialize, Debug, Clone)]
230#[cfg_attr(feature = "openapi", derive(ToSchema))]
231#[cfg_attr(feature = "typescript", derive(TS))]
232#[cfg_attr(feature = "typescript", ts(export))]
233pub struct BridgeCreateRequest {
234    pub name: Option<String>,
235    pub description: Option<String>,
236    /// The identifier of the governance contract
237    pub governance_id: Option<String>,
238    /// The identifier of the schema used to validate the event
239    pub schema_id: String,
240    /// The namespace of the subject
241    pub namespace: Option<String>,
242}
243
244#[derive(Serialize, Deserialize, Debug, Clone)]
245#[cfg_attr(feature = "openapi", derive(ToSchema))]
246#[cfg_attr(feature = "typescript", derive(TS))]
247#[cfg_attr(feature = "typescript", ts(export))]
248pub struct BridgeFactRequest {
249    /// Subject identifier
250    pub subject_id: String,
251    /// Changes to be applied to the subject
252    pub payload: Value,
253}
254
255#[derive(Debug, Clone, Serialize, Deserialize)]
256#[cfg_attr(feature = "openapi", derive(ToSchema))]
257#[cfg_attr(feature = "typescript", derive(TS))]
258#[cfg_attr(feature = "typescript", ts(export))]
259pub struct BridgeTransferRequest {
260    /// Subject identifier
261    pub subject_id: String,
262    /// Public key of the new owner
263    pub new_owner: String,
264}
265
266/// EOL request
267#[derive(Debug, Clone, Serialize, Deserialize)]
268#[cfg_attr(feature = "openapi", derive(ToSchema))]
269#[cfg_attr(feature = "typescript", derive(TS))]
270#[cfg_attr(feature = "typescript", ts(export))]
271pub struct BridgeEOLRequest {
272    /// Subject identifier
273    pub subject_id: String,
274}
275
276#[derive(Debug, Clone, Serialize, Deserialize)]
277#[cfg_attr(feature = "openapi", derive(ToSchema))]
278#[cfg_attr(feature = "typescript", derive(TS))]
279#[cfg_attr(feature = "typescript", ts(export))]
280pub struct BridgeConfirmRequest {
281    /// Subject identifier
282    pub subject_id: String,
283    pub name_old_owner: Option<String>,
284}