use crate::transaction::Event;
use certified_vars::{Hash, HashTree};
use ic_kit::candid::{CandidType, Deserialize};
use ic_kit::ic;
use ic_kit::Principal;
use serde::Serialize;
pub type TransactionId = u64;
pub type IndexCanisterId = Principal;
pub type RootBucketId = Principal;
pub type BucketId = Principal;
pub type TokenContractId = Principal;
pub type UserId = Principal;
pub type EventHash = Hash;
#[derive(Serialize, Deserialize, CandidType)]
pub struct Witness {
#[serde(with = "serde_bytes")]
certificate: Vec<u8>,
#[serde(with = "serde_bytes")]
tree: Vec<u8>,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetTokenContractRootBucketArg {
pub canister: TokenContractId,
pub witness: bool,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetTokenContractRootBucketResponse {
pub canister: Option<RootBucketId>,
pub witness: Option<Witness>,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetUserRootBucketsArg {
pub user: UserId,
pub witness: bool,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetUserRootBucketsResponse {
pub contracts: Vec<RootBucketId>,
pub witness: Option<Witness>,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct WithWitnessArg {
pub witness: bool,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetIndexCanistersResponse {
pub canisters: Vec<IndexCanisterId>,
pub witness: Option<Witness>,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetNextCanistersResponse {
pub canisters: Vec<IndexCanisterId>,
pub witness: Option<Witness>,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct WithIdArg {
pub id: TransactionId,
pub witness: bool,
}
#[derive(Serialize, Deserialize, CandidType)]
pub enum GetTransactionResponse {
Delegate(BucketId, Option<Witness>),
Found(Option<Event>, Option<Witness>),
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetTransactionsArg {
pub page: Option<u32>,
pub witness: bool,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetTransactionsResponse {
pub data: Vec<Event>,
pub page: u32,
pub witness: Option<Witness>,
}
#[derive(Serialize, CandidType)]
pub struct GetTransactionsResponseBorrowed<'a> {
pub data: Vec<&'a Event>,
pub page: u32,
pub witness: Option<Witness>,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetUserTransactionsArg {
pub user: UserId,
pub page: Option<u32>,
pub witness: bool,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetTokenTransactionsArg {
pub token_id: u64,
pub page: Option<u32>,
pub witness: bool,
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct GetBucketResponse {
pub canister: BucketId,
pub witness: Option<Witness>,
}
impl From<HashTree<'_>> for Witness {
fn from(tree: HashTree) -> Self {
Self {
certificate: ic::data_certificate().unwrap(),
tree: serde_cbor::to_vec(&tree).unwrap(),
}
}
}
#[derive(Serialize, Deserialize, CandidType)]
pub struct BucketInitArgs {
pub contract: TokenContractId,
pub offset: u64,
pub next_canisters: Vec<BucketId>,
}