unspent.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. proto.eth.tw.unspent: 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. has_age = False
  35. can_group = False
  36. hdr_lbl = 'tracked accounts'
  37. desc = 'account balances'
  38. item_desc = 'account'
  39. dump_fn_pfx = 'balances'
  40. prompt_fs_in = [
  41. 'Sort options: [a]mount, a[d]dr, [M]mgen addr, [r]everse',
  42. 'Display options: show [m]mgen addr, r[e]draw screen',
  43. 'View/Print: pager [v]iew, [w]ide pager view, [p]rint to file{s}',
  44. 'Actions: [q]uit menu, [D]elete addr, add [l]abel, [R]efresh balance:']
  45. key_mappings = {
  46. 'a':'s_amt',
  47. 'd':'s_addr',
  48. 'r':'s_reverse',
  49. 'M':'s_twmmid',
  50. 'm':'d_mmid',
  51. 'e':'d_redraw',
  52. 'p':'a_print_detail',
  53. 'v':'a_view',
  54. 'w':'a_view_detail',
  55. 'l':'i_comment_add',
  56. 'D':'i_addr_delete',
  57. 'R':'i_balance_refresh'}
  58. no_data_errmsg = 'No accounts in tracking wallet!'
  59. def get_column_widths(self, data, wide, interactive):
  60. # min screen width: 80 cols
  61. # num addr [mmid] [comment] amt [amt2]
  62. return self.compute_column_widths(
  63. widths = { # fixed cols
  64. 'num': max(2, len(str(len(data)))+1),
  65. 'mmid': max(len(d.twmmid.disp) for d in data) if self.show_mmid else 0,
  66. 'amt': self.amt_widths['amt'],
  67. 'amt2': self.amt_widths.get('amt2', 0),
  68. 'spc': (5 if self.show_mmid else 3) + self.has_amt2, # 5(3) spaces in fs
  69. 'txid': 0,
  70. 'vout': 0,
  71. 'block': 0,
  72. 'date': 0,
  73. 'date_time': 0,
  74. },
  75. maxws = { # expandable cols
  76. 'addr': max(len(d.addr) for d in data),
  77. 'comment': max(d.comment.screen_width for d in data) if self.show_mmid else 0,
  78. },
  79. minws = {
  80. 'addr': 10,
  81. 'comment': len('Comment') if self.show_mmid else 0,
  82. },
  83. maxws_nice = {'addr': 14} if self.show_mmid else {},
  84. wide = wide,
  85. interactive = interactive,
  86. )
  87. def do_sort(self, key=None, reverse=False):
  88. if key == 'txid':
  89. return
  90. super().do_sort(key=key, reverse=reverse)
  91. async def get_rpc_data(self):
  92. wl = self.twctl.sorted_list
  93. if self.addrs:
  94. wl = [d for d in wl if d['addr'] in self.addrs]
  95. return [{
  96. 'account': TwLabel(self.proto, d['mmid']+' '+d['comment']),
  97. 'address': d['addr'],
  98. 'amt': await self.twctl.get_balance(d['addr']),
  99. 'confirmations': 0, # TODO
  100. } for d in wl]
  101. class EthereumTokenTwUnspentOutputs(EthereumTwUnspentOutputs):
  102. has_amt2 = True
  103. async def get_data(self, *args, **kwargs):
  104. await super().get_data(*args, **kwargs)
  105. for e in self.data:
  106. e.amt2 = await self.twctl.get_eth_balance(e.addr)