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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
open Protocol
open Alpha_context
open Expr_common
module Storage = struct
type t = {
tokenPool : Z.t;
xtzPool : Tez.t;
lqtTotal : Z.t;
tokenAddress : Contract_hash.t;
lqtAddress : Contract_hash.t;
}
let zero : t =
{
tokenPool = Z.zero;
xtzPool = Tez.zero;
lqtTotal = Z.zero;
tokenAddress = Contract_hash.zero;
lqtAddress = Contract_hash.zero;
}
let to_string {tokenPool; xtzPool; lqtTotal; tokenAddress; lqtAddress} =
Format.asprintf
"{tokenPool : %a; xtzPool : %s; lqtTotal : %a; tokenAddress : %s; \
lqtAddress : %s;}"
Z.pp_print
tokenPool
(Int64.to_string @@ Tez.to_mutez xtzPool)
Z.pp_print
lqtTotal
(Contract_hash.to_b58check tokenAddress)
(Contract_hash.to_b58check lqtAddress)
let pp fmt s = Format.fprintf fmt "%s" (to_string s)
let eq s s' = s = s'
let to_expr :
loc:'a ->
t ->
('a, Michelson_v1_primitives.prim) Tezos_micheline.Micheline.node =
fun ~loc {tokenPool; xtzPool; lqtTotal; tokenAddress; lqtAddress} ->
comb
~loc
[
int ~loc tokenPool;
mutez ~loc xtzPool;
int ~loc lqtTotal;
address_string ~loc (Contract.Originated tokenAddress);
address_string ~loc (Contract.Originated lqtAddress);
]
let to_michelson_string e =
let e = to_expr ~loc:0 e in
Format.asprintf
"%a"
Michelson_v1_printer.print_expr
(Micheline.strip_locations e)
type exn += Invalid_storage_expr of string
(** Note: parses a storage unparsed in readable mode (as
e.g. returned by [Alpha_services.Contract.storage]), so that
contracts are represented by strings. *)
let of_expr_exn :
('a, Michelson_v1_primitives.prim) Tezos_micheline.Micheline.node -> t =
function
| Tezos_micheline.Micheline.Prim
( _,
Script.D_Pair,
[
Tezos_micheline.Micheline.Int (_, tokenPool);
Tezos_micheline.Micheline.Int (_, xtzPool);
Tezos_micheline.Micheline.Int (_, lqtTotal);
Tezos_micheline.Micheline.String (_, tokenAddress);
Tezos_micheline.Micheline.String (_, lqtAddress);
],
[] ) ->
let xtzPool = Tez.of_mutez_exn (Z.to_int64 xtzPool) in
let tokenAddress = originated_of_string_exn tokenAddress in
let lqtAddress = originated_of_string_exn lqtAddress in
{tokenPool; xtzPool; lqtTotal; tokenAddress; lqtAddress}
| e ->
let canonical = Micheline.strip_locations e in
let msg =
Format.asprintf
"Not a valid CPMM storage: %s /// %a"
(try
Michelson_v1_printer.micheline_string_of_expression
~zero_loc:true
canonical
with Z.Overflow ->
"Cannot represent as micheline due to overflowing Z -> int")
Michelson_v1_printer.print_expr
canonical
in
raise (Invalid_storage_expr msg)
let get (ctxt : Context.t) ~(contract : Contract.t) : t tzresult Lwt.t =
match contract with
| Implicit _ ->
invalid_arg "Cpmm_repr.Storage.get called on implicit account"
| Originated c ->
Context.Contract.storage ctxt c >|=? Micheline.root >|=? of_expr_exn
let of_tuple (tokenPool, xtzPool, lqtTotal, tokenAddress, lqtAddress) =
{tokenPool; xtzPool; lqtTotal; tokenAddress; lqtAddress}
let to_tuple {tokenPool; xtzPool; lqtTotal; tokenAddress; lqtAddress} =
(tokenPool, xtzPool, lqtTotal, tokenAddress, lqtAddress)
let valid {tokenPool; xtzPool; lqtTotal; _} =
tokenPool > Z.zero && lqtTotal > Z.zero && Tez.(xtzPool > Tez.zero)
end
module Parameter = struct
type add_liquidity = {
owner : Contract.t;
minLqtMinted : Z.t;
maxTokensDeposited : Z.t;
deadline : Script_timestamp.t;
}
type remove_liquidity = {
to_ : Contract.t;
lqtBurned : Z.t;
minXtzWithdrawn : Tez.t;
minTokensWithdrawn : Z.t;
deadline : Script_timestamp.t;
}
type token_to_token = {
outputDexterContract : Contract.t;
minTokensBought : Z.t;
to_ : Contract.t;
tokensSold : Z.t;
deadline : Script_timestamp.t;
}
type token_to_xtz = {
to_ : Contract.t;
tokensSold : Z.t;
minXtzBought : Tez.t;
deadline : Script_timestamp.t;
}
type xtz_to_token = {
to_ : Contract.t;
minTokensBought : Z.t;
deadline : Script_timestamp.t;
}
type t =
| AddLiquidity of add_liquidity
| Default of unit
| RemoveLiquidity of remove_liquidity
| TokenToToken of token_to_token
| TokenToXtz of token_to_xtz
| XtzToToken of xtz_to_token
let addLiquidity p = AddLiquidity p
let default p = Default p
let removeLiquidity p = RemoveLiquidity p
let tokenToToken p = TokenToToken p
let tokenToXtz p = TokenToXtz p
let xtzToToken p = XtzToToken p
let add_liquidity_to_string : add_liquidity -> string =
fun {owner; minLqtMinted; maxTokensDeposited; deadline} ->
Format.asprintf
"{owner : %s; minLqtMinted : %a; maxTokensDeposited : %a; deadline : %s }"
(Contract.to_b58check owner)
Z.pp_print
minLqtMinted
Z.pp_print
maxTokensDeposited
(Script_timestamp.to_string deadline)
let remove_liquidity_to_string : remove_liquidity -> string =
fun {to_; lqtBurned; minXtzWithdrawn; minTokensWithdrawn; deadline} ->
Format.asprintf
"{owner : %s; lqtBurned : %a; minXtzWithdrawn : %s; minTokensWithdrawn : \
%a; deadline : %s }"
(Contract.to_b58check to_)
Z.pp_print
lqtBurned
(Int64.to_string @@ Tez.to_mutez minXtzWithdrawn)
Z.pp_print
minTokensWithdrawn
(Script_timestamp.to_string deadline)
let token_to_token_to_string : token_to_token -> string =
fun {outputDexterContract; minTokensBought; to_; tokensSold; deadline} ->
Format.asprintf
"{outputDexterContract : %s; minTokensBought : %a; to_ : %s; tokensSold \
: %a; deadline : %s }"
(Contract.to_b58check outputDexterContract)
Z.pp_print
minTokensBought
(Contract.to_b58check to_)
Z.pp_print
tokensSold
(Script_timestamp.to_string deadline)
let token_to_xtz_to_string : token_to_xtz -> string =
fun {to_; tokensSold; minXtzBought; deadline} ->
Format.asprintf
"{to_ : %s; tokensSold : %a; minXtzBought : %s; deadline : %s }"
(Contract.to_b58check to_)
Z.pp_print
tokensSold
(Int64.to_string @@ Tez.to_mutez minXtzBought)
(Script_timestamp.to_string deadline)
let xtz_to_token_to_string : xtz_to_token -> string =
fun {to_; minTokensBought; deadline} ->
Format.asprintf
"{to_ : %s; minTokensBought : %a; deadline : %s }"
(Contract.to_b58check to_)
Z.pp_print
minTokensBought
(Script_timestamp.to_string deadline)
let to_string : t -> string = function
| AddLiquidity p ->
Format.asprintf "AddLiquidity %s" (add_liquidity_to_string p)
| Default () -> "Default ()"
| RemoveLiquidity p ->
Format.asprintf "RemoveLiquidity %s" (remove_liquidity_to_string p)
| TokenToToken p ->
Format.asprintf "TokenToToken (%s)" (token_to_token_to_string p)
| TokenToXtz p ->
Format.asprintf "TokenToXtz (%s)" (token_to_xtz_to_string p)
| XtzToToken p ->
Format.asprintf "XtzToToken (%s)" (xtz_to_token_to_string p)
let entrypoint_of_parameter : t -> Entrypoint.t = function
| AddLiquidity _ -> Entrypoint.of_string_strict_exn "addLiquidity"
| Default _ -> Entrypoint.default
| RemoveLiquidity _ -> Entrypoint.of_string_strict_exn "removeLiquidity"
| TokenToToken _ -> Entrypoint.of_string_strict_exn "tokenToToken"
| TokenToXtz _ -> Entrypoint.of_string_strict_exn "tokenToXtz"
| XtzToToken _ -> Entrypoint.of_string_strict_exn "xtzToToken"
let pp fmt s = Format.fprintf fmt "%s" (to_string s)
let eq s s' = s = s'
let to_expr_rooted :
loc:'a ->
t ->
('a, Michelson_v1_primitives.prim) Tezos_micheline.Micheline.node =
fun ~loc -> function
| AddLiquidity {owner; minLqtMinted; maxTokensDeposited; deadline} ->
comb
~loc
[
address_string ~loc owner;
int ~loc minLqtMinted;
int ~loc maxTokensDeposited;
timestamp ~loc deadline;
]
| Default () -> unit ~loc ()
| RemoveLiquidity
{to_; lqtBurned; minXtzWithdrawn; minTokensWithdrawn; deadline} ->
comb
~loc
[
address_string ~loc to_;
int ~loc lqtBurned;
mutez ~loc minXtzWithdrawn;
int ~loc minTokensWithdrawn;
timestamp ~loc deadline;
]
| TokenToToken
{outputDexterContract; minTokensBought; to_; tokensSold; deadline} ->
comb
~loc
[
address_string ~loc outputDexterContract;
int ~loc minTokensBought;
address_string ~loc to_;
int ~loc tokensSold;
timestamp ~loc deadline;
]
| TokenToXtz {to_; tokensSold; minXtzBought; deadline} ->
comb
~loc
[
address_string ~loc to_;
int ~loc tokensSold;
mutez ~loc minXtzBought;
timestamp ~loc deadline;
]
| XtzToToken {to_; minTokensBought; deadline} ->
comb
~loc
[
address_string ~loc to_;
int ~loc minTokensBought;
timestamp ~loc deadline;
]
let to_expr :
loc:'a ->
t ->
('a, Michelson_v1_primitives.prim) Tezos_micheline.Micheline.node =
fun ~loc p ->
let rooted = to_expr_rooted ~loc p in
match p with
| AddLiquidity _ -> left ~loc @@ left ~loc @@ left ~loc rooted
| Default () -> left ~loc @@ left ~loc @@ right ~loc rooted
| RemoveLiquidity _ -> left ~loc @@ right ~loc @@ left ~loc rooted
| TokenToToken _ -> left ~loc @@ right ~loc @@ right ~loc rooted
| TokenToXtz _ -> right ~loc @@ left ~loc rooted
| XtzToToken _ -> right ~loc @@ right ~loc rooted
let to_michelson_string e =
let e = to_expr ~loc:0 e in
Format.asprintf
"%a"
Michelson_v1_printer.print_expr
(Micheline.strip_locations e)
end
let transaction (ctxt : Context.t) ~(contract : Contract.t) ~(src : Contract.t)
?(amount = Tez.zero) (parameters : Parameter.t) =
let entrypoint = Parameter.entrypoint_of_parameter parameters in
let rooted_param_lazy =
parameters
|> Parameter.to_expr_rooted ~loc:0
|> Micheline.strip_locations |> Alpha_context.Script.lazy_expr
in
Op.transaction
ctxt
src
contract
amount
~entrypoint
~parameters:rooted_param_lazy