isp_sdk/isp/
isp_did.rs

1use candid::{Nat, Principal};
2use ic_cdk::api::call::CallResult;
3use ic_cdk::export::candid::{CandidType, Deserialize};
4
5type BlockIndex = u64;
6
7#[derive(CandidType, Deserialize, Debug)]
8pub struct Token {
9    e8s: u64,
10}
11
12#[derive(CandidType, Deserialize, Debug)]
13pub enum Error {
14    Create_Canister_Failed(Nat),
15    Ledger_Transfer_Failed(Nat),
16    Unauthorized,
17}
18
19#[derive(CandidType, Deserialize, Debug)]
20pub enum CreateICSPResult {
21    ok(Principal),
22    err(Error),
23}
24
25#[derive(CandidType, Deserialize, Debug)]
26pub enum TransferError {
27    TxTooOld { allowed_window_nanos: u64 },
28    BadFee { expected_fee: Token },
29    TxDuplicate { duplicate_of: BlockIndex },
30    TxCreatedInFuture,
31    InsufficientFunds { balance: Token },
32}
33
34#[derive(CandidType, Deserialize, Debug)]
35pub enum TransferResult {
36    Ok(BlockIndex),
37    Err(TransferError),
38}
39
40#[derive(CandidType, Deserialize, Debug)]
41pub struct TopUpArgs {
42    pub icsp_canisterId: Principal,
43    pub icp_amount: u64,
44}
45
46#[derive(CandidType, Deserialize, Debug)]
47pub enum TopUpResult {
48    ok,
49    err(Error),
50}
51
52#[derive(CandidType, Deserialize, Debug)]
53pub struct TransformArgs {
54    pub to_canister_id: Principal,
55    pub icp_amount: u64,
56}
57
58pub type ISP = candid::Service;
59pub type AccountIdentifier = Vec<u8>;
60
61pub struct SERVICE(pub candid::Principal);
62impl SERVICE {
63    pub async fn get_sub_account(&self) -> CallResult<(AccountIdentifier,)> {
64        ic_cdk::call(self.0, "getSubAccount", ()).await
65    }
66
67    pub async fn create_icsp(&self, name: String, amount: u64) -> CallResult<(CreateICSPResult,)> {
68        ic_cdk::call(self.0, "createICSP", (name, amount)).await
69    }
70
71    pub async fn get_admins(&self) -> CallResult<(Vec<Principal>,)> {
72        ic_cdk::call(self.0, "getAdmins", ()).await
73    }
74
75    pub async fn get_icp_balance(&self) -> CallResult<(u64,)> {
76        ic_cdk::call(self.0, "getUserSubAccountICPBalance", ()).await
77    }
78
79    pub async fn transfer_out_icp(
80        &self,
81        to: AccountIdentifier,
82        amount: u64,
83    ) -> CallResult<(TransferResult,)> {
84        ic_cdk::call(self.0, "transferOutUserSubAccountICP", (to, amount)).await
85    }
86
87    pub async fn get_user_icsps(&self) -> CallResult<(Vec<(String, Principal)>,)> {
88        ic_cdk::call(self.0, "getUserICSPs", ()).await
89    }
90
91    pub async fn top_up_icsp(&self, args: TopUpArgs) -> CallResult<(TopUpResult,)> {
92        ic_cdk::call(self.0, "topUpICSP", (args,)).await
93    }
94
95    pub async fn transform_icp(&self, args: TransformArgs) -> CallResult<(TopUpResult,)> {
96        ic_cdk::call(self.0, "transformIcp", (args,)).await
97    }
98}