ethswap.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.ethswap: Ethereum swap tests for the cmdtest.py test suite
  12. """
  13. from mmgen.cfg import Config
  14. from mmgen.protocol import init_proto
  15. from .include.runner import CmdTestRunner
  16. from .include.common import dfl_sid
  17. from .httpd.thornode import ThornodeServer
  18. from .regtest import CmdTestRegtest
  19. from .swap import CmdTestSwapMethods
  20. from .ethdev import CmdTestEthdev
  21. thornode_server = ThornodeServer()
  22. method_template = """
  23. def {name}(self):
  24. self.spawn(log_only=True)
  25. return ethswap_eth.run_test("{eth_name}", sub=True)
  26. """
  27. class CmdTestEthSwapMethods:
  28. async def token_deploy_a(self):
  29. return await self._token_deploy_math(num=1, get_receipt=False)
  30. async def token_deploy_b(self):
  31. return await self._token_deploy_owned(num=1, get_receipt=False)
  32. async def token_deploy_c(self):
  33. return await self._token_deploy_token(num=1, get_receipt=False)
  34. def token_fund_user(self):
  35. return self._token_transfer_ops(
  36. op = 'fund_user',
  37. mm_idxs = [1],
  38. amt = self.token_fund_amt,
  39. get_receipt = False)
  40. def token_addrgen(self):
  41. return self._token_addrgen(mm_idxs=[1], naddrs=5)
  42. def token_addrimport(self):
  43. return self._token_addrimport('token_addr1', '1-5', expect='5/5')
  44. def token_bal1(self):
  45. return self._token_bal_check(pat=rf'{dfl_sid}:E:1\s+{self.token_fund_amt}\s')
  46. class CmdTestEthSwap(CmdTestSwapMethods, CmdTestRegtest):
  47. 'Ethereum swap operations'
  48. bdb_wallet = True
  49. tmpdir_nums = [47]
  50. networks = ('btc',)
  51. passthru_opts = ('coin', 'rpc_backend', 'eth_daemon_id')
  52. eth_group = 'ethswap_eth'
  53. cmd_group_in = (
  54. ('setup', 'regtest (Bob and Alice) mode setup'),
  55. ('eth_setup', 'Ethereum devnet setup'),
  56. ('subgroup.init', []),
  57. ('subgroup.fund', ['init']),
  58. ('subgroup.eth_init', []),
  59. ('subgroup.eth_fund', ['eth_init']),
  60. ('subgroup.swap', ['fund', 'eth_fund']),
  61. ('subgroup.eth_swap', ['fund', 'eth_fund']),
  62. ('stop', 'stopping regtest daemon'),
  63. ('eth_stop', 'stopping Ethereum daemon'),
  64. ('thornode_server_stop', 'stopping the Thornode server'),
  65. )
  66. cmd_subgroups = {
  67. 'init': (
  68. 'creating Bob’s MMGen wallet and tracking wallet',
  69. ('walletconv_bob', 'wallet creation (Bob)'),
  70. ('addrgen_bob', 'address generation (Bob)'),
  71. ('addrimport_bob', 'importing Bob’s addresses'),
  72. ),
  73. 'fund': (
  74. 'funding Bob’s wallet',
  75. ('bob_import_miner_addr', 'importing miner’s coinbase addr into Bob’s wallet'),
  76. ('fund_bob', 'funding Bob’s wallet'),
  77. ('generate', 'mining a block'),
  78. ('bob_bal1', 'Bob’s balance'),
  79. ),
  80. 'eth_init': (
  81. 'initializing the ETH tracking wallet',
  82. ('eth_addrgen', ''),
  83. ('eth_addrimport', ''),
  84. ('eth_addrimport_devaddr', ''),
  85. ('eth_fund_devaddr', ''),
  86. ),
  87. 'eth_fund': (
  88. 'funding the ETH tracking wallet',
  89. ('eth_fund_mmgen_addr1', ''),
  90. ('eth_fund_mmgen_addr2', ''),
  91. ('eth_bal1', ''),
  92. ),
  93. 'swap': (
  94. 'swap operations (BTC -> ETH)',
  95. ('swaptxcreate1', 'creating a BTC->ETH swap transaction'),
  96. ('swaptxcreate2', 'creating a BTC->ETH swap transaction (used account)'),
  97. ('swaptxsign1', 'signing the swap transaction'),
  98. ('swaptxsend1', 'sending the swap transaction'),
  99. ('swaptxbump1', 'bumping the swap transaction'),
  100. ('swaptxsign2', 'signing the bump transaction'),
  101. ('swaptxsend2', 'sending the bump transaction'),
  102. ('generate', 'generating a block'),
  103. ('bob_bal2', 'Bob’s balance'),
  104. ('swaptxdo1', 'creating, signing and sending a swap transaction'),
  105. ('generate', 'generating a block'),
  106. ('bob_bal3', 'Bob’s balance'),
  107. ),
  108. 'eth_swap': (
  109. 'swap operations (ETH -> BTC)',
  110. ('eth_swaptxcreate1', ''),
  111. ('eth_swaptxcreate2', ''),
  112. ('eth_swaptxsign1', ''),
  113. ('eth_swaptxsend1', ''),
  114. ('eth_swaptxstatus1', ''),
  115. ('eth_bal2', ''),
  116. ),
  117. }
  118. eth_tests = [c[0] for v in tuple(cmd_subgroups.values()) + (cmd_group_in,)
  119. for c in v if isinstance(c, tuple) and c[0].startswith('eth_')]
  120. exec(''.join(method_template.format(name=k, eth_name=k.removeprefix('eth_')) for k in eth_tests))
  121. def __init__(self, cfg, trunner, cfgs, spawn):
  122. super().__init__(cfg, trunner, cfgs, spawn)
  123. if not trunner:
  124. return
  125. global ethswap_eth
  126. cfg = Config({
  127. '_clone': trunner.cfg,
  128. 'coin': 'eth',
  129. 'eth_daemon_id': trunner.cfg.eth_daemon_id,
  130. 'resume': None,
  131. 'resume_after': None,
  132. 'exit_after': None,
  133. 'log': None})
  134. t = trunner
  135. ethswap_eth = CmdTestRunner(cfg, t.repo_root, t.data_dir, t.trash_dir, t.trash_dir2)
  136. ethswap_eth.init_group(self.eth_group)
  137. thornode_server.start()
  138. def swaptxcreate1(self):
  139. t = self._swaptxcreate(['BTC', '8.765', 'ETH'])
  140. t.expect('OK? (Y/n): ', 'y')
  141. t.expect(':E:2')
  142. t.expect('OK? (Y/n): ', 'y')
  143. return self._swaptxcreate_ui_common(t)
  144. def swaptxcreate2(self):
  145. t = self._swaptxcreate(['BTC', '8.765', 'ETH', f'{dfl_sid}:E:1'])
  146. t.expect('OK? (Y/n): ', 'y')
  147. return self._swaptxcreate_ui_common(t)
  148. def swaptxsign1(self):
  149. return self._swaptxsign()
  150. def swaptxsign2(self):
  151. return self._swaptxsign()
  152. def swaptxsend1(self):
  153. return self._swaptxsend()
  154. def swaptxsend2(self):
  155. return self._swaptxsend()
  156. def swaptxbump1(self): # create one-output TX back to self to rescue funds
  157. return self._swaptxbump('40s', output_args=[f'{dfl_sid}:B:1'])
  158. def swaptxdo1(self):
  159. return self._swaptxcreate_ui_common(
  160. self._swaptxcreate(
  161. ['BTC', '0.223344', f'{dfl_sid}:B:3', 'ETH', f'{dfl_sid}:E:2'],
  162. action = 'txdo'),
  163. sign_and_send = True,
  164. file_desc = 'Sent transaction')
  165. def bob_bal2(self):
  166. return self._user_bal_cli('bob', chk='499.9999252')
  167. def bob_bal3(self):
  168. return self._user_bal_cli('bob', chk='499.77656902')
  169. def thornode_server_stop(self):
  170. self.spawn(msg_only=True)
  171. thornode_server.stop()
  172. return 'ok'
  173. class CmdTestEthSwapEth(CmdTestEthSwapMethods, CmdTestSwapMethods, CmdTestEthdev):
  174. 'Ethereum swap operations - Ethereum wallet'
  175. networks = ('eth',)
  176. tmpdir_nums = [48]
  177. fund_amt = '123.456'
  178. token_fund_amt = 1000
  179. bals = lambda self, k: {
  180. 'swap1': [('98831F3A:E:1', '123.456')],
  181. 'swap2': [('98831F3A:E:1', '114.690978056')],
  182. }[k]
  183. cmd_group_in = CmdTestEthdev.cmd_group_in + (
  184. ('fund_mmgen_addr1', 'funding user address :1)'),
  185. ('fund_mmgen_addr2', 'funding user address :11)'),
  186. ('swaptxcreate1', 'creating an ETH->BTC swap transaction'),
  187. ('swaptxcreate2', 'creating an ETH->BTC swap transaction (specific address, trade limit)'),
  188. ('swaptxsign1', 'signing the transaction'),
  189. ('swaptxsend1', 'sending the transaction'),
  190. ('swaptxstatus1', 'getting the transaction status (with --verbose)'),
  191. ('bal1', 'the ETH balance'),
  192. ('bal2', 'the ETH balance'),
  193. )
  194. def swaptxcreate1(self):
  195. t = self._swaptxcreate(['ETH', '8.765', 'BTC'])
  196. t.expect('OK? (Y/n): ', 'y')
  197. return self._swaptxcreate_ui_common(t)
  198. def swaptxcreate2(self):
  199. return self._swaptxcreate_ui_common(
  200. self._swaptxcreate(
  201. ['ETH', '8.765', 'BTC', f'{dfl_sid}:B:4'],
  202. add_opts = ['--trade-limit=3%']),
  203. expect = ':2019e4/1/0')
  204. def swaptxsign1(self):
  205. return self._swaptxsign()
  206. def swaptxsend1(self):
  207. return self._swaptxsend()
  208. def swaptxstatus1(self):
  209. self.mining_delay()
  210. return self._swaptxsend(add_opts=['--verbose', '--status'], status=True)
  211. def bal1(self):
  212. return self.bal('swap1')
  213. def bal2(self):
  214. return self.bal('swap2')