Skip to content

Commit

Permalink
update to marlowe-rs 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
OlofBlomqvist committed Sep 2, 2023
1 parent 6253eae commit 924b847
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 103 deletions.
34 changes: 23 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "marlowe"
version = "0.1.4"
version = "0.1.5"
edition = "2021"

[lib]
Expand All @@ -13,6 +13,7 @@ path = "src/lib.rs"
pyo3 = { version = "0.19.1", features = ["full"] }
#marlowe_lang = "*"
#marlowe_lang = { git = "https://github.com/olofblomqvist/marlowe-rs", features=["utils","unstable"], default-features = false }
marlowe_lang = {version="0.2.1", features=["utils","unstable"], default-features = false }
marlowe_lang = {version="0.3.0", features=["utils","unstable"], default-features = false }
#marlowe_lang = { path = "../marlowe_rust",features=["unstable","utils"], default-features = false }
serde_json = "1.0.103"
serde = "1.0.171"
51 changes: 25 additions & 26 deletions example.ipynb

Large diffs are not rendered by default.

45 changes: 24 additions & 21 deletions src/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,37 +160,39 @@ pub fn party_as_python(x:&Party) -> String {
}
}

pub fn case_as_python(x:&Case) -> String {
pub fn case_as_python(x:&PossiblyMerkleizedCase) -> String {

let continue_with_py = match &x.then {
Some(c) => {
match c {
PossiblyMerkleizedContract::Raw(contract_continuation) => {
format!("PossiblyMerkleizedContract.raw({})",contract_box_as_python(contract_continuation))
},
PossiblyMerkleizedContract::Merkleized(m) => {
format!("PossiblyMerkleizedContract.merkleized(\"{m}\")")
},
}
let (continue_with_py,case) = match x {
PossiblyMerkleizedCase::Raw { case, then } => {
let py_continuation = if let Some(c) = then {
format!("PossiblyMerkleizedContract.raw({})",contract_as_python(c))
} else {
"null".into()
};
(py_continuation, case.clone())
},
None => "null".into(),
PossiblyMerkleizedCase::Merkleized { case, then } => {
let py_continuation = format!("PossiblyMerkleizedContract.merkleized(\"{then}\")");
(py_continuation, Some(case.clone()))
}
};

match x.case.as_ref() {

match case {
Some(marlowe_lang::types::marlowe::Action::Notify { notify_if }) => {
match notify_if {
match &notify_if {
Some(_obs) => {
let obs_py = opt_py(notify_if, observation_as_python);
let obs_py = opt_py(&notify_if, observation_as_python);
format!("Case.Notify(obs={obs_py},then_continue_with={continue_with_py})")
},
None => format!("Case.Notify(obs=null,then_continue_with={continue_with_py})"),
}
},
Some(marlowe_lang::types::marlowe::Action::Deposit { into_account, party, of_token, deposits }) => {
let into_account_py = opt_py(into_account,party_as_python);
let party_py = opt_py(party,party_as_python);
let of_token_py = opt_py(of_token,token_as_python);
let deposits_py = opt_py(deposits,value_as_python);
let into_account_py = opt_py(&into_account,party_as_python);
let party_py = opt_py(&party,party_as_python);
let of_token_py = opt_py(&of_token,token_as_python);
let deposits_py = opt_py(&deposits,value_as_python);
format!("Case.Deposit(into_account={into_account_py},by={party_py},of_token={of_token_py},value={deposits_py},then_continue_with={continue_with_py})")
},
Some(marlowe_lang::types::marlowe::Action::Choice { for_choice, choose_between }) => {
Expand All @@ -200,7 +202,7 @@ pub fn case_as_python(x:&Case) -> String {
(
format!("\"{}\"",choice_name),
match choice_owner {
Some(o) => party_as_python(o),
Some(o) => party_as_python(&o),
_ => "null".into()
}
)
Expand All @@ -215,7 +217,8 @@ pub fn case_as_python(x:&Case) -> String {
format!("Case.Choice(choice_name={choice_name_py}, choice_owner={choice_owner_py},bounds={bounds_py},then_continue_with={continue_with_py})")
},
None => "null".into()
}
}

}

pub fn timeout_as_python(x:&Timeout) -> String {
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ pub fn merkleized(hash:&str) -> PossiblyMerkleizedContract {
PossiblyMerkleizedContract::merkleized(hash)
}

#[pyfunction]
pub fn version() -> String {
"marlowe-py: 0.1.5, marlowe-rs: 0.3.0".into()
}

#[pymodule]
#[pyo3(name = "marlowe")]
pub fn rust(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(version, m)?)?;
m.add_function(wrap_pyfunction!(merkleized, m)?)?;
m.add_function(wrap_pyfunction!(raw, m)?)?;
m.add_function(wrap_pyfunction!(decode_redeemer_from_cbor_hex, m)?)?;
Expand Down
2 changes: 1 addition & 1 deletion src/pytypes/bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Bound {
}
}
#[new]
fn new(from: i64, to: i64) -> Self {
fn new(from: i128, to: i128) -> Self {
Bound(marlowe_lang::types::marlowe::Bound(from,to))
}

Expand Down
114 changes: 84 additions & 30 deletions src/pytypes/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{*, contract::PossiblyMerkleizedContract};

#[pyclass]
#[derive(Clone,Debug)]
pub struct Case(pub(crate)marlowe_lang::types::marlowe::Case);
pub struct Case(pub(crate)marlowe_lang::types::marlowe::PossiblyMerkleizedCase);

#[pymethods]
impl Case {
Expand All @@ -27,45 +27,99 @@ impl Case {
#[staticmethod]
#[pyo3(name="Notify")]
fn notify(observation:Observation,then_continue_with:PossiblyMerkleizedContract) -> Self {
Case(marlowe_lang::types::marlowe::Case {
case: Some(marlowe_lang::types::marlowe::Action::Notify {
notify_if: Some(observation.0)
}),
then: Some(then_continue_with.0)
})
match then_continue_with.0 {
marlowe_lang::types::marlowe::PossiblyMerkleizedContract::Raw(boxed_continuation) => {
Case(marlowe_lang::types::marlowe::PossiblyMerkleizedCase::Raw {
case: Some(marlowe_lang::types::marlowe::Action::Notify {
notify_if: Some(observation.0)
}),
then: Some(*boxed_continuation)
})
},
marlowe_lang::types::marlowe::PossiblyMerkleizedContract::Merkleized(hash) => {
Case(marlowe_lang::types::marlowe::PossiblyMerkleizedCase::Merkleized {
case: marlowe_lang::types::marlowe::Action::Notify {
notify_if: Some(observation.0)
},
then: hash
})
},
}

}


#[staticmethod]
#[pyo3(name="Choice")]
fn choice(choice_name:&str,choice_owner:Party,bounds:Vec<Bound>,then_continue_with:PossiblyMerkleizedContract) -> Case {
Case(
marlowe_lang::types::marlowe::Case {
case: Some(marlowe_lang::types::marlowe::Action::Choice {
for_choice: Some(marlowe_lang::types::marlowe::ChoiceId {
choice_name: choice_name.into(),
choice_owner: Some(choice_owner.0)
}),
choose_between: bounds.iter().map(|x|
marlowe_lang::types::marlowe::Bound(x.0.0,x.0.1)).map(Some).collect()
}),
then: Some(then_continue_with.0)
})

match then_continue_with.0 {
marlowe_lang::types::marlowe::PossiblyMerkleizedContract::Raw(boxed_continuation) => {
Case(
marlowe_lang::types::marlowe::PossiblyMerkleizedCase::Raw {
case: Some(marlowe_lang::types::marlowe::Action::Choice {
for_choice: Some(marlowe_lang::types::marlowe::ChoiceId {
choice_name: choice_name.into(),
choice_owner: Some(choice_owner.0)
}),
choose_between: bounds.iter().map(|x|
marlowe_lang::types::marlowe::Bound(x.0.0,x.0.1)).map(Some).collect()
}),
then: Some(*boxed_continuation)
})
},
marlowe_lang::types::marlowe::PossiblyMerkleizedContract::Merkleized(hash) => {
Case(
marlowe_lang::types::marlowe::PossiblyMerkleizedCase::Merkleized {
case: marlowe_lang::types::marlowe::Action::Choice {
for_choice: Some(marlowe_lang::types::marlowe::ChoiceId {
choice_name: choice_name.into(),
choice_owner: Some(choice_owner.0)
}),
choose_between: bounds.iter().map(|x|
marlowe_lang::types::marlowe::Bound(x.0.0,x.0.1)).map(Some).collect()
},
then: hash
})

},
}



}

#[staticmethod]
#[pyo3(name="Deposit")]
fn deposit(into_account:Party,by:Party,of_token:Token,value:Value,then_continue_with:PossiblyMerkleizedContract) -> Case {
Case(
marlowe_lang::types::marlowe::Case {
case: Some(marlowe_lang::types::marlowe::Action::Deposit {
into_account: Some(into_account.0),
party: Some(by.0),
of_token: Some(of_token.0),
deposits: Some(value.0)
}
),
then: Some(then_continue_with.0)
})
match then_continue_with.0 {
marlowe_lang::types::marlowe::PossiblyMerkleizedContract::Raw(boxed_continuation) => {
Case(
marlowe_lang::types::marlowe::PossiblyMerkleizedCase::Raw {
case: Some(marlowe_lang::types::marlowe::Action::Deposit {
into_account: Some(into_account.0),
party: Some(by.0),
of_token: Some(of_token.0),
deposits: Some(value.0)
}
),
then: Some(*boxed_continuation)
})
},
marlowe_lang::types::marlowe::PossiblyMerkleizedContract::Merkleized(hash) => {
Case(
marlowe_lang::types::marlowe::PossiblyMerkleizedCase::Merkleized {
case: marlowe_lang::types::marlowe::Action::Deposit {
into_account: Some(into_account.0),
party: Some(by.0),
of_token: Some(of_token.0),
deposits: Some(value.0)
}
,
then: hash
})
},
}

}
}
13 changes: 6 additions & 7 deletions src/pytypes/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Contract {


#[staticmethod]
fn from_marlowe_dsl(dsl:&str,variables:Vec<(String, i64)>) -> PyResult<Self> {
fn from_marlowe_dsl(dsl:&str,variables:Vec<(String, i128)>) -> PyResult<Self> {
match marlowe_lang::types::marlowe::Contract::from_dsl(dsl,variables) {
Ok(c) => Ok(Contract(c)),
Err(e) => Err(PyValueError::new_err(format!("did not work! {:?}",e)))
Expand Down Expand Up @@ -139,15 +139,14 @@ impl Contract {
#[staticmethod]
#[pyo3(name="When")]
fn when(case: Vec<Case>, timeout: Timeout, timeout_continuation: Contract) -> PyResult<Self> {
let mut cc = vec![];

let mut cases = vec![];
for x in case {
cc.push(Some(marlowe_lang::types::marlowe::Case {
case: x.0.case,
then: x.0.then
}))
cases.push(Some(x.0))
}

Ok(Contract(marlowe_lang::types::marlowe::Contract::When {
when: cc,
when: cases,
timeout: Some(timeout.0),
timeout_continuation: Some(Box::new(timeout_continuation.0))
}))
Expand Down
2 changes: 1 addition & 1 deletion src/pytypes/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Value {

#[staticmethod]
#[pyo3(name="Constant")]
fn constant(value:i64) -> Self {
fn constant(value:i128) -> Self {
Self(
marlowe_lang::types::marlowe::Value::ConstantValue(value)
)
Expand Down
Loading

0 comments on commit 924b847

Please sign in to comment.