status.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 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
  9. # https://gitlab.com/mmgen/mmgen
  10. """
  11. proto.eth.tx.status: Ethereum transaction status class
  12. """
  13. import mmgen.tx.status as TxBase
  14. from ....util import msg,die,suf,capfirst
  15. class Status(TxBase.Status):
  16. async def display(self,usr_req=False):
  17. tx = self.tx
  18. async def is_in_mempool():
  19. if not 'full_node' in tx.rpc.caps:
  20. return False
  21. if tx.rpc.daemon.id in ('parity','openethereum'):
  22. pool = [x['hash'] for x in await tx.rpc.call('parity_pendingTransactions')]
  23. elif tx.rpc.daemon.id in ('geth','erigon'):
  24. res = await tx.rpc.call('txpool_content')
  25. pool = list(res['pending']) + list(res['queued'])
  26. return '0x'+tx.coin_txid in pool
  27. async def is_in_wallet():
  28. d = await tx.rpc.call('eth_getTransactionReceipt','0x'+tx.coin_txid)
  29. if d and 'blockNumber' in d and d['blockNumber'] is not None:
  30. from collections import namedtuple
  31. receipt_info = namedtuple('receipt_info',['confs','exec_status'])
  32. return receipt_info(
  33. confs = 1 + int(await tx.rpc.call('eth_blockNumber'),16) - int(d['blockNumber'],16),
  34. exec_status = int(d['status'],16)
  35. )
  36. if await is_in_mempool():
  37. msg(
  38. 'Transaction is in mempool' if usr_req else
  39. 'Warning: transaction is in mempool!' )
  40. return
  41. if usr_req:
  42. ret = await is_in_wallet()
  43. if ret:
  44. if tx.txobj['data']:
  45. cd = capfirst(tx.contract_desc)
  46. if ret.exec_status == 0:
  47. msg(f'{cd} failed to execute!')
  48. else:
  49. msg(f'{cd} successfully executed with status {ret.exec_status}')
  50. die(0,f'Transaction has {ret.confs} confirmation{suf(ret.confs)}')
  51. die(1,'Transaction is neither in mempool nor blockchain!')
  52. class TokenStatus(Status):
  53. pass