use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type Slot = u64;
pub type Epoch = u64;
pub type BlockHeight = u64;
pub type Lovelace = u64;
pub type LovelaceDelta = i128;
pub type TransactionId = String;
pub type PolicyId = String;
pub type AssetName = String;
pub type AssetQuantity = i128;
pub type ScriptHash = String;
pub type DatumHash = String;
pub type PoolId = String;
pub type StakePoolId = String;
pub type VerificationKeyHash = String;
pub type Digest = String;
pub type DigestBlake2b224 = String;
pub type DigestBlake2b256 = String;
pub type RatioString = String;
pub type NetworkMagic = u32;
pub type RelativeTime = f64;
pub type UtcTime = String;
pub type NumberOfBytes = u64;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Origin;
impl Origin {
pub const ORIGIN: &'static str = "origin";
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Point {
Origin(String),
Point {
slot: Slot,
id: DigestBlake2b256,
},
}
impl Point {
pub fn origin() -> Self {
Point::Origin("origin".to_string())
}
pub fn at(slot: Slot, id: impl Into<String>) -> Self {
Point::Point { slot, id: id.into() }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Tip {
Origin(String),
Tip {
slot: Slot,
id: DigestBlake2b256,
height: BlockHeight,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Ratio {
pub numerator: u64,
pub denominator: u64,
}
impl Ratio {
pub fn new(numerator: u64, denominator: u64) -> Self {
Self { numerator, denominator }
}
pub fn to_f64(&self) -> f64 {
self.numerator as f64 / self.denominator as f64
}
}
pub type Assets = HashMap<PolicyId, HashMap<AssetName, AssetQuantity>>;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Value {
AdaOnly {
ada: AdaValue,
},
WithAssets {
ada: AdaValue,
#[serde(flatten)]
assets: Assets,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AdaValue {
pub lovelace: Lovelace,
}
impl Value {
pub fn ada_only(lovelace: Lovelace) -> Self {
Value::AdaOnly {
ada: AdaValue { lovelace },
}
}
pub fn lovelace(&self) -> Lovelace {
match self {
Value::AdaOnly { ada } => ada.lovelace,
Value::WithAssets { ada, .. } => ada.lovelace,
}
}
}
pub type Address = String;
pub type StakeAddress = String;
pub type RewardAccount = String;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Nonce {
Neutral,
#[serde(untagged)]
Value(String),
}
pub type VerificationKey = String;
pub type ExtendedVerificationKey = String;
pub type KesVerificationKey = String;
pub type VrfVerificationKey = String;
pub type Signature = String;
pub type MetadataLabels = HashMap<String, Metadatum>;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Metadatum {
Int(i128),
Bytes(String),
String(String),
List(Vec<Metadatum>),
Map(Vec<MetadatumMapEntry>),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MetadatumMapEntry {
pub k: Metadatum,
pub v: Metadatum,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Anchor {
pub url: String,
#[serde(rename = "hash")]
pub content_hash: DigestBlake2b256,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CredentialOrigin {
Key,
Script,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StakeCredential {
Key { key: DigestBlake2b224 },
Script { script: ScriptHash },
}
pub type PaymentCredential = StakeCredential;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Nullable<T> {
Null,
Value(T),
}
impl<T> Nullable<T> {
pub fn is_null(&self) -> bool {
matches!(self, Nullable::Null)
}
pub fn into_option(self) -> Option<T> {
match self {
Nullable::Null => None,
Nullable::Value(v) => Some(v),
}
}
}
impl<T> From<Option<T>> for Nullable<T> {
fn from(opt: Option<T>) -> Self {
match opt {
Some(v) => Nullable::Value(v),
None => Nullable::Null,
}
}
}