diff options
| -rw-r--r-- | ledger_process.py | 57 | 
1 files changed, 23 insertions, 34 deletions
diff --git a/ledger_process.py b/ledger_process.py index 08aea10..99e48ba 100644 --- a/ledger_process.py +++ b/ledger_process.py @@ -18,37 +18,35 @@ class LedgerProcess:          self.external_wallet: Dict[str, FIFOQueue] = {}      def process_ledger(self, read_actions: List[LedgerAction]): +        for currency in {ac.asset for ac in read_actions}: +            self.external_wallet.setdefault(currency, FIFOQueue()) +            self.fifo_queues.setdefault(currency, FIFOQueue()) + +        # --- Trades          # don't make any assumtptions about ledger sorting          # groupby requires sorted inputs -        actions_sorted = sorted(read_actions, key=lambda a: (a.refid, a.timestamp)) -        grouped_actions = groupby(actions_sorted, lambda a: a.refid) - -        process_after: List[LedgerAction] = [] - +        trades_sorted = sorted( +            (ac for ac in read_actions if ac.type == "trade"), +            key=lambda a: (a.refid, a.timestamp), +        ) +        grouped_actions = groupby(trades_sorted, lambda a: a.refid)          for refid, actions in grouped_actions:              actions = list(actions) - -            if len(actions) == 0: -                logger.error("actions is empty") -                continue -            action = actions[0] - -            # Group trades by refid -            if action.type == "trade": -                self._process_trade(refid, actions) - -            elif action.asset == "EUR": -                continue - -            elif action.type == "deposit" or action.type == "withdrawal": -                assert len(actions) == 1 -                process_after.append(action) - -        for action in sorted(process_after, key=lambda a: a.timestamp): -            if action.type == "deposit" and action.asset != "EUR": +            self._process_trade(refid, actions) + +        # --- Deposit/Withdrawal +        for action in sorted( +            ( +                ac +                for ac in read_actions +                if ac.type in ["deposit", "withdrawal"] and ac.asset != "EUR" +            ), +            key=lambda a: a.timestamp, +        ): +            if action.type == "deposit":                  self._process_deposit(action) -            elif action.type == "withdrawal" and action.asset != "EUR": +            elif action.type == "withdrawal":                  self._process_withdrawal(action)      def _process_deposit(self, action: LedgerAction): @@ -56,8 +54,6 @@ class LedgerProcess:          assert action.fee >= 0          currency = action.asset -        self.external_wallet.setdefault(currency, FIFOQueue()) -        self.fifo_queues.setdefault(currency, FIFOQueue())          t = self.external_wallet[currency].remove(              lambda t: t.amount == action.amount and t.timestamp < action.timestamp @@ -68,17 +64,11 @@ class LedgerProcess:          t.remove_coins(action.fee)          wallet.add_trade(t) -        # TODO: Sort - -        logger.error("Don't know how do handle deposits yet.") -      def _process_withdrawal(self, action: LedgerAction):          assert action.amount < 0          assert action.fee >= 0          currency = action.asset -        self.external_wallet.setdefault(currency, FIFOQueue()) -        self.fifo_queues.setdefault(currency, FIFOQueue())          withdraw_total = -action.amount + action.fee          t = self.fifo_queues[currency].remove( @@ -100,7 +90,6 @@ class LedgerProcess:                  eur_fee = Decimal(eur_trade.fee)                  crypto_amount = Decimal(crypto_trade.amount)                  crypto_fee = Decimal(crypto_trade.fee) -                self.fifo_queues.setdefault(crypto_asset, FIFOQueue())                  date_sold = eur_trade.date  | 
