koios_sdk/
types.rs

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use serde::{Deserialize, Serialize};

/// Block height for specifying time delta
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct AfterBlockHeight(pub u64);

/// Epoch Number to fetch details for
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EpochNo(pub String);

/// Cardano staking address (reward account) in bech32 format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StakeAddress(pub String);

/// Transaction Hash in hexadecimal format (hex)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxHash(pub String);

/// Asset Policy ID in hexadecimal format (hex)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssetPolicy(pub String);

/// Asset Name in hexadecimal format (hex)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssetName(pub String);

/// NFT Policy ID in hexadecimal format (hex)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssetPolicyNft(pub String);

/// NFT Name in hexadecimal format (hex)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssetNameNft(pub String);

/// Voter ID (Drep, SPO, Committee Member) in Bech32 format (CIP-5 | CIP-129)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoterId(pub String);

/// DRep ID in bech32 format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DrepId(pub String);

/// Committee member hot key ID in Bech32 format (CIP-5 | CIP-129)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CcHotId(pub String);

/// Controls whether or not certain optional fields are populated
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Extended(pub bool);

/// Include all historical transactions
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct History(pub bool);

/// Include information about nearing but not yet started epoch
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct IncludeNextEpoch(pub bool);

/// Pool ID in bech32 format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoolBech32(pub String);

/// Script hash in hexadecimal format (hex)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScriptHash(pub String);

/// Government proposal ID in CIP-129 Bech32 format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProposalId(pub String);

// Implementations for parameter validation and conversion

impl AfterBlockHeight {
    pub fn new(height: u64) -> Self {
        Self(height)
    }

    pub fn value(&self) -> u64 {
        self.0
    }
}

impl EpochNo {
    pub fn new<S: Into<String>>(epoch: S) -> Self {
        Self(epoch.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }
}

impl StakeAddress {
    pub fn new<S: Into<String>>(address: S) -> Self {
        Self(address.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for bech32 stake address format
    pub fn validate(&self) -> bool {
        self.0.starts_with("stake1")
    }
}

impl TxHash {
    pub fn new<S: Into<String>>(hash: S) -> Self {
        Self(hash.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for hex format
    pub fn validate(&self) -> bool {
        self.0.chars().all(|c| c.is_ascii_hexdigit())
    }
}

impl AssetPolicy {
    pub fn new<S: Into<String>>(policy: S) -> Self {
        Self(policy.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for hex format
    pub fn validate(&self) -> bool {
        self.0.chars().all(|c| c.is_ascii_hexdigit())
    }
}

impl AssetName {
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self(name.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for hex format
    pub fn validate(&self) -> bool {
        self.0.chars().all(|c| c.is_ascii_hexdigit())
    }
}

impl AssetPolicyNft {
    pub fn new<S: Into<String>>(policy: S) -> Self {
        Self(policy.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for hex format
    pub fn validate(&self) -> bool {
        self.0.chars().all(|c| c.is_ascii_hexdigit())
    }
}

impl AssetNameNft {
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self(name.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for hex format
    pub fn validate(&self) -> bool {
        self.0.chars().all(|c| c.is_ascii_hexdigit())
    }
}

impl VoterId {
    pub fn new<S: Into<String>>(voter_id: S) -> Self {
        Self(voter_id.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for bech32 format
    pub fn validate(&self) -> bool {
        self.0.starts_with("drep1") || self.0.starts_with("pool1") || self.0.starts_with("cc_hot1")
    }
}

impl DrepId {
    pub fn new<S: Into<String>>(drep_id: S) -> Self {
        Self(drep_id.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for bech32 format
    pub fn validate(&self) -> bool {
        self.0.starts_with("drep1")
    }
}

impl CcHotId {
    pub fn new<S: Into<String>>(cc_hot_id: S) -> Self {
        Self(cc_hot_id.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for bech32 format
    pub fn validate(&self) -> bool {
        self.0.starts_with("cc_hot1")
    }
}

impl Extended {
    pub fn new(extended: bool) -> Self {
        Self(extended)
    }

    pub fn value(&self) -> bool {
        self.0
    }
}

impl History {
    pub fn new(history: bool) -> Self {
        Self(history)
    }

    pub fn value(&self) -> bool {
        self.0
    }
}

impl IncludeNextEpoch {
    pub fn new(include: bool) -> Self {
        Self(include)
    }

    pub fn value(&self) -> bool {
        self.0
    }
}

impl PoolBech32 {
    pub fn new<S: Into<String>>(pool_id: S) -> Self {
        Self(pool_id.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for bech32 format
    pub fn validate(&self) -> bool {
        self.0.starts_with("pool1")
    }
}

impl ScriptHash {
    pub fn new<S: Into<String>>(hash: S) -> Self {
        Self(hash.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for hex format
    pub fn validate(&self) -> bool {
        self.0.chars().all(|c| c.is_ascii_hexdigit())
    }
}

impl ProposalId {
    pub fn new<S: Into<String>>(proposal_id: S) -> Self {
        Self(proposal_id.into())
    }

    pub fn value(&self) -> &str {
        &self.0
    }

    // Basic validation for bech32 format
    pub fn validate(&self) -> bool {
        self.0.starts_with("gov_action1")
    }
}

// Unit tests for parameter validation
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stake_address_validation() {
        let valid =
            StakeAddress::new("stake1u8yxtugdv63wxafy9d00nuz6hjyyp4qnggvc9a3vxh8yl0ckml2uz");
        let invalid = StakeAddress::new("invalid_address");

        assert!(valid.validate());
        assert!(!invalid.validate());
    }
    #[test]
    fn test_tx_hash_validation() {
        let valid = TxHash::new("f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e");
        let invalid = TxHash::new("invalid_hash!");

        assert!(valid.validate());
        assert!(!invalid.validate());
    }

    // Add more tests for other parameter validations...
}