Michelson: adds -amount flag to run program

This commit is contained in:
Milo Davis 2017-07-19 11:35:01 +02:00 committed by Benjamin Canou
parent 800f4b555d
commit b52d4a78d9
8 changed files with 48 additions and 29 deletions

View File

@ -15,16 +15,6 @@ let tez_of_string s =
| None -> invalid_arg "tez_of_string" | None -> invalid_arg "tez_of_string"
| Some t -> t | Some t -> t
let fee = ref (tez_of_string "0.05")
let fee_arg =
"-fee",
Arg.String (fun s ->
try fee := tez_of_string s
with _ -> raise (Arg.Bad "invalid \xEA\x9C\xA9 notation in parameter -fee")),
"The fee in \xEA\x9C\xA9 to pay to the miner.\n\
default: \'0.05\"\n\
text format: D,DDD,DDD.DD (centiles are optional, comas are optional)"
let init = ref "Unit" let init = ref "Unit"
let init_arg = let init_arg =
"-init", "-init",
@ -79,16 +69,37 @@ let delegatable_args =
Arg.Clear delegatable, Arg.Clear delegatable,
"Set the created contract to be non delegatable (default)" ] "Set the created contract to be non delegatable (default)" ]
let tez_format = "text format: D,DDD,DDD.DD (centiles are optional, commas are optional)"
let tez_arg ~name ~desc ~default =
let ref_cell = ref (tez_of_string default) in
(ref_cell,
(name,
Arg.String (fun s ->
try ref_cell := tez_of_string s
with _ -> raise (Arg.Bad
("invalid \xEA\x9C\xA9 notation in parameter " ^ name))),
(Printf.sprintf
"%s\ndefault: \"%s\"\n%s"
desc
default
tez_format)))
let tez_param ~name ~desc next = let tez_param ~name ~desc next =
Cli_entries.param Cli_entries.param
name name
(desc ^ " in \xEA\x9C\xA9\n\ (desc ^ " in \xEA\x9C\xA9\n" ^ tez_format)
text format: D,DDD,DDD.DD (centiles and comas are optional)")
(fun _ s -> (fun _ s ->
try return (tez_of_string s) try return (tez_of_string s)
with _ -> failwith "invalid \xEA\x9C\xA9 notation") with _ -> failwith "invalid \xEA\x9C\xA9 notation")
next next
let fee, fee_arg =
tez_arg
~name:"-fee"
~desc:"The fee in \xEA\x9C\xA9 to pay to the miner."
~default:"0.05"
let max_priority = ref None let max_priority = ref None
let max_priority_arg = let max_priority_arg =
"-max-priority", "-max-priority",

View File

@ -21,6 +21,11 @@ val free_mining_arg: string * Arg.spec * string
val force_arg: string * Arg.spec * string val force_arg: string * Arg.spec * string
val endorsement_delay_arg: string * Arg.spec * string val endorsement_delay_arg: string * Arg.spec * string
val tez_arg :
name:string ->
desc:string ->
default:string ->
Tez.tez ref * (string * Arg.spec * string)
val tez_param : val tez_param :
name:string -> name:string ->
desc:string -> desc:string ->

View File

@ -535,6 +535,11 @@ let commands () =
"-trace-stack", "-trace-stack",
Arg.Set trace_stack, Arg.Set trace_stack,
"Show the stack after each step" in "Show the stack after each step" in
let amount, amount_arg =
Client_proto_args.tez_arg
~name:"-amount"
~desc:"The amount of the transfer in \xEA\x9C\xA9."
~default: "0.00" in
[ [
command ~group ~desc: "lists all known programs" command ~group ~desc: "lists all known programs"
@ -567,7 +572,7 @@ let commands () =
return ()) ; return ()) ;
command ~group ~desc: "ask the node to run a program" command ~group ~desc: "ask the node to run a program"
~args: [ trace_stack_arg ] ~args: [ trace_stack_arg ; amount_arg ]
(prefixes [ "run" ; "program" ] (prefixes [ "run" ; "program" ]
@@ Program.source_param @@ Program.source_param
@@ prefixes [ "on" ; "storage" ] @@ prefixes [ "on" ; "storage" ]
@ -581,7 +586,7 @@ let commands () =
let open Data_encoding in let open Data_encoding in
if !trace_stack then if !trace_stack then
Client_proto_rpcs.Helpers.trace_code cctxt.rpc_config Client_proto_rpcs.Helpers.trace_code cctxt.rpc_config
cctxt.config.block program (storage, input) >>= function cctxt.config.block program (storage, input, !amount) >>= function
| Ok (storage, output, trace) -> | Ok (storage, output, trace) ->
cctxt.message cctxt.message
"@[<v 0>@[<v 2>storage@,%a@]@,\ "@[<v 0>@[<v 2>storage@,%a@]@,\
@ -604,7 +609,7 @@ let commands () =
return () return ()
else else
Client_proto_rpcs.Helpers.run_code cctxt.rpc_config Client_proto_rpcs.Helpers.run_code cctxt.rpc_config
cctxt.config.block program (storage, input) >>= function cctxt.config.block program (storage, input, !amount) >>= function
| Ok (storage, output) -> | Ok (storage, output) ->
cctxt.message "@[<v 0>@[<v 2>storage@,%a@]@,@[<v 2>output@,%a@]@]@." cctxt.message "@[<v 0>@[<v 2>storage@,%a@]@,@[<v 2>output@,%a@]@]@."
(print_expr no_locations) storage (print_expr no_locations) storage

View File

@ -148,13 +148,13 @@ module Helpers = struct
call_error_service1 cctxt Services.Helpers.apply_operation call_error_service1 cctxt Services.Helpers.apply_operation
block (pred_block, hash, forged_operation, signature) block (pred_block, hash, forged_operation, signature)
let run_code cctxt block code (storage, input) = let run_code cctxt block code (storage, input, amount) =
call_error_service1 cctxt Services.Helpers.run_code call_error_service1 cctxt Services.Helpers.run_code
block (code, storage, input, None, None, None) block (code, storage, input, amount, None, None)
let trace_code cctxt block code (storage, input) = let trace_code cctxt block code (storage, input, amount) =
call_error_service1 cctxt Services.Helpers.trace_code call_error_service1 cctxt Services.Helpers.trace_code
block (code, storage, input, None, None, None) block (code, storage, input, amount, None, None)
let typecheck_data cctxt = let typecheck_data cctxt =
call_error_service1 cctxt Services.Helpers.typecheck_data call_error_service1 cctxt Services.Helpers.typecheck_data

View File

@ -153,12 +153,12 @@ module Helpers : sig
val run_code: val run_code:
Client_rpcs.config -> Client_rpcs.config ->
block -> Script.code -> block -> Script.code ->
(Script.expr * Script.expr) -> (Script.expr * Script.expr * Tez.t) ->
(Script.expr * Script.expr) tzresult Lwt.t (Script.expr * Script.expr) tzresult Lwt.t
val trace_code: val trace_code:
Client_rpcs.config -> Client_rpcs.config ->
block -> Script.code -> block -> Script.code ->
(Script.expr * Script.expr) -> (Script.expr * Script.expr * Tez.t) ->
(Script.expr * Script.expr * (Script.expr * Script.expr *
(Script.location * int * Script.expr list) list) tzresult Lwt.t (Script.location * int * Script.expr list) list) tzresult Lwt.t
val typecheck_code: val typecheck_code:

View File

@ -361,7 +361,7 @@ module Helpers = struct
(req "script" Script.code_encoding) (req "script" Script.code_encoding)
(req "storage" Script.expr_encoding) (req "storage" Script.expr_encoding)
(req "input" Script.expr_encoding) (req "input" Script.expr_encoding)
(opt "amount" Tez.encoding) (req "amount" Tez.encoding)
(opt "contract" Contract.encoding) (opt "contract" Contract.encoding)
(opt "origination_nonce" Contract.origination_nonce_encoding)) (opt "origination_nonce" Contract.origination_nonce_encoding))

View File

@ -258,13 +258,6 @@ let () =
| (_ctxt, _, Some script_err) -> Lwt.return (Error script_err) | (_ctxt, _, Some script_err) -> Lwt.return (Error script_err)
| (_ctxt, contracts, None) -> Lwt.return (Ok contracts)) ; | (_ctxt, contracts, None) -> Lwt.return (Ok contracts)) ;
let run_parameters ctxt (script, storage, input, amount, contract, origination_nonce) = let run_parameters ctxt (script, storage, input, amount, contract, origination_nonce) =
let amount =
match amount with
| Some amount -> amount
| None ->
match Tez.of_cents 100_00L with
| Some tez -> tez
| None -> Tez.zero in
let contract = let contract =
match contract with match contract with
| Some contract -> contract | Some contract -> contract

View File

@ -0,0 +1,5 @@
# Fail if the amount transferred is less than 10
parameter unit;
storage unit;
return unit;
code {CAAR; PUSH tez "10"; CMPGT; IF {FAIL} {UNIT; UNIT; PAIR}}