123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2020 Metastate AG <hello@metastate.dev> *)(* *)(* 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. *)(* *)(*****************************************************************************)typebalance=|ContractofContract_repr.t|Block_fees|DepositsofSignature.Public_key_hash.t|Nonce_revelation_rewards|Double_signing_evidence_rewards|Endorsing_rewards|Baking_rewards|Baking_bonuses|Storage_fees|Double_signing_punishments|Lost_endorsing_rewardsofSignature.Public_key_hash.t*bool*bool|Liquidity_baking_subsidies|Burned|CommitmentsofBlinded_public_key_hash.t|Bootstrap|Invoice|Initial_commitments|Minted|Frozen_bondsofContract_repr.t*Bond_id_repr.t|Tx_rollup_rejection_punishments|Tx_rollup_rejection_rewards|Sc_rollup_refutation_punishments|Sc_rollup_refutation_rewardsletbalance_encoding=letopenData_encodingindef"operation_metadata.alpha.balance"@@union[case(Tag0)~title:"Contract"(obj2(req"kind"(constant"contract"))(req"contract"Contract_repr.encoding))(functionContractc->Some((),c)|_->None)(fun((),c)->Contractc);case(Tag2)~title:"Block_fees"(obj2(req"kind"(constant"accumulator"))(req"category"(constant"block fees")))(functionBlock_fees->Some((),())|_->None)(fun((),())->Block_fees);case(Tag4)~title:"Deposits"(obj3(req"kind"(constant"freezer"))(req"category"(constant"deposits"))(req"delegate"Signature.Public_key_hash.encoding))(functionDepositsd->Some((),(),d)|_->None)(fun((),(),d)->Depositsd);case(Tag5)~title:"Nonce_revelation_rewards"(obj2(req"kind"(constant"minted"))(req"category"(constant"nonce revelation rewards")))(functionNonce_revelation_rewards->Some((),())|_->None)(fun((),())->Nonce_revelation_rewards);case(Tag6)~title:"Double_signing_evidence_rewards"(obj2(req"kind"(constant"minted"))(req"category"(constant"double signing evidence rewards")))(function|Double_signing_evidence_rewards->Some((),())|_->None)(fun((),())->Double_signing_evidence_rewards);case(Tag7)~title:"Endorsing_rewards"(obj2(req"kind"(constant"minted"))(req"category"(constant"endorsing rewards")))(functionEndorsing_rewards->Some((),())|_->None)(fun((),())->Endorsing_rewards);case(Tag8)~title:"Baking_rewards"(obj2(req"kind"(constant"minted"))(req"category"(constant"baking rewards")))(functionBaking_rewards->Some((),())|_->None)(fun((),())->Baking_rewards);case(Tag9)~title:"Baking_bonuses"(obj2(req"kind"(constant"minted"))(req"category"(constant"baking bonuses")))(functionBaking_bonuses->Some((),())|_->None)(fun((),())->Baking_bonuses);case(Tag11)~title:"Storage_fees"(obj2(req"kind"(constant"burned"))(req"category"(constant"storage fees")))(functionStorage_fees->Some((),())|_->None)(fun((),())->Storage_fees);case(Tag12)~title:"Double_signing_punishments"(obj2(req"kind"(constant"burned"))(req"category"(constant"punishments")))(functionDouble_signing_punishments->Some((),())|_->None)(fun((),())->Double_signing_punishments);case(Tag13)~title:"Lost_endorsing_rewards"(obj5(req"kind"(constant"burned"))(req"category"(constant"lost endorsing rewards"))(req"delegate"Signature.Public_key_hash.encoding)(req"participation"Data_encoding.bool)(req"revelation"Data_encoding.bool))(function|Lost_endorsing_rewards(d,p,r)->Some((),(),d,p,r)|_->None)(fun((),(),d,p,r)->Lost_endorsing_rewards(d,p,r));case(Tag14)~title:"Liquidity_baking_subsidies"(obj2(req"kind"(constant"minted"))(req"category"(constant"subsidy")))(functionLiquidity_baking_subsidies->Some((),())|_->None)(fun((),())->Liquidity_baking_subsidies);case(Tag15)~title:"Burned"(obj2(req"kind"(constant"burned"))(req"category"(constant"burned")))(functionBurned->Some((),())|_->None)(fun((),())->Burned);case(Tag16)~title:"Commitments"(obj3(req"kind"(constant"commitment"))(req"category"(constant"commitment"))(req"committer"Blinded_public_key_hash.encoding))(functionCommitmentsbpkh->Some((),(),bpkh)|_->None)(fun((),(),bpkh)->Commitmentsbpkh);case(Tag17)~title:"Bootstrap"(obj2(req"kind"(constant"minted"))(req"category"(constant"bootstrap")))(functionBootstrap->Some((),())|_->None)(fun((),())->Bootstrap);case(Tag18)~title:"Invoice"(obj2(req"kind"(constant"minted"))(req"category"(constant"invoice")))(functionInvoice->Some((),())|_->None)(fun((),())->Invoice);case(Tag19)~title:"Initial_commitments"(obj2(req"kind"(constant"minted"))(req"category"(constant"commitment")))(functionInitial_commitments->Some((),())|_->None)(fun((),())->Initial_commitments);case(Tag20)~title:"Minted"(obj2(req"kind"(constant"minted"))(req"category"(constant"minted")))(functionMinted->Some((),())|_->None)(fun((),())->Minted);case(Tag21)~title:"Frozen_bonds"(obj4(req"kind"(constant"freezer"))(req"category"(constant"bonds"))(req"contract"Contract_repr.encoding)(req"bond_id"Bond_id_repr.encoding))(functionFrozen_bonds(c,r)->Some((),(),c,r)|_->None)(fun((),(),c,r)->Frozen_bonds(c,r));case(Tag22)~title:"Tx_rollup_rejection_rewards"(obj2(req"kind"(constant"minted"))(req"category"(constant"tx_rollup_rejection_rewards")))(functionTx_rollup_rejection_rewards->Some((),())|_->None)(fun((),())->Tx_rollup_rejection_rewards);case(Tag23)~title:"Tx_rollup_rejection_punishments"(obj2(req"kind"(constant"burned"))(req"category"(constant"tx_rollup_rejection_punishments")))(function|Tx_rollup_rejection_punishments->Some((),())|_->None)(fun((),())->Tx_rollup_rejection_punishments);case(Tag24)~title:"Smart_rollup_refutation_punishments"(obj2(req"kind"(constant"burned"))(req"category"(constant"smart_rollup_refutation_punishments")))(function|Sc_rollup_refutation_punishments->Some((),())|_->None)(fun((),())->Sc_rollup_refutation_punishments);case(Tag25)~title:"Smart_rollup_refutation_rewards"(obj2(req"kind"(constant"minted"))(req"category"(constant"smart_rollup_refutation_rewards")))(function|Sc_rollup_refutation_rewards->Some((),())|_->None)(fun((),())->Sc_rollup_refutation_rewards);]letis_not_zeroc=not(Compare.Int.equalc0)letcompare_balancebabb=match(ba,bb)with|Contractca,Contractcb->Contract_repr.comparecacb|Depositspkha,Depositspkhb->Signature.Public_key_hash.comparepkhapkhb|Lost_endorsing_rewards(pkha,pa,ra),Lost_endorsing_rewards(pkhb,pb,rb)->letc=Signature.Public_key_hash.comparepkhapkhbinifis_not_zerocthencelseletc=Compare.Bool.comparepapbinifis_not_zerocthencelseCompare.Bool.comparerarb|Commitmentsbpkha,Commitmentsbpkhb->Blinded_public_key_hash.comparebpkhabpkhb|Frozen_bonds(ca,ra),Frozen_bonds(cb,rb)->letc=Contract_repr.comparecacbinifis_not_zerocthencelseBond_id_repr.comparerarb|_,_->letindexb=matchbwith|Contract_->0|Block_fees->1|Deposits_->2|Nonce_revelation_rewards->3|Double_signing_evidence_rewards->4|Endorsing_rewards->5|Baking_rewards->6|Baking_bonuses->7|Storage_fees->8|Double_signing_punishments->9|Lost_endorsing_rewards_->10|Liquidity_baking_subsidies->11|Burned->12|Commitments_->13|Bootstrap->14|Invoice->15|Initial_commitments->16|Minted->17|Frozen_bonds_->18|Tx_rollup_rejection_punishments->19|Tx_rollup_rejection_rewards->20|Sc_rollup_refutation_punishments->21|Sc_rollup_refutation_rewards->22(* don't forget to add parameterized cases in the first part of the function *)inCompare.Int.compare(indexba)(indexbb)typebalance_update=DebitedofTez_repr.t|CreditedofTez_repr.tletis_zero_update=functionDebitedt|Creditedt->Tez_repr.(t=zero)letbalance_update_encoding=letopenData_encodingindef"operation_metadata.alpha.balance_update"@@obj1(req"change"(conv(function|Creditedv->Tez_repr.to_mutezv|Debitedv->Int64.neg(Tez_repr.to_mutezv))(Json.wrap_error@@funv->ifCompare.Int64.(v<0L)thenmatchTez_repr.of_mutez(Int64.negv)with|Somev->Debitedv|None->assertfalse(* [of_mutez z] is [None] iff [z < 0] *)elsematchTez_repr.of_mutezvwith|Somev->Creditedv|None->assertfalse(* same *))int64))typeupdate_origin=|Block_application|Protocol_migration|Subsidy|Simulationletcompare_update_originoaob=letindexo=matchowith|Block_application->0|Protocol_migration->1|Subsidy->2|Simulation->3inCompare.Int.compare(indexoa)(indexob)letupdate_origin_encoding=letopenData_encodingindef"operation_metadata.alpha.update_origin"@@obj1@@req"origin"@@union[case(Tag0)~title:"Block_application"(constant"block")(functionBlock_application->Some()|_->None)(fun()->Block_application);case(Tag1)~title:"Protocol_migration"(constant"migration")(functionProtocol_migration->Some()|_->None)(fun()->Protocol_migration);case(Tag2)~title:"Subsidy"(constant"subsidy")(functionSubsidy->Some()|_->None)(fun()->Subsidy);case(Tag3)~title:"Simulation"(constant"simulation")(functionSimulation->Some()|_->None)(fun()->Simulation);]typebalance_updates=(balance*balance_update*update_origin)listletbalance_updates_encoding=letopenData_encodingindef"operation_metadata.alpha.balance_updates"@@list(conv(function|balance,balance_update,update_origin->((balance,balance_update),update_origin))(fun((balance,balance_update),update_origin)->(balance,balance_update,update_origin))(merge_objs(merge_objsbalance_encodingbalance_update_encoding)update_origin_encoding))moduleBalanceMap=structincludeMap.Make(structtypet=balance*update_originletcompare(ba,ua)(bb,ub)=letc=compare_balancebabbinifis_not_zerocthencelsecompare_update_originuaubend)letupdate_rkey(f:'aoption->'boptiontzresult)map=f(findkeymap)>>?function|Somev->ok(addkeyvmap)|None->ok(removekeymap)endletgroup_balance_updatesbalance_updates=List.fold_left_e(funacc(b,update,o)->(* Do not do anything if the update is zero *)ifis_zero_updateupdatethenokaccelseBalanceMap.update_r(b,o)(function|None->ok(Someupdate)|Somebalance->(match(balance,update)with|Crediteda,Debitedb|Debitedb,Crediteda->(* Remove the binding since it just fell down to zero *)ifTez_repr.(a=b)thenokNoneelseifTez_repr.(a>b)thenTez_repr.(a-?b)>>?funupdate->ok(Some(Creditedupdate))elseTez_repr.(b-?a)>>?funupdate->ok(Some(Debitedupdate))|Crediteda,Creditedb->Tez_repr.(a+?b)>>?funupdate->ok(Some(Creditedupdate))|Debiteda,Debitedb->Tez_repr.(a+?b)>>?funupdate->ok(Some(Debitedupdate))))acc)BalanceMap.emptybalance_updates>>?funmap->ok(BalanceMap.fold(fun(b,o)uacc->(b,u,o)::acc)map[])