bal.py 3.4 KB

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