calimero_context_config/
lib.rs

1#![allow(single_use_lifetimes, reason = "False positive")]
2
3use std::borrow::Cow;
4
5use borsh::{BorshDeserialize, BorshSerialize};
6use serde::{Deserialize, Serialize};
7
8#[cfg(feature = "client")]
9pub mod client;
10#[cfg(feature = "icp")]
11pub mod icp;
12pub mod repr;
13pub mod types;
14
15use repr::Repr;
16use types::{Application, Capability, ContextId, ContextIdentity, ProposalId, SignerId};
17
18pub type Timestamp = u64;
19
20#[derive(Debug, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22#[serde(deny_unknown_fields)]
23#[non_exhaustive]
24pub struct Request<'a> {
25    pub signer_id: Repr<SignerId>,
26    pub nonce: u64,
27
28    #[serde(borrow, flatten)]
29    pub kind: RequestKind<'a>,
30}
31
32impl<'a> Request<'a> {
33    #[must_use]
34    pub fn new(signer_id: SignerId, kind: RequestKind<'a>, nonce: u64) -> Self {
35        Request {
36            signer_id: Repr::new(signer_id),
37            kind,
38            nonce,
39        }
40    }
41}
42
43#[derive(Debug, Serialize, Deserialize)]
44#[serde(tag = "scope", content = "params")]
45#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")]
46pub enum RequestKind<'a> {
47    #[serde(borrow)]
48    Context(ContextRequest<'a>),
49}
50
51#[derive(Debug, Serialize, Deserialize)]
52#[serde(rename_all = "camelCase")]
53#[serde(deny_unknown_fields)]
54#[non_exhaustive]
55pub struct ContextRequest<'a> {
56    pub context_id: Repr<ContextId>,
57
58    #[serde(borrow, flatten)]
59    pub kind: ContextRequestKind<'a>,
60}
61
62impl<'a> ContextRequest<'a> {
63    #[must_use]
64    pub const fn new(context_id: Repr<ContextId>, kind: ContextRequestKind<'a>) -> Self {
65        ContextRequest { context_id, kind }
66    }
67}
68
69#[derive(Debug, Serialize, Deserialize)]
70#[serde(tag = "scope", content = "params")]
71#[serde(deny_unknown_fields)]
72#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")]
73pub enum ContextRequestKind<'a> {
74    Add {
75        author_id: Repr<ContextIdentity>,
76        #[serde(borrow)]
77        application: Application<'a>,
78    },
79    UpdateApplication {
80        #[serde(borrow)]
81        application: Application<'a>,
82    },
83    AddMembers {
84        members: Cow<'a, [Repr<ContextIdentity>]>,
85    },
86    RemoveMembers {
87        members: Cow<'a, [Repr<ContextIdentity>]>,
88    },
89    Grant {
90        capabilities: Cow<'a, [(Repr<ContextIdentity>, Capability)]>,
91    },
92    Revoke {
93        capabilities: Cow<'a, [(Repr<ContextIdentity>, Capability)]>,
94    },
95    UpdateProxyContract,
96}
97
98#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
99#[serde(tag = "scope", content = "params")]
100#[serde(deny_unknown_fields)]
101#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")]
102pub enum SystemRequest {
103    #[serde(rename_all = "camelCase")]
104    SetValidityThreshold { threshold_ms: Timestamp },
105}
106
107/// Proxy contract
108/// TODO: move these to a separate cratexs
109pub type Gas = u64;
110pub type NativeToken = u128;
111
112#[derive(
113    Debug,
114    Clone,
115    PartialEq,
116    Serialize,
117    Deserialize,
118    BorshDeserialize,
119    BorshSerialize,
120    Ord,
121    PartialOrd,
122    Eq,
123)]
124#[serde(tag = "scope", content = "params")]
125#[serde(deny_unknown_fields)]
126#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")]
127pub enum ProposalAction {
128    ExternalFunctionCall {
129        receiver_id: String,
130        method_name: String,
131        args: String,
132        deposit: NativeToken,
133    },
134    Transfer {
135        receiver_id: String,
136        amount: NativeToken,
137    },
138    SetNumApprovals {
139        num_approvals: u32,
140    },
141    SetActiveProposalsLimit {
142        active_proposals_limit: u32,
143    },
144    SetContextValue {
145        key: Box<[u8]>,
146        value: Box<[u8]>,
147    },
148    DeleteProposal {
149        proposal_id: Repr<ProposalId>,
150    },
151}
152
153// The proposal the user makes specifying the receiving account and actions they want to execute (1 tx)
154#[derive(
155    Debug,
156    Clone,
157    PartialEq,
158    Serialize,
159    Deserialize,
160    BorshDeserialize,
161    BorshSerialize,
162    Ord,
163    PartialOrd,
164    Eq,
165)]
166#[serde(deny_unknown_fields)]
167#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")]
168pub struct Proposal {
169    pub id: Repr<ProposalId>,
170    pub author_id: Repr<SignerId>,
171    pub actions: Vec<ProposalAction>,
172}
173
174#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
175#[serde(deny_unknown_fields)]
176pub struct ProposalApprovalWithSigner {
177    pub proposal_id: Repr<ProposalId>,
178    pub signer_id: Repr<SignerId>,
179    pub added_timestamp: u64,
180}
181
182#[derive(Debug, Serialize, Deserialize)]
183#[serde(tag = "scope", content = "params")]
184#[serde(deny_unknown_fields)]
185#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")]
186pub enum ProxyMutateRequest {
187    Propose {
188        proposal: Proposal,
189    },
190    Approve {
191        approval: ProposalApprovalWithSigner,
192    },
193}
194
195#[derive(PartialEq, Serialize, Deserialize, Copy, Clone, Debug)]
196#[serde(deny_unknown_fields)]
197#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")]
198pub struct ProposalWithApprovals {
199    pub proposal_id: Repr<ProposalId>,
200    pub num_approvals: usize,
201}