package octez-protocol-019-PtParisB-libs
Octez protocol 019-PtParisB libraries
Install
Dune Dependency
Authors
Maintainers
Sources
tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65
doc/src/octez-protocol-019-PtParisB-libs.plugin/view_helpers.ml.html
Source file view_helpers.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2018 Nomadic Development. <contact@tezcore.com> *) (* Copyright (c) 2021-2022 Nomadic Labs, <contact@nomadic-labs.com> *) (* Copyright (c) 2022 TriliTech <contact@trili.tech> *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) (* to deal in the Software without restriction, including without limitation *) (* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) (* and/or sell copies of the Software, and to permit persons to whom the *) (* Software is furnished to do so, subject to the following conditions: *) (* *) (* The above copyright notice and this permission notice shall be included *) (* in all copies or substantial portions of the Software. *) (* *) (* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) (* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) (* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) (* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) (* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) (* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) open Protocol open Alpha_context open Tezos_micheline type Environment.Error_monad.error += Viewed_contract_has_no_script type Environment.Error_monad.error += View_callback_origination_failed type Environment.Error_monad.error += | Illformed_view_type of Entrypoint.t * Script.expr type Environment.Error_monad.error += | View_never_returns of Entrypoint.t * Contract_hash.t type Environment.Error_monad.error += | View_unexpected_return of Entrypoint.t * Contract_hash.t type Environment.Error_monad.error += View_not_found of Contract_hash.t * string type Environment.Error_monad.error += Viewer_unexpected_storage let () = Environment.Error_monad.register_error_kind `Permanent ~id:"viewedContractHasNoScript" ~title:"Viewed contract has no script" ~description:"A view was called on a contract with no script." ~pp:(fun ppf () -> Format.fprintf ppf "A view was called on a contract with no script.") Data_encoding.(unit) (function Viewed_contract_has_no_script -> Some () | _ -> None) (fun () -> Viewed_contract_has_no_script) ; Environment.Error_monad.register_error_kind `Permanent ~id:"viewCallbackOriginationFailed" ~title:"View callback origination failed" ~description:"View callback origination failed" ~pp:(fun ppf () -> Format.fprintf ppf "Error during origination of view callback contract.") Data_encoding.(unit) (function View_callback_origination_failed -> Some () | _ -> None) (fun () -> View_callback_origination_failed) ; Environment.Error_monad.register_error_kind `Permanent ~id:"illformedViewType" ~title:"An entrypoint type is incompatible with TZIP-4 view type." ~description:"An entrypoint type is incompatible with TZIP-4 view type." ~pp:(fun ppf (entrypoint, typ) -> Format.fprintf ppf "The view %a has type %a, it is not compatible with a TZIP-4 view type." Entrypoint.pp entrypoint Micheline_printer.print_expr (Micheline_printer.printable (fun x -> x) (Michelson_v1_primitives.strings_of_prims typ))) Data_encoding.( obj2 (req "entrypoint" Entrypoint.simple_encoding) (req "type" Script.expr_encoding)) (function Illformed_view_type (etp, exp) -> Some (etp, exp) | _ -> None) (fun (etp, exp) -> Illformed_view_type (etp, exp)) ; Environment.Error_monad.register_error_kind `Permanent ~id:"viewNeverReturns" ~title:"A view never returned a transaction to the given callback contract" ~description: "A view never initiated a transaction to the given callback contract." ~pp:(fun ppf (entrypoint, callback) -> Format.fprintf ppf "The view %a never initiated a transaction to the given callback \ contract %a." Entrypoint.pp entrypoint Contract_hash.pp callback) Data_encoding.( obj2 (req "entrypoint" Entrypoint.simple_encoding) (req "callback" Contract.originated_encoding)) (function View_never_returns (e, c) -> Some (e, c) | _ -> None) (fun (e, c) -> View_never_returns (e, c)) ; Environment.Error_monad.register_error_kind `Permanent ~id:"viewUnexpectedReturn" ~title:"A view returned an unexpected list of operations" ~description: "A view initiated a list of operations while the TZIP-4 standard expects \ only a transaction to the given callback contract." ~pp:(fun ppf (entrypoint, callback) -> Format.fprintf ppf "The view %a initiated a list of operations while the TZIP-4 standard \ expects only a transaction to the given callback contract %a." Entrypoint.pp entrypoint Contract_hash.pp callback) Data_encoding.( obj2 (req "entrypoint" Entrypoint.simple_encoding) (req "callback" Contract.originated_encoding)) (function View_unexpected_return (e, c) -> Some (e, c) | _ -> None) (fun (e, c) -> View_unexpected_return (e, c)) ; Environment.Error_monad.register_error_kind `Permanent ~id:"viewNotFound" ~title:"A view could not be found" ~description:"The contract does not have a view of the given name." ~pp:(fun ppf (contract, name) -> Format.fprintf ppf "The contract %a does not have a view named `%s`." Contract_hash.pp contract name) Data_encoding.( obj2 (req "contract" Contract.originated_encoding) (req "view" string)) (function View_not_found (k, n) -> Some (k, n) | _ -> None) (fun (k, n) -> View_not_found (k, n)) ; Environment.Error_monad.register_error_kind `Permanent ~id:"viewerUnexpectedStorage" ~title:"A VIEW instruction returned an unexpected value" ~description:"A VIEW instruction returned an unexpected value." ~pp:(fun ppf () -> Format.fprintf ppf "The simulated view returned an unexpected value.") Data_encoding.unit (function Viewer_unexpected_storage -> Some () | _ -> None) (fun () -> Viewer_unexpected_storage) (* This script is actually never run, its usage is to ensure a contract that has the type `contract <ty>` is originated, which will be required as callback of the view. *) let make_tzip4_viewer_script ty : Script.t = let loc = 0 in let ty = Micheline.root ty in let code = Micheline.strip_locations @@ Micheline.Seq ( loc, [ Micheline.Prim (loc, Script.K_parameter, [ty], []); Micheline.Prim ( loc, Script.K_storage, [Micheline.Prim (loc, Script.T_unit, [], [])], [] ); Micheline.Prim ( loc, Script.K_code, [Micheline.Prim (loc, Script.I_FAILWITH, [], [])], [] ); ] ) in let storage = Micheline.strip_locations (Micheline.Prim (loc, Script.D_Unit, [], [])) in {code = Script.lazy_expr code; storage = Script.lazy_expr storage} let make_view_parameter input callback = let loc = 0 in Micheline.strip_locations (Micheline.Prim ( loc, Script.D_Pair, [ input; Micheline.Bytes (loc, Data_encoding.Binary.to_bytes_exn Contract.encoding callback); ], [] )) let extract_view_output_type entrypoint ty = match Micheline.root ty with | Micheline.Prim (_, Script.T_pair, [_; Micheline.Prim (_, Script.T_contract, [ty], _)], _) -> Ok (Micheline.strip_locations ty) | _ -> Environment.Error_monad.error (Illformed_view_type (entrypoint, ty)) (* 'view' entrypoints returns their value by calling a callback contract, thus the expected result is a unique internal transaction to this callback. *) let extract_parameter_from_operations entrypoint operations callback = let unexpected_return = Environment.Error_monad.error @@ View_unexpected_return (entrypoint, callback) in match operations with | [ Script_typed_ir.Internal_operation { operation = Transaction_to_smart_contract { destination; unparsed_parameters; entrypoint = _; amount = _; parameters = _; parameters_ty = _; location = _; }; sender = _; nonce = _; }; ] when Contract_hash.equal destination callback -> Ok unparsed_parameters | [] -> Environment.Error_monad.error (View_never_returns (entrypoint, callback)) | _ -> unexpected_return (* [make_michelson_viewer_script contract view input input_ty output_ty] generates a script that calls a view from a given contract, and stores the result in its storage. *) let make_michelson_viewer_script address view input input_ty output_ty : Script.t = let loc = 0 in let address = Micheline.String (loc, Contract.to_b58check address) in let push ty value = Micheline.Prim (loc, Script.I_PUSH, [ty; value], []) in let storage_decl = Micheline.Prim (loc, Script.T_option, [output_ty], []) in let body = Micheline.Seq ( loc, [ Micheline.Prim (loc, Script.I_DROP, [], []); push (Micheline.Prim (loc, Script.T_address, [], [])) address; push input_ty (Micheline.root input); Micheline.Prim (loc, Script.I_VIEW, [Micheline.String (loc, view); output_ty], []); Micheline.Prim ( loc, Script.I_NIL, [Micheline.Prim (loc, Script.T_operation, [], [])], [] ); Micheline.Prim (loc, Script.I_PAIR, [], []); ] ) in let code = Micheline.strip_locations @@ Micheline.Seq ( loc, [ Micheline.Prim ( loc, Script.K_parameter, [Micheline.Prim (loc, Script.T_unit, [], [])], [] ); Micheline.Prim (loc, Script.K_storage, [storage_decl], []); Micheline.Prim (loc, Script.K_code, [body], []); ] ) in let storage = Micheline.strip_locations (Micheline.Prim (loc, Script.D_None, [], [])) in {code = Script.lazy_expr code; storage = Script.lazy_expr storage} (* Extracts the value from the mock script generated by [make_michelson_viewer_script]. *) let extract_value_from_storage (storage : Script.expr) = match Micheline.root storage with | Micheline.Prim (_, Script.D_Some, [value], []) -> Ok value | _ -> Environment.Error_monad.error @@ Viewer_unexpected_storage
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>