2018-05-25 08:03:25 +02:00
|
|
|
from typing import List, Dict, Type
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-06-12 16:02:04 +02:00
|
|
|
from torba.baseledger import BaseLedger, LedgerRegistry
|
2018-05-25 08:03:25 +02:00
|
|
|
from torba.wallet import Wallet, WalletStorage
|
|
|
|
|
|
|
|
|
2018-06-11 15:33:32 +02:00
|
|
|
class WalletManager(object):
|
2018-05-25 08:03:25 +02:00
|
|
|
|
|
|
|
def __init__(self, wallets=None, ledgers=None):
|
2018-06-14 21:17:59 +02:00
|
|
|
# type: (List[Wallet], Dict[Type[BaseLedger],BaseLedger]) -> None
|
|
|
|
self.wallets = wallets or []
|
|
|
|
self.ledgers = ledgers or {}
|
2018-05-25 08:03:25 +02:00
|
|
|
self.running = False
|
|
|
|
|
|
|
|
@classmethod
|
2018-06-14 21:17:59 +02:00
|
|
|
def from_config(cls, config): # type: (Dict) -> WalletManager
|
2018-05-25 08:03:25 +02:00
|
|
|
wallets = []
|
|
|
|
manager = cls(wallets)
|
|
|
|
for coin_id, ledger_config in config.get('ledgers', {}).items():
|
|
|
|
manager.get_or_create_ledger(coin_id, ledger_config)
|
|
|
|
for wallet_path in config.get('wallets', []):
|
|
|
|
wallet_storage = WalletStorage(wallet_path)
|
|
|
|
wallet = Wallet.from_storage(wallet_storage, manager)
|
|
|
|
wallets.append(wallet)
|
|
|
|
return manager
|
|
|
|
|
2018-06-12 16:02:04 +02:00
|
|
|
def get_or_create_ledger(self, ledger_id, ledger_config=None):
|
|
|
|
ledger_class = LedgerRegistry.get_ledger_class(ledger_id)
|
2018-05-25 08:03:25 +02:00
|
|
|
ledger = self.ledgers.get(ledger_class)
|
|
|
|
if ledger is None:
|
2018-06-14 21:17:59 +02:00
|
|
|
ledger = ledger_class(ledger_config or {})
|
2018-05-25 08:03:25 +02:00
|
|
|
self.ledgers[ledger_class] = ledger
|
|
|
|
return ledger
|
|
|
|
|
2018-06-14 21:17:59 +02:00
|
|
|
def create_wallet(self, path):
|
|
|
|
storage = WalletStorage(path)
|
|
|
|
wallet = Wallet.from_storage(storage, self)
|
|
|
|
self.wallets.append(wallet)
|
|
|
|
return wallet
|
2018-06-08 05:47:46 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_balance(self):
|
|
|
|
balances = {}
|
2018-06-14 21:17:59 +02:00
|
|
|
for ledger in self.ledgers.values():
|
|
|
|
for account in ledger.accounts:
|
|
|
|
balances.setdefault(ledger.get_id(), 0)
|
|
|
|
balances[ledger.get_id()] += yield account.get_balance()
|
2018-06-08 05:47:46 +02:00
|
|
|
defer.returnValue(balances)
|
|
|
|
|
2018-05-25 08:03:25 +02:00
|
|
|
@property
|
|
|
|
def default_wallet(self):
|
|
|
|
for wallet in self.wallets:
|
|
|
|
return wallet
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default_account(self):
|
|
|
|
for wallet in self.wallets:
|
|
|
|
return wallet.default_account
|
|
|
|
|
|
|
|
def get_accounts(self, coin_class):
|
|
|
|
for wallet in self.wallets:
|
|
|
|
for account in wallet.accounts:
|
|
|
|
if account.coin.__class__ is coin_class:
|
|
|
|
yield account
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-06-08 05:47:46 +02:00
|
|
|
def start(self):
|
2018-05-25 08:03:25 +02:00
|
|
|
self.running = True
|
|
|
|
yield defer.DeferredList([
|
|
|
|
l.start() for l in self.ledgers.values()
|
|
|
|
])
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-06-08 05:47:46 +02:00
|
|
|
def stop(self):
|
2018-05-25 08:03:25 +02:00
|
|
|
yield defer.DeferredList([
|
|
|
|
l.stop() for l in self.ledgers.values()
|
|
|
|
])
|
|
|
|
self.running = False
|