Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file tez_repr.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2020 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* 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. *)(* *)(*****************************************************************************)letid="tez"letname="mutez"includeCompare.Int64(* invariant: positive *)typeerror+=|Addition_overflowoft*t(* `Temporary *)|Subtraction_underflowoft*t(* `Temporary *)|Multiplication_overflowoft*int64(* `Temporary *)|Negative_multiplicatoroft*int64(* `Temporary *)|Invalid_divisoroft*int64(* `Temporary *)letzero=0L(* all other constant are defined from the value of one micro tez *)letone_mutez=1Lletone_cent=Int64.mulone_mutez10_000Lletfifty_cents=Int64.mulone_cent50L(* 1 tez = 100 cents = 1_000_000 mutez *)letone=Int64.mulone_cent100Lletof_strings=lettriplets=function|hd::tl->letlen=String.lengthhdinCompare.Int.(len<=3&&len>0&&List.for_all(funs->String.lengths=3)tl)|[]->falseinletintegerss=triplets(String.split_on_char','s)inletdecimalss=letl=String.split_on_char','sinifCompare.Int.(List.lengthl>2)thenfalseelsetriplets(List.revl)inletparseleftright=letremove_commass=String.concat""(String.split_on_char','s)inletpad_to_sixs=letlen=String.lengthsinString.init6(funi->ifCompare.Int.(i<len)thens.[i]else'0')inInt64.of_string_opt(remove_commasleft^pad_to_six(remove_commasright))inmatchString.split_on_char'.'swith|[left;right]->ifString.containss','thenifintegersleft&&decimalsrightthenparseleftrightelseNoneelseifCompare.Int.(String.lengthright>0)&&Compare.Int.(String.lengthright<=6)thenparseleftrightelseNone|[left]->if(not(String.containss','))||integersleftthenparseleft""elseNone|_->Noneletppppfamount=letmult_int=1_000_000Linletrecleftppfamount=let(d,r)=(Int64.(divamount1000L),Int64.(remamount1000L))inifd>0LthenFormat.fprintfppf"%a%03Ld"leftdrelseFormat.fprintfppf"%Ld"rinletrightppfamount=lettripletppfv=ifCompare.Int.(vmod10>0)thenFormat.fprintfppf"%03d"velseifCompare.Int.(vmod100>0)thenFormat.fprintfppf"%02d"(v/10)elseFormat.fprintfppf"%d"(v/100)inlet(hi,lo)=(amount/1000,amountmod1000)inifCompare.Int.(lo=0)thenFormat.fprintfppf"%a"triplethielseFormat.fprintfppf"%03d%a"hitripletloinlet(ints,decs)=(Int64.(divamountmult_int),Int64.(to_int(remamountmult_int)))inleftppfints;ifCompare.Int.(decs>0)thenFormat.fprintfppf".%a"rightdecsletto_stringt=Format.asprintf"%a"pptlet(-?)t1t2=ift2<=t1thenok(Int64.subt1t2)elseerror(Subtraction_underflow(t1,t2))let(+?)t1t2=lett=Int64.addt1t2inift<t1thenerror(Addition_overflow(t1,t2))elseoktlet(*?)tm=letopenCompare.Int64inletopenInt64inletrecstepcurpowacc=ifcur=0Lthenokaccelsepow+?pow>>?funnpow->iflogandcur1L=1Lthenacc+?pow>>?funnacc->step(shift_right_logicalcur1)npownaccelsestep(shift_right_logicalcur1)npowaccinifm<0Lthenerror(Negative_multiplicator(t,m))elsematchstepmt0Lwith|Okres->Okres|Error([Addition_overflow_]aserrs)->Error(Multiplication_overflow(t,m)::errs)|Errorerrs->Errorerrslet(/?)td=ifd<=0Lthenerror(Invalid_divisor(t,d))elseok(Int64.divtd)letmul_exntm=matcht*?Int64.(of_intm)with|Okv->v|Error_->invalid_arg"mul_exn"letof_mutezt=ift<0LthenNoneelseSometletof_mutez_exnx=matchof_mutezxwithNone->invalid_arg"Tez.of_mutez"|Somev->vletto_int64t=tletto_mutezt=tletencoding=letopenData_encodinginData_encoding.defname(check_size10(convZ.of_int64(Json.wrap_errorZ.to_int64)n))let()=letopenData_encodinginregister_error_kind`Temporary~id:(id^".addition_overflow")~title:("Overflowing "^id^" addition")~pp:(funppf(opa,opb)->Format.fprintfppf"Overflowing addition of %a %s and %a %s"ppopaidppopbid)~description:("An addition of two "^id^" amounts overflowed")(obj1(req"amounts"(tup2encodingencoding)))(functionAddition_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:(funppf(opa,opb)->Format.fprintfppf"Underflowing subtraction of %a %s and %a %s"ppopaidppopbid)~description:("An subtraction of two "^id^" amounts underflowed")(obj1(req"amounts"(tup2encodingencoding)))(functionSubtraction_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:(funppf(opa,opb)->Format.fprintfppf"Overflowing multiplication of %a %s and %Ld"ppopaidopb)~description:("A multiplication of a "^id^" amount by an integer overflowed")(obj2(req"amount"encoding)(req"multiplicator"int64))(functionMultiplication_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:(funppf(opa,opb)->Format.fprintfppf"Multiplication of %a %s by negative integer %Ld"ppopaidopb)~description:("Multiplication of a "^id^" amount by a negative integer")(obj2(req"amount"encoding)(req"multiplicator"int64))(functionNegative_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:(funppf(opa,opb)->Format.fprintfppf"Division of %a %s by non positive integer %Ld"ppopaidopb)~description:("Multiplication of a "^id^" amount by a non positive integer")(obj2(req"amount"encoding)(req"divisor"int64))(functionInvalid_divisor(a,b)->Some(a,b)|_->None)(fun(a,b)->Invalid_divisor(a,b))typetez=t