thornode.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.httpd.thornode: Thornode WSGI http server
  12. """
  13. import time, re, json
  14. from mmgen.cfg import Config
  15. from . import HTTPD
  16. cfg = Config()
  17. # https://thornode.ninerealms.com/thorchain/quote/swap?from_asset=BCH.BCH&to_asset=LTC.LTC&amount=1000000
  18. sample_request = 'GET /thorchain/quote/swap?from_asset=BCH.BCH&to_asset=LTC.LTC&amount=1000000000'
  19. request_pat = r'/thorchain/quote/swap\?from_asset=(\S+)\.(\S+)&to_asset=(\S+)\.(\S+)&amount=(\d+)'
  20. prices = { 'BTC': 97000, 'LTC': 115, 'BCH': 330 }
  21. data_template = {
  22. 'inbound_address': None,
  23. 'inbound_confirmation_blocks': 4,
  24. 'inbound_confirmation_seconds': 2400,
  25. 'outbound_delay_blocks': 5,
  26. 'outbound_delay_seconds': 30,
  27. 'fees': {
  28. 'asset': 'LTC.LTC',
  29. 'affiliate': '0',
  30. 'outbound': '878656',
  31. 'liquidity': '8945012',
  32. 'total': '9823668',
  33. 'slippage_bps': 31,
  34. 'total_bps': 34
  35. },
  36. 'expiry': None,
  37. 'warning': 'Do not cache this response. Do not send funds after the expiry.',
  38. 'notes': 'First output should be to inbound_address, second output should be change back to self, third output should be OP_RETURN, limited to 80 bytes. Do not send below the dust threshold. Do not use exotic spend scripts, locks or address formats.',
  39. 'dust_threshold': '10000',
  40. 'recommended_min_amount_in': '1222064',
  41. 'recommended_gas_rate': '6',
  42. 'gas_rate_units': 'satsperbyte',
  43. 'expected_amount_out': None,
  44. 'max_streaming_quantity': 0,
  45. 'streaming_swap_blocks': 0,
  46. 'total_swap_seconds': 2430
  47. }
  48. def make_inbound_addr(proto, mmtype):
  49. from mmgen.tool.coin import tool_cmd
  50. n = int(time.time()) // (60 * 60 * 24) # increments once every 24 hrs
  51. return tool_cmd(
  52. cfg = cfg,
  53. cmdname = 'pubhash2addr',
  54. proto = proto,
  55. mmtype = mmtype).pubhash2addr(f'{n:040x}')
  56. class ThornodeServer(HTTPD):
  57. name = 'thornode server'
  58. port = 18800
  59. content_type = 'application/json'
  60. def make_response_body(self, method, environ):
  61. from wsgiref.util import request_uri
  62. m = re.search(request_pat, request_uri(environ))
  63. _, send_coin, _, recv_coin, amt_atomic = m.groups()
  64. from mmgen.protocol import init_proto
  65. send_proto = init_proto(cfg, send_coin, network='regtest', need_amt=True)
  66. in_amt = send_proto.coin_amt(int(amt_atomic), from_unit='satoshi')
  67. out_amt = in_amt * (prices[send_coin] / prices[recv_coin])
  68. addr = make_inbound_addr(send_proto, send_proto.preferred_mmtypes[0])
  69. data = data_template | {
  70. 'expected_amount_out': str(out_amt.to_unit('satoshi')),
  71. 'expiry': int(time.time()) + (10 * 60),
  72. 'inbound_address': addr,
  73. }
  74. return json.dumps(data).encode()