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 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)
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 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))
let 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 = _;
};
source = _;
nonce = _;
};
]
when Contract_hash.equal destination callback ->
ok unparsed_parameters
| [] ->
Environment.Error_monad.error (View_never_returns (entrypoint, callback))
| _ -> unexpected_return
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}
let (storage : Script.expr) =
match Micheline.root storage with
| Micheline.Prim (_, Script.D_Some, [value], []) -> ok value
| _ -> Environment.Error_monad.error @@ Viewer_unexpected_storage