unspent.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 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. proto.eth.twuo: Ethereum tracking wallet unspent outputs class
  20. """
  21. from ....tw.shared import TwLabel
  22. from ....tw.unspent import TwUnspentOutputs
  23. from .view import EthereumTwView
  24. # No unspent outputs with Ethereum, but naming must be consistent
  25. class EthereumTwUnspentOutputs(EthereumTwView,TwUnspentOutputs):
  26. class display_type(TwUnspentOutputs.display_type):
  27. class squeezed(TwUnspentOutputs.display_type.squeezed):
  28. cols = ('num','addr','mmid','comment','amt','amt2')
  29. class detail(TwUnspentOutputs.display_type.detail):
  30. cols = ('num','addr','mmid','amt','amt2','comment')
  31. class MMGenTwUnspentOutput(TwUnspentOutputs.MMGenTwUnspentOutput):
  32. valid_attrs = {'txid','vout','amt','amt2','comment','twmmid','addr','confs','skip'}
  33. invalid_attrs = {'proto'}
  34. token_cls = False
  35. has_age = False
  36. can_group = False
  37. hdr_lbl = 'tracked accounts'
  38. desc = 'account balances'
  39. item_desc = 'account'
  40. dump_fn_pfx = 'balances'
  41. prompt = """
  42. Sort options: [a]mount, a[d]dress, [r]everse, [M]mgen addr
  43. Display options: show [m]mgen addr, r[e]draw screen
  44. Actions: [q]uit view, [p]rint to file, pager [v]iew, [w]ide view,
  45. [D]elete address, add [l]abel, [R]efresh balance:
  46. """
  47. key_mappings = {
  48. 'a':'s_amt',
  49. 'd':'s_addr',
  50. 'r':'d_reverse',
  51. 'M':'s_twmmid',
  52. 'm':'d_mmid',
  53. 'e':'d_redraw',
  54. 'q':'a_quit',
  55. 'p':'a_print_detail',
  56. 'v':'a_view',
  57. 'w':'a_view_detail',
  58. 'l':'a_comment_add',
  59. 'D':'a_addr_delete',
  60. 'R':'a_balance_refresh' }
  61. no_data_errmsg = 'No accounts in tracking wallet!'
  62. def get_column_widths(self,data,wide=False):
  63. # min screen width: 80 cols
  64. # num addr [mmid] [comment] amt [amt2]
  65. return self.compute_column_widths(
  66. widths = { # fixed cols
  67. 'num': max(2,len(str(len(data)))+1),
  68. 'mmid': max(len(d.twmmid.disp) for d in data) if self.show_mmid else 0,
  69. 'amt': self.disp_prec + 5,
  70. 'amt2': self.disp_prec + 5 if self.token_cls else 0,
  71. 'spc': (5 if self.show_mmid else 3) + self.token_cls, # 5(3) spaces in fs
  72. 'txid': 0,
  73. 'vout': 0,
  74. 'block': 0,
  75. 'date': 0,
  76. 'date_time': 0,
  77. },
  78. maxws = { # expandable cols
  79. 'addr': max(len(d.addr) for d in data),
  80. 'comment': max(d.comment.screen_width for d in data) if self.show_mmid else 0,
  81. },
  82. minws = {
  83. 'addr': 10,
  84. 'comment': len('Comment') if self.show_mmid else 0,
  85. },
  86. maxws_nice = {'addr': 14} if self.show_mmid else {},
  87. wide = wide,
  88. )
  89. def do_sort(self,key=None,reverse=False):
  90. if key == 'txid': return
  91. super().do_sort(key=key,reverse=reverse)
  92. async def get_rpc_data(self):
  93. wl = self.wallet.sorted_list
  94. if self.addrs:
  95. wl = [d for d in wl if d['addr'] in self.addrs]
  96. return [{
  97. 'account': TwLabel(self.proto,d['mmid']+' '+d['comment']),
  98. 'address': d['addr'],
  99. 'amount': await self.wallet.get_balance(d['addr']),
  100. 'confirmations': 0, # TODO
  101. } for d in wl]
  102. class EthereumTokenTwUnspentOutputs(EthereumTwUnspentOutputs):
  103. prompt_fs = 'Total to spend: {} {}\n\n'
  104. token_cls = True
  105. async def __init__(self,proto,*args,**kwargs):
  106. await super().__init__(proto,*args,**kwargs)
  107. self.proto.tokensym = self.wallet.symbol
  108. async def get_data(self,*args,**kwargs):
  109. await super().get_data(*args,**kwargs)
  110. for e in self.data:
  111. e.amt2 = await self.wallet.get_eth_balance(e.addr)