Skip to content

Commit

Permalink
Merge pull request #5 from OlofBlomqvist/throw_out_cml
Browse files Browse the repository at this point in the history
Replace CML with Pallas
  • Loading branch information
OlofBlomqvist committed Jul 19, 2023
2 parents 50d8be1 + 32de863 commit 0bfcc10
Show file tree
Hide file tree
Showing 25 changed files with 1,039 additions and 1,247 deletions.
818 changes: 331 additions & 487 deletions Cargo.lock

Large diffs are not rendered by default.

30 changes: 20 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "marlowe_lang"
description = "experimental parser lib for Cardano Marlowe DSL"
version = "0.1.25"
version = "0.2.0"
edition = "2021"
authors = ["Olof Blomqvist <olof@twnet.se>"]
repository = "https://github.com/OlofBlomqvist/marlowe_rust"
Expand All @@ -12,16 +12,15 @@ pest_derive = "2.7.1"
pest = { version = "2.7.1", features= ["pretty-print"] }
serde = { version = "1.0.171", features = ["derive"] }
serde_json = { version = "1.0.103" , features = ["unbounded_depth"] }
clap = { version = "4.0.19", features = ["derive"] }
# =============================================================
plutus_data = { version = "0.0.5", optional = true }
clap = { version = "4.0.19", features = ["derive"] , optional = true}
plutus_data = { version = "0.0.6", optional = true }
#plutus_data = { path = "../plutus_data/plutus_data", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }
serde_derive = { version = "1.0.171", optional = true }
#uplc = { git = "https://github.com/txpipe/aiken", optional = true }
hex = { version = "0.4.3", optional = true }
minicbor = { version = "0.19.1", optional = true }
cardano-multiplatform-lib = { version = "3.1.3", optional = true, feature=["wasm"] }

getrandom = { version = "0.2.10", features = ["js"], optional = true}
wasm-bindgen = { version = "0.2.83", optional = true , features=["serde-serialize"]}
web-sys = { version = "0.3.59", features = [ 'console' ], optional = true }
Expand All @@ -30,15 +29,24 @@ time = "0.3.23"
chrono = { version = "0.4.26", features = ["wasmbind"] }
#aiken-lang = "*"
js-sys = { version = "0.3.59", optional=true}
serde_stacker = {version= "0.1.10"}
serde_stacker = {version= "0.1.10",optional = true}
proc-macro2 = "1.0.66"

#pallas-primitives = { path = "c:/users/oblink/documents/github/pallas/pallas-primitives" }
#pallas-codec = {path = "c:/users/oblink/documents/github/pallas/pallas-codec" }
#pallas = { path = "c:/users/oblink/documents/github/pallas/pallas" }

pallas-primitives = "0.18.1"
pallas-codec = "0.18.1"
pallas = "0.18.1"

[features]
default = ["utils"]
bin-features = ["clap","utils","unstable"]
infinite-recursion = ["serde_stacker"]
default = ["utils","infinite-recursion"]
unstable = []
wasi = []
utils = [
"dep:cardano-multiplatform-lib",
"dep:minicbor",
"dep:plutus_data",
"dep:hex",
Expand All @@ -50,6 +58,7 @@ js = [
"dep:wasm-bindgen",
"dep:console_error_panic_hook",
"dep:web-sys" ]

[lib]
name = "marlowe_lang"
path = "src/lib/lib.rs"
Expand All @@ -58,11 +67,12 @@ crate-type = ["cdylib","rlib"]
[[bin]]
name = "marlowe_lang_cli"
path = "src/cli_tool_bin/main.rs"
required-features = ["utils"]
required-features = ["bin-features"]



[profile.release]
strip = true # Automatically strip symbols from the binary.
opt-level = "z" # Optimize for size.
lto = true # Enable Link Time Optimization (LTO)
codegen-units = 1 # Reduce Parallel Code Generation Units to Increase Optimization
codegen-units = 1 # Reduce Parallel Code Generation Units to Increase Optimization
3 changes: 1 addition & 2 deletions build_wasm_for_wasi.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
throw "this script is disabled pending wasi support in cml"
Remove-Item pkg_wasi/* -ea stop
cargo build --features="wasi,utils" --target wasm32-wasi --release --out-dir pkg_wasi -Z unstable-options
cargo build --features="wasi,utils,unstable,bin-features" --target wasm32-wasi --release --out-dir pkg_wasi -Z unstable-options
remove-item pkg_wasi/*.rlib -ea stop
if($null -eq (test-path .\pkg_wasi\marlowe_lang_cli.wasm)) {
throw "marlowe_lang_cli.wasm was not correctly built"
Expand Down
2 changes: 1 addition & 1 deletion examples/react/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const Home = () => {
setData(e.error_message)
} else {
console.log(e)
setData("Something is broken, see console.")
setData("That did not work, see console.")
}

}
Expand Down
Binary file added pkg_wasi/marlowe_lang.wasm
Binary file not shown.
Binary file added pkg_wasi/marlowe_lang_cli.wasm
Binary file not shown.
18 changes: 3 additions & 15 deletions src/cli_tool_bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ pub enum Args {
#[clap(subcommand)]
Contract(ContractArgs),

/// Tools for working with generic plutus data
#[clap(subcommand)]
PlutusData(PlutusArgs)

}


Expand All @@ -46,8 +42,6 @@ pub enum ContractOutputInfoType {
CborHex, // plutus data
MarloweDSL, // plain marlowe text
JSON, // json format as used in marlowe-cli (haskell impl)
// PlutusData encoded using detailed schema
PlutusDataDetailedJson, // plutus data detailed json format
/// WIP - Unstable feature
ExpectedActions,
/// List marlowe-extended parameters
Expand All @@ -61,37 +55,31 @@ pub enum ContractInputEncoding {
/// Plain text marlowe contract
MarloweDSL,
/// JSON encoded contract
JSON,
/// Plutus data encoded with detailed json schema
PlutusDataDetailedJson
JSON
}

#[derive(clap::ValueEnum, Clone)]
pub enum RedeemerOutputEncoding {
Json,
CborHex,
PlutusDataDetailedJson,
MarloweDSL
}


#[derive(clap::ValueEnum, Clone)]
pub enum RedeemerInputEncoding {
CborHex,
PlutusDataDetailedJson
CborHex
}


#[derive(clap::ValueEnum, Clone)]
pub enum DatumInputEncoding {
CborHex,
PlutusDataDetailedJson
CborHex
}

#[derive(clap::ValueEnum, Clone)]
pub enum DatumOutputEncoding {
CborHex,
PlutusDataDetailedJson,
JSON,
SimpleText
}
Expand Down
79 changes: 33 additions & 46 deletions src/cli_tool_bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod args;
use args::{DatumArgs, RedeemerArgs, StateArgs, ContractArgs, PlutusArgs};
use marlowe_lang::{types::marlowe::{Contract, MarloweDatum, PossiblyMerkleizedInput, Token}};
use std::{collections::HashMap};
use args::{DatumArgs, RedeemerArgs, StateArgs, ContractArgs};
use marlowe_lang::types::marlowe::{Contract, MarloweDatum, PossiblyMerkleizedInput, Token, Address};
use std::collections::HashMap;
use marlowe_lang::extras::utils::*;
use plutus_data::{ToPlutusData, PlutusData, FromPlutusData};
use plutus_data::ToPlutusData;

use crate::args::{ContractOutputInfoType, ContractInputEncoding, DatumInputEncoding, DatumOutputEncoding, RedeemerInputEncoding, RedeemerOutputEncoding};

#[cfg(feature="unstable")]
use marlowe_lang::semantics::{MachineState, ContractSemantics,ContractInstance};

Expand All @@ -13,9 +15,7 @@ fn datum_handler(args:DatumArgs) {
fn decode(input:&str,e:DatumInputEncoding) -> MarloweDatum {
match e {
DatumInputEncoding::CborHex =>
try_decode_cborhex_marlowe_plutus_datum(&input).unwrap(),
DatumInputEncoding::PlutusDataDetailedJson =>
try_decode_json_encoded_marlowe_plutus_datum(input).unwrap()
try_decode_cborhex_marlowe_plutus_datum(&input).unwrap()
}
}

Expand All @@ -27,13 +27,9 @@ fn datum_handler(args:DatumArgs) {
DatumOutputEncoding::JSON => {
serde_json::to_string_pretty(&x).unwrap()
},
DatumOutputEncoding::PlutusDataDetailedJson => {
let pl = x.to_plutus_data(&vec![]).unwrap();
datum_to_json(&pl).unwrap()
},
DatumOutputEncoding::CborHex => {
let pl = x.to_plutus_data(&vec![]).unwrap();
hex::encode(pl.to_bytes())
plutus_data::to_hex(&pl).unwrap()
}
}
}
Expand Down Expand Up @@ -62,7 +58,6 @@ fn input_redeemer_handler(args:RedeemerArgs) {

fn decode(s:&str,d:RedeemerInputEncoding) -> Vec<PossiblyMerkleizedInput> {
match d {
RedeemerInputEncoding::PlutusDataDetailedJson => try_decode_redeemer_input_json(s).unwrap(),
RedeemerInputEncoding::CborHex => try_decode_redeemer_input_cbor_hex(&s).unwrap()
}
}
Expand All @@ -76,9 +71,7 @@ fn input_redeemer_handler(args:RedeemerArgs) {
RedeemerOutputEncoding::Json =>
serde_json::to_string_pretty(&s).unwrap(),
RedeemerOutputEncoding::CborHex =>
hex::encode(&s.to_plutus_data(&vec![]).unwrap().to_bytes()),
RedeemerOutputEncoding::PlutusDataDetailedJson =>
datum_to_json(&s.to_plutus_data(&vec![]).unwrap()).unwrap()
plutus_data::to_hex(&s.to_plutus_data(&vec![]).unwrap()).unwrap()
}
}

Expand Down Expand Up @@ -109,11 +102,12 @@ fn state_handler(args:StateArgs) {
match args {
StateArgs::InitUsingRole { creator_role, initial_ada } =>
create_state(initial_ada,&creator_role),
StateArgs::InitUsingAddr { creator_addr:_, initial_ada:_ } =>
todo!()
StateArgs::InitUsingAddr { creator_addr, initial_ada } =>
create_state_addr(initial_ada,&creator_addr)
}
}


fn contract_handler(args:ContractArgs) {

fn serialize(c:Contract,e:ContractOutputInfoType) -> String {
Expand Down Expand Up @@ -148,16 +142,12 @@ fn contract_handler(args:ContractArgs) {
panic!("This feature is only available when using the marlowe_lang crate feature: 'unstable'.")
}
ContractOutputInfoType::CborHex =>
hex::encode(c.to_plutus_data(&vec![]).unwrap().to_bytes()),
plutus_data::to_hex(&c.to_plutus_data(&vec![]).unwrap()).unwrap(),
ContractOutputInfoType::MarloweDSL =>
marlowe_lang::parsing::fmt::fmt(&
marlowe_lang::serialization::marlowe::serialize(c)),
ContractOutputInfoType::JSON =>
marlowe_lang::serialization::json::serialize(c).unwrap(),
ContractOutputInfoType::PlutusDataDetailedJson => {
let pl = c.to_plutus_data(&vec![]).unwrap();
datum_to_json(&pl).unwrap()
},
marlowe_lang::serialization::json::serialize(c).unwrap()
}
}

Expand All @@ -167,7 +157,7 @@ fn contract_handler(args:ContractArgs) {
if input_variables.is_some() {
panic!("It is not possible to add inputs to contracts that are already encoded to json.")
}
marlowe_lang::deserialization::json::deserialize(s).unwrap()
marlowe_lang::deserialization::json::deserialize(s.into()).unwrap()
}
ContractInputEncoding::CborHex => {
if input_variables.is_some() {
Expand All @@ -189,10 +179,6 @@ fn contract_handler(args:ContractArgs) {
None => marlowe_lang::deserialization::marlowe::deserialize(&s).unwrap().contract
}
}
ContractInputEncoding::PlutusDataDetailedJson => {
let pl = cardano_multiplatform_lib::plutus::encode_json_str_to_plutus_datum(&s, cardano_multiplatform_lib::plutus::PlutusDatumSchema::DetailedSchema).unwrap();
Contract::from_plutus_data(pl, &vec![]).unwrap()
},
}
}

Expand Down Expand Up @@ -224,24 +210,9 @@ fn contract_handler(args:ContractArgs) {
}
}

fn plutus_data_handler(x:PlutusArgs) {
fn decode_and_print(s:&str) {
let hex = hex::decode(s).unwrap();
let item = PlutusData::from_bytes(hex).unwrap();
let json = marlowe_lang::extras::utils::datum_to_json(&item).unwrap();
println!("{}",json);
}
match x {
PlutusArgs::ConvertCborHexToJson { cborhex } =>
decode_and_print(&cborhex),
PlutusArgs::ConvertCborHexFileToJson { path } =>
decode_and_print(&std::fs::read_to_string(path).unwrap())
}
}

fn main() {
match <args::Args as clap::Parser>::parse() {
args::Args::PlutusData(x) => plutus_data_handler(x),
args::Args::Datum(x) => datum_handler(x),
args::Args::State(x) => state_handler(x),
args::Args::Redeemer(x) => input_redeemer_handler(x),
Expand All @@ -253,7 +224,6 @@ fn main() {
#[cfg(feature="wasi")]
fn cli_main_wasi(args:&str) {
match <args::Args as clap::Parser>::parse_from(args.split("|")) {
args::Args::PlutusData(x) => plutus_data_handler(x),
args::Args::Datum(x) => datum_handler(x),
args::Args::State(x) => state_handler(x),
args::Args::Redeemer(x) => input_redeemer_handler(x),
Expand All @@ -262,7 +232,6 @@ fn cli_main_wasi(args:&str) {
}



// probably should support Address as creator as well? "address": "addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x"
fn create_state(initial_ada:i64,creator_role:&str) {

Expand All @@ -282,6 +251,24 @@ fn create_state(initial_ada:i64,creator_role:&str) {
println!("{}",serde_json::to_string_pretty(&state).unwrap());

}
fn create_state_addr(initial_ada:i64,creator_addr:&str) {

let mut state = marlowe_lang::types::marlowe::State {
accounts: HashMap::new(),
bound_values: HashMap::new(),
choices: HashMap::new(),
min_time: 1
};

let creator = marlowe_lang::types::marlowe::Party::Address(Address::from_bech32(creator_addr).unwrap());

state.accounts.insert(
(creator,Token { currency_symbol:"".into(), token_name:"".into()}),
(initial_ada*1000) as u64);

println!("{}",serde_json::to_string_pretty(&state).unwrap());

}



Expand Down
Loading

0 comments on commit 0bfcc10

Please sign in to comment.