thornode.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_btc = {
  25. 'inbound_confirmation_blocks': 4,
  26. 'inbound_confirmation_seconds': 2400,
  27. 'outbound_delay_blocks': 5,
  28. 'outbound_delay_seconds': 30,
  29. 'fees': {
  30. 'asset': 'LTC.LTC',
  31. 'affiliate': '0',
  32. 'outbound': '878656',
  33. 'liquidity': '8945012',
  34. 'total': '9823668',
  35. 'slippage_bps': 31,
  36. 'total_bps': 34
  37. },
  38. 'warning': 'Do not cache this response. Do not send funds after the expiry.',
  39. '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.',
  40. 'dust_threshold': '10000',
  41. 'max_streaming_quantity': 0,
  42. 'streaming_swap_blocks': 0,
  43. 'total_swap_seconds': 2430
  44. }
  45. data_template_eth = {
  46. 'inbound_confirmation_blocks': 2,
  47. 'inbound_confirmation_seconds': 24,
  48. 'outbound_delay_blocks': 0,
  49. 'outbound_delay_seconds': 0,
  50. 'fees': {
  51. 'asset': 'BTC.BTC',
  52. 'affiliate': '0',
  53. 'outbound': '1097',
  54. 'liquidity': '77',
  55. 'total': '1174',
  56. 'slippage_bps': 15,
  57. 'total_bps': 237
  58. },
  59. 'router': '0xD37BbE5744D730a1d98d8DC97c42F0Ca46aD7146',
  60. 'warning': 'Do not cache this response. Do not send funds after the expiry.',
  61. 'notes': 'Base Asset: Send the inbound_address the asset with the memo encoded in hex in the data field. Tokens: First approve router to spend tokens from user: asset.approve(router, amount). Then call router.depositWithExpiry(inbound_address, asset, amount, memo, expiry). Asset is the token contract address. Amount should be in native asset decimals (eg 1e18 for most tokens). Do not swap to smart contract addresses.',
  62. 'recommended_gas_rate': '1',
  63. 'max_streaming_quantity': 0,
  64. 'streaming_swap_blocks': 0,
  65. 'total_swap_seconds': 24
  66. }
  67. def make_inbound_addr(proto, mmtype):
  68. from mmgen.tool.coin import tool_cmd
  69. n = int(time.time()) // (60 * 60 * 24) # increments once every 24 hrs
  70. ret = tool_cmd(
  71. cfg = cfg,
  72. cmdname = 'pubhash2addr',
  73. proto = proto,
  74. mmtype = mmtype).pubhash2addr(f'{n:040x}')
  75. return '0x' + ret if proto.is_evm else ret
  76. class ThornodeServer(HTTPD):
  77. name = 'thornode server'
  78. port = 18800
  79. content_type = 'application/json'
  80. def make_response_body(self, method, environ):
  81. from wsgiref.util import request_uri
  82. m = re.search(request_pat, request_uri(environ))
  83. send_chain, send_asset, recv_chain, recv_asset, amt_atomic = m.groups()
  84. in_amt = UniAmt(int(amt_atomic), from_unit='satoshi')
  85. out_amt = in_amt * (prices[send_asset] / prices[recv_asset])
  86. data_template = (
  87. data_template_eth if send_asset == 'ETH' else
  88. data_template_btc)
  89. from mmgen.protocol import init_proto
  90. send_proto = init_proto(cfg, send_chain, network='regtest', need_amt=True)
  91. data = data_template | {
  92. 'recommended_min_amount_in': str(int(70 * 10**8 / prices[send_asset])), # $70
  93. 'expected_amount_out': str(out_amt.to_unit('satoshi')),
  94. 'expiry': int(time.time()) + (10 * 60),
  95. 'inbound_address': make_inbound_addr(send_proto, send_proto.preferred_mmtypes[0]),
  96. 'gas_rate_units': gas_rate_units[send_proto.base_proto_coin],
  97. 'recommended_gas_rate': recommended_gas_rate[send_proto.base_proto_coin],
  98. }
  99. return json.dumps(data).encode()