Skip to main content

ogmios_client/schema/
primitives.rs

1//! Primitive types used throughout the Ogmios schema.
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// A 64-bit unsigned integer slot number.
7pub type Slot = u64;
8
9/// A 64-bit unsigned integer epoch number.
10pub type Epoch = u64;
11
12/// Block height as a 64-bit unsigned integer.
13pub type BlockHeight = u64;
14
15/// A lovelace amount (1 ADA = 1,000,000 lovelace).
16pub type Lovelace = u64;
17
18/// A signed lovelace delta for value changes.
19pub type LovelaceDelta = i128;
20
21/// Transaction ID as a hex-encoded string (64 characters).
22pub type TransactionId = String;
23
24/// Policy ID as a hex-encoded string (56 characters).
25pub type PolicyId = String;
26
27/// Asset name as a hex-encoded string.
28pub type AssetName = String;
29
30/// Asset quantity (can be negative for burning).
31pub type AssetQuantity = i128;
32
33/// Script hash as a hex-encoded string.
34pub type ScriptHash = String;
35
36/// Datum hash as a hex-encoded string (64 characters, Blake2b-256).
37pub type DatumHash = String;
38
39/// Pool ID as a Bech32-encoded string.
40pub type PoolId = String;
41
42/// Stake pool ID (hex-encoded hash).
43pub type StakePoolId = String;
44
45/// Verification key hash.
46pub type VerificationKeyHash = String;
47
48/// A generic digest type (hex-encoded).
49pub type Digest = String;
50
51/// Blake2b-224 digest (56 hex characters).
52pub type DigestBlake2b224 = String;
53
54/// Blake2b-256 digest (64 hex characters).
55pub type DigestBlake2b256 = String;
56
57/// A ratio represented as a string "numerator/denominator".
58pub type RatioString = String;
59
60/// Network magic number.
61pub type NetworkMagic = u32;
62
63/// Relative time in seconds.
64pub type RelativeTime = f64;
65
66/// UTC time as ISO 8601 string.
67pub type UtcTime = String;
68
69/// Number of bytes.
70pub type NumberOfBytes = u64;
71
72/// The origin point of the blockchain.
73#[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/// A point on the blockchain, either origin or a specific slot/hash.
82#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
83#[serde(untagged)]
84pub enum Point {
85    /// The origin point.
86    Origin(String),
87    /// A specific point with slot and block ID.
88    Point {
89        slot: Slot,
90        id: DigestBlake2b256,
91    },
92}
93
94impl Point {
95    /// Create an origin point.
96    pub fn origin() -> Self {
97        Point::Origin("origin".to_string())
98    }
99
100    /// Create a point at a specific slot and block ID.
101    pub fn at(slot: Slot, id: impl Into<String>) -> Self {
102        Point::Point { slot, id: id.into() }
103    }
104}
105
106/// The tip of the blockchain.
107#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
108#[serde(untagged)]
109pub enum Tip {
110    /// The origin (empty chain).
111    Origin(String),
112    /// A specific tip with slot, ID, and height.
113    Tip {
114        slot: Slot,
115        id: DigestBlake2b256,
116        height: BlockHeight,
117    },
118}
119
120/// A rational number represented as numerator and denominator.
121#[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
137/// Assets as a map of policy ID to a map of asset name to quantity.
138pub type Assets = HashMap<PolicyId, HashMap<AssetName, AssetQuantity>>;
139
140/// Value containing ADA and optional multi-assets.
141#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
142#[serde(untagged)]
143pub enum Value {
144    /// ADA only value.
145    AdaOnly {
146        ada: AdaValue,
147    },
148    /// Value with ADA and other assets.
149    WithAssets {
150        ada: AdaValue,
151        #[serde(flatten)]
152        assets: Assets,
153    },
154}
155
156/// ADA value container.
157#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
158pub struct AdaValue {
159    pub lovelace: Lovelace,
160}
161
162impl Value {
163    /// Create an ADA-only value.
164    pub fn ada_only(lovelace: Lovelace) -> Self {
165        Value::AdaOnly {
166            ada: AdaValue { lovelace },
167        }
168    }
169
170    /// Get the lovelace amount.
171    pub fn lovelace(&self) -> Lovelace {
172        match self {
173            Value::AdaOnly { ada } => ada.lovelace,
174            Value::WithAssets { ada, .. } => ada.lovelace,
175        }
176    }
177}
178
179/// A Cardano address (Bech32 or Base58 encoded).
180pub type Address = String;
181
182/// A stake address (Bech32 encoded, starts with "stake").
183pub type StakeAddress = String;
184
185/// A reward account (same as stake address).
186pub type RewardAccount = String;
187
188/// Nonce for protocol parameters.
189#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
190#[serde(rename_all = "camelCase")]
191pub enum Nonce {
192    /// Neutral nonce.
193    Neutral,
194    /// Specific nonce value.
195    #[serde(untagged)]
196    Value(String),
197}
198
199/// A verification key (hex-encoded).
200pub type VerificationKey = String;
201
202/// An extended verification key.
203pub type ExtendedVerificationKey = String;
204
205/// A KES verification key.
206pub type KesVerificationKey = String;
207
208/// A VRF verification key.
209pub type VrfVerificationKey = String;
210
211/// A signature (hex-encoded).
212pub type Signature = String;
213
214/// Metadata labels type.
215pub type MetadataLabels = HashMap<String, Metadatum>;
216
217/// Metadata value that can be various types.
218#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
219#[serde(untagged)]
220pub enum Metadatum {
221    /// Integer value.
222    Int(i128),
223    /// Byte string (hex-encoded).
224    Bytes(String),
225    /// Text string.
226    String(String),
227    /// List of metadatums.
228    List(Vec<Metadatum>),
229    /// Map of metadatums.
230    Map(Vec<MetadatumMapEntry>),
231}
232
233/// A key-value entry in a metadatum map.
234#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
235pub struct MetadatumMapEntry {
236    pub k: Metadatum,
237    pub v: Metadatum,
238}
239
240/// Anchor for governance actions.
241#[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/// Credential origin - either from a key or a script.
249#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
250#[serde(rename_all = "camelCase")]
251pub enum CredentialOrigin {
252    Key,
253    Script,
254}
255
256/// A stake credential.
257#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
258#[serde(untagged)]
259pub enum StakeCredential {
260    /// Key-based credential.
261    Key { key: DigestBlake2b224 },
262    /// Script-based credential.
263    Script { script: ScriptHash },
264}
265
266/// A payment credential.
267pub type PaymentCredential = StakeCredential;
268
269/// Nullable type wrapper.
270#[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}