Skip to content

Commit

Permalink
Refactor to reflect haskell impl:
Browse files Browse the repository at this point in the history
- switch to indexmap to maintain order in state maps.
 - switch to 128 bit numbers to match haskell impl.
 - add more tests for datum re-serialization. use str for wasm to support 128 bit apis.
 - drop empty accounts from state.
 - fix tag encoding of parties to match haskell impl.
 - update serde
 - add json-diff for plutus data tests
- add indexmap for maps
- use plutus_data derive macro for contracts instead of custom impl
- refactor case enum to match haskell impl
- rename ada to lovelace in examples
  • Loading branch information
OlofBlomqvist committed Aug 27, 2023
1 parent 29fc5a6 commit 316cd76
Show file tree
Hide file tree
Showing 27 changed files with 1,434 additions and 642 deletions.
70 changes: 52 additions & 18 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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"] , optional = true}
plutus_data = { version = "0.0.7", optional = true }
plutus_data = { version = "0.0.91", 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 }
Expand All @@ -23,7 +23,7 @@ minicbor = { version = "0.19.1", optional = true }

getrandom = { version = "0.2.10", features = ["js"], optional = true}

wasm-bindgen = { version = "0.2.83", optional = true , features=["serde-serialize"]}
wasm-bindgen = { version = "0.2.87", optional = true , features=["serde-serialize"]}
web-sys = { version = "0.3.59", features = [ 'console' ], optional = true }
bech32 = "0.9.1"
time = "0.3.23"
Expand All @@ -40,6 +40,8 @@ proc-macro2 = "1.0.66"
pallas-primitives = "0.18.1"
pallas-codec = "0.18.1"
pallas = "0.18.1"
assert-json-diff = "2.0.2"
indexmap = "2.0.0"

[features]
bin-features = ["clap","utils","unstable"]
Expand Down
62 changes: 62 additions & 0 deletions marlowe.lbnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<Contract> ::= "Close"
| "Pay" <Party> <Payee> <Token> <Value> <Contract>
| "If" <Observation> <Contract> <Contract>
| "When" "[" {<Case> ","} "]" <Timeout> <Contract>
| "Let" <ValueId> <Value> <Contract>
| "Assert" <Observation> <Contract>
| "(" <Contract> ")"

<Case> ::= "Case" <Action> <Contract>
| "MerkleizedCase" <Action> <String>

<Action> ::= "Deposit" <Party> <Party> <Token> <Value>
| "Choice" <ChoiceId> "[" {<Bound> ","} "]"
| "Notify" <Observation>
| "(" <Action> ")"

<Value> ::= "AvailableMoney" <Party> <Token>
| "Constant" <Integer>
| "NegValue" <Value>
| "AddValue" <Value> <Value>
| "SubValue" <Value> <Value>
| "MulValue" <Value> <Value>
| "DivValue" <Value> <Value>
| "ChoiceValue" <ChoiceId>
| "TimeIntervalStart"
| "TimeIntervalEnd"
| "UseValue" <ValueId>
| "Cond" <Observation> <Value> <Value>
| "(" <Value> ")"

<Observation> ::= "AndObs" <Observation> <Observation>
| "OrObs" <Observation> <Observation>
| "NotObs" <Observation>
| "ChoseSomething" <ChoiceId>
| "ValueGE" <Value> <Value>
| "ValueGT" <Value> <Value>
| "ValueLT" <Value> <Value>
| "ValueLE" <Value> <Value>
| "ValueEQ" <Value> <Value>
| "TrueObs"
| "FalseObs"
| "(" <Observation> ")"

<Token> ::= "Token" <String> <String>
| "(" <Token> ")"

<Party> ::= "Address" <String>
| "Role" <String>
| "(" <Party> ")"

<Payee> ::= "Account" <Party>
| "Party" <Party>
| "(" <Payee> ")"

<ChoiceId> ::= "ChoiceId" <String> <Party>
| "(" <ChoiceId> ")"

<ValueId> ::= <String>

<Bound> ::= "Bound" <Integer> <Integer>

<Timeout> ::= <Integer>
Binary file modified pkg_wasi/marlowe_lang.wasm
Binary file not shown.
Binary file modified pkg_wasi/marlowe_lang_cli.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions src/cli_tool_bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ pub enum ContractArgs {
pub enum StateArgs {
InitUsingRole {
creator_role: String,
/// The amount of ada that will be sent to the contract in the initial utxo. (most likely the min amount of ada possible for a transaction)
initial_ada: i64
/// The amount of lovelace that will be sent to the contract in the initial utxo. (most likely the min amount of ada possible for a transaction)
initial_lovelace: i128
},
/// Not yet implemented
InitUsingAddr {
/// Bech32 Cardano address
creator_addr: String,
/// The amount of ada that will be sent to the contract in the initial utxo. (most likely the min amount of ada possible for a transaction)
initial_ada: i64
/// The amount of lovelace that will be sent to the contract in the initial utxo. (most likely the min amount of ada possible for a transaction)
initial_lovelace: i128
}
}

Expand Down
32 changes: 16 additions & 16 deletions src/cli_tool_bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod args;
use args::{DatumArgs, RedeemerArgs, StateArgs, ContractArgs};
use marlowe_lang::types::marlowe::{Contract, MarloweDatum, PossiblyMerkleizedInput, Token, Address};
use marlowe_lang::types::marlowe::{Contract, MarloweDatum, PossiblyMerkleizedInput, Token, Address, AccMap};
use std::collections::HashMap;
use marlowe_lang::extras::utils::*;
use plutus_data::ToPlutusData;
Expand Down Expand Up @@ -100,10 +100,10 @@ fn input_redeemer_handler(args:RedeemerArgs) {

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 } =>
create_state_addr(initial_ada,&creator_addr)
StateArgs::InitUsingRole { creator_role, initial_lovelace } =>
create_state(initial_lovelace,&creator_role),
StateArgs::InitUsingAddr { creator_addr, initial_lovelace } =>
create_state_addr(initial_lovelace,&creator_addr)
}
}

Expand Down Expand Up @@ -171,7 +171,7 @@ fn contract_handler(args:ContractArgs) {
let mut h = HashMap::new();
for x in v.split(",") {
let (name,value) = x.split_once("=").unwrap();
let value_num = value.trim().parse::<i64>().unwrap();
let value_num = value.trim().parse::<i128>().unwrap();
h.insert(name.trim().to_string(),value_num);
}
marlowe_lang::deserialization::marlowe::deserialize_with_input(&s,h).unwrap().contract
Expand Down Expand Up @@ -233,38 +233,38 @@ 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) {
fn create_state(initial_lovelace:i128,creator_role:&str) {

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

let creator = marlowe_lang::types::marlowe::Party::role(creator_role);

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

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

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

let mut state = marlowe_lang::types::marlowe::State {
accounts: HashMap::new(),
bound_values: HashMap::new(),
choices: HashMap::new(),
accounts: AccMap::new(),
bound_values: AccMap::new(),
choices: AccMap::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);
(initial_lovelace*1000) as u128);

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

Expand Down
Loading

0 comments on commit 316cd76

Please sign in to comment.