Source file tez_repr.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
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
let id = "tez"
let name = "mutez"
open Compare.Int64
type repr = t
type t = Tez_tag of repr [@@ocaml.unboxed]
let wrap t = Tez_tag t [@@ocaml.inline always]
type error +=
| Addition_overflow of t * t
| Subtraction_underflow of t * t
| Multiplication_overflow of t * Z.t
| Negative_multiplicator of t * Z.t
| Invalid_divisor of t * Z.t
let zero = Tez_tag 0L
let one_mutez = Tez_tag 1L
let max_mutez = Tez_tag Int64.max_int
let mul_int (Tez_tag tez) i = Tez_tag (Int64.mul tez i)
let one_cent = mul_int one_mutez 10_000L
let fifty_cents = mul_int one_cent 50L
let one = mul_int one_cent 100L
let of_string s =
let triplets = function
| hd :: tl ->
let len = String.length hd in
Compare.Int.(
len <= 3 && len > 0 && List.for_all (fun s -> String.length s = 3) tl)
| [] -> false
in
let integers s = triplets (String.split_on_char ',' s) in
let decimals s =
let l = String.split_on_char ',' s in
if Compare.List_length_with.(l > 2) then false else triplets (List.rev l)
in
let parse left right =
let remove_commas s = String.concat "" (String.split_on_char ',' s) in
let pad_to_six s =
let len = String.length s in
String.init 6 (fun i -> if Compare.Int.(i < len) then s.[i] else '0')
in
let prepared = remove_commas left ^ pad_to_six (remove_commas right) in
Option.map wrap (Int64.of_string_opt prepared)
in
match String.split_on_char '.' s with
| [left; right] ->
if String.contains s ',' then
if integers left && decimals right then parse left right else None
else if
Compare.Int.(String.length right > 0)
&& Compare.Int.(String.length right <= 6)
then parse left right
else None
| [left] ->
if (not (String.contains s ',')) || integers left then parse left ""
else None
| _ -> None
let pp ppf (Tez_tag amount) =
let mult_int = 1_000_000L in
let rec left ppf amount =
let d, r = (Int64.div amount 1000L, Int64.rem amount 1000L) in
if Compare.Int64.(d > 0L) then Format.fprintf ppf "%a%03Ld" left d r
else Format.fprintf ppf "%Ld" r
in
let right ppf amount =
let triplet ppf v =
if Compare.Int.(v mod 10 > 0) then Format.fprintf ppf "%03d" v
else if Compare.Int.(v mod 100 > 0) then Format.fprintf ppf "%02d" (v / 10)
else Format.fprintf ppf "%d" (v / 100)
in
let hi, lo = (amount / 1000, amount mod 1000) in
if Compare.Int.(lo = 0) then Format.fprintf ppf "%a" triplet hi
else Format.fprintf ppf "%03d%a" hi triplet lo
in
let ints, decs =
(Int64.div amount mult_int, Int64.(to_int (rem amount mult_int)))
in
left ppf ints ;
if Compare.Int.(decs > 0) then Format.fprintf ppf ".%a" right decs
let to_string t = Format.asprintf "%a" pp t
let ( -? ) tez1 tez2 =
let open Result_syntax in
let (Tez_tag t1) = tez1 in
let (Tez_tag t2) = tez2 in
if t2 <= t1 then return (Tez_tag (Int64.sub t1 t2))
else tzfail (Subtraction_underflow (tez1, tez2))
let sub_opt (Tez_tag t1) (Tez_tag t2) =
if t2 <= t1 then Some (Tez_tag (Int64.sub t1 t2)) else None
let ( +? ) tez1 tez2 =
let open Result_syntax in
let (Tez_tag t1) = tez1 in
let (Tez_tag t2) = tez2 in
let t = Int64.add t1 t2 in
if t < t1 then tzfail (Addition_overflow (tez1, tez2)) else return (Tez_tag t)
let ( *? ) tez m =
let open Result_syntax in
let (Tez_tag t) = tez in
if m < 0L then tzfail (Negative_multiplicator (tez, Z.of_int64 m))
else if m = 0L then return (Tez_tag 0L)
else if t > Int64.(div max_int m) then
tzfail (Multiplication_overflow (tez, Z.of_int64 m))
else return (Tez_tag (Int64.mul t m))
let ( /? ) tez d =
let open Result_syntax in
let (Tez_tag t) = tez in
if d <= 0L then tzfail (Invalid_divisor (tez, Z.of_int64 d))
else return (Tez_tag (Int64.div t d))
let div2 (Tez_tag t) = Tez_tag (Int64.div t 2L)
let mul_exn t m =
match t *? Int64.of_int m with Ok v -> v | Error _ -> invalid_arg "mul_exn"
let div_exn t d =
match t /? Int64.of_int d with Ok v -> v | Error _ -> invalid_arg "div_exn"
let mul_ratio_z ~rounding tez ~num ~den =
let open Result_syntax in
let (Tez_tag t) = tez in
if Z.(lt num zero) then tzfail (Negative_multiplicator (tez, num))
else if Z.(leq den zero) then tzfail (Invalid_divisor (tez, den))
else
let numerator = Z.(mul (of_int64 t) num) in
let z =
match rounding with
| `Down -> Z.div numerator den
| `Up -> Z.cdiv numerator den
in
if Z.fits_int64 z then return (Tez_tag (Z.to_int64 z))
else tzfail (Multiplication_overflow (tez, num))
let mul_ratio ~rounding tez ~num ~den =
mul_ratio_z ~rounding tez ~num:(Z.of_int64 num) ~den:(Z.of_int64 den)
let mul_q ~rounding tez {Q.num; den} = mul_ratio_z ~rounding tez ~num ~den
let mul_percentage ~rounding (Tez_tag t) (percentage : Percentage.t) =
let {Q.num; den} = Percentage.to_q percentage in
let div' = match rounding with `Down -> Z.div | `Up -> Z.cdiv in
Tez_tag Z.(to_int64 (div' (mul (of_int64 t) num) den))
let of_mutez t = if t < 0L then None else Some (Tez_tag t)
let of_mutez_exn x =
match of_mutez x with None -> invalid_arg "Tez.of_mutez" | Some v -> v
let to_mutez (Tez_tag t) = t
let encoding =
let open Data_encoding in
let decode (Tez_tag t) = Z.of_int64 t in
let encode = Json.wrap_error (fun i -> Tez_tag (Z.to_int64 i)) in
Data_encoding.def name (check_size 10 (conv decode encode n))
let balance_update_encoding =
let open Data_encoding in
conv
(function
| `Credited v -> to_mutez v | `Debited v -> Int64.neg (to_mutez v))
( Json.wrap_error @@ fun v ->
if Compare.Int64.(v < 0L) then `Debited (Tez_tag (Int64.neg v))
else `Credited (Tez_tag v) )
int64
let () =
let open Data_encoding in
register_error_kind
`Temporary
~id:(id ^ ".addition_overflow")
~title:("Overflowing " ^ id ^ " addition")
~pp:(fun ppf (opa, opb) ->
Format.fprintf
ppf
"Overflowing addition of %a %s and %a %s"
pp
opa
id
pp
opb
id)
~description:("An addition of two " ^ id ^ " amounts overflowed")
(obj1 (req "amounts" (tup2 encoding encoding)))
(function Addition_overflow (a, b) -> Some (a, b) | _ -> None)
(fun (a, b) -> Addition_overflow (a, b)) ;
register_error_kind
`Temporary
~id:(id ^ ".subtraction_underflow")
~title:("Underflowing " ^ id ^ " subtraction")
~pp:(fun ppf (opa, opb) ->
Format.fprintf
ppf
"Underflowing subtraction of %a %s and %a %s"
pp
opa
id
pp
opb
id)
~description:
("A subtraction of two " ^ id
^ " amounts underflowed (i.e., would have led to a negative amount)")
(obj1 (req "amounts" (tup2 encoding encoding)))
(function Subtraction_underflow (a, b) -> Some (a, b) | _ -> None)
(fun (a, b) -> Subtraction_underflow (a, b)) ;
register_error_kind
`Temporary
~id:(id ^ ".multiplication_overflow")
~title:("Overflowing " ^ id ^ " multiplication")
~pp:(fun ppf (opa, opb) ->
Format.fprintf
ppf
"Overflowing multiplication of %a %s and %a"
pp
opa
id
Z.pp_print
opb)
~description:
("A multiplication of a " ^ id ^ " amount by an integer overflowed")
(obj2 (req "amount" encoding) (req "multiplicator" z))
(function Multiplication_overflow (a, b) -> Some (a, b) | _ -> None)
(fun (a, b) -> Multiplication_overflow (a, b)) ;
register_error_kind
`Temporary
~id:(id ^ ".negative_multiplicator")
~title:("Negative " ^ id ^ " multiplicator")
~pp:(fun ppf (opa, opb) ->
Format.fprintf
ppf
"Multiplication of %a %s by negative integer %a"
pp
opa
id
Z.pp_print
opb)
~description:("Multiplication of a " ^ id ^ " amount by a negative integer")
(obj2 (req "amount" encoding) (req "multiplicator" z))
(function Negative_multiplicator (a, b) -> Some (a, b) | _ -> None)
(fun (a, b) -> Negative_multiplicator (a, b)) ;
register_error_kind
`Temporary
~id:(id ^ ".invalid_divisor")
~title:("Invalid " ^ id ^ " divisor")
~pp:(fun ppf (opa, opb) ->
Format.fprintf
ppf
"Division of %a %s by non positive integer %a"
pp
opa
id
Z.pp_print
opb)
~description:
("Multiplication of a " ^ id ^ " amount by a non positive integer")
(obj2 (req "amount" encoding) (req "divisor" z))
(function Invalid_divisor (a, b) -> Some (a, b) | _ -> None)
(fun (a, b) -> Invalid_divisor (a, b))
let compare (Tez_tag x) (Tez_tag y) = compare x y
let ( = ) (Tez_tag x) (Tez_tag y) = x = y
let ( <> ) (Tez_tag x) (Tez_tag y) = x <> y
let ( < ) (Tez_tag x) (Tez_tag y) = x < y
let ( > ) (Tez_tag x) (Tez_tag y) = x > y
let ( <= ) (Tez_tag x) (Tez_tag y) = x <= y
let ( >= ) (Tez_tag x) (Tez_tag y) = x >= y
let equal (Tez_tag x) (Tez_tag y) = equal x y
let max (Tez_tag x) (Tez_tag y) = Tez_tag (max x y)
let min (Tez_tag x) (Tez_tag y) = Tez_tag (min x y)