runeswap.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. # 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. test.cmdtest_d.runeswap: THORChain swap tests for the cmdtest.py test suite
  12. """
  13. from hashlib import md5
  14. from mmgen.fileutil import get_data_from_file
  15. from .httpd.thornode.swap import ThornodeSwapServer
  16. from .include.proxy import TestProxy
  17. from .regtest import CmdTestRegtest
  18. from .swap import CmdTestSwapMethods, create_cross_methods
  19. from .rune import CmdTestRune
  20. class CmdTestRuneSwap(CmdTestSwapMethods, CmdTestRegtest):
  21. 'RUNE swap operations'
  22. bdb_wallet = True
  23. tmpdir_nums = [57]
  24. networks = ('btc',)
  25. passthru_opts = ('coin', 'rpc_backend')
  26. cross_group = 'runeswap_rune'
  27. cross_coin = 'rune'
  28. cmd_group_in = (
  29. ('setup', 'regtest (Bob and Alice) mode setup'),
  30. ('subgroup.init', []),
  31. ('subgroup.rune_init', ['init']),
  32. ('subgroup.rune_swap', ['rune_init']),
  33. ('rune_rpc_server_stop', 'stopping the Thornode RPC server'),
  34. ('swap_server_stop', 'stopping the Thornode swap server'),
  35. ('stop', 'stopping the regtest daemon'),
  36. )
  37. cmd_subgroups = {
  38. 'init': (
  39. 'creating Bob’s MMGen wallet and tracking wallet',
  40. ('walletconv_bob', 'wallet creation (Bob)'),
  41. ('addrgen_bob', 'address generation (Bob)'),
  42. ('addrimport_bob', 'importing Bob’s addresses'),
  43. ),
  44. 'rune_init': (
  45. 'initializing the RUNE tracking wallet',
  46. ('rune_addrgen', ''),
  47. ('rune_addrimport', ''),
  48. ('rune_bal_refresh', ''),
  49. ('rune_twview', ''),
  50. ),
  51. 'rune_swap': (
  52. 'swap operations (RUNE -> BTC)',
  53. ('rune_swaptxcreate1', ''),
  54. ('rune_swaptxsign1', ''),
  55. ('rune_swaptxsend1', ''),
  56. ('rune_swaptxstatus1', ''),
  57. ('rune_swaptxreceipt1', ''),
  58. ('rune_swaptxhex1', ''),
  59. ),
  60. }
  61. exec(create_cross_methods(cross_coin, cross_group, cmd_group_in, cmd_subgroups))
  62. def __init__(self, cfg, trunner, cfgs, spawn):
  63. super().__init__(cfg, trunner, cfgs, spawn)
  64. if not trunner:
  65. return
  66. globals()[self.cross_group] = self.create_cross_runner(trunner)
  67. self.swap_server = ThornodeSwapServer()
  68. self.swap_server.start()
  69. TestProxy(self, cfg)
  70. def swap_server_stop(self):
  71. return self._thornode_server_stop()
  72. class CmdTestRuneSwapRune(CmdTestSwapMethods, CmdTestRune):
  73. 'RUNE swap operations - RUNE wallet'
  74. networks = ('rune',)
  75. tmpdir_nums = [58]
  76. input_sels_prompt = 'to spend from: '
  77. is_helper = True
  78. txhex_chksum = '34980b41'
  79. cmd_group_in = CmdTestRune.cmd_group_in + (
  80. # rune_swap:
  81. ('swaptxcreate1', 'creating a RUNE->BTC swap transaction'),
  82. ('swaptxsign1', 'signing the transaction'),
  83. ('swaptxsend1', 'sending the transaction'),
  84. ('swaptxstatus1', 'getting the transaction status'),
  85. ('swaptxreceipt1', 'getting the transaction receipt'),
  86. ('swaptxhex1', 'dumping the transaction hex'),
  87. ('thornode_server_stop', 'stopping Thornode server'),
  88. )
  89. def __init__(self, *args, **kwargs):
  90. super().__init__(*args, **kwargs)
  91. self.txhex_file = f'{self.tmpdir}/tx_dump.hex'
  92. def swaptxcreate1(self):
  93. t = self._swaptxcreate(['RUNE', '8.765', 'BTC'])
  94. t.expect('OK? (Y/n): ', 'y')
  95. return self._swaptxcreate_ui_common(t, inputs='3')
  96. def swaptxsign1(self):
  97. return self._swaptxsign()
  98. def swaptxsend1(self):
  99. return self._swaptxsend(add_opts=[f'--proxy=localhost:{TestProxy.port}'])
  100. def swaptxstatus1(self):
  101. return self._swaptxsend(add_opts=['--verbose', '--status'], status=True)
  102. def swaptxreceipt1(self):
  103. return self._swaptxsend(add_opts=['--receipt'], spawn_only=True)
  104. def swaptxhex1(self):
  105. t = self._swaptxsend(add_opts=[f'--dump-hex={self.txhex_file}'], dump_hex=True)
  106. t.read()
  107. txhex = get_data_from_file(self.cfg, self.txhex_file, silent=True)
  108. assert md5(txhex.encode()).hexdigest()[:8] == self.txhex_chksum
  109. return t