Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/tx3-cardano/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr as _;

use pallas::{codec::utils::Int, ledger::primitives::conway as primitives};
use tx3_tir::compile::Error;
use tx3_tir::model::core::UtxoRef;
use tx3_tir::model::core::{UtxoRef, Utxo};
use tx3_tir::model::v1beta0 as tir;

use crate::Network;
Expand Down Expand Up @@ -86,6 +86,18 @@ pub fn expr_into_utxo_refs(expr: &tir::Expression) -> Result<Vec<UtxoRef>, Error
}
}

pub fn expr_into_utxo(expr: &tir::Expression) -> Result<Vec<Utxo>, Error> {
match expr {
tir::Expression::UtxoSet(x) => Ok(Vec::from_iter(x.iter().map(|x| x.clone()))),
tir::Expression::UtxoRefs(_) => Ok(vec![]),
tir::Expression::String(_) => Ok(vec![]),
_ => Err(Error::CoerceError(
format!("{expr:?}"),
"Utxo".to_string(),
)),
}
}

pub fn expr_into_assets(ir: &tir::Expression) -> Result<Vec<tir::AssetExpr>, Error> {
match ir {
tir::Expression::Assets(x) => Ok(x.clone()),
Expand Down
81 changes: 79 additions & 2 deletions crates/tx3-cardano/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,82 @@ fn compile_collateral(tx: &tir::Tx) -> Result<Vec<TransactionInput>, Error> {
.collect())
}

fn compile_collateral_return(tx: &tir::Tx) -> Result<Option<primitives::TransactionOutput<'static>>, Error> {
if tx.collateral.is_empty() {
return Ok(None);
}

// Is it okay to sum all collateral UTxOs amounts?
let collateral_amount = tx
.collateral
.iter()
.filter_map(|collateral| collateral.utxos.as_option())
.flat_map(coercion::expr_into_utxo)
.flatten()
.map(|x| x.assets.naked_amount().unwrap_or(0))
.reduce(|acc, x| acc + x)
.unwrap_or(0);

let fees = coercion::expr_into_number(&tx.fees)?;

// We should substract the min_amount from the collateral block
let return_amount = collateral_amount - fees;

if return_amount <= 0 {
return Ok(None);
}

// Is it okay to take the address from the first collateral UTxO?
let address = tx
.collateral
.iter()
.filter_map(|collateral| collateral.utxos.as_option())
.flat_map(coercion::expr_into_utxo)
.flatten()
.next()
.ok_or(Error::MissingExpression(
"collateral return address".to_string(),
))?.address.clone();

return Ok(Some(primitives::TransactionOutput::PostAlonzo(
primitives::PostAlonzoTransactionOutput {
address: crate::compile::primitives::Bytes::from(address),
value: value!(return_amount as u64),
datum_option: None,
script_ref: None,
}
.into(),
)));
}

fn compile_total_collateral(tx: &tir::Tx) -> Result<Option<u64>, Error> {
if tx.collateral.is_empty() {
return Ok(None);
}

// Is it okay to sum all collateral UTxOs amounts?
let collateral_amount = tx
.collateral
.iter()
.filter_map(|collateral| collateral.utxos.as_option())
.flat_map(coercion::expr_into_utxo)
.flatten()
.map(|x| x.assets.naked_amount().unwrap_or(0))
.reduce(|acc, x| acc + x)
.unwrap_or(0);

let fees = coercion::expr_into_number(&tx.fees)?;

// We should substract the min_amount from the collateral block
let return_amount = collateral_amount - fees;

if return_amount <= 0 {
return Ok(None);
}

return Ok(Some(return_amount as u64));
}

fn compile_required_signers(tx: &tir::Tx) -> Result<Option<primitives::RequiredSigners>, Error> {
let Some(signers) = &tx.signers else {
return Ok(None);
Expand Down Expand Up @@ -528,8 +604,9 @@ fn compile_tx_body(
script_data_hash: None,
collateral: primitives::NonEmptySet::from_vec(compile_collateral(tx)?),
required_signers: compile_required_signers(tx)?,
collateral_return: None,
total_collateral: None,
// I don't think it's necessary to declare the collateral return, with the total collateral looks like it's enough?
collateral_return: compile_collateral_return(tx)?,
total_collateral: compile_total_collateral(tx)?,
voting_procedures: None,
proposal_procedures: None,
treasury_value: None,
Expand Down