info.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. proto.eth.tx.info: Ethereum transaction info class
  12. """
  13. from ....tx.info import TxInfo
  14. from ....util import fmt, pp_fmt
  15. from ....color import pink, yellow, blue
  16. from ....addr import MMGenID
  17. class TxInfo(TxInfo):
  18. txinfo_hdr_fs = '{hdr}\n ID={i} ({a} {c}) Sig={s} Locktime={l}\n'
  19. txinfo_hdr_fs_short = 'TX {i} ({a} {c}) Sig={s} Locktime={l}\n'
  20. txinfo_ftr_fs = fmt("""
  21. Total in account: {i} {d}
  22. Total to spend: {o} {d}
  23. Remaining balance: {C} {d}
  24. TX fee: {a} {c}{r}
  25. """)
  26. to_addr_key = 'to'
  27. def format_body(self, blockcount, nonmm_str, max_mmwid, enl, terse, sort):
  28. tx = self.tx
  29. m = {}
  30. for k in ('inputs', 'outputs'):
  31. if len(getattr(tx, k)):
  32. m[k] = getattr(tx, k)[0].mmid if len(getattr(tx, k)) else ''
  33. m[k] = ' ' + m[k].hl() if m[k] else ' ' + MMGenID.hlc(nonmm_str)
  34. fs = """
  35. From: {f}{f_mmid}
  36. To: {t}{t_mmid}
  37. Amount: {a} {c}
  38. Gas price: {g} Gwei
  39. Start gas: {G} Kwei
  40. Nonce: {n}
  41. Data: {d}
  42. """.strip().replace('\t', '')
  43. t = tx.txobj
  44. td = t['data']
  45. to_addr = t[self.to_addr_key]
  46. return fs.format(
  47. f = t['from'].hl(0),
  48. t = to_addr.hl(0) if to_addr else blue('None'),
  49. a = t['amt'].hl(),
  50. n = t['nonce'].hl(),
  51. d = '{}... ({} bytes)'.format(td[:40], len(td)//2) if len(td) else blue('None'),
  52. c = tx.proto.dcoin if len(tx.outputs) else '',
  53. g = yellow(tx.pretty_fmt_fee(t['gasPrice'].to_unit('Gwei'))),
  54. G = yellow(tx.pretty_fmt_fee(t['startGas'].to_unit('Kwei'))),
  55. t_mmid = m['outputs'] if len(tx.outputs) else '',
  56. f_mmid = m['inputs']) + '\n\n'
  57. def format_abs_fee(self, color, iwidth):
  58. return self.tx.fee.fmt(color=color, iwidth=iwidth) + (' (max)' if self.tx.txobj['data'] else '')
  59. def format_rel_fee(self):
  60. return ' ({} of spend amount)'.format(
  61. pink('{:0.6f}%'.format(self.tx.fee / self.tx.send_amt * 100))
  62. )
  63. def format_verbose_footer(self):
  64. if self.tx.txobj['data']:
  65. from ..contract import parse_abi
  66. return '\nParsed contract data: ' + pp_fmt(parse_abi(self.tx.txobj['data']))
  67. else:
  68. return ''
  69. class TokenTxInfo(TxInfo):
  70. to_addr_key = 'token_to'
  71. def format_rel_fee(self):
  72. return ''
  73. def format_body(self, *args, **kwargs):
  74. return 'Token: {d} {c}\n{r}'.format(
  75. d = self.tx.txobj['token_addr'].hl(0),
  76. c = blue('(' + self.tx.proto.dcoin + ')'),
  77. r = super().format_body(*args, **kwargs))