Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ticket_token_map.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Trili Tech, <contact@trili.tech> *)(* *)(* 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. *)(* *)(*****************************************************************************)openAlpha_context(** A carbonated map where the keys are [Ticket_hash.t] values. *)moduleTicket_token_map=Carbonated_map.Make(structtypecontext=Alpha_context.contextletconsume=Gas.consumeend)(structtypet=Ticket_hash.tletcompare=Ticket_hash.compareletcompare_cost_=Ticket_costs.Constants.cost_compare_ticket_hashend)(** Conceptually a map from [Ticket_token.ex_token] to values. Since
ticket-tokens are expensive to compare we use [Ticket_hash.t] keys instead,
and store the ticket-token along with the value. *)type'at=(Ticket_token.ex_token*'a)Ticket_token_map.tletempty=Ticket_token_map.emptyletkey_of_ticket_tokenctxt(Ticket_token.Ex_token{ticketer;_}astoken)=(* We use the [ticket_balance_key] function for generating a key-hash
for comparing tokens. Since an owner contract is required we use [ticketer]
but any dummy value would work as long as it's consistent.
*)Ticket_balance_key.of_ex_tokenctxt~owner:(Destination.Contractticketer)tokenletupdatectxtkeyfm=key_of_ticket_tokenctxtkey>>=?fun(key_hash,ctxt)->letfctxtval_opt=(matchval_optwith|Some(_tkn,value)->fctxt(Somevalue)|None->fctxtNone)>|?fun(val_opt,ctxt)->(Option.map(funv->(key,v))val_opt,ctxt)inTicket_token_map.updatectxtkey_hashfm|>Lwt.returnletfold_ectxtf=Ticket_token_map.fold_ectxt(functxtacc_key_hash(tkn,value)->fctxtacctknvalue)letfold_esctxtf=Ticket_token_map.fold_esctxt(functxtacc_key_hash(tkn,value)->fctxtacctknvalue)letfindctxtticket_tokenmap=key_of_ticket_tokenctxtticket_token>>=?fun(key_hash,ctxt)->Ticket_token_map.findctxtkey_hashmap>>?=fun(val_opt,ctxt)->return(Option.mapsndval_opt,ctxt)letlift_merge_overlapmerge_overlapctxt(tkn1,v1)(_tkn2,v2)=merge_overlapctxtv1v2>|?fun(v,ctxt)->((tkn1,v),ctxt)letof_listctxt~merge_overlaptoken_values=List.fold_left_es(fun(map,ctxt)(token,value)->key_of_ticket_tokenctxttoken>>=?fun(key_hash,ctxt)->Lwt.return(Ticket_token_map.updatectxtkey_hash(functxtold_val->matchold_valwith|None->ok(Some(token,value),ctxt)|Someold->lift_merge_overlapmerge_overlapctxtold(token,value)>|?fun(x,ctxt)->(Somex,ctxt))map))(Ticket_token_map.empty,ctxt)token_valuesletmap_ectxtf=Ticket_token_map.map_ectxt(functxt_key(tkn,value)->fctxttknvalue>|?fun(new_value,ctxt)->((tkn,new_value),ctxt))letto_listctxtmap=Ticket_token_map.to_listctxtmap>>?fun(list,ctxt)->(* Consume gas for traversing the list again and remove the key-hash. *)Gas.consumectxt(Carbonated_map_costs.fold_cost~size:(Ticket_token_map.sizemap))>|?functxt->(List.mapsndlist,ctxt)letmergectxt~merge_overlap=Ticket_token_map.mergectxt~merge_overlap:(lift_merge_overlapmerge_overlap)letto_ticket_receiptctxt~ownerticket_token_map=letopenLwt_result_syntaxinTicket_token_map.fold_esctxt(functxtacc_ticket_hash(ex_ticket,amount)->ifZ.(equalamountzero)thenreturn(acc,ctxt)elselet*ticket_token,ctxt=Ticket_token_unparser.unparsectxtex_ticketinletupdate=Ticket_receipt.{ticket_token;updates=[{account=owner;amount}]}inreturn(update::acc,ctxt))[]ticket_token_map