bal.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen
  9. # https://gitlab.com/mmgen/mmgen
  10. """
  11. proto.btc.twbal: Bitcoin base protocol tracking wallet balance class
  12. """
  13. from ....tw.bal import TwGetBalance
  14. from ....tw.shared import get_tw_label
  15. class BitcoinTwGetBalance(TwGetBalance):
  16. fs = '{w:13} {u:<16} {p:<16} {c}'
  17. async def create_data(self):
  18. # 0: unconfirmed, 1: below minconf, 2: confirmed, 3: spendable (privkey in wallet)
  19. lbl_id = ('account','label')['label_api' in self.rpc.caps]
  20. amt0 = self.proto.coin_amt('0')
  21. for d in await self.rpc.call('listunspent',0):
  22. lbl = get_tw_label(self.proto,d[lbl_id])
  23. if lbl:
  24. if lbl.mmid.type == 'mmgen':
  25. key = lbl.mmid.obj.sid
  26. if key not in self.data:
  27. self.data[key] = [amt0] * 4
  28. else:
  29. key = 'Non-MMGen'
  30. else:
  31. lbl,key = None,'Non-wallet'
  32. amt = self.proto.coin_amt(d['amount'])
  33. if not d['confirmations']:
  34. self.data['TOTAL'][0] += amt
  35. self.data[key][0] += amt
  36. conf_level = (1,2)[d['confirmations'] >= self.minconf]
  37. self.data['TOTAL'][conf_level] += amt
  38. self.data[key][conf_level] += amt
  39. if d['spendable']:
  40. self.data[key][3] += amt