Skip to main content

ave_common/bridge/
request.rs

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