class Ledger: def __init__(self) -> None: self._balances: dict[str, int] = {} self._applied: set[str] = set() def balance(self, account: str) -> int: return self._balances.get(account, 0) def apply(self, operation_id: str, account: str, delta: int) -> int: if operation_id in self._applied: return self.balance(account) self._applied.add(operation_id) updated = self.balance(account) + delta if updated < 0: raise ValueError("insufficient funds") self._balances[account] = updated return updated