cap_common/
did.rs

1//! This file contains all of the type definitions used in the candid
2//! files across the different canisters and the services.
3
4use crate::transaction::Event;
5use certified_vars::{Hash, HashTree};
6use ic_kit::candid::{CandidType, Deserialize};
7use ic_kit::ic;
8use ic_kit::Principal;
9use serde::Serialize;
10
11/// The numeric type used to represent a transaction id.
12pub type TransactionId = u64;
13
14/// The principal id of a index canister.
15pub type IndexCanisterId = Principal;
16
17/// The principal id of a root bucket canister.
18pub type RootBucketId = Principal;
19
20/// The principal id of a bucket canister.
21pub type BucketId = Principal;
22
23/// The principal id of a token contract this is integrating Cap.
24pub type TokenContractId = Principal;
25
26/// The principal id of a user.
27pub type UserId = Principal;
28
29/// Hash of an even which is obtained by `Event::hash`
30pub type EventHash = Hash;
31
32#[derive(Serialize, Deserialize, CandidType)]
33pub struct Witness {
34    #[serde(with = "serde_bytes")]
35    certificate: Vec<u8>,
36    #[serde(with = "serde_bytes")]
37    tree: Vec<u8>,
38}
39
40#[derive(Serialize, Deserialize, CandidType)]
41pub struct GetTokenContractRootBucketArg {
42    pub canister: TokenContractId,
43    pub witness: bool,
44}
45
46#[derive(Serialize, Deserialize, CandidType)]
47pub struct GetTokenContractRootBucketResponse {
48    pub canister: Option<RootBucketId>,
49    pub witness: Option<Witness>,
50}
51
52#[derive(Serialize, Deserialize, CandidType)]
53pub struct GetUserRootBucketsArg {
54    pub user: UserId,
55    pub witness: bool,
56}
57
58#[derive(Serialize, Deserialize, CandidType)]
59pub struct GetUserRootBucketsResponse {
60    pub contracts: Vec<RootBucketId>,
61    pub witness: Option<Witness>,
62}
63
64#[derive(Serialize, Deserialize, CandidType)]
65pub struct WithWitnessArg {
66    pub witness: bool,
67}
68
69#[derive(Serialize, Deserialize, CandidType)]
70pub struct GetIndexCanistersResponse {
71    pub canisters: Vec<IndexCanisterId>,
72    pub witness: Option<Witness>,
73}
74
75#[derive(Serialize, Deserialize, CandidType)]
76pub struct GetNextCanistersResponse {
77    pub canisters: Vec<IndexCanisterId>,
78    pub witness: Option<Witness>,
79}
80
81#[derive(Serialize, Deserialize, CandidType)]
82pub struct WithIdArg {
83    pub id: TransactionId,
84    pub witness: bool,
85}
86
87#[derive(Serialize, Deserialize, CandidType)]
88pub enum GetTransactionResponse {
89    Delegate(BucketId, Option<Witness>),
90    Found(Option<Event>, Option<Witness>),
91}
92
93#[derive(Serialize, Deserialize, CandidType)]
94pub struct GetTransactionsArg {
95    pub page: Option<u32>,
96    pub witness: bool,
97}
98
99#[derive(Serialize, Deserialize, CandidType)]
100pub struct GetTransactionsResponse {
101    pub data: Vec<Event>,
102    pub page: u32,
103    pub witness: Option<Witness>,
104}
105
106#[derive(Serialize, CandidType)]
107pub struct GetTransactionsResponseBorrowed<'a> {
108    pub data: Vec<&'a Event>,
109    pub page: u32,
110    pub witness: Option<Witness>,
111}
112
113#[derive(Serialize, Deserialize, CandidType)]
114pub struct GetUserTransactionsArg {
115    pub user: UserId,
116    pub page: Option<u32>,
117    pub witness: bool,
118}
119
120#[derive(Serialize, Deserialize, CandidType)]
121pub struct GetTokenTransactionsArg {
122    pub token_id: u64,
123    pub page: Option<u32>,
124    pub witness: bool,
125}
126
127#[derive(Serialize, Deserialize, CandidType)]
128pub struct GetBucketResponse {
129    pub canister: BucketId,
130    pub witness: Option<Witness>,
131}
132
133impl From<HashTree<'_>> for Witness {
134    fn from(tree: HashTree) -> Self {
135        Self {
136            certificate: ic::data_certificate().unwrap(),
137            tree: serde_cbor::to_vec(&tree).unwrap(),
138        }
139    }
140}
141
142#[derive(Serialize, Deserialize, CandidType)]
143pub struct BucketInitArgs {
144    pub contract: TokenContractId,
145    pub offset: u64,
146    pub next_canisters: Vec<BucketId>,
147}