ogmios_client/
transactions.rs

1//! Requests to evalute and submit transactions via Ogmios`.
2
3use crate::{ByNameParamsBuilder, OgmiosClient, OgmiosClientError, types::OgmiosTx};
4use serde::Deserialize;
5
6/// Trait that defines the methods for evaluating and submitting transactions via Ogmios.
7pub trait Transactions {
8	/// Evaluates a transaction.
9	///
10	/// Does not support additional UTXO inputs yet.
11	///
12	/// Parameters:
13	/// - `tx_bytes: &[u8]` - CBOR-serialized transaction
14	#[allow(async_fn_in_trait)]
15	async fn evaluate_transaction(
16		&self,
17		tx_bytes: &[u8],
18	) -> Result<Vec<OgmiosEvaluateTransactionResponse>, OgmiosClientError>;
19
20	/// Submits a signed transaction.
21	///
22	/// Parameters:
23	/// - `tx_bytes: &[u8]` - CBOR-serialized signed transaction
24	#[allow(async_fn_in_trait)]
25	async fn submit_transaction(
26		&self,
27		tx_bytes: &[u8],
28	) -> Result<SubmitTransactionResponse, OgmiosClientError>;
29}
30
31impl<T: OgmiosClient> Transactions for T {
32	async fn evaluate_transaction(
33		&self,
34		tx_bytes: &[u8],
35	) -> Result<Vec<OgmiosEvaluateTransactionResponse>, OgmiosClientError> {
36		let params = ByNameParamsBuilder::new()
37			.insert("transaction", serde_json::json!({"cbor": hex::encode(tx_bytes)}))?
38			.insert("additionalUtxo", serde_json::json!([]))?
39			.build();
40		self.request("evaluateTransaction", params).await
41	}
42
43	async fn submit_transaction(
44		&self,
45		tx_bytes: &[u8],
46	) -> Result<SubmitTransactionResponse, OgmiosClientError> {
47		let params = ByNameParamsBuilder::new()
48			.insert("transaction", serde_json::json!({"cbor": hex::encode(tx_bytes)}))?
49			.build();
50		self.request("submitTransaction", params).await
51	}
52}
53
54#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Default)]
55#[serde(rename_all = "camelCase")]
56/// Represents the response from evaluating a transaction.
57pub struct OgmiosEvaluateTransactionResponse {
58	/// The smart contract index.
59	pub validator: OgmiosValidatorIndex,
60	/// The costs of smart contract execution.
61	pub budget: OgmiosBudget,
62}
63
64#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Default)]
65/// Represents the smart contract index.
66pub struct OgmiosValidatorIndex {
67	/// The index of the smart contract.
68	pub index: u32,
69	/// The purpose of the smart contract. Values allowed are:
70	/// * spend
71	/// * mint
72	/// * publish
73	/// * withdraw
74	/// * vote
75	/// * propose
76	pub purpose: String,
77}
78
79impl OgmiosValidatorIndex {
80	/// Creates a new smart contract index.
81	pub fn new(index: u32, purpose: &str) -> Self {
82		Self { index, purpose: purpose.into() }
83	}
84}
85
86#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Default)]
87/// Represents the budget for smart contract execution.
88pub struct OgmiosBudget {
89	/// The memory budget.
90	pub memory: u64,
91	/// The CPU budget.
92	pub cpu: u64,
93}
94
95impl OgmiosBudget {
96	/// Creates a new budget.
97	pub fn new(memory: u64, cpu: u64) -> Self {
98		Self { memory, cpu }
99	}
100}
101
102#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Default)]
103/// Represents the response from submitting a transaction.
104pub struct SubmitTransactionResponse {
105	/// The transaction hash.
106	pub transaction: OgmiosTx,
107}