Skip to main content

ogmios_client/schema/
transaction.rs

1//! Transaction types for Cardano.
2
3use super::certificates::Certificate;
4use super::primitives::*;
5use super::scripts::{Datum, Redeemer, Script};
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9/// A Cardano transaction.
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Transaction {
13    /// Transaction ID.
14    pub id: TransactionId,
15    /// Whether the transaction is valid.
16    #[serde(default = "default_true")]
17    pub valid: bool,
18    /// Transaction inputs.
19    #[serde(default)]
20    pub inputs: Vec<TransactionInput>,
21    /// Transaction outputs.
22    #[serde(default)]
23    pub outputs: Vec<TransactionOutput>,
24    /// Collateral inputs (for Plutus transactions).
25    #[serde(default)]
26    pub collaterals: Vec<TransactionInput>,
27    /// Collateral return output.
28    #[serde(default)]
29    pub collateral_return: Option<TransactionOutput>,
30    /// Total collateral amount.
31    #[serde(default)]
32    pub total_collateral: Option<Lovelace>,
33    /// Reference inputs.
34    #[serde(default)]
35    pub references: Vec<TransactionInput>,
36    /// Transaction fee.
37    #[serde(default)]
38    pub fee: Option<Lovelace>,
39    /// Validity interval start (slot).
40    #[serde(default)]
41    pub valid_from: Option<Slot>,
42    /// Validity interval end (slot).
43    #[serde(default)]
44    pub valid_until: Option<Slot>,
45    /// Certificates included in the transaction.
46    #[serde(default)]
47    pub certificates: Vec<Certificate>,
48    /// Withdrawals from reward accounts.
49    #[serde(default)]
50    pub withdrawals: HashMap<RewardAccount, Lovelace>,
51    /// Minted/burned assets.
52    #[serde(default)]
53    pub mint: Assets,
54    /// Required signers (for Plutus).
55    #[serde(default)]
56    pub required_extra_signers: Vec<DigestBlake2b224>,
57    /// Required scripts.
58    #[serde(default)]
59    pub required_extra_scripts: Vec<ScriptHash>,
60    /// Network ID.
61    #[serde(default)]
62    pub network: Option<String>,
63    /// Script integrity hash.
64    #[serde(default)]
65    pub script_integrity_hash: Option<DigestBlake2b256>,
66    /// Witness set.
67    #[serde(default)]
68    pub witnesses: Option<Witnesses>,
69    /// Metadata.
70    #[serde(default)]
71    pub metadata: Option<Metadata>,
72    /// CBOR representation (hex-encoded).
73    #[serde(default)]
74    pub cbor: Option<String>,
75    /// Proposals (Conway era).
76    #[serde(default)]
77    pub proposals: Vec<serde_json::Value>,
78    /// Votes (Conway era).
79    #[serde(default)]
80    pub votes: Vec<serde_json::Value>,
81}
82
83fn default_true() -> bool {
84    true
85}
86
87/// A transaction input.
88#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
89#[serde(rename_all = "camelCase")]
90pub struct TransactionInput {
91    /// Transaction ID containing the output.
92    pub transaction: TransactionOutputReference,
93}
94
95/// Reference to a transaction output.
96#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
97#[serde(rename_all = "camelCase")]
98pub struct TransactionOutputReference {
99    /// Transaction ID.
100    pub id: TransactionId,
101    /// Output index.
102    pub index: u32,
103}
104
105impl TransactionOutputReference {
106    pub fn new(id: impl Into<String>, index: u32) -> Self {
107        Self {
108            id: id.into(),
109            index,
110        }
111    }
112}
113
114/// A transaction output.
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116#[serde(rename_all = "camelCase")]
117pub struct TransactionOutput {
118    /// Output address.
119    pub address: Address,
120    /// Output value.
121    pub value: Value,
122    /// Datum hash (Alonzo style).
123    #[serde(default)]
124    pub datum_hash: Option<DatumHash>,
125    /// Inline datum (Babbage style).
126    #[serde(default)]
127    pub datum: Option<Datum>,
128    /// Reference script.
129    #[serde(default)]
130    pub script: Option<Script>,
131}
132
133/// UTXO - a transaction output with its reference.
134#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
135#[serde(rename_all = "camelCase")]
136pub struct Utxo {
137    /// Transaction reference.
138    pub transaction: TransactionOutputReference,
139    /// The output.
140    #[serde(flatten)]
141    pub output: TransactionOutput,
142}
143
144/// Witness set for a transaction.
145#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
146#[serde(rename_all = "camelCase")]
147pub struct Witnesses {
148    /// Key witnesses.
149    #[serde(default)]
150    pub keys: Vec<KeyWitness>,
151    /// Script witnesses.
152    #[serde(default)]
153    pub scripts: HashMap<ScriptHash, Script>,
154    /// Bootstrap witnesses (Byron).
155    #[serde(default)]
156    pub bootstrap: Vec<BootstrapWitness>,
157    /// Datums.
158    #[serde(default)]
159    pub datums: HashMap<DatumHash, Datum>,
160    /// Redeemers.
161    #[serde(default)]
162    pub redeemers: Vec<Redeemer>,
163}
164
165/// A key witness (signature).
166#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
167#[serde(rename_all = "camelCase")]
168pub struct KeyWitness {
169    /// Verification key.
170    pub key: VerificationKey,
171    /// Signature.
172    pub signature: Signature,
173}
174
175/// Bootstrap witness for Byron-era transactions.
176#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
177#[serde(rename_all = "camelCase")]
178pub struct BootstrapWitness {
179    /// Verification key.
180    pub key: VerificationKey,
181    /// Chain code.
182    pub chain_code: String,
183    /// Address attributes.
184    pub address_attributes: String,
185    /// Signature.
186    pub signature: Signature,
187}
188
189/// Transaction metadata.
190#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
191#[serde(rename_all = "camelCase")]
192pub struct Metadata {
193    /// Metadata labels and values.
194    #[serde(default)]
195    pub labels: MetadataLabels,
196    /// Hash of the metadata.
197    #[serde(default)]
198    pub hash: Option<DigestBlake2b256>,
199}
200
201/// Input source type.
202#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
203#[serde(rename_all = "camelCase")]
204pub enum InputSource {
205    /// Input is from spending.
206    Inputs,
207    /// Input is from collateral.
208    Collaterals,
209}
210
211/// Transaction evaluation result.
212#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
213#[serde(rename_all = "camelCase")]
214pub struct EvaluationResult {
215    /// Validator index.
216    pub validator: ValidatorIndex,
217    /// Execution budget used.
218    pub budget: ExUnits,
219}
220
221/// Validator index in a transaction.
222#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
223#[serde(rename_all = "camelCase")]
224pub struct ValidatorIndex {
225    /// Purpose of the script.
226    pub purpose: ScriptPurpose,
227    /// Index.
228    pub index: u32,
229}
230
231/// Purpose of a script execution.
232#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
233#[serde(rename_all = "camelCase")]
234pub enum ScriptPurpose {
235    /// Spending a UTXO.
236    Spend,
237    /// Minting/burning tokens.
238    Mint,
239    /// Publishing a certificate.
240    Publish,
241    /// Withdrawing rewards.
242    Withdraw,
243    /// Proposing governance action (Conway).
244    Propose,
245    /// Voting (Conway).
246    Vote,
247}
248
249/// Execution units (memory and CPU steps).
250#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
251#[serde(rename_all = "camelCase")]
252pub struct ExUnits {
253    /// Memory units.
254    pub memory: u64,
255    /// CPU steps.
256    pub cpu: u64,
257}
258
259impl ExUnits {
260    pub fn new(memory: u64, cpu: u64) -> Self {
261        Self { memory, cpu }
262    }
263}