1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use candid::{CandidType, Nat, Principal};
use ex3_ic_stable_structures::{storable::Bound, Storable};
use number::Ex3Uint;
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;

pub use version::Version;

pub mod asset;
pub mod balance_changed;
pub mod block;
pub mod chain;
pub mod market;
pub mod number;
pub mod package;
pub mod secret;
pub mod settings;
pub mod transaction;
pub mod vault;
mod version;
pub mod wallet_identifier;

pub type PublicKey = ByteBuf;
pub type WalletRegisterId = Ex3Uint;
pub type SpotMarketId = Ex3Uint;
pub type OrderId = Ex3Uint;
pub type AssetId = Ex3Uint;
pub type PackageId = Ex3Uint;
pub type AssetAmount = Ex3Uint;
pub type Nonce = Ex3Uint;
pub type BlockHeight = Ex3Uint;
pub type MerkleRoot = [u8; 32];
pub type MerkleNode = [u8; 32];
pub type BlockHash = [u8; 32];
pub type TransactionHash = [u8; 32];
pub type VaultSeqId = u64;
pub type WalletRegistrySeqId = VaultSeqId;
pub type BalanceVaultSeqId = VaultSeqId;
pub type SecretVaultSeqId = VaultSeqId;
pub type DepositId = Ex3Uint;
pub type CandidWalletRegisterId = Nat;
pub type CandidSpotMarketId = Nat;
pub type CandidOrderId = Nat;
pub type CandidPackageId = Nat;
pub type CandidAssetId = Nat;
pub type CandidAssetAmount = Nat;
pub type CandidNonce = Nat;
pub type CandidBlockHeight = Nat;
pub type CandidDepositId = Nat;
new_type_principal!(NodeProvider);
new_type_principal!(CanisterId);

#[macro_export(local_inner_macros)]
macro_rules! impl_from_uint_for {
    ($to_type:ty, $($t:ty),*) => {
        $(
            impl From<$t> for $to_type {
                fn from(value: $t) -> Self {
                    <$to_type>::from(BigUint::from(value))
                }
            }
        )*
    };
}

#[macro_export]
macro_rules! new_type_principal {
    ($name:ident) => {
        #[derive(
            CandidType, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize, Serialize,
        )]
        #[serde(transparent)]
        pub struct $name(Principal);

        impl From<Principal> for $name {
            fn from(principal: Principal) -> Self {
                Self(principal)
            }
        }

        impl $name {
            pub fn new(principal: Principal) -> Self {
                Self(principal)
            }

            pub fn as_slice(&self) -> &[u8] {
                self.0.as_slice()
            }

            pub fn as_ref(&self) -> &Principal {
                &self.0
            }
        }

        impl Storable for $name {
            fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
                self.0.as_ref().into()
            }

            fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
                Self(Principal::from_slice(bytes.as_ref()))
            }
            const BOUND: Bound = Bound::Bounded {
                max_size: 29,
                is_fixed_size: false,
            };
        }
    };
}