1#![allow(unused)]
2
3use crate::error::*;
4use serde::Deserialize;
5use serde_json::Map;
6use std::collections::HashMap;
7
8#[derive(Debug)]
9pub enum HTTPResponse<T> {
10 HttpOk(T),
11 HttpError(HttpErrorInner),
12}
13
14#[derive(Deserialize, Debug)]
15pub struct HttpErrorInner {
16 pub status_code: u16,
17 pub error: String,
18 pub message: String,
19}
20
21#[derive(Deserialize, Debug)]
22pub struct Genesis {
23 active_slots_coefficient: f32,
24 update_quorum: u32,
25 max_lovelace_supply: String,
26 network_magic: u32,
27 epoch_length: u32,
28 system_start: u32,
29 slots_per_kes_period: u32,
30 slot_length: u32,
31 max_kes_evolutions: u32,
32 security_param: u32,
33}
34
35#[derive(Deserialize, Debug)]
36pub struct BlockInfo {
37 time: u64,
38 height: u64,
39 hash: String,
40 slot: u64,
41 epoch: u64,
42 epoch_slot: u64,
43 slot_leader: String,
44 size: u64,
45 tx_count: u64,
46 output: Option<String>,
47 fees: Option<String>,
48 block_vrf: String,
49 op_cert: String,
50 op_cert_counter: String,
51 previous_block: String,
52 next_block: Option<String>,
53 confirmations: u64,
54}
55
56impl BlockInfo {
57 pub fn time(&self) -> u64 {
58 self.time
59 }
60 pub fn height(&self) -> u64 {
61 self.height
62 }
63
64 pub fn hash(&self) -> &str {
65 &self.hash
66 }
67
68 pub fn slot(&self) -> u64 {
69 self.slot
70 }
71
72 pub fn epoch(&self) -> u64 {
73 self.epoch
74 }
75
76 pub fn epoch_slot(&self) -> u64 {
77 self.epoch_slot
78 }
79
80 pub fn slot_leader(&self) -> &str {
81 &self.slot_leader
82 }
83
84 pub fn size(&self) -> u64 {
85 self.size
86 }
87
88 pub fn tx_count(&self) -> u64 {
89 self.tx_count
90 }
91
92 pub fn output(&self) -> &Option<String> {
93 &self.output
94 }
95
96 pub fn fees(&self) -> &Option<String> {
97 &self.fees
98 }
99
100 pub fn block_vrf(&self) -> &str {
101 &self.block_vrf
102 }
103
104 pub fn op_cert(&self) -> &str {
105 &self.op_cert
106 }
107
108 pub fn op_cert_counter(&self) -> &str {
109 &self.op_cert_counter
110 }
111
112 pub fn previous_block(&self) -> &str {
113 &self.previous_block
114 }
115
116 pub fn next_block(&self) -> &Option<String> {
117 &self.next_block
118 }
119
120 pub fn confirmations(&self) -> u64 {
121 self.confirmations
122 }
123}
124
125#[allow(non_snake_case)]
126#[derive(Deserialize, Debug)]
127pub struct CostModels {
128 PlutusV1: HashMap<String, u64>,
129 PlutusV2: HashMap<String, u64>,
130}
131
132#[derive(Deserialize, Debug)]
133pub struct ProtocolParams {
134 epoch: u32,
135 min_fee_a: u32,
136 min_fee_b: u32,
137 max_block_size: u32,
138 max_tx_size: u32,
139 max_block_header_size: u32,
140 key_deposit: String,
141 pool_deposit: String,
142 e_max: u32,
143 n_opt: u32,
144 a0: f32,
145 rho: f32,
146 tau: f32,
147 decentralisation_param: f32,
148 extra_entropy: Option<serde_json::Value>,
149 protocol_major_ver: u32,
150 protocol_minor_ver: u32,
151 min_utxo: String,
152 min_pool_cost: String,
153 nonce: String,
154 price_mem: f32,
155 price_step: f32,
156 max_tx_ex_mem: String,
157 max_tx_ex_steps: String,
158 max_block_ex_mem: String,
159 max_block_ex_steps: String,
160 max_val_size: String,
161 collateral_percent: u32,
162 max_collateral_inputs: u32,
163 coins_per_utxo_size: String,
164 coins_per_utxo_word: String,
165 cost_models: CostModels,
166}
167
168#[derive(Deserialize, Debug)]
169pub struct UTxO {
170 tx_hash: String,
171 output_index: u64,
172 amount: Vec<Value>,
173 block: String,
174 data_hash: Option<String>,
175 inline_datum: Option<serde_json::Value>,
176 reference_script_hash: Option<String>,
177}
178
179impl UTxO {
180 pub fn tx_hash(&self) -> &str {
181 &self.tx_hash
182 }
183
184 pub fn data_hash(&self) -> &Option<String> {
185 &self.data_hash
186 }
187
188 pub fn with_data(&self, data: Option<serde_json::Value>) -> UTxOWithData {
190 UTxOWithData {
191 tx_hash: self.tx_hash.clone(),
192 output_index: self.output_index,
193 amount: self.amount.clone(),
194 block: self.block.clone(),
195 data,
196 }
197 }
198
199 pub fn block(&self) -> &str {
200 &self.block
201 }
202
203 pub fn output_index(&self) -> u64 {
204 self.output_index
205 }
206
207 pub fn amount(&self) -> &Vec<Value> {
208 &self.amount
209 }
210}
211
212#[derive(Deserialize, Debug)]
213pub struct UTxOWithData {
214 tx_hash: String,
215 output_index: u64,
216 amount: Vec<Value>,
217 block: String,
218 data: Option<serde_json::Value>,
219}
220
221#[derive(Deserialize, Debug, Clone)]
222pub struct Value {
223 pub(crate) unit: String,
224 pub(crate) quantity: String,
225}
226
227impl Value {
228 pub fn unit(&self) -> &str {
229 &self.unit
230 }
231
232 pub fn quantity(&self) -> &str {
233 &self.quantity
234 }
235}
236
237#[derive(Deserialize, Debug)]
238pub struct AddressInfo {
239 address: String,
240 amount: Vec<Value>,
241 stake_address: Option<String>,
242 r#type: String,
243 script: bool,
244}
245
246#[derive(Clone, Deserialize, Debug)]
247pub struct Address {
248 address: String,
249}
250
251impl Address {
252 pub fn as_string(&self) -> &str {
253 &self.address
254 }
255}
256
257#[derive(Deserialize, Debug)]
258pub struct AccountAssocAddrTotal {
259 stake_addr: String,
260 received_sum: Vec<Value>,
261 sent_sum: Vec<Value>,
262 tx_count: u32,
263}
264
265#[derive(Deserialize, Debug)]
266pub struct Fault {
267 code: String,
268 string: String,
269}
270
271#[derive(Deserialize, Debug)]
272pub struct EvaluateTxResult {
273 methodname: Option<String>,
274 reflection: HashMap<String, String>,
275 result: Option<RequestResult>,
276 fault: Option<HashMap<String, String>>,
277 servicename: String,
278 r#type: String,
279 version: String,
280}
281
282impl EvaluateTxResult {
283 pub fn get_execution_costs(&self) -> Result<HashMap<u64, ExecutionCostsWithType>> {
284 let mut execution_costs = HashMap::new();
285 if let Some(result) = &self.result {
286 if let Some(eval_result) = &result.evaluatation_result {
287 if let Some(inner) = eval_result.as_object() {
288 let keys = inner.keys();
289 for key in keys {
290 if key.contains("spend:") | key.contains("mint:") {
291 if let Some(val) = inner.get(key) {
292 let index_str = key
293 .split(':')
294 .collect::<Vec<_>>()
295 .get(1)
296 .unwrap()
297 .to_owned();
298 let index = index_str
299 .parse::<u64>()
300 .map_err(|e| Error::EvaluateTxResult(Box::new(e)))?;
301 let serialized = serde_json::to_string(val)
302 .map_err(|e| Error::EvaluateTxResult(Box::new(e)))?;
303 let deserialized: ExecutionCosts =
304 serde_json::from_str(&serialized).unwrap();
305
306 if key.contains("spend:") {
307 execution_costs.insert(index, deserialized.spend());
308 } else if key.contains("mint:") {
309 execution_costs.insert(index, deserialized.mint());
310 }
311 }
312 }
313 }
314 }
315 } else if let Some(eval_failure) = &result.evalutation_failure {
316 return Err(Error::EvaluateTxFailure(eval_failure.to_string()));
317 }
318 }
319 Ok(execution_costs)
320 }
321}
322
323#[derive(Deserialize, Debug)]
324pub struct RequestResult {
325 #[serde(rename = "EvaluationResult")]
326 evaluatation_result: Option<serde_json::Value>,
327 #[serde(rename = "EvaluationFailure")]
328 evalutation_failure: Option<serde_json::Value>,
329}
330
331#[derive(Clone, Debug)]
332pub enum ExecutionType {
333 Spend,
334 Mint,
335}
336
337#[derive(Deserialize, Debug, Clone)]
338pub struct ExecutionCosts {
339 memory: u64,
340 steps: u64,
341}
342
343impl ExecutionCosts {
344 pub fn memory(&self) -> u64 {
345 self.memory
346 }
347 pub fn steps(&self) -> u64 {
348 self.steps
349 }
350}
351
352impl ExecutionCosts {
353 fn mint(&self) -> ExecutionCostsWithType {
354 ExecutionCostsWithType {
355 execution_type: ExecutionType::Mint,
356 memory: self.memory,
357 steps: self.steps,
358 }
359 }
360
361 fn spend(&self) -> ExecutionCostsWithType {
362 ExecutionCostsWithType {
363 execution_type: ExecutionType::Spend,
364 memory: self.memory,
365 steps: self.steps,
366 }
367 }
368}
369
370pub struct ExecutionCostsWithType {
371 execution_type: ExecutionType,
372 memory: u64,
373 steps: u64,
374}
375impl ExecutionCostsWithType {
376 pub fn memory(&self) -> u64 {
377 self.memory
378 }
379 pub fn steps(&self) -> u64 {
380 self.steps
381 }
382
383 pub fn execution_type(&self) -> ExecutionType {
384 self.execution_type.clone()
385 }
386}
387
388#[derive(Deserialize, Debug)]
389pub struct TxSubmitResult(String);
390
391impl TxSubmitResult {
392 pub fn tx_id(&self) -> &str {
393 &self.0
394 }
395}