unspent.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. tw.unspent: Tracking wallet unspent outputs class for the MMGen suite
  20. """
  21. from ..util import msg, suf
  22. from ..obj import (
  23. ImmutableAttr,
  24. ListItemAttr,
  25. MMGenListItem,
  26. TwComment,
  27. CoinTxID,
  28. NonNegativeInt)
  29. from ..addr import CoinAddr
  30. from ..amt import CoinAmtChk
  31. from .shared import TwMMGenID, TwLabel, get_tw_label
  32. from .view import TwView
  33. class TwUnspentOutputs(TwView):
  34. has_age = False
  35. can_group = False
  36. show_mmid = True
  37. hdr_lbl = 'tracked addresses'
  38. desc = 'address balances'
  39. item_desc = 'address'
  40. item_desc_pl = 'addresses'
  41. no_rpcdata_errmsg = """
  42. No spendable outputs found! Import addresses with balances into your
  43. watch-only wallet using 'mmgen-addrimport' and then re-run this program.
  44. """
  45. update_widths_on_age_toggle = False
  46. print_output_types = ('detail',)
  47. mod_subpath = 'tw.unspent'
  48. dump_fn_pfx = 'balances'
  49. prompt_fs_in = [
  50. 'Sort options: [a]mount, a[d]dr, [M]mgen addr, [r]everse',
  51. 'Display options: show [m]mgen addr, r[e]draw screen',
  52. 'View/Print: pager [v]iew, [w]ide pager view, [p]rint to file{s}',
  53. 'Actions: [q]uit menu, [D]elete addr, add [l]abel, [R]efresh balance:']
  54. key_mappings = {
  55. 'a':'s_amt',
  56. 'd':'s_addr',
  57. 'r':'s_reverse',
  58. 'M':'s_twmmid',
  59. 'm':'d_mmid',
  60. 'e':'d_redraw',
  61. 'p':'a_print_detail',
  62. 'v':'a_view',
  63. 'w':'a_view_detail',
  64. 'l':'i_comment_add'}
  65. extra_key_mappings = {
  66. 'D':'i_addr_delete',
  67. 'R':'i_balance_refresh'}
  68. disp_spc = 3
  69. vout_w = 0
  70. class display_type(TwView.display_type):
  71. class squeezed(TwView.display_type.squeezed):
  72. cols = ('num', 'addr', 'mmid', 'comment', 'amt', 'amt2')
  73. class detail(TwView.display_type.detail):
  74. cols = ('num', 'addr', 'mmid', 'amt', 'amt2', 'comment')
  75. class MMGenTwUnspentOutput(MMGenListItem):
  76. valid_attrs = {'txid', 'vout', 'amt', 'amt2', 'comment', 'twmmid', 'addr', 'confs', 'skip'}
  77. invalid_attrs = {'proto'}
  78. txid = ListItemAttr(CoinTxID)
  79. vout = ListItemAttr(NonNegativeInt)
  80. amt = ImmutableAttr(CoinAmtChk, include_proto=True)
  81. amt2 = ListItemAttr(CoinAmtChk, include_proto=True) # the ETH balance for token account
  82. comment = ListItemAttr(TwComment, reassign_ok=True)
  83. twmmid = ImmutableAttr(TwMMGenID, include_proto=True)
  84. addr = ImmutableAttr(CoinAddr, include_proto=True)
  85. confs = ImmutableAttr(int, typeconv=False)
  86. skip = ListItemAttr(str, typeconv=False, reassign_ok=True)
  87. def __init__(self, proto, **kwargs):
  88. self.__dict__['proto'] = proto
  89. MMGenListItem.__init__(self, **kwargs)
  90. async def __init__(self, cfg, proto, *, minconf=1, addrs=[]):
  91. await super().__init__(cfg, proto)
  92. self.minconf = NonNegativeInt(minconf)
  93. self.addrs = addrs
  94. from ..cfg import gc
  95. self.min_cols = gc.min_screen_width
  96. @property
  97. def total(self):
  98. return sum(i.amt for i in self.data)
  99. def gen_data(self, rpc_data, lbl_id):
  100. for o in rpc_data:
  101. if not lbl_id in o:
  102. continue # coinbase outputs have no account field
  103. l = get_tw_label(self.proto, o[lbl_id])
  104. if l:
  105. if not 'amt' in o:
  106. o['amt'] = self.proto.coin_amt(o['amount'])
  107. o.update({
  108. 'twmmid': l.mmid,
  109. 'comment': l.comment or '',
  110. 'addr': CoinAddr(self.proto, o['address']),
  111. 'confs': o['confirmations']
  112. })
  113. yield self.MMGenTwUnspentOutput(
  114. self.proto,
  115. **{k:v for k, v in o.items() if k in self.MMGenTwUnspentOutput.valid_attrs})
  116. async def get_rpc_data(self):
  117. wl = self.twctl.sorted_list
  118. minconf = int(self.minconf)
  119. block = self.twctl.rpc.get_block_from_minconf(minconf)
  120. if self.addrs:
  121. wl = [d for d in wl if d['addr'] in self.addrs]
  122. return [{
  123. 'account': TwLabel(self.proto, d['mmid']+' '+d['comment']),
  124. 'address': d['addr'],
  125. 'amt': await self.twctl.get_balance(d['addr'], block=block),
  126. 'confirmations': minconf,
  127. } for d in wl]
  128. def filter_data(self):
  129. data = self.data.copy()
  130. for d in data:
  131. d.skip = ''
  132. gkeys = {'addr': 'addr', 'twmmid': 'addr', 'txid': 'txid'}
  133. if self.group and self.sort_key in gkeys:
  134. for a, b in [(data[i], data[i+1]) for i in range(len(data)-1)]:
  135. for k in gkeys:
  136. if self.sort_key == k and getattr(a, k) == getattr(b, k):
  137. b.skip = gkeys[k]
  138. return data
  139. def get_column_widths(self, data, *, wide, interactive):
  140. show_mmid = self.show_mmid or wide
  141. return self.compute_column_widths(
  142. widths = { # fixed cols
  143. 'num': max(2, len(str(len(data)))+1),
  144. 'txid': 0,
  145. 'vout': self.vout_w,
  146. 'mmid': max(len(d.twmmid.disp) for d in data) if show_mmid else 0,
  147. 'amt': self.amt_widths['amt'],
  148. 'amt2': self.amt_widths.get('amt2', 0),
  149. 'block': self.age_col_params['block'][0] if wide else 0,
  150. 'date_time': self.age_col_params['date_time'][0] if wide else 0,
  151. 'date': self.age_w,
  152. 'spc': self.disp_spc + (2 * show_mmid) + self.has_amt2
  153. },
  154. maxws = { # expandable cols
  155. 'addr': max(len(d.addr) for d in data),
  156. 'comment': max(d.comment.screen_width for d in data) if show_mmid else 0,
  157. } | self.txid_max_w,
  158. minws = {
  159. 'addr': 10,
  160. 'comment': len('Comment') if show_mmid else 0,
  161. } | self.txid_min_w,
  162. maxws_nice = (
  163. self.nice_addr_w if show_mmid else {}
  164. ) | self.txid_nice_w,
  165. wide = wide,
  166. interactive = interactive,
  167. )
  168. def squeezed_col_hdr(self, cw, fs, color):
  169. return fs.format(
  170. n = '',
  171. t = 'TxID',
  172. v = 'Vout',
  173. a = 'Address',
  174. m = 'MMGenID',
  175. c = 'Comment',
  176. A = 'Amt({})'.format(self.proto.dcoin),
  177. B = 'Amt({})'.format(self.proto.coin),
  178. d = self.age_hdr)
  179. def detail_col_hdr(self, cw, fs, color):
  180. return fs.format(
  181. n = '',
  182. t = 'TxID',
  183. v = 'Vout',
  184. a = 'Address',
  185. m = 'MMGenID',
  186. A = 'Amt({})'.format(self.proto.dcoin),
  187. B = 'Amt({})'.format(self.proto.coin),
  188. b = 'Block',
  189. D = 'Date/Time',
  190. c = 'Comment')
  191. def gen_squeezed_display(self, data, cw, fs, color, fmt_method):
  192. for n, d in enumerate(data):
  193. yield fs.format(
  194. n = str(n+1) + ')',
  195. t = (d.txid.fmtc('|' + '.'*(cw.txid-1), cw.txid, color=color) if d.skip == 'txid'
  196. else d.txid.truncate(cw.txid, color=color)) if cw.txid else None,
  197. v = ' ' + d.vout.fmt(cw.vout-1, color=color) if cw.vout else None,
  198. a = d.addr.fmtc('|' + '.'*(cw.addr-1), cw.addr, color=color) if d.skip == 'addr'
  199. else d.addr.fmt(self.addr_view_pref, cw.addr, color=color),
  200. m = (d.twmmid.fmtc('.'*cw.mmid, cw.mmid, color=color) if d.skip == 'addr'
  201. else d.twmmid.fmt(cw.mmid, color=color)) if cw.mmid else None,
  202. c = d.comment.fmt2(cw.comment, color=color, nullrepl='-') if cw.comment else None,
  203. A = d.amt.fmt(cw.iwidth, color=color, prec=self.disp_prec),
  204. B = d.amt2.fmt(cw.iwidth2, color=color, prec=self.disp_prec) if cw.amt2 else None,
  205. d = self.age_disp(d, self.age_fmt),
  206. )
  207. def gen_detail_display(self, data, cw, fs, color, fmt_method):
  208. for n, d in enumerate(data):
  209. yield fs.format(
  210. n = str(n+1) + ')',
  211. t = d.txid.fmt(cw.txid, color=color) if cw.txid else None,
  212. v = ' ' + d.vout.fmt(cw.vout-1, color=color) if cw.vout else None,
  213. a = d.addr.fmt(self.addr_view_pref, cw.addr, color=color),
  214. m = d.twmmid.fmt(cw.mmid, color=color),
  215. A = d.amt.fmt(cw.iwidth, color=color, prec=self.disp_prec),
  216. B = d.amt2.fmt(cw.iwidth2, color=color, prec=self.disp_prec) if cw.amt2 else None,
  217. b = self.age_disp(d, 'block'),
  218. D = self.age_disp(d, 'date_time'),
  219. c = d.comment.fmt2(cw.comment, color=color, nullrepl='-'))
  220. def display_total(self):
  221. msg('\nTotal unspent: {} {} ({} {}{})'.format(
  222. self.total.hl(),
  223. self.proto.dcoin,
  224. len(self.data),
  225. self.item_desc,
  226. suf(self.data)))
  227. async def set_dates(self, us):
  228. if not self.dates_set:
  229. # 'blocktime' differs from 'time', is same as getblockheader['time']
  230. dates = [o.get('blocktime', 0)
  231. for o in await self.rpc.gathered_icall('gettransaction', [(o.txid, True, False) for o in us])]
  232. for idx, o in enumerate(us):
  233. o.date = dates[idx]
  234. self.dates_set = True
  235. class sort_action(TwView.sort_action):
  236. def s_twmmid(self, parent):
  237. parent.do_sort('twmmid')
  238. parent.show_mmid = True
  239. class display_action(TwView.display_action):
  240. def d_mmid(self, parent):
  241. parent.show_mmid = not parent.show_mmid
  242. def d_group(self, parent):
  243. if parent.can_group:
  244. parent.group = not parent.group