Skip to main content

tx3_tir/model/
core.rs

1use std::collections::HashSet;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Serialize, Deserialize, Debug, Clone, Hash, PartialEq, Eq)]
6pub struct UtxoRef {
7    pub txid: Vec<u8>,
8    pub index: u32,
9}
10
11impl UtxoRef {
12    pub fn new(txid: &[u8], index: u32) -> Self {
13        Self {
14            txid: txid.to_vec(),
15            index,
16        }
17    }
18}
19
20impl std::fmt::Display for UtxoRef {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        write!(f, "{}#{}", hex::encode(&self.txid), self.index)
23    }
24}
25
26#[derive(Serialize, Deserialize, Debug, Clone)]
27pub struct Utxo {
28    pub r#ref: UtxoRef,
29    pub address: Vec<u8>,
30    pub assets: super::assets::CanonicalAssets,
31
32    // TODO: we should remove the dependency on v1beta0 here and treat the UTxO fields as data instead of expressions
33    pub datum: Option<super::v1beta0::Expression>,
34    pub script: Option<super::v1beta0::Expression>,
35}
36
37impl std::hash::Hash for Utxo {
38    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
39        self.r#ref.hash(state);
40    }
41}
42
43impl PartialEq for Utxo {
44    fn eq(&self, other: &Self) -> bool {
45        self.r#ref == other.r#ref
46    }
47}
48
49impl Eq for Utxo {}
50
51pub type UtxoSet = HashSet<Utxo>;
52
53#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
54pub enum Type {
55    Undefined,
56    Unit,
57    Int,
58    Bool,
59    Bytes,
60    Address,
61    Utxo,
62    UtxoRef,
63    AnyAsset,
64    List,
65    Map,
66    Custom(String),
67}