Skip to content
Draft
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
1 change: 1 addition & 0 deletions bin/tx3c/src/tii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn map_ast_type_to_json_schema(r#type: &tx3_lang::ast::Type) -> Value {
tx3_lang::ast::Type::Bytes => {
json!({ "$ref": "https://tx3.land/specs/v1beta0/core#Bytes" })
}
tx3_lang::ast::Type::AnyData => json!({}),
tx3_lang::ast::Type::Address => {
json!({ "$ref": "https://tx3.land/specs/v1beta0/core#Address" })
}
Expand Down
17 changes: 17 additions & 0 deletions crates/tx3-lang/src/analyzing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,23 @@ mod tests {
assert!(result.errors.is_empty());
}

#[test]
fn test_record_field_any_data_type_success() {
let mut ast = crate::parsing::parse_string(
r#"
type MyRecord {
field1: Int,
field2: AnyData,
}
"#,
)
.unwrap();

let result = analyze(&mut ast);

assert!(result.errors.is_empty());
}

#[test]
fn test_min_utxo_undefined_output_error() {
let mut ast = crate::parsing::parse_string(
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 @@ -809,6 +809,7 @@ pub enum Type {
Int,
Bool,
Bytes,
AnyData,
Address,
Utxo,
UtxoRef,
Expand All @@ -826,6 +827,7 @@ impl std::fmt::Display for Type {
Type::Int => write!(f, "Int"),
Type::Bool => write!(f, "Bool"),
Type::Bytes => write!(f, "Bytes"),
Type::AnyData => write!(f, "AnyData"),
Type::Address => write!(f, "Address"),
Type::UtxoRef => write!(f, "UtxoRef"),
Type::AnyAsset => write!(f, "AnyAsset"),
Expand Down
1 change: 1 addition & 0 deletions crates/tx3-lang/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ impl IntoLower for ast::Type {
ast::Type::Int => Ok(Type::Int),
ast::Type::Bool => Ok(Type::Bool),
ast::Type::Bytes => Ok(Type::Bytes),
ast::Type::AnyData => Ok(Type::AnyData),
ast::Type::Address => Ok(Type::Address),
ast::Type::Utxo => Ok(Type::Utxo),
ast::Type::UtxoRef => Ok(Type::UtxoRef),
Expand Down
3 changes: 3 additions & 0 deletions crates/tx3-lang/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,7 @@ impl AstNode for Type {
"Int" => Ok(Type::Int),
"Bool" => Ok(Type::Bool),
"Bytes" => Ok(Type::Bytes),
"AnyData" => Ok(Type::AnyData),
"Address" => Ok(Type::Address),
"UtxoRef" => Ok(Type::UtxoRef),
"AnyAsset" => Ok(Type::AnyAsset),
Expand Down Expand Up @@ -1575,6 +1576,8 @@ mod tests {

input_to_ast_check!(Type, "bytes", "Bytes", Type::Bytes);

input_to_ast_check!(Type, "any_data", "AnyData", Type::AnyData);

input_to_ast_check!(Type, "address", "Address", Type::Address);

input_to_ast_check!(Type, "utxo_ref", "UtxoRef", Type::UtxoRef);
Expand Down
1 change: 1 addition & 0 deletions crates/tx3-lang/src/tx3.pest
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ primitive_type = {
"Bool" |
"Bytes" |
"AnyAsset" |
"AnyData" |
"Address" |
"UtxoRef"
}
Expand Down
42 changes: 40 additions & 2 deletions crates/tx3-resolver/src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde_json::{Number, Value};
use thiserror::Error;

use tx3_tir::model::core::{Type, UtxoRef};
use tx3_tir::model::v1beta0::Expression;
pub use tx3_tir::reduce::ArgValue;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -188,7 +189,7 @@ fn value_to_address(value: Value) -> Result<Vec<u8>, Error> {
Ok(out)
}

fn value_to_underfined(value: Value) -> Result<ArgValue, Error> {
fn value_to_undefined(value: Value) -> Result<ArgValue, Error> {
match value {
Value::Bool(b) => Ok(ArgValue::Bool(b)),
Value::Number(x) => Ok(ArgValue::Int(number_to_bigint(x)?)),
Expand All @@ -197,6 +198,26 @@ fn value_to_underfined(value: Value) -> Result<ArgValue, Error> {
}
}

fn value_to_data_expr(value: Value) -> Result<Expression, Error> {
match value {
Value::Null => Ok(Expression::None),
Value::Bool(b) => Ok(Expression::Bool(b)),
Value::Number(n) => Ok(Expression::Number(number_to_bigint(n)?)),
Value::String(s) => Ok(Expression::String(s)),
Value::Array(items) => Ok(Expression::List(
items
.into_iter()
.map(value_to_data_expr)
.collect::<Result<Vec<_>, _>>()?,
)),
Value::Object(map) => Ok(Expression::Map(
map.into_iter()
.map(|(key, value)| Ok((Expression::String(key), value_to_data_expr(value)?)))
.collect::<Result<Vec<_>, Error>>()?,
)),
}
}

fn string_to_utxo_ref(s: &str) -> Result<UtxoRef, Error> {
let (txid, index) = s
.split_once('#')
Expand Down Expand Up @@ -239,7 +260,11 @@ pub fn from_json(value: Value, target: &Type) -> Result<ArgValue, Error> {
let x = value_to_utxo_ref(value)?;
Ok(ArgValue::UtxoRef(x))
}
Type::Undefined => value_to_underfined(value),
Type::AnyData => {
let x = value_to_data_expr(value)?;
Ok(ArgValue::Data(x))
}
Type::Undefined => value_to_undefined(value),
x => Err(Error::TargetTypeNotSupported(x.clone())),
}
}
Expand Down Expand Up @@ -280,6 +305,10 @@ mod tests {
ArgValue::UtxoRef(b) => utxo_ref == b,
_ => false,
},
ArgValue::Data(a) => match b {
ArgValue::Data(b) => a == b,
_ => false,
},
}
}

Expand Down Expand Up @@ -403,4 +432,13 @@ mod tests {

json_to_value_test(json, Type::UtxoRef, ArgValue::UtxoRef(utxo_ref));
}

#[test]
fn test_round_trip_any_data_bool() {
json_to_value_test(
json!(true),
Type::AnyData,
ArgValue::Data(Expression::Bool(true)),
);
}
}
1 change: 1 addition & 0 deletions crates/tx3-tir/src/model/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub enum Type {
Int,
Bool,
Bytes,
AnyData,
Address,
Utxo,
UtxoRef,
Expand Down
24 changes: 13 additions & 11 deletions crates/tx3-tir/src/reduce/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ fn arg_value_into_expr(arg: ArgValue) -> Expression {
ArgValue::Bool(x) => Expression::Bool(x),
ArgValue::String(x) => Expression::String(x),
ArgValue::Bytes(x) => Expression::Bytes(x),
ArgValue::Data(x) => x,
ArgValue::UtxoSet(x) => Expression::UtxoSet(x),
ArgValue::UtxoRef(x) => Expression::UtxoRefs(vec![x]),
}
Expand Down Expand Up @@ -1386,17 +1387,17 @@ impl Apply for Tx {

fn reduce(self) -> Result<Self, Error> {
Ok(Self {
references: self.references.reduce()?,
inputs: self.inputs.reduce()?,
outputs: self.outputs.reduce()?,
validity: self.validity.reduce()?,
mints: self.mints.reduce()?,
burns: self.burns.reduce()?,
fees: self.fees.reduce()?,
adhoc: self.adhoc.reduce()?,
collateral: self.collateral.reduce()?,
signers: self.signers.reduce()?,
metadata: self.metadata.reduce()?,
references: Apply::reduce(self.references)?,
inputs: Apply::reduce(self.inputs)?,
outputs: Apply::reduce(self.outputs)?,
validity: Apply::reduce(self.validity)?,
mints: Apply::reduce(self.mints)?,
burns: Apply::reduce(self.burns)?,
fees: Apply::reduce(self.fees)?,
adhoc: Apply::reduce(self.adhoc)?,
collateral: Apply::reduce(self.collateral)?,
signers: Apply::reduce(self.signers)?,
metadata: Apply::reduce(self.metadata)?,
})
}
}
Expand All @@ -1408,6 +1409,7 @@ pub enum ArgValue {
String(String),
Bytes(Vec<u8>),
Address(Vec<u8>),
Data(Expression),
UtxoSet(UtxoSet),
UtxoRef(UtxoRef),
}
Expand Down