From 924b847d365696c9241eb8949473539d60e7a062 Mon Sep 17 00:00:00 2001 From: Olof Blomqvist Date: Sat, 2 Sep 2023 20:52:18 +0200 Subject: [PATCH] update to marlowe-rs 0.3.0 --- Cargo.lock | 34 ++++++++---- Cargo.toml | 5 +- example.ipynb | 51 +++++++++--------- src/code_gen.rs | 45 ++++++++-------- src/lib.rs | 6 +++ src/pytypes/bound.rs | 2 +- src/pytypes/case.rs | 114 +++++++++++++++++++++++++++++----------- src/pytypes/contract.rs | 13 +++-- src/pytypes/value.rs | 2 +- src/tests.rs | 7 ++- 10 files changed, 176 insertions(+), 103 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f1693a..77ec3f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,6 +46,16 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" +[[package]] +name = "assert-json-diff" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -317,7 +327,7 @@ checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "marlowe" -version = "0.1.3" +version = "0.1.5" dependencies = [ "marlowe_lang", "pyo3", @@ -327,13 +337,15 @@ dependencies = [ [[package]] name = "marlowe_lang" -version = "0.2.1" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b21b4f425b9d61222ccf703d3210b4d5bc96654c9d8c8e6d9efa1e64e05a1dc4" +checksum = "0d4652cfa787d52c1a1987e5fc73743c9f8c9de8e7d87343659dc86ff3a88617" dependencies = [ + "assert-json-diff", "bech32", "chrono", "hex", + "indexmap", "minicbor", "pallas", "pallas-codec", @@ -380,9 +392,9 @@ dependencies = [ [[package]] name = "num-bigint" -version = "0.4.3" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" dependencies = [ "autocfg", "num-integer", @@ -410,9 +422,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" dependencies = [ "autocfg", ] @@ -613,9 +625,9 @@ checksum = "4c40d25201921e5ff0c862a505c6557ea88568a4e3ace775ab55e93f2f4f9d57" [[package]] name = "plutus_data" -version = "0.0.7" +version = "0.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93e0a6d0ecbd437a74fa90a114e9dd1017bee705929a77a55b884764d1ea607d" +checksum = "a488eaba93c4cb88928abffa7d05b3c9cb8bb679adf5e927cde52082e5ec5e96" dependencies = [ "hex", "pallas", @@ -626,9 +638,9 @@ dependencies = [ [[package]] name = "plutus_data_derive" -version = "0.0.7" +version = "0.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e72b80c43fca82f9e30284951ab6772a73c385e9bcc7f41527ab295dc10ee441" +checksum = "a6896f87244be61fa9cbe2f50b8094f0459fa7d55ddc223dd9b744e0623caef9" dependencies = [ "hex", "quote", diff --git a/Cargo.toml b/Cargo.toml index 84e783c..30bf9cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "marlowe" -version = "0.1.4" +version = "0.1.5" edition = "2021" [lib] @@ -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" \ No newline at end of file diff --git a/example.ipynb b/example.ipynb index f93b848..9a4a1f6 100644 --- a/example.ipynb +++ b/example.ipynb @@ -19,7 +19,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -109,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -176,16 +176,16 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'[Action(Deposit { into_account: Some(Address(Address { is_mainnet: true, addr: PubKeyCredential { pkh: PubKeyHash { pkhash: \"1fdc22c9b2339e644d229335129b35e65161839660636675b29aa5a8\" }, staking: Some(Hash { creds: PubKeyHash(\"43b85595ee70ca74e98583872933bec6c67a99982ca2f25547a89b4d\") }) } })), input_from_party: Some(Address(Address { is_mainnet: true, addr: PubKeyCredential { pkh: PubKeyHash { pkhash: \"1fdc22c9b2339e644d229335129b35e65161839660636675b29aa5a8\" }, staking: Some(Hash { creds: PubKeyHash(\"43b85595ee70ca74e98583872933bec6c67a99982ca2f25547a89b4d\") }) } })), of_tokens: Some(Token { currency_symbol: \"85556ab05acc925edff2af02ef819a8b4903bfb5d5e100a7c95ab908\", token_name: \"MarloweLisbon\" }), that_deposits: 1 })]'" + "'[Action(Deposit { into_account: Some(Address(addr1qy0acgkfkgeeuezdy2fn2y5mxhn9zcvrjesxxen4k2d2t2zrhp2etmnsef6wnpvrsu5n80kxceafnxpv5te923agndxshwgt4u)), input_from_party: Some(Address(addr1qy0acgkfkgeeuezdy2fn2y5mxhn9zcvrjesxxen4k2d2t2zrhp2etmnsef6wnpvrsu5n80kxceafnxpv5te923agndxshwgt4u)), of_tokens: Some(Token { currency_symbol: \"85556ab05acc925edff2af02ef819a8b4903bfb5d5e100a7c95ab908\", token_name: \"MarloweLisbon\" }), that_deposits: 1 })]'" ] }, - "execution_count": 5, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -204,7 +204,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -213,7 +213,7 @@ "'d8799fd8799f581c8bb3b343d8e404472337966a722150048c768d0a92a9813596c5338dffd8799fa1d8799fd8799fd87980d8799fd8799f581ce0dc70fa9698727e439df458436797d057cdfea5e7c2844d12b6af8affd87a80ffffd8799f4040ffff1a002dc6c0a0a000ffd87a9fd8799fd87980d8799fd8799f581ce0dc70fa9698727e439df458436797d057cdfea5e7c2844d12b6af8affd87a80ffffd87a9fd87a9f4f5769746864726177616c5465737431ffffd8799f4040ffd87a9f1a00989680ffd87980ffff'" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -233,20 +233,18 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Bad contract: Generic(\"Unable to perform payment as the account ((Address \\\"addr_test1vrsdcu86j6v8yljrnh69ssm8jlg90n075hnu9pzdz2m2lzsdlspjq\\\")) does not have enough tokens ((Token \\\"\\\" \\\"\\\")). Contract attempted to send '10000000' when the account only contains '3000000'.\")\n", - "MarloweDatum { marlowe_params: MarloweParams(\"8bb3b343d8e404472337966a722150048c768d0a92a9813596c5338d\"), state: State { accounts: {(Address(Address { is_mainnet: false, addr: PubKeyCredential { pkh: PubKeyHash { pkhash: \"e0dc70fa9698727e439df458436797d057cdfea5e7c2844d12b6af8a\" }, staking: None } }), Token { currency_symbol: \"\", token_name: \"\" }): 3000000}, choices: {}, bound_values: {}, min_time: 0 }, contract: Pay { from_account: Some(Address(Address { is_mainnet: false, addr: PubKeyCredential { pkh: PubKeyHash { pkhash: \"e0dc70fa9698727e439df458436797d057cdfea5e7c2844d12b6af8a\" }, staking: None } })), to: Some(Party(Some(Role { role_token: \"WithdrawalTest1\" }))), token: Some(Token { currency_symbol: \"\", token_name: \"\" }), pay: Some(ConstantValue(10000000)), then: Some(Close) } }\n", "\n", "------\n", "\n", "Good contract: Closed\n", - "MarloweDatum { marlowe_params: MarloweParams(\"7bb1099758c0e54996a560d225f76dc4686aa8a436588baa3d0e0588\"), state: State { accounts: {(Address(Address { is_mainnet: false, addr: PubKeyCredential { pkh: PubKeyHash { pkhash: \"331aaa0e2267ea6c2aece55a24e45de443235bf078f08a615ff49534\" }, staking: Some(Hash { creds: PubKeyHash(\"f5b3b821dcf83c502b8c14161de22f60c1f5115077bba0b8baa63fb2\") }) } }), Token { currency_symbol: \"\", token_name: \"\" }): 3000000}, choices: {}, bound_values: {}, min_time: 0 }, contract: When { when: [Some(Case { case: Some(Deposit { into_account: Some(Role { role_token: \"Provider NFT Handle\" }), party: Some(Role { role_token: \"Provider NFT Handle\" }), of_token: Some(Token { currency_symbol: \"\", token_name: \"\" }), deposits: Some(ConstantValue(7)) }), then: Some(Raw(When { when: [Some(Case { case: Some(Deposit { into_account: Some(Role { role_token: \"Swapper NFT Handle\" }), party: Some(Role { role_token: \"Swapper NFT Handle\" }), of_token: Some(Token { currency_symbol: \"8db269c3ec630e06ae29f74bc39edd1f87c819f1056206e879a1cd61\", token_name: \"djed\" }), deposits: Some(ConstantValue(5)) }), then: Some(Raw(Pay { from_account: Some(Role { role_token: \"Provider NFT Handle\" }), to: Some(Party(Some(Role { role_token: \"Swapper NFT Handle\" }))), token: Some(Token { currency_symbol: \"\", token_name: \"\" }), pay: Some(ConstantValue(7)), then: Some(Pay { from_account: Some(Role { role_token: \"Swapper NFT Handle\" }), to: Some(Party(Some(Role { role_token: \"Provider NFT Handle\" }))), token: Some(Token { currency_symbol: \"8db269c3ec630e06ae29f74bc39edd1f87c819f1056206e879a1cd61\", token_name: \"djed\" }), pay: Some(ConstantValue(5)), then: Some(Close) }) })) })], timeout: Some(TimeConstant(1689270008000)), timeout_continuation: Some(Pay { from_account: Some(Role { role_token: \"Provider NFT Handle\" }), to: Some(Party(Some(Role { role_token: \"Provider NFT Handle\" }))), token: Some(Token { currency_symbol: \"\", token_name: \"\" }), pay: Some(ConstantValue(7)), then: Some(Close) }) })) })], timeout: Some(TimeConstant(1689183608000)), timeout_continuation: Some(Close) } }\n", + "MarloweDatum { marlowe_params: MarloweParams(\"7bb1099758c0e54996a560d225f76dc4686aa8a436588baa3d0e0588\"), state: State { accounts: AccMap({(Address(addr_test1qqe342swyfn75mp2anj45f8ythjyxg6m7pu0pznptl6f2d84kwuzrh8c83gzhrq5zcw7ytmqc863z5rhhwst3w4x87eq0td9ja), Token { currency_symbol: \"\", token_name: \"\" }): 3000000}), choices: AccMap({}), bound_values: AccMap({}), min_time: 0 }, contract: When { when: [Some(Raw { case: Some(Deposit { into_account: Some(Role { role_token: \"Provider NFT Handle\" }), party: Some(Role { role_token: \"Provider NFT Handle\" }), of_token: Some(Token { currency_symbol: \"\", token_name: \"\" }), deposits: Some(ConstantValue(7)) }), then: Some(When { when: [Some(Raw { case: Some(Deposit { into_account: Some(Role { role_token: \"Swapper NFT Handle\" }), party: Some(Role { role_token: \"Swapper NFT Handle\" }), of_token: Some(Token { currency_symbol: \"8db269c3ec630e06ae29f74bc39edd1f87c819f1056206e879a1cd61\", token_name: \"djed\" }), deposits: Some(ConstantValue(5)) }), then: Some(Pay { from_account: Some(Role { role_token: \"Provider NFT Handle\" }), to: Some(Party(Some(Role { role_token: \"Swapper NFT Handle\" }))), token: Some(Token { currency_symbol: \"\", token_name: \"\" }), pay: Some(ConstantValue(7)), then: Some(Pay { from_account: Some(Role { role_token: \"Swapper NFT Handle\" }), to: Some(Party(Some(Role { role_token: \"Provider NFT Handle\" }))), token: Some(Token { currency_symbol: \"8db269c3ec630e06ae29f74bc39edd1f87c819f1056206e879a1cd61\", token_name: \"djed\" }), pay: Some(ConstantValue(5)), then: Some(Close) }) }) })], timeout: Some(TimeConstant(1689270008000)), timeout_continuation: Some(Pay { from_account: Some(Role { role_token: \"Provider NFT Handle\" }), to: Some(Party(Some(Role { role_token: \"Provider NFT Handle\" }))), token: Some(Token { currency_symbol: \"\", token_name: \"\" }), pay: Some(ConstantValue(7)), then: Some(Close) }) }) })], timeout: Some(TimeConstant(1689183608000)), timeout_continuation: Some(Close) } }\n", "\n", "------\n", "\n" @@ -269,6 +267,7 @@ "cbor_hex = \"d8799fd8799f581c7bb1099758c0e54996a560d225f76dc4686aa8a436588baa3d0e0588ffd8799fa1d8799fd8799fd87980d8799fd8799f581c331aaa0e2267ea6c2aece55a24e45de443235bf078f08a615ff49534ffd8799fd8799fd8799f581cf5b3b821dcf83c502b8c14161de22f60c1f5115077bba0b8baa63fb2ffffffffffd8799f4040ffff1a002dc6c0a0a000ffd87c9f9fd8799fd8799fd87a9f5350726f7669646572204e46542048616e646c65ffd87a9f5350726f7669646572204e46542048616e646c65ffd8799f4040ffd87a9f07ffffd87c9f9fd8799fd8799fd87a9f5253776170706572204e46542048616e646c65ffd87a9f5253776170706572204e46542048616e646c65ffd8799f581c8db269c3ec630e06ae29f74bc39edd1f87c819f1056206e879a1cd6144646a6564ffd87a9f05ffffd87a9fd87a9f5350726f7669646572204e46542048616e646c65ffd87a9fd87a9f5253776170706572204e46542048616e646c65ffffd8799f4040ffd87a9f07ffd87a9fd87a9f5253776170706572204e46542048616e646c65ffd87a9fd87a9f5350726f7669646572204e46542048616e646c65ffffd8799f581c8db269c3ec630e06ae29f74bc39edd1f87c819f1056206e879a1cd6144646a6564ffd87a9f05ffd87980ffffffff1b000001895056b8c0d87a9fd87a9f5350726f7669646572204e46542048616e646c65ffd87a9fd87a9f5350726f7669646572204e46542048616e646c65ffffd8799f4040ffd87a9f07ffd87980ffffffff1b000001894b305cc0d87980ffff\"\n", "d = Datum.from_cbor_hex(cbor_hex)\n", "print(\"Good contract:\",d.show_status())\n", + "\n", "print(d.as_string())\n", "\n", "print(\"\\n------\\n\")\n" @@ -292,7 +291,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -318,7 +317,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -457,16 +456,16 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "DATUM AS RUST STRING : MarloweDatum { marlowe_params: MarloweParams(\"\"), state: State { accounts: {}, choices: {}, bound_values: {}, min_time: 1689599369052 }, contract: When { when: [Some(Case { case: Some(Deposit { into_account: Some(Role { role_token: \"KALLE\" }), party: Some(Role { role_token: \"kalle\" }), of_token: Some(Token { currency_symbol: \"\", token_name: \"\" }), deposits: Some(ConstantValue(12345678)) }), then: Some(Merkleized(\"aa\")) })], timeout: Some(TimeConstant(999999999)), timeout_continuation: Some(Close) } }\n", + "DATUM AS RUST STRING : MarloweDatum { marlowe_params: MarloweParams(\"\"), state: State { accounts: AccMap({}), choices: AccMap({}), bound_values: AccMap({}), min_time: 1693680196000 }, contract: When { when: [Some(Raw { case: Some(Deposit { into_account: Some(Role { role_token: \"KALLE\" }), party: Some(Role { role_token: \"kalle\" }), of_token: Some(Token { currency_symbol: \"\", token_name: \"\" }), deposits: Some(ConstantValue(12345678)) }), then: Some(Close) })], timeout: Some(TimeConstant(999999999)), timeout_continuation: Some(Close) } }\n", "\n", - "DATUM AS CBORHEX : d8799fd8799f40ffd8799fa0a0a01b0000018963f85f5cffd87c9f9fd8799fd8799fd87a9f454b414c4c45ffd87a9f456b616c6c65ffd8799f4040ffd87a9f1a00bc614effff41aaffff1a3b9ac9ffd87980ffff\n" + "DATUM AS CBORHEX : d8799fd8799f40ffd8799fa0a0a01b0000018a5734d9a0ffd87c9f9fd8799fd8799fd87a9f454b414c4c45ffd87a9f456b616c6c65ffd8799f4040ffd87a9f1a00bc614effffd87980ffff1a3b9ac9ffd87980ffff\n" ] } ], @@ -504,7 +503,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -519,14 +518,14 @@ " of_token=Token.ADA(),\n", " value=Value.ConstantParam(\"Price\"),\n", " then_continue_with=PossiblyMerkleizedContract.raw(\n", - " contract=Contract.When(\n", + " Contract.When(\n", " case=[\n", " Case.Choice(\n", " choice_name=\"Everything is alright\",\n", " choice_owner=Party.Role(\"Buyer\"),\n", " bounds=[Bound(0, 0)],\n", " then_continue_with=PossiblyMerkleizedContract.raw(\n", - " contract=Contract.Close()\n", + " Contract.Close()\n", " ),\n", " ),\n", " Case.Choice(\n", @@ -534,7 +533,7 @@ " choice_owner=Party.Role(\"Buyer\"),\n", " bounds=[Bound(1, 1)],\n", " then_continue_with=PossiblyMerkleizedContract.raw(\n", - " contract=Contract.Pay(\n", + " Contract.Pay(\n", " from_account_of=Party.Role(\"Seller\"),\n", " to=Payee.Account(Party.Role(\"Buyer\")),\n", " token=Token.ADA(),\n", @@ -546,7 +545,7 @@ " choice_owner=Party.Role(\"Seller\"),\n", " bounds=[Bound(1, 1)],\n", " then_continue_with=PossiblyMerkleizedContract.raw(\n", - " contract=Contract.Close()\n", + " Contract.Close()\n", " ),\n", " ),\n", " Case.Choice(\n", @@ -554,7 +553,7 @@ " choice_owner=Party.Role(\"Seller\"),\n", " bounds=[Bound(0, 0)],\n", " then_continue_with=PossiblyMerkleizedContract.raw(\n", - " contract=Contract.When(\n", + " Contract.When(\n", " case=[\n", " Case.Choice(\n", " choice_name=\"Dismiss claim\",\n", @@ -563,7 +562,7 @@ " ),\n", " bounds=[Bound(0, 0)],\n", " then_continue_with=PossiblyMerkleizedContract.raw(\n", - " contract=Contract.Pay(\n", + " Contract.Pay(\n", " from_account_of=Party.Role(\n", " \"Buyer\"\n", " ),\n", @@ -587,7 +586,7 @@ " ),\n", " bounds=[Bound(1, 1)],\n", " then_continue_with=PossiblyMerkleizedContract.raw(\n", - " contract=Contract.Close()\n", + " Contract.Close()\n", " ),\n", " ),\n", " ],\n", diff --git a/src/code_gen.rs b/src/code_gen.rs index dafcc5d..3d56c9e 100644 --- a/src/code_gen.rs +++ b/src/code_gen.rs @@ -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 ¬ify_if { Some(_obs) => { - let obs_py = opt_py(notify_if, observation_as_python); + let obs_py = opt_py(¬ify_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 }) => { @@ -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() } ) @@ -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 { diff --git a/src/lib.rs b/src/lib.rs index d6639eb..4c26880 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)?)?; diff --git a/src/pytypes/bound.rs b/src/pytypes/bound.rs index cddfd0d..718b2ef 100644 --- a/src/pytypes/bound.rs +++ b/src/pytypes/bound.rs @@ -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)) } diff --git a/src/pytypes/case.rs b/src/pytypes/case.rs index ef27c83..574a50d 100644 --- a/src/pytypes/case.rs +++ b/src/pytypes/case.rs @@ -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 { @@ -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,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 + }) + }, + } + } } diff --git a/src/pytypes/contract.rs b/src/pytypes/contract.rs index 8889ae2..ec9c843 100644 --- a/src/pytypes/contract.rs +++ b/src/pytypes/contract.rs @@ -96,7 +96,7 @@ impl Contract { #[staticmethod] - fn from_marlowe_dsl(dsl:&str,variables:Vec<(String, i64)>) -> PyResult { + fn from_marlowe_dsl(dsl:&str,variables:Vec<(String, i128)>) -> PyResult { 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))) @@ -139,15 +139,14 @@ impl Contract { #[staticmethod] #[pyo3(name="When")] fn when(case: Vec, timeout: Timeout, timeout_continuation: Contract) -> PyResult { - 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)) })) diff --git a/src/pytypes/value.rs b/src/pytypes/value.rs index 84a4b28..2677bab 100644 --- a/src/pytypes/value.rs +++ b/src/pytypes/value.rs @@ -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) ) diff --git a/src/tests.rs b/src/tests.rs index d764a47..3f3b198 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -20,14 +20,13 @@ fn call_python_script(file_name:&str) -> Result { } -// todo: include one of each construct! -// this test is too make sure that generated code produces +// this test is to make sure that generated code produces // the same dsl back as the original input we genrated it from #[test] fn as_python() { let temp_file_name = "temp_test_code_gen.py"; - let mut temp_file = std::fs::File::create(temp_file_name.clone()).unwrap(); + let mut temp_file = std::fs::File::create(temp_file_name).unwrap(); let dsl = r#"When [ (Case @@ -130,7 +129,7 @@ fn as_python() { std::fs::remove_file(temp_file_name).unwrap_or_default(); match result { Ok(result) => { - let mut inputs = std::collections::HashMap::::new(); + let mut inputs = std::collections::HashMap::::new(); for x in &parse_result.uninitialized_const_params { inputs.insert(x.clone(),42); }