thornode.py 3.1 KB

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