123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Tocqueville Group, Inc. <contact@tezos.com> *)(* Copyright (c) 2022 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. *)(* *)(*****************************************************************************)(** Options available for the Liquidity Baking per-block vote *)typeliquidity_baking_toggle_vote=LB_on|LB_off|LB_passletliquidity_baking_toggle_vote_encoding=letof_int8=function|0->OkLB_on|1->OkLB_off|2->OkLB_pass|_->Error"liquidity_baking_toggle_vote_of_int8"inletto_int8=functionLB_on->0|LB_off->1|LB_pass->2inletopenData_encodingin(* union *)def"liquidity_baking_toggle_vote"@@splitted~binary:(conv_with_guardto_int8of_int8int8)~json:(string_enum[("on",LB_on);("off",LB_off);("pass",LB_pass)])moduleToggle_EMA:sig(* The exponential moving average is represented as an Int32 between 0l and 2_000_000_000l *)typetvalzero:tvalof_int32:Int32.t->ttzresultLwt.tvalto_int32:t->Int32.tvalupdate_ema_off:t->tvalupdate_ema_on:t->tval(<):t->Int32.t->boolvalencoding:tData_encoding.tend=structtypet=Int32.t(* Invariant 0 <= ema <= 2_000_000_000l *)(* This error is not registered because we don't expect it to be
raised. *)typeerror+=Liquidity_baking_toggle_ema_out_of_boundofInt32.tletcheck_boundsx=Compare.Int32.(0l<=x&&x<=2_000_000_000l)letof_int32x=ifcheck_boundsxthenreturnxelsetzfail@@Liquidity_baking_toggle_ema_out_of_boundxletzero=Int32.zero(* The conv_with_guard combinator of Data_encoding expects a (_, string) result. *)letof_int32_for_encodingx=ifcheck_boundsxthenOkxelseError"out of bounds"letto_int32ema=ema(* We perform the computations in Z to avoid overflows. *)letz_1999=Z.of_int1999letz_2000=Z.of_int2000letattenuatez=Z.(div(mulz_1999z)z_2000)letz_1_000_000_000=Z.of_int1_000_000_000(* Outside of this module, the EMA is always between 0 and 2,000,000,000.
This [recenter] wrappers, puts it in between -1,000,000,000 and 1,000,000,000.
The goal of this recentering around zero is to make [update_ema_off] and
[update_ema_on] behave symmetrically with respect to rounding. *)letrecenterfema=Z.(addz_1_000_000_000(f(subemaz_1_000_000_000)))letz_500_000=Z.of_int500_000letupdate_ema_offema=letema=Z.of_int32emainrecenter(funema->Z.add(attenuateema)z_500_000)ema|>Z.to_int32letupdate_ema_onema=letema=Z.of_int32emainrecenter(funema->Z.sub(attenuateema)z_500_000)ema|>Z.to_int32let(<)=Compare.Int32.(<)letencoding=Data_encoding.(conv_with_guardto_int32of_int32_for_encodingint32)end(* Invariant: 0 <= ema <= 2_000_000 *)letcompute_new_ema~toggle_voteema=matchtoggle_votewith|LB_pass->ema|LB_off->Toggle_EMA.update_ema_offema|LB_on->Toggle_EMA.update_ema_onema