diff --git a/src/proto_alpha/lib_client/managed_contract.ml b/src/proto_alpha/lib_client/managed_contract.ml index 5d406c1276484f09886e9aa0085023eb0b6364cd..3d5c2c6996a1d25462f1e72ca86173acb9f16378 100644 --- a/src/proto_alpha/lib_client/managed_contract.ml +++ b/src/proto_alpha/lib_client/managed_contract.ml @@ -39,13 +39,15 @@ let check_smart_contract (cctxt : #full) opt_res some = let get_contract_manager (cctxt : #full) contract = let open Micheline in let open Michelson_v1_primitives in - Client_proto_context.get_storage - cctxt - ~chain:cctxt#chain - ~block:cctxt#block - ~unparsing_mode:Optimized - contract - >>=? fun storage -> + let open Lwt_result_syntax in + let* storage = + Client_proto_context.get_storage + cctxt + ~chain:cctxt#chain + ~block:cctxt#block + ~unparsing_mode:Optimized + contract + in check_smart_contract cctxt storage @@ fun storage -> match root storage with | Prim (_, D_Pair, Bytes (_, bytes) :: _, _) | Bytes (_, bytes) -> ( @@ -81,10 +83,11 @@ let get_contract_manager (cctxt : #full) contract = storage let parse code = - Lwt.return - ( Micheline_parser.no_parsing_error - @@ Michelson_v1_parser.parse_expression code - >>? fun exp -> ok @@ Script.lazy_expr Michelson_v1_parser.(exp.expanded) ) + let open Lwt_result_syntax in + let parsed_exp = Michelson_v1_parser.parse_expression code in + let*? {expanded; _} = Micheline_parser.no_parsing_error parsed_exp in + let la = Script.lazy_expr expanded in + return la let build_lambda_for_set_delegate ~delegate = match delegate with @@ -106,49 +109,56 @@ let build_delegate_operation (cctxt : #full) ~chain ~block ?fee contract (* the KT1 to delegate *) (delegate : Signature.public_key_hash option) = let entrypoint = entrypoint_do in - (Michelson_v1_entrypoints.contract_entrypoint_type - cctxt - ~chain - ~block - ~contract - ~entrypoint - ~normalize_types:true - >>=? function - | Some _ -> - (* their is a "do" entrypoint (we could check its type here)*) - parse @@ build_lambda_for_set_delegate ~delegate >>=? fun param -> - return (param, entrypoint) - | None -> ( - (* their is no "do" entrypoint trying "set/remove_delegate" *) - let entrypoint = - match delegate with - | Some _ -> entrypoint_set_delegate - | None -> entrypoint_remove_delegate - in - Michelson_v1_entrypoints.contract_entrypoint_type - cctxt - ~chain - ~block - ~contract - ~entrypoint - ~normalize_types:true - >>=? function - | Some _ -> - (* their is a "set/remove_delegate" entrypoint *) - let delegate_data = - match delegate with - | Some delegate -> - let (`Hex delegate) = - Signature.Public_key_hash.to_hex delegate - in - "0x" ^ delegate - | None -> "Unit" - in - parse delegate_data >>=? fun param -> return (param, entrypoint) - | None -> - cctxt#error - "Cannot find a %%do or %%set_delegate entrypoint in contract@.")) - >>=? fun (parameters, entrypoint) -> + let open Lwt_result_syntax in + let* expr_opt = + Michelson_v1_entrypoints.contract_entrypoint_type + cctxt + ~chain + ~block + ~contract + ~entrypoint + ~normalize_types:true + in + let* parameters, entrypoint = + match expr_opt with + | Some _ -> + (* there is a "do" entrypoint (we could check its type here)*) + let* param = parse @@ build_lambda_for_set_delegate ~delegate in + return (param, entrypoint) + | None -> ( + (* there is no "do" entrypoint trying "set/remove_delegate" *) + let entrypoint = + match delegate with + | Some _ -> entrypoint_set_delegate + | None -> entrypoint_remove_delegate + in + let* expr_opt = + Michelson_v1_entrypoints.contract_entrypoint_type + cctxt + ~chain + ~block + ~contract + ~entrypoint + ~normalize_types:true + in + match expr_opt with + | Some _ -> + (* there is a "set/remove_delegate" entrypoint *) + let delegate_data = + match delegate with + | Some delegate -> + let (`Hex delegate) = + Signature.Public_key_hash.to_hex delegate + in + "0x" ^ delegate + | None -> "Unit" + in + let* param = parse delegate_data in + return (param, entrypoint) + | None -> + cctxt#error + "Cannot find a %%do or %%set_delegate entrypoint in contract@.") + in return (Client_proto_context.build_transaction_operation ~amount:Tez.zero @@ -161,27 +171,31 @@ let set_delegate (cctxt : #full) ~chain ~block ?confirmations ?dry_run ?verbose_signing ?simulation ?branch ~fee_parameter ?fee ~source ~src_pk ~src_sk contract (* the KT1 to delegate *) (delegate : Signature.public_key_hash option) = - build_delegate_operation cctxt ~chain ~block ?fee contract delegate - >>=? fun operation -> + let open Lwt_result_syntax in + let* operation = + build_delegate_operation cctxt ~chain ~block ?fee contract delegate + in let operation = Annotated_manager_operation.Single_manager operation in - Injection.inject_manager_operation - cctxt - ~chain - ~block - ?confirmations - ?dry_run - ?verbose_signing - ?simulation - ?branch - ~source - ~fee:(Limit.of_option fee) - ~gas_limit:Limit.unknown - ~storage_limit:(Limit.known Z.zero) - ~src_pk - ~src_sk - ~fee_parameter - operation - >>=? return_single_manager_result + let* result = + Injection.inject_manager_operation + cctxt + ~chain + ~block + ?confirmations + ?dry_run + ?verbose_signing + ?simulation + ?branch + ~source + ~fee:(Limit.of_option fee) + ~gas_limit:Limit.unknown + ~storage_limit:(Limit.known Z.zero) + ~src_pk + ~src_sk + ~fee_parameter + operation + in + return_single_manager_result result let d_unit = Micheline.strip_locations (Prim (0, Michelson_v1_primitives.D_Unit, [], [])) @@ -231,52 +245,60 @@ let build_lambda_for_transfer_to_originated ~destination ~entrypoint ~amount let build_transaction_operation (cctxt : #full) ~chain ~block ~contract ~(destination : Contract.t) ?(entrypoint = Entrypoint.default) ?arg ~amount ?fee ?gas_limit ?storage_limit () = - (match destination with - | Implicit destination when Entrypoint.is_default entrypoint -> - return @@ build_lambda_for_transfer_to_implicit ~destination ~amount - | Implicit _ -> - cctxt#error - "Implicit accounts have no entrypoints. (targeted entrypoint %%%a on \ - contract %a)" - Entrypoint.pp - entrypoint - Contract.pp - destination - | Originated destination -> - (Michelson_v1_entrypoints.contract_entrypoint_type - cctxt - ~chain - ~block - ~contract:destination - ~entrypoint - ~normalize_types:true - >>=? function - | None -> - cctxt#error - "Contract %a has no entrypoint named %a" - Contract_hash.pp - destination - Entrypoint.pp - entrypoint - | Some parameter_type -> return parameter_type) - >>=? fun parameter_type -> - (match arg with - | Some arg -> - Lwt.return @@ Micheline_parser.no_parsing_error - @@ Michelson_v1_parser.parse_expression arg - >>=? fun {expanded = arg; _} -> return_some arg - | None -> return_none) - >>=? fun parameter -> - let parameter = Option.value ~default:d_unit parameter in - return - @@ build_lambda_for_transfer_to_originated - ~destination - ~entrypoint - ~amount - ~parameter_type - ~parameter) - >>=? fun lambda -> - parse lambda >>=? fun parameters -> + let open Lwt_result_syntax in + let* lambda = + match destination with + | Implicit destination when Entrypoint.is_default entrypoint -> + return @@ build_lambda_for_transfer_to_implicit ~destination ~amount + | Implicit _ -> + cctxt#error + "Implicit accounts have no entrypoints. (targeted entrypoint %%%a on \ + contract %a)" + Entrypoint.pp + entrypoint + Contract.pp + destination + | Originated destination -> + let* parameter_type = + let* expr_opt = + Michelson_v1_entrypoints.contract_entrypoint_type + cctxt + ~chain + ~block + ~contract:destination + ~entrypoint + ~normalize_types:true + in + match expr_opt with + | None -> + cctxt#error + "Contract %a has no entrypoint named %a" + Contract_hash.pp + destination + Entrypoint.pp + entrypoint + | Some parameter_type -> return parameter_type + in + let* parameter = + match arg with + | Some arg -> + let* {expanded = arg; _} = + Lwt.return @@ Micheline_parser.no_parsing_error + @@ Michelson_v1_parser.parse_expression arg + in + return_some arg + | None -> return_none + in + let parameter = Option.value ~default:d_unit parameter in + return + @@ build_lambda_for_transfer_to_originated + ~destination + ~entrypoint + ~amount + ~parameter_type + ~parameter + in + let* parameters = parse lambda in let entrypoint = entrypoint_do in return (Client_proto_context.build_transaction_operation @@ -295,41 +317,44 @@ let transfer (cctxt : #full) ~chain ~block ?confirmations ?dry_run (Kind.transaction Kind.manager Injection.result * Contract_hash.t list) tzresult Lwt.t = - build_transaction_operation - cctxt - ~chain - ~block - ~contract - ~destination - ~entrypoint - ?arg - ~amount - ?fee - ?gas_limit - ?storage_limit - () - >>=? fun operation -> + let open Lwt_result_syntax in + let* operation = + build_transaction_operation + cctxt + ~chain + ~block + ~contract + ~destination + ~entrypoint + ?arg + ~amount + ?fee + ?gas_limit + ?storage_limit + () + in let operation = Annotated_manager_operation.Single_manager operation in - Injection.inject_manager_operation - cctxt - ~chain - ~block - ?confirmations - ?dry_run - ?verbose_signing - ?simulation - ~force - ?branch - ~source - ~fee:(Limit.of_option fee) - ~gas_limit:(Limit.of_option gas_limit) - ~storage_limit:(Limit.of_option storage_limit) - ?counter - ~src_pk - ~src_sk - ~fee_parameter - operation - >>=? fun ((_, _, _, result) as res) -> - Lwt.return (Injection.originated_contracts ~force result) - >>=? fun contracts -> - return_single_manager_result res >>=? fun res -> return (res, contracts) + let* ((_, _, _, result) as res) = + Injection.inject_manager_operation + cctxt + ~chain + ~block + ?confirmations + ?dry_run + ?verbose_signing + ?simulation + ~force + ?branch + ~source + ~fee:(Limit.of_option fee) + ~gas_limit:(Limit.of_option gas_limit) + ~storage_limit:(Limit.of_option storage_limit) + ?counter + ~src_pk + ~src_sk + ~fee_parameter + operation + in + let*? contracts = Injection.originated_contracts ~force result in + let* res = return_single_manager_result res in + return (res, contracts)