[docs]classLedger:"""A ledger to keep track of transactions that occur in the order management system."""def__init__(self):self.transactions:'List[Transaction]'=[]
[docs]defcommit(self,wallet:'Wallet',quantity:'Quantity',source:str,target:str,memo:str)->None:"""Commits a transaction to the ledger records. Parameters ---------- wallet : `Wallet` The wallet involved in this transaction. quantity : `Quantity` The amount being used. source : str The source of funds being transferred. target : str The destination the funds are being transferred to. memo : str A description of the transaction. """poid=quantity.path_idlocked_poid_balance=Noneifpoidnotinwallet.locked.keys()elsewallet.locked[poid]transaction=Transaction(poid,wallet.exchange.clock.step,source,target,memo,quantity,wallet.balance,wallet.locked_balance,locked_poid_balance)self.transactions+=[transaction]
[docs]defas_frame(self,sort_by_order_seq:bool=False)->'pd.DataFrame':"""Converts the ledger records into a data frame. Parameters ---------- sort_by_order_seq : bool, default False If records should be sorted by each order path. Returns ------- `pd.DataFrame` A data frame containing all the records in the ledger. """ifnotsort_by_order_seq:returnpd.DataFrame(self.transactions)df=pd.DataFrame(self.transactions)frames=[]forpoidindf.poid.unique():frames+=[df.loc[df.poid==poid,:]]returnpd.concat(frames,ignore_index=True,axis=0)
[docs]defreset(self):"""Resets the ledger."""self.transactions=[]