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
65 changes: 54 additions & 11 deletions crates/tx3-cardano/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,67 @@ fn compile_outputs(
tx: &tir::Tx,
network: Network,
) -> Result<Vec<primitives::TransactionOutput<'static>>, Error> {
let mut resolved: Vec<_> = tx
.outputs
.iter()
.map(|out| (out.optional, compile_output_block(out, network)))
.filter(|(optional, output)| !optional || output_has_assets(output))
.map(|(_, output)| output)
.collect::<Result<Vec<_>, _>>()?;
let regular_outputs = tx.outputs.iter().filter_map(|out| {
let compiled = compile_output_block(out, network);

if out.optional && !output_has_assets(&compiled) {
return None;
}

let idx = out.index.as_number().map(|n| n as usize);
Some(compiled.map(|o| (idx, o)))
});

let cardano_outputs = tx
let publish_outputs = tx
.adhoc
.iter()
.filter(|x| x.name.as_str() == "cardano_publish")
.map(|adhoc| compile_cardano_publish_directive(adhoc, network))
.map(|adhoc| {
let idx = adhoc
.data
.get("index")
.and_then(|expr| expr.as_number())
.map(|n| n as usize);

compile_cardano_publish_directive(adhoc, network).map(|o| (idx, o))
});

let all: Vec<_> = publish_outputs
.chain(regular_outputs)
.collect::<Result<Vec<_>, _>>()?;

resolved.extend(cardano_outputs);
order_by_index(all)
}

fn order_by_index(
outputs: Vec<(Option<usize>, primitives::TransactionOutput<'static>)>,
) -> Result<Vec<primitives::TransactionOutput<'static>>, Error> {
let total = outputs.len();
let mut slots: Vec<Option<primitives::TransactionOutput<'static>>> = vec![None; total];
let mut non_indexed = Vec::new();

for (idx, compiled) in outputs {
match idx {
Some(pos) => {
if pos >= total {
return Err(Error::ConsistencyError(format!(
"output index {pos} is out of range (total outputs: {total})"
)));
}
slots[pos] = Some(compiled);
}
None => non_indexed.push(compiled),
}
}

let mut filler = non_indexed.into_iter();
for slot in &mut slots {
if slot.is_none() {
*slot = filler.next();
}
}

Ok(resolved)
Ok(slots.into_iter().flatten().collect())
}

pub fn compile_cardano_publish_directive(
Expand Down
2 changes: 2 additions & 0 deletions crates/tx3-lang/src/analyzing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ impl Analyzable for OutputBlockField {
OutputBlockField::To(x) => x.analyze(parent),
OutputBlockField::Amount(x) => x.analyze(parent),
OutputBlockField::Datum(x) => x.analyze(parent),
OutputBlockField::Index(x) => x.analyze(parent),
}
}

Expand All @@ -1027,6 +1028,7 @@ impl Analyzable for OutputBlockField {
OutputBlockField::To(x) => x.is_resolved(),
OutputBlockField::Amount(x) => x.is_resolved(),
OutputBlockField::Datum(x) => x.is_resolved(),
OutputBlockField::Index(x) => x.is_resolved(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/tx3-lang/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ pub enum OutputBlockField {
To(Box<DataExpr>),
Amount(Box<DataExpr>),
Datum(Box<DataExpr>),
Index(Box<DataExpr>),
}

impl OutputBlockField {
Expand All @@ -394,6 +395,7 @@ impl OutputBlockField {
OutputBlockField::To(_) => "to",
OutputBlockField::Amount(_) => "amount",
OutputBlockField::Datum(_) => "datum",
OutputBlockField::Index(_) => "index",
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion crates/tx3-lang/src/cardano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ pub enum CardanoPublishBlockField {
Datum(Box<DataExpr>),
Version(Box<DataExpr>),
Script(Box<DataExpr>),
Index(Box<DataExpr>),
}

impl CardanoPublishBlockField {
Expand All @@ -577,6 +578,7 @@ impl CardanoPublishBlockField {
CardanoPublishBlockField::Datum(_) => "datum",
CardanoPublishBlockField::Version(_) => "version",
CardanoPublishBlockField::Script(_) => "script",
CardanoPublishBlockField::Index(_) => "index",
}
}
}
Expand Down Expand Up @@ -627,6 +629,12 @@ impl AstNode for CardanoPublishBlockField {
DataExpr::parse(pair)?.into(),
))
}
Rule::cardano_publish_block_index => {
let pair = pair.into_inner().next().unwrap();
Ok(CardanoPublishBlockField::Index(
DataExpr::parse(pair)?.into(),
))
}
x => unreachable!("Unexpected rule in cardano_publish_block_field: {:?}", x),
}
}
Expand All @@ -638,6 +646,7 @@ impl AstNode for CardanoPublishBlockField {
Self::Datum(x) => x.span(),
Self::Version(x) => x.span(),
Self::Script(x) => x.span(),
Self::Index(x) => x.span(),
}
}
}
Expand Down Expand Up @@ -679,6 +688,7 @@ impl Analyzable for CardanoPublishBlockField {
CardanoPublishBlockField::Datum(x) => x.analyze(parent),
CardanoPublishBlockField::Version(x) => x.analyze(parent),
CardanoPublishBlockField::Script(x) => x.analyze(parent),
CardanoPublishBlockField::Index(x) => x.analyze(parent),
}
}

Expand All @@ -689,6 +699,7 @@ impl Analyzable for CardanoPublishBlockField {
CardanoPublishBlockField::Datum(x) => x.is_resolved(),
CardanoPublishBlockField::Version(x) => x.is_resolved(),
CardanoPublishBlockField::Script(x) => x.is_resolved(),
CardanoPublishBlockField::Index(x) => x.is_resolved(),
}
}
}
Expand Down Expand Up @@ -725,6 +736,7 @@ impl IntoLower for CardanoPublishBlockField {
}
CardanoPublishBlockField::Version(x) => Ok(("version".to_string(), x.into_lower(ctx)?)),
CardanoPublishBlockField::Script(x) => Ok(("script".to_string(), x.into_lower(ctx)?)),
CardanoPublishBlockField::Index(x) => Ok(("index".to_string(), x.into_lower(ctx)?)),
}
}
}
Expand All @@ -736,7 +748,7 @@ impl IntoLower for CardanoPublishBlock {
&self,
ctx: &crate::lowering::Context,
) -> Result<Self::Output, crate::lowering::Error> {
let data = self
let data: HashMap<String, ir::Expression> = self
.fields
.iter()
.map(|x| x.into_lower(ctx))
Expand Down
3 changes: 3 additions & 0 deletions crates/tx3-lang/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ impl IntoLower for ast::OutputBlockField {
let ctx = ctx.enter_datum_expr();
x.into_lower(&ctx)
}
ast::OutputBlockField::Index(x) => x.into_lower(ctx),
}
}
}
Expand All @@ -697,12 +698,14 @@ impl IntoLower for ast::OutputBlock {
let address = self.find("to").into_lower(ctx)?.unwrap_or_default();
let datum = self.find("datum").into_lower(ctx)?.unwrap_or_default();
let amount = self.find("amount").into_lower(ctx)?.unwrap_or_default();
let index = self.find("index").into_lower(ctx)?.unwrap_or_default();

Ok(ir::Output {
address,
datum,
amount,
optional: self.optional,
index,
})
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/tx3-lang/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,11 @@ impl AstNode for OutputBlockField {
let x = OutputBlockField::Datum(DataExpr::parse(pair)?.into());
Ok(x)
}
Rule::output_block_index => {
let pair = pair.into_inner().next().unwrap();
let x = OutputBlockField::Index(DataExpr::parse(pair)?.into());
Ok(x)
}
x => unreachable!("Unexpected rule in output_block_field: {:?}", x),
}
}
Expand All @@ -578,6 +583,7 @@ impl AstNode for OutputBlockField {
Self::To(x) => x.span(),
Self::Amount(x) => x.span(),
Self::Datum(x) => x.span(),
Self::Index(x) => x.span(),
}
}
}
Expand Down Expand Up @@ -2817,4 +2823,6 @@ mod tests {
test_parsing!(list_concat);

test_parsing!(buidler_fest_2026);

test_parsing!(index_outputs);
}
8 changes: 6 additions & 2 deletions crates/tx3-lang/src/tx3.pest
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,13 @@ reference_block = {
output_block_to = { "to" ~ ":" ~ data_expr }
output_block_amount = { "amount" ~ ":" ~ data_expr }
output_block_datum = { "datum" ~ ":" ~ data_expr }
output_block_index = { "index" ~ ":" ~ data_expr }

output_block_field = _{
output_block_to |
output_block_amount |
output_block_datum
output_block_datum |
output_block_index
}

output_block = {
Expand Down Expand Up @@ -374,14 +376,16 @@ cardano_publish_block_amount = { "amount" ~ ":" ~ data_expr }
cardano_publish_block_datum = { "datum" ~ ":" ~ data_expr }
cardano_publish_block_version = { "version" ~ ":" ~ data_expr }
cardano_publish_block_script = { "script" ~ ":" ~ data_expr }
cardano_publish_block_index = { "index" ~ ":" ~ data_expr }


cardano_publish_block_field = _{
cardano_publish_block_to |
cardano_publish_block_amount |
cardano_publish_block_datum |
cardano_publish_block_version |
cardano_publish_block_script
cardano_publish_block_script |
cardano_publish_block_index
}

cardano_publish_block = {
Expand Down
3 changes: 3 additions & 0 deletions crates/tx3-tir/src/model/v1beta0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ pub struct Output {
pub datum: Expression,
pub amount: Expression,
pub optional: bool,
#[serde(default)]
pub index: Expression,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -522,6 +524,7 @@ impl Node for Output {
datum: self.datum.apply(visitor)?,
amount: self.amount.apply(visitor)?,
optional: self.optional,
index: self.index,
};

Ok(visited)
Expand Down
1 change: 1 addition & 0 deletions crates/tx3-tir/src/reduce/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ impl Composite for Output {
datum: f(self.datum)?,
amount: f(self.amount)?,
optional: self.optional,
index: self.index,
})
}
}
Expand Down
6 changes: 4 additions & 2 deletions examples/asteria.move_ship.tir
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,8 @@
]
}
},
"optional": false
"optional": false,
"index": "None"
},
{
"address": {
Expand Down Expand Up @@ -1262,7 +1263,8 @@
]
}
},
"optional": false
"optional": false,
"index": "None"
}
],
"validity": null,
Expand Down
9 changes: 6 additions & 3 deletions examples/buidler_fest_2026.buy_ticket.tir
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@
]
}
},
"optional": false
"optional": false,
"index": "None"
},
{
"address": {
Expand Down Expand Up @@ -458,7 +459,8 @@
}
}
},
"optional": false
"optional": false,
"index": "None"
},
{
"address": {
Expand Down Expand Up @@ -486,7 +488,8 @@
}
]
},
"optional": false
"optional": false,
"index": "None"
}
],
"validity": {
Expand Down
3 changes: 2 additions & 1 deletion examples/burn.burn_stuff.tir
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@
]
}
},
"optional": false
"optional": false,
"index": "None"
}
],
"validity": null,
Expand Down
3 changes: 2 additions & 1 deletion examples/cardano_witness.mint_from_native_script.tir
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
]
}
},
"optional": false
"optional": false,
"index": "None"
}
],
"validity": null,
Expand Down
9 changes: 5 additions & 4 deletions examples/cardano_witness.mint_from_plutus.tir
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
]
}
},
"optional": false
"optional": false,
"index": "None"
}
],
"validity": null,
Expand Down Expand Up @@ -202,9 +203,6 @@
{
"name": "plutus_witness",
"data": {
"version": {
"Number": 3
},
"script": {
"Bytes": [
81,
Expand All @@ -226,6 +224,9 @@
174,
105
]
},
"version": {
"Number": 3
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/donation.mint_from_plutus.tir
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@
]
}
},
"optional": false
"optional": false,
"index": "None"
}
],
"validity": null,
Expand Down
Loading
Loading