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