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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
pub mod abi;
pub mod ffi;
pub mod graphql;
pub mod tx;

pub use crate::abi::*;
pub use crate::tx::*;
pub use fuel_types::{
    Address, AssetId, Bytes32, Bytes4, Bytes8, ContractId, MessageId, Salt, Word,
};
pub use fuels::{
    core::try_from_bytes,
    types::{
        bech32::{Bech32Address, Bech32ContractId},
        Bits256, SizedAsciiString,
    },
};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

pub type Error = Box<dyn std::error::Error>;
pub type ID = u64;
pub type Int4 = i32;
pub type Int8 = i64;
pub type Int16 = i128;
pub type UInt4 = u32;
pub type UInt8 = u64;
pub type UInt16 = u128;
pub type Timestamp = u64;
pub type Charfield = String;
pub type Boolean = bool;

#[derive(Deserialize, Serialize, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Blob(pub Vec<u8>);

impl From<Vec<u8>> for Blob {
    fn from(value: Vec<u8>) -> Self {
        Blob(value)
    }
}

impl AsRef<[u8]> for Blob {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl From<Blob> for Vec<u8> {
    fn from(value: Blob) -> Self {
        value.0
    }
}

#[derive(Deserialize, Serialize, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Json(pub String);

macro_rules! json_impl {
    ($($ty:ty),*) => {
        $(
            impl From<$ty> for Json {
                fn from(value: $ty) -> Self {
                    Json(value.to_string())
                }
            }
        )*
    }
}

macro_rules! blob_impl {
    ($($ty:ty),*) => {
        $(
            impl From<$ty> for Blob {
                fn from(value: $ty) -> Self {
                    Blob::from(bincode::serialize(&value).unwrap().to_vec())
                }
            }
        )*
    }
}

json_impl!(i32, i64, i128, u32, u64, u128);
blob_impl!(i32, i64, i128, u32, u64, u128);

// IMPORTANT: https://github.com/launchbadge/sqlx/issues/499
pub fn type_id(namespace: &str, type_name: &str) -> i64 {
    let mut bytes = [0u8; 8];
    bytes.copy_from_slice(
        &Sha256::digest(format!("{namespace}:{type_name}").as_bytes())[..8],
    );
    i64::from_le_bytes(bytes)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_into_json_blob_id() {
        let id: ID = 123;
        let as_json: Json = id.into();
        let as_bytes: Blob = id.into();

        assert_eq!(as_json, Json("123".to_string()));
        assert_eq!(as_bytes, Blob(vec![123, 0, 0, 0, 0, 0, 0, 0]));
    }

    #[test]
    fn test_into_json_blob_int4() {
        let int: Int4 = 42;
        let as_json: Json = int.into();
        let as_bytes: Blob = int.into();

        assert_eq!(as_json, Json("42".to_string()));
        assert_eq!(as_bytes, Blob(vec![42, 0, 0, 0]));
    }

    #[test]
    fn test_into_json_blob_int8() {
        let int: Int8 = 1234567890;
        let as_json: Json = int.into();
        let as_bytes: Blob = int.into();

        assert_eq!(as_json, Json("1234567890".to_string()));
        assert_eq!(as_bytes, Blob(vec![210, 2, 150, 73, 0, 0, 0, 0]));
    }

    #[test]
    fn test_into_json_blob_int16() {
        let int: Int16 = 123456789012345;
        let as_json: Json = int.into();
        let as_bytes: Blob = int.into();

        assert_eq!(as_json, Json("123456789012345".to_string()));
        assert_eq!(
            as_bytes,
            Blob(vec![
                121, 223, 13, 134, 72, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ])
        );
    }

    #[test]
    fn test_into_json_blob_uint4() {
        let uint: UInt4 = 7;
        let as_json: Json = uint.into();
        let as_bytes: Blob = uint.into();

        assert_eq!(as_json, Json("7".to_string()));
        assert_eq!(as_bytes, Blob(vec![7, 0, 0, 0]));
    }

    #[test]
    fn test_into_json_blob_uint8() {
        let uint: UInt8 = 1234567890;
        let as_json: Json = uint.into();
        let as_bytes: Blob = uint.into();

        assert_eq!(as_json, Json("1234567890".to_string()));
        assert_eq!(as_bytes, Blob(vec![210, 2, 150, 73, 0, 0, 0, 0]));
    }

    #[test]
    fn test_into_json_blob_uint16() {
        let uint: UInt16 = 123456789012345;
        let as_json: Json = uint.into();
        let as_bytes: Blob = uint.into();

        assert_eq!(as_json, Json("123456789012345".to_string()));
        assert_eq!(
            as_bytes,
            Blob(vec![
                121, 223, 13, 134, 72, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ])
        );
    }

    #[test]
    fn test_into_json_blob_timestamp() {
        let timestamp: Timestamp = 1234567890;
        let as_json: Json = timestamp.into();
        let as_bytes: Blob = timestamp.into();

        assert_eq!(as_json, Json("1234567890".to_string()));
        assert_eq!(as_bytes, Blob(vec![210, 2, 150, 73, 0, 0, 0, 0]));
    }
}