Skip to content

Commit 37fedc4

Browse files
committed
Merge #200: Add the possibility to insert .args files for compiling complex contracts
3c384e1 Add the possibility to insert .args files for compiling complex contracts (Illia Kripaka) Pull request description: This pr adds `--args` field for compiling complex programs. You can use the CLI to compile contracts such as those written in https://github.com/BlockstreamResearch/simplicity-contracts. * add `--args` for `.args` file * move default second witness file parameter to `--wit` For example, arguments for `option.simf` ```json { "TAKER_FUNDING_START_TIME": { "value": "1769163845", "type": "u32" }, "SETTLEMENT_ASSET_ID": { "value": "0xa5502895799e276b4af246c821423b4ed5ec5e6b4e6df7a861606939d9a2fc38", "type": "u256" }, "FEE_BASIS_POINTS": { "value": "0", "type": "u64" }, "EARLY_TERMINATION_END_TIME": { "value": "1769163835", "type": "u32" }, "FILLER_PER_SETTLEMENT_COLLATERAL": { "value": "11", "type": "u64" }, "INCENTIVE_BASIS_POINTS": { "value": "1000", "type": "u64" }, "GRANTOR_SETTLEMENT_TOKEN_ASSET": { "value": "0x1f58afc32aa70a012a9ebf30a543148be00aec1b371f9793a153c274acce2de5", "type": "u256" }, "COLLATERAL_ASSET_ID": { "value": "0x6d521c38ec1ea15734ae22b7c46064412829c0d0579f0a713d1c04ede979026f", "type": "u256" },"FILLER_PER_SETTLEMENT_ASSET": { "value": "110", "type": "u64" }, "FILLER_PER_PRINCIPAL_COLLATERAL": { "value": "10", "type": "u64" }, "GRANTOR_SETTLEMENT_PER_DEPOSITED_ASSET": { "value": "110", "type": "u64" }, "GRANTOR_PER_SETTLEMENT_COLLATERAL": { "value": "11", "type": "u64" }, "ORACLE_PK": { "value": "0x531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe337", "type": "u256" }, "GRANTOR_COLLATERAL_PER_DEPOSITED_COLLATERAL": { "value": "1", "type": "u64" }, "SETTLEMENT_HEIGHT": { "value": "0", "type": "u32" }, "FILLER_TOKEN_ASSET": { "value": "0x3c3fe87795809f74ed54005e0e50395fdc3b2862048641f439bd4ba6ca009502", "type": "u256" },"GRANTOR_PER_SETTLEMENT_ASSET": { "value": "110", "type": "u64" },"FEE_SCRIPT_HASH": { "value": "0x0000000000000000000000000000000000000000000000000000000000000000", "type": "u256" }, "TAKER_FUNDING_END_TIME": { "value": "1769163855", "type": "u32" }, "CONTRACT_EXPIRY_TIME": { "value": "1769163865", "type": "u32" }, "STRIKE_PRICE": { "value": "10", "type": "u64" },"GRANTOR_COLLATERAL_TOKEN_ASSET": { "value": "0x1c01566a1e789e1a3f84c73b2c04352d30008eb30d905a0f22ba3e1a5a835b6f", "type": "u256" } } ``` Partially related to these issues #196, #157. PS This PR does not intend to rewrite the CLI build using clap macros. It simply adds new functionality. ACKs for top commit: apoelstra: ACK 3c384e1; successfully ran local tests Tree-SHA512: f00d115384c891cf5abf24220012e16cff4971c519e6e6d8ec90983b8383c1f788f0e939c2a02c56d6980a804a0123d0d194ec953230cfd16cd87049a9d0f2f3
2 parents 6bd62dc + 3c384e1 commit 37fedc4

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

src/main.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use base64::display::Base64Display;
22
use base64::engine::general_purpose::STANDARD;
33
use clap::{Arg, ArgAction, Command};
44

5-
use simplicityhl::{Arguments, CompiledProgram};
5+
use simplicityhl::CompiledProgram;
66
use std::{env, fmt};
77

88
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
@@ -43,10 +43,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4343
)
4444
.arg(
4545
Arg::new("wit_file")
46+
.long("wit")
47+
.short('w')
4648
.value_name("WITNESS_FILE")
4749
.action(ArgAction::Set)
4850
.help("File containing the witness data"),
4951
)
52+
.arg(
53+
Arg::new("args_file")
54+
.long("args")
55+
.short('a')
56+
.value_name("ARGUMENTS_FILE")
57+
.action(ArgAction::Set)
58+
.help("File containing the arguments data"),
59+
)
5060
.arg(
5161
Arg::new("debug")
5262
.long("debug")
@@ -69,14 +79,32 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6979
let include_debug_symbols = matches.get_flag("debug");
7080
let output_json = matches.get_flag("json");
7181

72-
let compiled =
73-
match CompiledProgram::new(prog_text, Arguments::default(), include_debug_symbols) {
74-
Ok(program) => program,
75-
Err(e) => {
76-
eprintln!("{}", e);
77-
std::process::exit(1);
78-
}
79-
};
82+
#[cfg(feature = "serde")]
83+
let args_opt: simplicityhl::Arguments = match matches.get_one::<String>("args_file") {
84+
None => simplicityhl::Arguments::default(),
85+
Some(args_file) => {
86+
let args_path = std::path::Path::new(&args_file);
87+
let args_text = std::fs::read_to_string(args_path).map_err(|e| e.to_string())?;
88+
serde_json::from_str::<simplicityhl::Arguments>(&args_text)?
89+
}
90+
};
91+
#[cfg(not(feature = "serde"))]
92+
let args_opt: simplicityhl::Arguments = if matches.contains_id("args_file") {
93+
return Err(
94+
"Program was compiled without the 'serde' feature and cannot process .args files."
95+
.into(),
96+
);
97+
} else {
98+
simplicityhl::Arguments::default()
99+
};
100+
101+
let compiled = match CompiledProgram::new(prog_text, args_opt, include_debug_symbols) {
102+
Ok(program) => program,
103+
Err(e) => {
104+
eprintln!("{}", e);
105+
std::process::exit(1);
106+
}
107+
};
80108

81109
#[cfg(feature = "serde")]
82110
let witness_opt = matches

0 commit comments

Comments
 (0)