1use crate::Bucket;
7use cap_common::transaction::IndefiniteEvent;
8use cap_common::{GetBucketResponse, WithIdArg};
9use ic_kit::candid::CandidType;
10use ic_kit::{ic::call, Principal, RejectionCode};
11use serde::{Deserialize, Serialize};
12
13#[derive(Copy, Clone, Serialize, Deserialize, CandidType)]
22pub struct RootBucket(pub Principal);
23
24impl RootBucket {
25 pub async fn get_bucket_for(&self, id: u64) -> Result<Bucket, (RejectionCode, String)> {
27 let result: (GetBucketResponse,) = call(
28 self.0,
29 "get_bucket_for",
30 (WithIdArg { id, witness: false },),
31 )
32 .await?;
33
34 Ok(Bucket(result.0.canister))
35 }
36
37 pub async fn insert(&self, event: &IndefiniteEvent) -> Result<u64, (RejectionCode, String)> {
39 let result: (u64,) = call(self.0, "insert", (event,)).await?;
40
41 Ok(result.0)
42 }
43
44 pub async fn insert_many(
46 &self,
47 events: &[IndefiniteEvent],
48 ) -> Result<u64, (RejectionCode, String)> {
49 let result: (u64,) = call(self.0, "insert_many", (events,)).await?;
50
51 Ok(result.0)
52 }
53
54 pub async fn time(&self) -> Result<u64, (RejectionCode, String)> {
58 let result: (u64,) = call(self.0, "time", ()).await?;
59
60 Ok(result.0)
61 }
62}