use serde::{Deserialize, Serialize};
pub mod stringified_u64 {
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S: Serializer>(v: &u64, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&v.to_string())
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<u64, D::Error> {
let s = String::deserialize(d)?;
s.parse().map_err(serde::de::Error::custom)
}
}
pub mod stringified_i128 {
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S: Serializer>(v: &i128, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&v.to_string())
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<i128, D::Error> {
let s = String::deserialize(d)?;
s.parse().map_err(serde::de::Error::custom)
}
}
macro_rules! hex_newtype {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct $name(pub String);
impl $name {
#[inline]
pub fn new(hex: impl Into<String>) -> Self { Self(hex.into()) }
#[inline]
pub fn as_str(&self) -> &str { &self.0 }
}
impl From<String> for $name {
#[inline]
fn from(s: String) -> Self { Self(s) }
}
impl From<&str> for $name {
#[inline]
fn from(s: &str) -> Self { Self(s.to_string()) }
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
};
}
hex_newtype!(
TxHash
);
hex_newtype!(
ScriptHash
);
hex_newtype!(
PolicyId
);
hex_newtype!(
AssetName
);
hex_newtype!(
DataHash
);
hex_newtype!(
KeyHash
);
hex_newtype!(
PoolId
);
hex_newtype!(
DRepKeyHash
);
hex_newtype!(
VrfKeyHash
);
hex_newtype!(
Hash28
);
hex_newtype!(
Hash32
);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Lovelace(#[serde(with = "stringified_u64")] pub u64);
impl From<u64> for Lovelace {
#[inline]
fn from(v: u64) -> Self { Self(v) }
}
impl Lovelace {
#[inline]
pub fn get(self) -> u64 { self.0 }
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Quantity(#[serde(with = "stringified_u64")] pub u64);
impl From<u64> for Quantity {
#[inline]
fn from(v: u64) -> Self { Self(v) }
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct MintQuantity(#[serde(with = "stringified_i128")] pub i128);
impl From<i128> for MintQuantity {
#[inline]
fn from(v: i128) -> Self { Self(v) }
}
impl From<i64> for MintQuantity {
#[inline]
fn from(v: i64) -> Self { Self(v as i128) }
}
pub type Slot = u64;
pub type Epoch = u64;