bal.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. tw.bal: Tracking wallet getbalance class for the MMGen suite
  20. """
  21. from ..base_obj import AsyncInit
  22. from ..objmethods import MMGenObject
  23. from ..obj import NonNegativeInt
  24. class TwGetBalance(MMGenObject, metaclass=AsyncInit):
  25. def __new__(cls, cfg, proto, *, minconf, quiet):
  26. return MMGenObject.__new__(proto.base_proto_subclass(cls, 'tw.bal'))
  27. async def __init__(self, cfg, proto, *, minconf, quiet):
  28. class BalanceInfo(dict):
  29. def __init__(self):
  30. amt0 = proto.coin_amt('0')
  31. data = {
  32. 'unconfirmed': amt0,
  33. 'lt_minconf': amt0,
  34. 'ge_minconf': amt0,
  35. 'spendable': amt0}
  36. dict.__init__(self, **data)
  37. self.minconf = NonNegativeInt(minconf)
  38. self.balance_info = BalanceInfo
  39. self.quiet = quiet
  40. self.proto = proto
  41. self.data = {k: self.balance_info() for k in self.start_labels}
  42. if minconf < 2 and 'lt_minconf' in self.conf_cols:
  43. del self.conf_cols['lt_minconf']
  44. await self.create_data()
  45. def format(self, color):
  46. def gen_output():
  47. if self.quiet:
  48. yield str(self.data['TOTAL']['ge_minconf'] if self.data else 0)
  49. else:
  50. def get_col_iwidth(colname):
  51. return len(str(int(max(v[colname] for v in self.data.values())))) + iwidth_adj
  52. def make_col(label, col):
  53. return self.data[label][col].fmt(iwidths[col], color=color)
  54. if color:
  55. from ..color import green, yellow
  56. else:
  57. from ..color import nocolor
  58. green = yellow = nocolor
  59. add_w = self.proto.coin_amt.max_prec + 1 # 1 = len('.')
  60. iwidth_adj = 1 # so that min iwidth (1) + add_w + iwidth_adj >= len('Unconfirmed')
  61. col1_w = max(len(l) for l in self.start_labels) + 1 # 1 = len(':')
  62. iwidths = {colname: get_col_iwidth(colname) for colname in self.conf_cols}
  63. net_desc = self.proto.coin + ' ' + self.proto.network.upper()
  64. if net_desc != 'BTC MAINNET':
  65. yield f'Network: {green(net_desc)}'
  66. yield '{lbl:{w}} {cols}'.format(
  67. lbl = 'Wallet',
  68. w = col1_w + iwidth_adj,
  69. cols = ' '.join(v.format(minconf=self.minconf).ljust(iwidths[k]+add_w)
  70. for k, v in self.conf_cols.items())).rstrip()
  71. from ..addr import MMGenID
  72. for label in sorted(self.data.keys()):
  73. yield '{lbl} {cols}'.format(
  74. lbl = yellow((label + ' ' + self.proto.coin).ljust(col1_w)) if label == 'TOTAL'
  75. else MMGenID.hlc((label+':').ljust(col1_w), color=color),
  76. cols = ' '.join(make_col(label, col) for col in self.conf_cols)
  77. ).rstrip()
  78. return '\n'.join(gen_output())