123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410(*****************************************************************************)(* *)(* 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|Legacy_rewardsofSignature.Public_key_hash.t*Cycle_repr.t|Block_fees|Legacy_depositsofSignature.Public_key_hash.t*Cycle_repr.t|DepositsofSignature.Public_key_hash.t|Nonce_revelation_rewards|Double_signing_evidence_rewards|Endorsing_rewards|Baking_rewards|Baking_bonuses|Legacy_feesofSignature.Public_key_hash.t*Cycle_repr.t|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|Mintedletbalance_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(Tag1)~title:"Legacy_rewards"(obj4(req"kind"(constant"freezer"))(req"category"(constant"legacy_rewards"))(req"delegate"Signature.Public_key_hash.encoding)(req"cycle"Cycle_repr.encoding))(functionLegacy_rewards(d,l)->Some((),(),d,l)|_->None)(fun((),(),d,l)->Legacy_rewards(d,l));case(Tag2)~title:"Block_fees"(obj2(req"kind"(constant"accumulator"))(req"category"(constant"block fees")))(functionBlock_fees->Some((),())|_->None)(fun((),())->Block_fees);case(Tag3)~title:"Legacy_deposits"(obj4(req"kind"(constant"freezer"))(req"category"(constant"legacy_deposits"))(req"delegate"Signature.Public_key_hash.encoding)(req"cycle"Cycle_repr.encoding))(function|Legacy_deposits(d,l)->Some((),(),d,l)|_->None)(fun((),(),d,l)->Legacy_deposits(d,l));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(Tag10)~title:"Legacy_fees"(obj4(req"kind"(constant"freezer"))(req"category"(constant"legacy_fees"))(req"delegate"Signature.Public_key_hash.encoding)(req"cycle"Cycle_repr.encoding))(functionLegacy_fees(d,l)->Some((),(),d,l)|_->None)(fun((),(),d,l)->Legacy_fees(d,l));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);]letis_not_zeroc=not(Compare.Int.equalc0)letcompare_balancebabb=match(ba,bb)with|(Contractca,Contractcb)->Contract_repr.comparecacb|(Legacy_rewards(pkha,ca),Legacy_rewards(pkhb,cb))->letc=Signature.Public_key_hash.comparepkhapkhbinifis_not_zerocthencelseCycle_repr.comparecacb|(Legacy_deposits(pkha,ca),Legacy_deposits(pkhb,cb))->letc=Signature.Public_key_hash.comparepkhapkhbinifis_not_zerocthencelseCycle_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|(Legacy_fees(pkha,ca),Legacy_fees(pkhb,cb))->letc=Signature.Public_key_hash.comparepkhapkhbinifis_not_zerocthencelseCycle_repr.comparecacb|(_,_)->letindexb=matchbwith|Contract_->0|Legacy_rewards_->1|Block_fees->2|Legacy_deposits_->3|Deposits_->4|Nonce_revelation_rewards->5|Double_signing_evidence_rewards->6|Endorsing_rewards->7|Baking_rewards->8|Baking_bonuses->9|Legacy_fees_->10|Storage_fees->11|Double_signing_punishments->12|Lost_endorsing_rewards_->13|Liquidity_baking_subsidies->14|Burned->15|Commitments_->16|Bootstrap->17|Invoice->18|Initial_commitments->19|Minted->20(* 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.tletbalance_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=Map.Make(structtypet=balance*update_originletcompare(ba,ua)(bb,ub)=letc=compare_balancebabbinifis_not_zerocthencelsecompare_update_originuaubend)letgroup_balance_updatesbalance_updates=List.fold_left_e(funacc(b,update,o)->(matchBalanceMap.find(b,o)accwith|None->okupdate|Somepresent->(match(present,update)with|(Crediteda,Debitedb)|(Debitedb,Crediteda)->ifTez_repr.(a>=b)thenTez_repr.(a-?b)>>?funupdate->ok(Creditedupdate)elseTez_repr.(b-?a)>>?funupdate->ok(Debitedupdate)|(Crediteda,Creditedb)->Tez_repr.(a+?b)>>?funupdate->ok(Creditedupdate)|(Debiteda,Debitedb)->Tez_repr.(a+?b)>>?funupdate->ok(Debitedupdate)))>>?function|CreditedupdatewhenTez_repr.(update=zero)->ok(BalanceMap.remove(b,o)acc)|update->ok(BalanceMap.add(b,o)updateacc))BalanceMap.emptybalance_updates>>?funmap->ok(BalanceMap.fold(fun(b,o)uacc->(b,u,o)::acc)map[])