use ic_kit::ic::call;
use ic_kit::{Principal, RejectionCode};
use crate::root::RootBucket;
use cap_common::{
GetIndexCanistersResponse, GetTransactionResponse, GetTransactionsArg, GetTransactionsResponse,
GetUserTransactionsArg, WithIdArg, WithWitnessArg,
};
#[derive(Copy, Clone)]
pub struct Bucket(pub(crate) Principal);
impl Bucket {
pub async fn get_next_canisters(&self) -> Result<Vec<Bucket>, (RejectionCode, String)> {
let result: (GetIndexCanistersResponse,) = call(
self.0,
"get_next_canisters",
(WithWitnessArg { witness: false },),
)
.await?;
Ok(result
.0
.canisters
.iter()
.map(|canister| Bucket(*canister))
.collect())
}
pub async fn get_transaction(
&self,
id: u64,
) -> Result<GetTransactionResponse, (RejectionCode, String)> {
let result: (GetTransactionResponse,) = call(
self.0,
"get_transaction",
(WithIdArg { id, witness: false },),
)
.await?;
Ok(result.0)
}
pub async fn get_transactions(
&self,
page: Option<u32>,
) -> Result<GetTransactionsResponse, (RejectionCode, String)> {
let result: (GetTransactionsResponse,) = call(
self.0,
"get_transactions",
(GetTransactionsArg {
page,
witness: false,
},),
)
.await?;
Ok(result.0)
}
pub async fn get_user_transactions(
&self,
user: Principal,
page: Option<u32>,
) -> Result<GetTransactionsResponse, (RejectionCode, String)> {
let result: (GetTransactionsResponse,) = call(
self.0,
"get_user_transactions",
(GetUserTransactionsArg {
user,
page,
witness: false,
},),
)
.await?;
Ok(result.0)
}
}
impl From<RootBucket> for Bucket {
fn from(root: RootBucket) -> Self {
Bucket(root.0)
}
}