Source file managed_contract.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
open Protocol
open Alpha_context
open Protocol_client_context
open Tezos_micheline
open Client_proto_context
let get_contract_manager (cctxt : #full) contract =
let open Micheline in
let open Michelson_v1_primitives in
get_storage cctxt ~chain:cctxt#chain ~block:cctxt#block contract >>=? function
| None -> cctxt#error "This is not a smart contract."
| Some storage -> (
match root storage with
| Prim (_, D_Pair, [Bytes (_, bytes); _], _) | Bytes (_, bytes) -> (
match
Data_encoding.Binary.of_bytes_opt
Tezos_crypto.Signature.V0.Public_key_hash.encoding
bytes
with
| Some k -> return k
| None ->
cctxt#error
"Cannot find a manager key in contracts storage (decoding \
bytes failed).\n\
Transfer from scripted contract are currently only supported \
for \"manager\" contract.")
| Prim (_, D_Pair, [String (_, value); _], _) | String (_, value) -> (
match
Tezos_crypto.Signature.V0.Public_key_hash.of_b58check_opt value
with
| Some k -> return k
| None ->
cctxt#error
"Cannot find a manager key in contracts storage (\"%s\" is not \
a valid key).\n\
Transfer from scripted contract are currently only supported \
for \"manager\" contract."
value)
| _raw_storage ->
cctxt#error
"Cannot find a manager key in contracts storage (wrong storage \
format : @[%a@]).\n\
Transfer from scripted contract are currently only supported for \
\"manager\" contract."
Michelson_v1_printer.print_expr
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 set_delegate (cctxt : #full) ~chain ~block ?confirmations ?dry_run
?verbose_signing ?branch ~fee_parameter ?fee ~source ~src_pk ~src_sk
contract
(delegate : Tezos_crypto.Signature.V0.public_key_hash option) =
let entrypoint = "do" in
(Michelson_v1_entrypoints.contract_entrypoint_type
cctxt
~chain
~block
~contract
~entrypoint
>>=? function
| Some _ ->
let lambda =
match delegate with
| Some delegate ->
let (`Hex delegate) =
Tezos_crypto.Signature.V0.Public_key_hash.to_hex delegate
in
Format.asprintf
"{ DROP ; NIL operation ; PUSH key_hash 0x%s ; SOME ; \
SET_DELEGATE ; CONS }"
delegate
| None ->
"{ DROP ; NIL operation ; NONE key_hash ; SET_DELEGATE ; CONS }"
in
parse lambda >>=? fun param -> return (param, entrypoint)
| None -> (
let entrypoint =
match delegate with
| Some _ -> "set_delegate"
| None -> "remove_delegate"
in
Michelson_v1_entrypoints.contract_entrypoint_type
cctxt
~chain
~block
~contract
~entrypoint
>>=? function
| Some _ ->
let delegate_data =
match delegate with
| Some delegate ->
let (`Hex delegate) =
Tezos_crypto.Signature.V0.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 operation =
Transaction
{amount = Tez.zero; parameters; entrypoint; destination = contract}
in
Injection.inject_manager_operation
cctxt
~chain
~block
?confirmations
?dry_run
?verbose_signing
?branch
~source
?fee
~storage_limit:Z.zero
~src_pk
~src_sk
~fee_parameter
operation
>>=? fun res -> return res
let d_unit =
Micheline.strip_locations (Prim (0, Michelson_v1_primitives.D_Unit, [], []))
let t_unit =
Micheline.strip_locations (Prim (0, Michelson_v1_primitives.T_unit, [], []))
let build_lambda_for_implicit ~delegate ~amount =
let (`Hex delegate) =
Tezos_crypto.Signature.V0.Public_key_hash.to_hex delegate
in
Format.asprintf
"{ DROP ; NIL operation ;PUSH key_hash 0x%s; IMPLICIT_ACCOUNT;PUSH mutez \
%Ld ;UNIT;TRANSFER_TOKENS ; CONS }"
delegate
(Tez.to_mutez amount)
let build_lambda_for_originated ~destination ~entrypoint ~amount ~parameter_type
~parameter =
let destination =
Data_encoding.Binary.to_bytes_exn Contract.encoding destination
in
let amount = Tez.to_mutez amount in
let (`Hex destination) = Hex.of_bytes destination in
let entrypoint = match entrypoint with "default" -> "" | s -> "%" ^ s in
if parameter_type = t_unit then
Format.asprintf
"{ DROP ; NIL operation ;PUSH address 0x%s; CONTRACT %s %a; \
ASSERT_SOME;PUSH mutez %Ld ;UNIT;TRANSFER_TOKENS ; CONS }"
destination
entrypoint
Michelson_v1_printer.print_expr
parameter_type
amount
else
Format.asprintf
"{ DROP ; NIL operation ;PUSH address 0x%s; CONTRACT %s %a; \
ASSERT_SOME;PUSH mutez %Ld ;PUSH %a %a;TRANSFER_TOKENS ; CONS }"
destination
entrypoint
Michelson_v1_printer.print_expr
parameter_type
amount
Michelson_v1_printer.print_expr
parameter_type
Michelson_v1_printer.print_expr
parameter
let transfer (cctxt : #full) ~chain ~block ?confirmations ?dry_run
?verbose_signing ?branch ~source ~src_pk ~src_sk ~contract ~destination
?(entrypoint = "default") ?arg ~amount ?fee ?gas_limit ?storage_limit
?counter ~fee_parameter () :
(Kind.transaction Kind.manager Injection.result * Contract.t list) tzresult
Lwt.t =
(match Alpha_context.Contract.is_implicit destination with
| Some delegate when entrypoint = "default" ->
return @@ build_lambda_for_implicit ~delegate ~amount
| Some _ ->
cctxt#error
"Implicit accounts have no entrypoints. (targeted entrypoint %%%s on \
contract %a)"
entrypoint
Contract.pp
destination
| None ->
(Michelson_v1_entrypoints.contract_entrypoint_type
cctxt
~chain
~block
~contract:destination
~entrypoint
>>=? function
| None ->
cctxt#error
"Contract %a has no entrypoint named %s"
Contract.pp
destination
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_originated
~destination
~entrypoint
~amount
~parameter_type
~parameter)
>>=? fun lambda ->
parse lambda >>=? fun parameters ->
let entrypoint = "do" in
let operation =
Transaction
{amount = Tez.zero; parameters; entrypoint; destination = contract}
in
Injection.inject_manager_operation
cctxt
~chain
~block
?confirmations
?dry_run
?verbose_signing
?branch
~source
?fee
?gas_limit
?storage_limit
?counter
~src_pk
~src_sk
~fee_parameter
operation
>>=? fun ((_oph, _op, result) as res) ->
Lwt.return (Injection.originated_contracts (Single_result result))
>>=? fun contracts -> return (res, contracts)