use crate::schema::{Block, Datum, Lovelace, Point, Script, TransactionOutput, Value};
pub const CONSTANT_OUTPUT_SERIALIZATION_OVERHEAD: u64 = 160;
pub fn is_block_ebb(block: &Block) -> bool {
matches!(block, Block::EBB(_))
}
pub fn is_block_bft(block: &Block) -> bool {
matches!(block, Block::BFT(_))
}
pub fn is_block_praos(block: &Block) -> bool {
matches!(block, Block::Praos(_))
}
pub fn is_object<T>(_value: &T) -> bool {
true
}
pub fn utxo_size(output: &TransactionOutput) -> u64 {
let address_size = size_of_address(&output.address);
let value_size = size_of_value(&output.value);
let datum_size = if let Some(ref datum) = output.datum {
size_of_inline_datum(datum)
} else if let Some(ref _datum_hash) = output.datum_hash {
size_of_datum_hash()
} else {
0
};
let script_size = if let Some(ref script) = output.script {
size_of_script(script)
} else {
0
};
CONSTANT_OUTPUT_SERIALIZATION_OVERHEAD + address_size + value_size + datum_size + script_size
}
fn size_of_integer(value: u64) -> u64 {
if value < 24 {
1
} else if value < 256 {
2
} else if value < 65536 {
3
} else if value < 4294967296 {
5
} else {
9
}
}
fn size_of_bytes_def(len: u64) -> u64 {
size_of_integer(len) + len
}
fn size_of_array_def(len: u64) -> u64 {
size_of_integer(len)
}
fn size_of_address(address: &str) -> u64 {
let len = address.len() as u64;
if address.starts_with("addr") || address.starts_with("stake") {
size_of_bytes_def(57)
} else {
size_of_bytes_def(len / 2)
}
}
fn size_of_value(value: &Value) -> u64 {
match value {
Value::AdaOnly { ada } => {
size_of_integer(ada.lovelace)
}
Value::WithAssets { ada, assets } => {
let mut size = size_of_array_def(2); size += size_of_integer(ada.lovelace);
size += size_of_array_def(assets.len() as u64);
for (_policy_id, asset_map) in assets {
size += size_of_bytes_def(28);
size += size_of_array_def(asset_map.len() as u64);
for (asset_name, quantity) in asset_map {
size += size_of_bytes_def(asset_name.len() as u64 / 2);
size += size_of_integer(*quantity as u64);
}
}
size
}
}
}
fn size_of_inline_datum(datum: &Datum) -> u64 {
match datum {
Datum::Cbor(cbor) => {
let datum_bytes = cbor.len() as u64 / 2;
1 + size_of_bytes_def(datum_bytes)
}
Datum::Value(_) => {
64
}
}
}
fn size_of_datum_hash() -> u64 {
size_of_bytes_def(32)
}
fn size_of_script(script: &Script) -> u64 {
match script {
Script::Native { cbor, .. } => {
if let Some(cbor) = cbor {
size_of_bytes_def(cbor.len() as u64 / 2)
} else {
32
}
}
Script::PlutusV1 { cbor } | Script::PlutusV2 { cbor } | Script::PlutusV3 { cbor } => {
size_of_bytes_def(cbor.len() as u64 / 2)
}
}
}
pub fn parse_point(value: &serde_json::Value) -> Option<Point> {
if let Some(s) = value.as_str() {
if s == "origin" {
return Some(Point::origin());
}
}
if let Some(obj) = value.as_object() {
if let (Some(slot), Some(id)) = (obj.get("slot"), obj.get("id")) {
if let (Some(slot), Some(id)) = (slot.as_u64(), id.as_str()) {
return Some(Point::at(slot, id));
}
}
}
None
}
pub fn lovelace_to_ada(lovelace: Lovelace) -> String {
let ada = lovelace as f64 / 1_000_000.0;
format!("{:.6}", ada)
}
pub fn ada_to_lovelace(ada: f64) -> Lovelace {
(ada * 1_000_000.0) as Lovelace
}
pub fn hex_encode(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{:02x}", b)).collect()
}
pub fn hex_decode(s: &str) -> Result<Vec<u8>, std::num::ParseIntError> {
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lovelace_to_ada() {
assert_eq!(lovelace_to_ada(1_000_000), "1.000000");
assert_eq!(lovelace_to_ada(1_500_000), "1.500000");
assert_eq!(lovelace_to_ada(500_000), "0.500000");
}
#[test]
fn test_ada_to_lovelace() {
assert_eq!(ada_to_lovelace(1.0), 1_000_000);
assert_eq!(ada_to_lovelace(1.5), 1_500_000);
assert_eq!(ada_to_lovelace(0.5), 500_000);
}
#[test]
fn test_size_of_integer() {
assert_eq!(size_of_integer(0), 1);
assert_eq!(size_of_integer(23), 1);
assert_eq!(size_of_integer(24), 2);
assert_eq!(size_of_integer(255), 2);
assert_eq!(size_of_integer(256), 3);
}
#[test]
fn test_hex_encode_decode() {
let bytes = vec![0xde, 0xad, 0xbe, 0xef];
let encoded = hex_encode(&bytes);
assert_eq!(encoded, "deadbeef");
let decoded = hex_decode(&encoded).unwrap();
assert_eq!(decoded, bytes);
}
#[test]
fn test_parse_point() {
let origin = serde_json::json!("origin");
assert_eq!(parse_point(&origin), Some(Point::origin()));
let point = serde_json::json!({
"slot": 12345,
"id": "abc123"
});
assert_eq!(parse_point(&point), Some(Point::at(12345, "abc123")));
}
}