contract.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2025 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. """
  18. Blueprint: contract_bp
  19. This module defines a Flask blueprint (`contract_bp`) for handling contract-related functionality,
  20. serving as a primary location for Flask code related to routes and related features associated with contracts.
  21. """
  22. from flask import request, render_template, Blueprint
  23. from pygments import highlight
  24. from pygments.lexers import RustLexer
  25. from pygments.formatters import HtmlFormatter
  26. import rpc
  27. # Create contract blueprint
  28. contract_bp = Blueprint("contract", __name__)
  29. @contract_bp.route('/contract/<contract_id>')
  30. async def contract_source_list(contract_id):
  31. """
  32. Fetches and displays a list of source files for the specified contract using an RPC
  33. call to the explorer daemon, returning a rendered template with the source code
  34. files associated with the contract.
  35. Args:
  36. contract_id (str): The Contract ID to fetch the source files for.
  37. Query Params:
  38. name (str, optional): The contract name to display alongside the source files.
  39. """
  40. # Obtain the contract name to display with the source
  41. contract_name = request.args.get('name')
  42. # Fetch the source file list associated with the contract
  43. source_paths = await rpc.get_contract_source_paths(contract_id)
  44. # Returned rendered contract source list
  45. return render_template('contract_source_list.html', contract_id=contract_id, source_paths=source_paths, contract_name=contract_name)
  46. @contract_bp.route('/contract/source/<contract_id>/<path:source_path>')
  47. async def contract_source(contract_id, source_path):
  48. """
  49. Fetches and displays the source code for a specific file of a contract using an RPC
  50. call to the explorer daemon, returning a rendered template with syntax-highlighted
  51. source code.
  52. Path Args:
  53. contract_id (str): The Contract ID to fetch the source file for.
  54. source_path (str): The path of the specific source file within the contract.
  55. Query Params:
  56. name (str, optional): The contract name to display alongside the source code.
  57. """
  58. # Obtain the contract name to display with the source
  59. contract_name = request.args.get('name')
  60. # Retrieve the contract source code
  61. raw_source = await rpc.get_contract_source(contract_id, source_path)
  62. # Style the source code
  63. formatter = HtmlFormatter(style='friendly', linenos=True)
  64. source = highlight(raw_source, RustLexer(), formatter)
  65. # Generate css for styled source code
  66. pygments_css = formatter.get_style_defs()
  67. # Returned rendered contract source code page
  68. return render_template(
  69. 'contract_source.html',
  70. source=source,
  71. contract_id=contract_id,
  72. source_path=source_path,
  73. pygments_css=pygments_css,
  74. contract_name=contract_name
  75. )