unspent.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.common import TwLabel
  22. from ....tw.unspent import TwUnspentOutputs
  23. # No unspent outputs with Ethereum, but naming must be consistent
  24. class EthereumTwUnspentOutputs(TwUnspentOutputs):
  25. class MMGenTwUnspentOutput(TwUnspentOutputs.MMGenTwUnspentOutput):
  26. valid_attrs = {'txid','vout','amt','amt2','comment','twmmid','addr','confs','skip'}
  27. invalid_attrs = {'proto'}
  28. has_age = False
  29. can_group = False
  30. col_adj = 29
  31. hdr_fmt = 'TRACKED ACCOUNTS (sort order: {a})\nTotal {b}: {c}'
  32. desc = 'account balances'
  33. item_desc = 'account'
  34. dump_fn_pfx = 'balances'
  35. prompt = """
  36. Sort options: [a]mount, a[d]dress, [r]everse, [M]mgen addr
  37. Display options: show [m]mgen addr, r[e]draw screen
  38. Actions: [q]uit view, [p]rint to file, pager [v]iew, [w]ide view,
  39. add [l]abel, [D]elete address, [R]efresh balance:
  40. """
  41. key_mappings = {
  42. 'a':'s_amt',
  43. 'd':'s_addr',
  44. 'r':'d_reverse',
  45. 'M':'s_twmmid',
  46. 'm':'d_mmid',
  47. 'e':'d_redraw',
  48. 'q':'a_quit',
  49. 'p':'a_print_detail',
  50. 'v':'a_view',
  51. 'w':'a_view_detail',
  52. 'l':'a_comment_add',
  53. 'D':'a_addr_delete',
  54. 'R':'a_balance_refresh' }
  55. squeezed_fs_fs = squeezed_hdr_fs_fs = ' {{n:{cw}}} {{a}} {{A}}'
  56. wide_fs_fs = ' {{n:4}} {{a}} {{m}} {{A:{aw}}} {{l}}'
  57. no_data_errmsg = 'No accounts in tracking wallet!'
  58. async def __init__(self,proto,*args,**kwargs):
  59. from ....globalvars import g
  60. if g.cached_balances:
  61. from ....color import yellow
  62. self.hdr_fmt += '\n' + yellow('WARNING: Using cached balances. These may be out of date!')
  63. await super().__init__(proto,*args,**kwargs)
  64. def do_sort(self,key=None,reverse=False):
  65. if key == 'txid': return
  66. super().do_sort(key=key,reverse=reverse)
  67. async def get_rpc_data(self):
  68. wl = self.wallet.sorted_list
  69. if self.addrs:
  70. wl = [d for d in wl if d['addr'] in self.addrs]
  71. return [{
  72. 'account': TwLabel(self.proto,d['mmid']+' '+d['comment']),
  73. 'address': d['addr'],
  74. 'amount': await self.wallet.get_balance(d['addr']),
  75. 'confirmations': 0, # TODO
  76. } for d in wl]
  77. def age_disp(self,o,age_fmt): # TODO
  78. pass
  79. class EthereumTokenTwUnspentOutputs(EthereumTwUnspentOutputs):
  80. prompt_fs = 'Total to spend: {} {}\n\n'
  81. col_adj = 37
  82. squeezed_fs_fs = squeezed_hdr_fs_fs = ' {{n:{cw}}} {{a}} {{A}} {{A2}}'
  83. wide_fs_fs = ' {{n:4}} {{a}} {{m}} {{A:{aw}}} {{A2:{aw}}} {{l}}'
  84. async def __init__(self,proto,*args,**kwargs):
  85. await super().__init__(proto,*args,**kwargs)
  86. self.proto.tokensym = self.wallet.symbol
  87. @property
  88. def disp_prec(self):
  89. return 10 # truncate precision for narrow display
  90. async def get_data(self,*args,**kwargs):
  91. await super().get_data(*args,**kwargs)
  92. for e in self.data:
  93. e.amt2 = await self.wallet.get_eth_balance(e.addr)