cap_sdk_core/
bucket.rs

1use ic_kit::ic::call;
2use ic_kit::{Principal, RejectionCode};
3
4use crate::root::RootBucket;
5use cap_common::{
6    GetIndexCanistersResponse, GetTransactionResponse, GetTransactionsArg, GetTransactionsResponse,
7    GetUserTransactionsArg, WithIdArg, WithWitnessArg,
8};
9
10/// A contract-specific bucket canister.
11///
12/// A bucket canister implements storage for its parent contract. The total storage for a given
13/// contract is created using multiple bucket canisters, which are interconnected using a root bucket
14/// and router system. Querying buckets also features pagination.
15#[derive(Copy, Clone)]
16pub struct Bucket(pub(crate) Principal);
17
18impl Bucket {
19    /// Returns the list of canisters which have different pages of data.
20    pub async fn get_next_canisters(&self) -> Result<Vec<Bucket>, (RejectionCode, String)> {
21        let result: (GetIndexCanistersResponse,) = call(
22            self.0,
23            "get_next_canisters",
24            (WithWitnessArg { witness: false },),
25        )
26        .await?;
27
28        Ok(result
29            .0
30            .canisters
31            .iter()
32            .map(|canister| Bucket(*canister))
33            .collect())
34    }
35
36    /// Returns the transaction corresponding to the passed transaction ID.
37    pub async fn get_transaction(
38        &self,
39        id: u64,
40    ) -> Result<GetTransactionResponse, (RejectionCode, String)> {
41        let result: (GetTransactionResponse,) = call(
42            self.0,
43            "get_transaction",
44            (WithIdArg { id, witness: false },),
45        )
46        .await?;
47
48        Ok(result.0)
49    }
50
51    /// Returns all of the transactions for this contract.
52    pub async fn get_transactions(
53        &self,
54        page: Option<u32>,
55    ) -> Result<GetTransactionsResponse, (RejectionCode, String)> {
56        let result: (GetTransactionsResponse,) = call(
57            self.0,
58            "get_transactions",
59            (GetTransactionsArg {
60                page,
61                witness: false,
62            },),
63        )
64        .await?;
65
66        Ok(result.0)
67    }
68
69    /// Returns all of the transactions associated with the given user.
70    pub async fn get_user_transactions(
71        &self,
72        user: Principal,
73        page: Option<u32>,
74    ) -> Result<GetTransactionsResponse, (RejectionCode, String)> {
75        let result: (GetTransactionsResponse,) = call(
76            self.0,
77            "get_user_transactions",
78            (GetUserTransactionsArg {
79                user,
80                page,
81                witness: false,
82            },),
83        )
84        .await?;
85
86        Ok(result.0)
87    }
88}
89
90impl From<RootBucket> for Bucket {
91    fn from(root: RootBucket) -> Self {
92        Bucket(root.0)
93    }
94}