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