1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6pub type Slot = u64;
8
9pub type Epoch = u64;
11
12pub type BlockHeight = u64;
14
15pub type Lovelace = u64;
17
18pub type LovelaceDelta = i128;
20
21pub type TransactionId = String;
23
24pub type PolicyId = String;
26
27pub type AssetName = String;
29
30pub type AssetQuantity = i128;
32
33pub type ScriptHash = String;
35
36pub type DatumHash = String;
38
39pub type PoolId = String;
41
42pub type StakePoolId = String;
44
45pub type VerificationKeyHash = String;
47
48pub type Digest = String;
50
51pub type DigestBlake2b224 = String;
53
54pub type DigestBlake2b256 = String;
56
57pub type RatioString = String;
59
60pub type NetworkMagic = u32;
62
63pub type RelativeTime = f64;
65
66pub type UtcTime = String;
68
69pub type NumberOfBytes = u64;
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
74#[serde(rename_all = "camelCase")]
75pub struct Origin;
76
77impl Origin {
78 pub const ORIGIN: &'static str = "origin";
79}
80
81#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
83#[serde(untagged)]
84pub enum Point {
85 Origin(String),
87 Point {
89 slot: Slot,
90 id: DigestBlake2b256,
91 },
92}
93
94impl Point {
95 pub fn origin() -> Self {
97 Point::Origin("origin".to_string())
98 }
99
100 pub fn at(slot: Slot, id: impl Into<String>) -> Self {
102 Point::Point { slot, id: id.into() }
103 }
104}
105
106#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
108#[serde(untagged)]
109pub enum Tip {
110 Origin(String),
112 Tip {
114 slot: Slot,
115 id: DigestBlake2b256,
116 height: BlockHeight,
117 },
118}
119
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
122pub struct Ratio {
123 pub numerator: u64,
124 pub denominator: u64,
125}
126
127impl Ratio {
128 pub fn new(numerator: u64, denominator: u64) -> Self {
129 Self { numerator, denominator }
130 }
131
132 pub fn to_f64(&self) -> f64 {
133 self.numerator as f64 / self.denominator as f64
134 }
135}
136
137pub type Assets = HashMap<PolicyId, HashMap<AssetName, AssetQuantity>>;
139
140#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
142#[serde(untagged)]
143pub enum Value {
144 AdaOnly {
146 ada: AdaValue,
147 },
148 WithAssets {
150 ada: AdaValue,
151 #[serde(flatten)]
152 assets: Assets,
153 },
154}
155
156#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
158pub struct AdaValue {
159 pub lovelace: Lovelace,
160}
161
162impl Value {
163 pub fn ada_only(lovelace: Lovelace) -> Self {
165 Value::AdaOnly {
166 ada: AdaValue { lovelace },
167 }
168 }
169
170 pub fn lovelace(&self) -> Lovelace {
172 match self {
173 Value::AdaOnly { ada } => ada.lovelace,
174 Value::WithAssets { ada, .. } => ada.lovelace,
175 }
176 }
177}
178
179pub type Address = String;
181
182pub type StakeAddress = String;
184
185pub type RewardAccount = String;
187
188#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
190#[serde(rename_all = "camelCase")]
191pub enum Nonce {
192 Neutral,
194 #[serde(untagged)]
196 Value(String),
197}
198
199pub type VerificationKey = String;
201
202pub type ExtendedVerificationKey = String;
204
205pub type KesVerificationKey = String;
207
208pub type VrfVerificationKey = String;
210
211pub type Signature = String;
213
214pub type MetadataLabels = HashMap<String, Metadatum>;
216
217#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
219#[serde(untagged)]
220pub enum Metadatum {
221 Int(i128),
223 Bytes(String),
225 String(String),
227 List(Vec<Metadatum>),
229 Map(Vec<MetadatumMapEntry>),
231}
232
233#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
235pub struct MetadatumMapEntry {
236 pub k: Metadatum,
237 pub v: Metadatum,
238}
239
240#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
242pub struct Anchor {
243 pub url: String,
244 #[serde(rename = "hash")]
245 pub content_hash: DigestBlake2b256,
246}
247
248#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
250#[serde(rename_all = "camelCase")]
251pub enum CredentialOrigin {
252 Key,
253 Script,
254}
255
256#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
258#[serde(untagged)]
259pub enum StakeCredential {
260 Key { key: DigestBlake2b224 },
262 Script { script: ScriptHash },
264}
265
266pub type PaymentCredential = StakeCredential;
268
269#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
271#[serde(untagged)]
272pub enum Nullable<T> {
273 Null,
274 Value(T),
275}
276
277impl<T> Nullable<T> {
278 pub fn is_null(&self) -> bool {
279 matches!(self, Nullable::Null)
280 }
281
282 pub fn into_option(self) -> Option<T> {
283 match self {
284 Nullable::Null => None,
285 Nullable::Value(v) => Some(v),
286 }
287 }
288}
289
290impl<T> From<Option<T>> for Nullable<T> {
291 fn from(opt: Option<T>) -> Self {
292 match opt {
293 Some(v) => Nullable::Value(v),
294 None => Nullable::Null,
295 }
296 }
297}