ex3_dft_client/
types.rs

1use candid::{CandidType, Deserialize, Nat, Principal};
2use ex3_common_error_info::ErrorInfo;
3use serde::Serialize;
4
5pub type BlockHeight = Nat;
6pub type TokenHolder = AccountIdentifier;
7pub type TokenReceiver = AccountIdentifier;
8pub type TransactionId = String;
9pub type BlockHash = [u8; 32];
10
11/// AccountIdentifier is a 32-byte array.
12/// The first 4 bytes is big-endian encoding of a CRC32 checksum of the last 28 bytes.
13#[derive(
14    CandidType, Serialize, Deserialize, Clone, Copy, Hash, Debug, PartialEq, Eq, PartialOrd, Ord,
15)]
16pub struct AccountIdentifier {
17    hash: [u8; 28],
18}
19
20/// Subaccount is an arbitrary 32-byte byte array.
21/// Ledger uses subaccounts to compute account pub_key, which enables one
22/// principal to control multiple ledger accounts.
23#[derive(CandidType, Serialize, Deserialize, Clone, Copy, Hash, Debug, PartialEq, Eq)]
24pub struct Subaccount(pub [u8; 32]);
25
26#[derive(CandidType, Deserialize, Debug, Clone)]
27pub enum OperationResult {
28    Ok {
29        #[serde(rename = "txId")]
30        tx_id: TransactionId,
31        #[serde(rename = "blockHeight")]
32        block_height: Nat,
33    },
34    Err(ErrorInfo),
35}
36
37#[derive(CandidType, Deserialize, Debug, Clone)]
38pub struct ArchivedBlocksRange {
39    pub start: Nat,
40    pub length: u64,
41    #[serde(rename = "storageCanisterId")]
42    pub storage_canister_id: Principal,
43}
44
45#[derive(CandidType, Deserialize, Debug, Clone)]
46pub struct QueryBlocksResult {
47    #[serde(rename = "chainLength")]
48    pub chain_length: Nat,
49    pub certificate: Option<serde_bytes::ByteBuf>,
50    pub blocks: Vec<Block>,
51    #[serde(rename = "firstBlockIndex")]
52    pub first_block_index: Nat,
53    #[serde(rename = "archivedBlocks")]
54    pub archived_blocks: Vec<ArchivedBlocksRange>,
55}
56
57#[derive(CandidType, Deserialize, Debug, Clone)]
58pub enum Operation {
59    Approve {
60        caller: Principal,
61        owner: TokenHolder,
62        spender: TokenHolder,
63        value: Nat,
64        fee: Nat,
65    },
66    Transfer {
67        caller: TokenHolder,
68        from: TokenHolder,
69        to: TokenReceiver,
70        value: Nat,
71        fee: Nat,
72    },
73    FeeModify {
74        caller: Principal,
75        #[serde(rename = "newFee")]
76        new_fee: TokenFee,
77    },
78    OwnerModify {
79        caller: Principal,
80        #[serde(rename = "newOwner")]
81        new_owner: Principal,
82    },
83    FeeToModify {
84        caller: Principal,
85        #[serde(rename = "newFeeTo")]
86        new_fee_to: TokenHolder,
87    },
88    AddMinter {
89        caller: Principal,
90        minter: Principal,
91    },
92    RemoveMinter {
93        caller: Principal,
94        minter: Principal,
95    },
96}
97
98#[derive(CandidType, Deserialize, Debug, Clone)]
99pub struct TokenFee {
100    pub minimum: Nat,
101    pub rate: u32,
102    #[serde(rename = "rateDecimals")]
103    pub rate_decimals: u8,
104}
105
106#[derive(CandidType, Deserialize, Debug, Clone)]
107pub struct Transaction {
108    pub operation: Operation,
109    /// The time this transaction was created.
110    #[serde(rename = "createdAt")]
111    pub created_at: u64,
112}
113
114#[derive(CandidType, Deserialize, Debug, Clone)]
115pub struct Block {
116    #[serde(rename = "parentHash")]
117    pub parent_hash: BlockHash,
118    pub transaction: Transaction,
119    pub timestamp: u64,
120}
121
122#[derive(CandidType, Deserialize, Debug, Clone)]
123pub enum BlockResult {
124    // Return tx record if exist in the DFT cache txs
125    Ok(Block),
126    // If not storage in DFT cache txs, return the storage canister id
127    Forward(Principal),
128    // Such as out of tx index or tx id not exist
129    Err(ErrorInfo),
130}