123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 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. *)(* *)(*****************************************************************************)openProtocolopenAlpha_context(** This is a simulation of the CPMM contract, as implemented in mligo
in [src/proto_alpha/lib_protocol/contracts/cpmm.mligo]. The
interested reader should look for comments in this file to gain a
better understanding of the contract logic. *)moduleSimulate_raw=structletmutez_to_naturalt=Z.of_int64(Tez.to_mutezt)letnatural_to_mutezn=Tez.of_mutez_exn(Z.to_int64n)letaddLiquidity~tokenPool~xtzPool~lqtTotal~amount=letxtzPool=mutez_to_naturalxtzPoolinletnat_amount=mutez_to_naturalamountinletlqt_minted=Z.(nat_amount*lqtTotal/xtzPool)inlettokens_deposited=Z.(cdiv(nat_amount*tokenPool)xtzPool)in(lqt_minted,tokens_deposited)letremoveLiquidity~tokenPool~xtzPool~lqtTotal~lqtBurned=letxtz_withdrawn=natural_to_mutezZ.(lqtBurned*mutez_to_naturalxtzPool/lqtTotal)inlettokens_withdrawn=Z.(lqtBurned*tokenPool/lqtTotal)in(xtz_withdrawn,tokens_withdrawn)lettokenToXtz~tokenPool~xtzPool~tokensSold=letfee=Z.of_int999inletxtz_bought_nat=Z.(tokensSold*fee*mutez_to_naturalxtzPool/((tokenPool*of_int1000)+(tokensSold*fee)))inletbought=Z.(xtz_bought_nat*of_int999/of_int1000)in(natural_to_mutezbought,xtz_bought_nat)letxtzToToken~tokenPool~xtzPool~amount=letfee=Z.of_int999inletxtzPool=mutez_to_naturalxtzPoolinletnat_amount=mutez_to_naturalamountinletamount_net_burn=Z.(nat_amount*Z.of_int999/Z.of_int1000)inlettokens_bought=Z.(amount_net_burn*fee*tokenPool/((xtzPool*Z.of_int1000)+(amount_net_burn*fee)))in(tokens_bought,amount_net_burn)lettokenToToken~tokenPool~xtzPool~tokensSold=letfee=Z.of_int999inletxtz_bought_nat=Z.(tokensSold*fee*mutez_to_naturalxtzPool/((tokenPool*of_int1000)+(tokensSold*fee)))inletxtz_bought_net_burn=Z.(xtz_bought_nat*of_int999/of_int1000)in(natural_to_mutezxtz_bought_net_burn,xtz_bought_nat)endmoduleSimulate=structopenCpmm_repr.StorageletaddLiquidity{tokenPool;xtzPool;lqtTotal;_}amount=Simulate_raw.addLiquidity~xtzPool~tokenPool~lqtTotal~amountletremoveLiquidity{tokenPool;xtzPool;lqtTotal;_}lqtBurned=Simulate_raw.removeLiquidity~tokenPool~xtzPool~lqtTotal~lqtBurnedlettokenToXtz{tokenPool;xtzPool;_}tokensSold=Simulate_raw.tokenToXtz~tokenPool~xtzPool~tokensSoldletxtzToToken{tokenPool;xtzPool;_}amount=Simulate_raw.xtzToToken~tokenPool~xtzPool~amountlettokenToToken{tokenPool;xtzPool;_}tokensSold=Simulate_raw.tokenToToken~tokenPool~xtzPool~tokensSoldend