bal.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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 collections import namedtuple
  22. from ..base_obj import AsyncInit
  23. from ..objmethods import MMGenObject
  24. from ..obj import NonNegativeInt
  25. from ..rpc import rpc_init
  26. class TwGetBalance(MMGenObject,metaclass=AsyncInit):
  27. def __new__(cls,cfg,proto,*args,**kwargs):
  28. return MMGenObject.__new__(proto.base_proto_subclass(cls,'tw.bal'))
  29. async def __init__(self,cfg,proto,minconf,quiet):
  30. class BalanceInfo(dict):
  31. def __init__(self):
  32. amt0 = proto.coin_amt('0')
  33. data = {
  34. 'unconfirmed': amt0,
  35. 'lt_minconf': amt0,
  36. 'ge_minconf': amt0,
  37. 'spendable': amt0,
  38. }
  39. return dict.__init__(self,**data)
  40. self.minconf = NonNegativeInt(minconf)
  41. self.balance_info = BalanceInfo
  42. self.quiet = quiet
  43. self.proto = proto
  44. self.data = {k:self.balance_info() for k in self.start_labels}
  45. self.rpc = await rpc_init(cfg,proto)
  46. if minconf < 2 and 'lt_minconf' in self.conf_cols:
  47. del self.conf_cols['lt_minconf']
  48. await self.create_data()
  49. def format(self,color):
  50. def gen_output():
  51. if self.quiet:
  52. yield str(self.data['TOTAL']['ge_minconf'] if self.data else 0)
  53. else:
  54. def get_col_iwidth(colname):
  55. return len(str(int(max(v[colname] for v in self.data.values())))) + iwidth_adj
  56. def make_col(label,col):
  57. return self.data[label][col].fmt( iwidth=iwidths[col], color=color )
  58. if color:
  59. from ..color import red,green,yellow
  60. else:
  61. from ..color import nocolor
  62. red = green = yellow = nocolor
  63. add_w = self.proto.coin_amt.max_prec + 1 # 1 = len('.')
  64. iwidth_adj = 1 # so that min iwidth (1) + add_w + iwidth_adj >= len('Unconfirmed')
  65. col1_w = max(len(l) for l in self.start_labels) + 1 # 1 = len(':')
  66. iwidths = {colname: get_col_iwidth(colname) for colname in self.conf_cols}
  67. net_desc = self.proto.coin + ' ' + self.proto.network.upper()
  68. if net_desc != 'BTC MAINNET':
  69. yield 'Network: {}'.format(green(net_desc))
  70. yield '{lbl:{w}} {cols}'.format(
  71. lbl = 'Wallet',
  72. w = col1_w + iwidth_adj,
  73. cols = ' '.join(v.format(minconf=self.minconf).ljust(iwidths[k]+add_w)
  74. for k,v in self.conf_cols.items()) ).rstrip()
  75. from ..addr import MMGenID
  76. for label in sorted(self.data.keys()):
  77. yield '{lbl} {cols}'.format(
  78. lbl = yellow((label + ' ' + self.proto.coin).ljust(col1_w)) if label == 'TOTAL'
  79. else MMGenID.hlc( (label+':').ljust(col1_w), color=color ),
  80. cols = ' '.join(make_col(label,col) for col in self.conf_cols)
  81. ).rstrip()
  82. for k,v in self.data.items():
  83. if k == 'TOTAL':
  84. continue
  85. if v['spendable']:
  86. yield red(f'Warning: this wallet contains PRIVATE KEYS for {k} outputs!')
  87. return '\n'.join(gen_output())