2019-05-13 00:56:22 +04:00
open Cmdliner
open Trace
2019-06-06 21:37:46 +04:00
let error_pp out ( e : error ) =
let open JSON_string_utils in
let message =
let opt = e | > member " message " | > string in
let msg = Option . unopt ~ default : " " opt in
2019-06-07 00:49:36 +04:00
if msg = " "
then " "
else " : " ^ msg in
2019-06-06 21:37:46 +04:00
let error_code =
let error_code = e | > member " error_code " in
match error_code with
| ` Null -> " "
| _ -> " ( " ^ ( J . to_string error_code ) ^ " ) " in
let title =
let opt = e | > member " title " | > string in
Option . unopt ~ default : " " opt in
let data =
let data = e | > member " data " in
match data with
| ` Null -> " "
| _ -> " " ^ ( J . to_string data ) ^ " \n " in
2019-06-07 00:49:36 +04:00
let infos =
let infos = e | > member " infos " in
match infos with
| ` Null -> " "
| _ -> " " ^ ( J . to_string infos ) ^ " \n " in
Format . fprintf out " %s%s%s. \n %s%s " title error_code message data infos
2019-06-06 21:37:46 +04:00
2019-05-13 00:56:22 +04:00
let toplevel x =
match x with
| Trace . Ok ( () , annotations ) -> ignore annotations ; ()
2019-06-06 21:37:46 +04:00
| Error ss -> (
2019-06-03 15:07:24 +04:00
Format . printf " %a%! " error_pp ( ss () )
2019-06-06 21:37:46 +04:00
)
2019-05-13 00:56:22 +04:00
let main =
let term = Term . ( const print_endline $ const " Ligo needs a command. Do ligo --help " ) in
( term , Term . info " ligo " )
2019-06-09 16:08:37 +04:00
let source n =
2019-05-30 02:09:40 +04:00
let open Arg in
let info =
let docv = " SOURCE_FILE " in
let doc = " $(docv) is the path to the .ligo file of the contract. " in
info ~ docv ~ doc [] in
2019-06-09 16:08:37 +04:00
required @@ pos n ( some string ) None info
2019-05-30 02:09:40 +04:00
2019-06-09 16:08:37 +04:00
let entry_point n =
2019-05-30 02:09:40 +04:00
let open Arg in
let info =
let docv = " ENTRY_POINT " in
let doc = " $(docv) is entry-point that will be compiled. " in
info ~ docv ~ doc [] in
2019-06-09 16:08:37 +04:00
required @@ pos n ( some string ) ( Some " main " ) info
2019-05-30 02:09:40 +04:00
2019-06-09 16:08:37 +04:00
let expression n =
2019-05-30 02:09:40 +04:00
let open Arg in
let docv = " EXPRESSION " in
let doc = " $(docv) is the expression that will be compiled. " in
let info = info ~ docv ~ doc [] in
2019-06-09 16:08:37 +04:00
required @@ pos n ( some string ) None info
2019-05-30 02:09:40 +04:00
let syntax =
let open Arg in
let info =
let docv = " SYNTAX " in
let doc = " $(docv) is the syntax that will be used. Currently supported syntaxes are \" pascaligo \" and \" cameligo \" . \" pascaligo \" is the default. " in
2019-06-06 13:30:29 +04:00
info ~ docv ~ doc [ " syntax " ; " s " ] in
2019-05-30 02:09:40 +04:00
value @@ opt string " pascaligo " info
2019-06-12 23:13:06 +04:00
let amount =
let open Arg in
let info =
let docv = " AMOUNT " in
let doc = " $(docv) is the amount the dry-run transaction will use. " in
info ~ docv ~ doc [ " amount " ] in
value @@ opt string " 0 " info
2019-05-13 00:56:22 +04:00
let compile_file =
2019-05-30 02:09:40 +04:00
let f source entry_point syntax =
2019-05-13 00:56:22 +04:00
toplevel @@
let % bind contract =
2019-06-05 21:16:54 +04:00
trace ( simple_info " compiling contract to michelson " ) @@
2019-06-01 15:51:49 +04:00
Ligo . Run . compile_contract_file source entry_point syntax in
2019-06-07 00:49:36 +04:00
Format . printf " %s \n " contract ;
2019-05-13 00:56:22 +04:00
ok ()
in
let term =
2019-06-09 16:08:37 +04:00
Term . ( const f $ source 0 $ entry_point 1 $ syntax ) in
2019-06-05 21:17:42 +04:00
let cmdname = " compile-contract " in
let docs = " Subcommand: compile a contract. See `ligo " ^ cmdname ^ " --help' for a list of options specific to this subcommand. " in
( term , Term . info ~ docs cmdname )
2019-05-13 00:56:22 +04:00
let compile_parameter =
2019-05-30 02:09:40 +04:00
let f source entry_point expression syntax =
2019-05-13 00:56:22 +04:00
toplevel @@
let % bind value =
trace ( simple_error " compile-input " ) @@
2019-06-01 15:51:49 +04:00
Ligo . Run . compile_contract_parameter source entry_point expression syntax in
2019-06-07 00:49:36 +04:00
Format . printf " %s \n " value ;
2019-05-13 00:56:22 +04:00
ok ()
in
let term =
2019-06-09 16:08:37 +04:00
Term . ( const f $ source 0 $ entry_point 1 $ expression 2 $ syntax ) in
2019-06-05 21:17:42 +04:00
let cmdname = " compile-parameter " in
let docs = " Subcommand: compile parameters to a michelson expression. The resulting michelson expression can be passed as an argument in a transaction which calls a contract. See `ligo " ^ cmdname ^ " --help' for a list of options specific to this subcommand. " in
( term , Term . info ~ docs cmdname )
2019-05-13 00:56:22 +04:00
let compile_storage =
2019-05-30 02:09:40 +04:00
let f source entry_point expression syntax =
2019-05-13 00:56:22 +04:00
toplevel @@
let % bind value =
trace ( simple_error " compile-storage " ) @@
2019-06-01 15:51:49 +04:00
Ligo . Run . compile_contract_storage source entry_point expression syntax in
2019-06-07 00:49:36 +04:00
Format . printf " %s \n " value ;
2019-05-13 00:56:22 +04:00
ok ()
in
let term =
2019-06-09 16:08:37 +04:00
Term . ( const f $ source 0 $ entry_point 1 $ expression 2 $ syntax ) in
2019-06-05 21:17:42 +04:00
let cmdname = " compile-storage " in
let docs = " Subcommand: compile an initial storage in ligo syntax to a michelson expression. The resulting michelson expression can be passed as an argument in a transaction which originates a contract. See `ligo " ^ cmdname ^ " --help' for a list of options specific to this subcommand. " in
( term , Term . info ~ docs cmdname )
2019-05-13 00:56:22 +04:00
2019-06-09 16:08:37 +04:00
let dry_run =
2019-06-12 23:13:06 +04:00
let f source entry_point storage input amount syntax =
2019-06-09 16:08:37 +04:00
toplevel @@
let % bind output =
2019-06-12 23:13:06 +04:00
Ligo . Run . run_contract ~ amount source entry_point storage input syntax in
2019-06-09 16:08:37 +04:00
Format . printf " %a \n " Ast_simplified . PP . expression output ;
ok ()
in
let term =
2019-06-12 23:13:06 +04:00
Term . ( const f $ source 0 $ entry_point 1 $ expression 2 $ expression 3 $ amount $ syntax ) in
2019-06-09 16:08:37 +04:00
let cmdname = " dry-run " in
let docs = " Subcommand: run a smart-contract with the given storage and input. " in
( term , Term . info ~ docs cmdname )
2019-05-13 00:56:22 +04:00
2019-06-11 21:57:07 +04:00
let run_function =
2019-06-12 23:13:06 +04:00
let f source entry_point parameter amount syntax =
2019-06-11 21:57:07 +04:00
toplevel @@
let % bind output =
2019-06-12 23:13:06 +04:00
Ligo . Run . run_function ~ amount source entry_point parameter syntax in
2019-06-11 21:57:07 +04:00
Format . printf " %a \n " Ast_simplified . PP . expression output ;
ok ()
in
let term =
2019-06-12 23:13:06 +04:00
Term . ( const f $ source 0 $ entry_point 1 $ expression 2 $ amount $ syntax ) in
2019-06-11 21:57:07 +04:00
let cmdname = " run-function " in
let docs = " Subcommand: run a function with the given parameter. " in
( term , Term . info ~ docs cmdname )
let evaluate_value =
2019-06-12 23:13:06 +04:00
let f source entry_point amount syntax =
2019-06-11 21:57:07 +04:00
toplevel @@
let % bind output =
2019-06-12 23:13:06 +04:00
Ligo . Run . evaluate_value ~ amount source entry_point syntax in
2019-06-11 21:57:07 +04:00
Format . printf " %a \n " Ast_simplified . PP . expression output ;
ok ()
in
let term =
2019-06-12 23:13:06 +04:00
Term . ( const f $ source 0 $ entry_point 1 $ amount $ syntax ) in
2019-06-11 21:57:07 +04:00
let cmdname = " evaluate-value " in
let docs = " Subcommand: evaluate a given definition. " in
( term , Term . info ~ docs cmdname )
let () = Term . exit @@ Term . eval_choice main [
compile_file ;
compile_parameter ;
compile_storage ;
dry_run ;
run_function ;
evaluate_value ;
]