diff --git a/elfpy/bots/__init__.py b/elfpy/bots/__init__.py index dad96bc9b0..e69de29bb2 100644 --- a/elfpy/bots/__init__.py +++ b/elfpy/bots/__init__.py @@ -1,5 +0,0 @@ -"""Utilities and classes for Agent.0 bots""" - -from .agent_config import AgentConfig -from .budget import Budget -from .environment_config import DEFAULT_USERNAME, EnvironmentConfig diff --git a/elfpy/bots/bot_server.py b/elfpy/bots/bot_server.py deleted file mode 100644 index f2ab59acc2..0000000000 --- a/elfpy/bots/bot_server.py +++ /dev/null @@ -1,73 +0,0 @@ -"""A simple Flask server to run python scripts.""" -import json -import subprocess -import sys -import tempfile - -from flask import Flask, jsonify, request - -app = Flask(__name__) - -# Store the running processes and their corresponding IDs -running_processes: dict[int, subprocess.Popen] = {} - - -@app.route("/run_script", methods=["POST"]) -def run_script(): - """Run a python script and return the script id.""" - # TODO: validate the json - data = request.json - - # Save the JSON payload to a temporary file - with tempfile.NamedTemporaryFile(delete=False) as temp_file: - json_data = json.dumps(data).encode("utf-8") - temp_file.write(json_data) - print(f"Received new bot with specification:\n{json_data}") - temp_file_path = temp_file.name - - # Execute the python script with the provided JSON as an argument - script_id = len(running_processes) + 1 - script_path = app.config["SCRIPT_PATH"] - with subprocess.Popen(["python", script_path, temp_file_path]) as process: - # Store the process in the dictionary - running_processes[script_id] = process - - return jsonify({"id": script_id}), 200 - - -@app.route("/kill_script", methods=["POST"]) -def kill_script(): - """Kill a running python script.""" - if not request.json: - return jsonify({"message": "Invalid request. Please provide a valid ID."}), 400 - try: - script_id = int(request.json["id"]) - except (KeyError, ValueError): - return jsonify({"message": "Invalid request. Please provide a valid ID."}), 400 - - # Check if the script is running - if script_id in running_processes: - process = running_processes[script_id] - process.kill() - del running_processes[script_id] - return jsonify({"message": f"Script with ID {script_id} has been killed."}), 200 - - return jsonify({"message": f"Script with ID {script_id} is not running."}), 404 - - -@app.route("/list_processes", methods=["GET"]) -def list_processes(): - """List all running python scripts.""" - process_list = [{"id": script_id, "status": process.poll()} for script_id, process in running_processes.items()] - return jsonify({"processes": process_list}), 200 - - -if __name__ == "__main__": - if len(sys.argv) != 2: - print("Usage: python app.py ") - sys.exit(1) - - SCRIPT_PATH = sys.argv[1] - app.config["SCRIPT_PATH"] = SCRIPT_PATH - - app.run(host="0.0.0.0", port=5001) diff --git a/elfpy/bots/checkpoint_bot.py b/elfpy/bots/checkpoint_bot.py index 32d15e2ab3..2f796ea1ee 100644 --- a/elfpy/bots/checkpoint_bot.py +++ b/elfpy/bots/checkpoint_bot.py @@ -11,12 +11,12 @@ from web3.contract.contract import Contract from elfpy import eth -from elfpy.bots.environment_config import EnvironmentConfig from elfpy.eth.accounts.eth_account import EthAgent from elfpy.eth.rpc_interface import set_anvil_account_balance from elfpy.eth.transactions import smart_contract_read, smart_contract_transact from elfpy.utils import logs from eth_bots import hyperdrive_interface +from eth_bots.core import EnvironmentConfig # The portion of the checkpoint that the bot will wait before attempting to # mint a new checkpoint. diff --git a/elfpy/bots/create_default_profile.py b/elfpy/bots/create_default_profile.py deleted file mode 100644 index 5419c3acbf..0000000000 --- a/elfpy/bots/create_default_profile.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Example script for writing a custom bot config to json""" -from elfpy.bots import DEFAULT_USERNAME, EnvironmentConfig - -if __name__ == "__main__": - # load the config file, which loads defaults - config = EnvironmentConfig() - - # Add username here if adjusting configurations in this script - config.username = DEFAULT_USERNAME - - # Write config to json - config.save_as_json("bots_config.default.json") diff --git a/elfpy/bots/sim_server.py b/elfpy/bots/sim_server.py deleted file mode 100644 index e698daa004..0000000000 --- a/elfpy/bots/sim_server.py +++ /dev/null @@ -1,82 +0,0 @@ -"""A simple Flask server to run python scripts.""" -import json -import os -import subprocess -from dataclasses import asdict, dataclass - -from flask import Flask, jsonify - -app = Flask(__name__) - - -# TODO: add container ids or some namespace so we can kill container groups -@dataclass -class AppInfo: - """App Info""" - - # TODO: don't use hard-coded values, pull these from constants - eth_port: int = 8545 - artifact_port: int = 80 - ui_port: int = 5173 - botserver_port: int = 5001 - - -# Store the running processes and their corresponding IDs -running_processes: dict[int, AppInfo] = {} - - -# TODO: Instead of exposing ports, add subroutes that let's us run commands. -# @app.route("/run/{app_id}/{container_id}", methods=["POST"]) -# def run_command(): -# """Run a command""" - - -@app.route("/create_app", methods=["POST"]) -def create_app(): - """Run a python script and return the script id.""" - # TODO: allow user to upload config json with flags to pass to setup_env.sh - # TODO: expose more config options like 'devnet', 'goerli' etc. - # data = request.json - - # TODO: Set environment variables. This is where we will update ports set for the different apps. - # ETH_PORT=8545-8555 - # ARTIFACT_PORT=80-90 - # UI_PORT=5173 - # BOTSERVER_PORT=5001-5011 - os.environ["VAR1"] = "value1" - os.environ["VAR2"] = "value2" - - # Run the shell script - script_path = "/home/ubuntu/infra/setup_env.sh" - subprocess.call(["bash", script_path]) - subprocess.call("docker compose up -d", shell=True) - - script_id = len(running_processes) + 1 - # TODO: don't use hard-coded values here, let's use the smallest value available. - app_info = AppInfo(eth_port=8545, artifact_port=80, ui_port=5173, botserver_port=5001) - running_processes[script_id] = app_info - data = asdict(app_info) - - # Convert the dictionary to JSON - json_data = json.dumps(data) - return json_data - - -@app.route("/list_apps", methods=["GET"]) -def list_apps(): - """List all running python scripts.""" - process_list = [{"id": script_id, "info": app_info} for script_id, app_info in running_processes.items()] - return jsonify({"processes": process_list}), 200 - - -@app.route("/kill", methods=["POST"]) -def kill_app(): - """kill an app.""" - # TODO: - process_list = [{"id": script_id, "info": app_info} for script_id, app_info in running_processes.items()] - return jsonify({"processes": process_list}), 200 - - -if __name__ == "__main__": - # TODO: don't hard-code port, pull from environment - app.run(host="0.0.0.0", port=8080) diff --git a/elfpy/eth/errors/__init__.py b/elfpy/eth/errors/__init__.py index e69de29bb2..cf0e441fb4 100644 --- a/elfpy/eth/errors/__init__.py +++ b/elfpy/eth/errors/__init__.py @@ -0,0 +1,3 @@ +"""Custom error reporting and contract error parsing.""" +from .errors import decode_error_selector_for_contract +from .types import ABIError, UnknownBlockError diff --git a/elfpy/eth/errors/test_errors.py b/elfpy/eth/errors/test_errors.py index 44cc8a515c..a81544695d 100644 --- a/elfpy/eth/errors/test_errors.py +++ b/elfpy/eth/errors/test_errors.py @@ -1,7 +1,7 @@ """Tests for errors.py""" import pytest -from elfpy.eth.errors.errors import decode_error_selector_for_contract +from elfpy.eth.errors import decode_error_selector_for_contract class TestDecodeErrorSelector: diff --git a/eth_bots/core/__init__.py b/eth_bots/core/__init__.py index e69de29bb2..759a8b3a18 100644 --- a/eth_bots/core/__init__.py +++ b/eth_bots/core/__init__.py @@ -0,0 +1,4 @@ +"""Core functions & utilities for running eth_bots""" +from .agent_config import AgentConfig +from .budget import Budget +from .environment_config import DEFAULT_USERNAME, EnvironmentConfig diff --git a/elfpy/bots/agent_config.py b/eth_bots/core/agent_config.py similarity index 100% rename from elfpy/bots/agent_config.py rename to eth_bots/core/agent_config.py diff --git a/elfpy/bots/budget.py b/eth_bots/core/budget.py similarity index 100% rename from elfpy/bots/budget.py rename to eth_bots/core/budget.py diff --git a/elfpy/bots/environment_config.py b/eth_bots/core/environment_config.py similarity index 100% rename from elfpy/bots/environment_config.py rename to eth_bots/core/environment_config.py diff --git a/eth_bots/core/execute_agent_trades.py b/eth_bots/core/execute_agent_trades.py index 87a13e3e76..e640c406c0 100644 --- a/eth_bots/core/execute_agent_trades.py +++ b/eth_bots/core/execute_agent_trades.py @@ -13,7 +13,7 @@ from elfpy import eth, types from elfpy.eth.accounts import EthAgent -from elfpy.eth.errors.types import UnknownBlockError +from elfpy.eth.errors import UnknownBlockError from elfpy.eth.transactions import smart_contract_preview_transaction from elfpy.markets.hyperdrive import HyperdriveMarket, MarketActionType from elfpy.markets.hyperdrive.hyperdrive_actions import HyperdriveMarketAction diff --git a/eth_bots/core/get_agent_accounts.py b/eth_bots/core/get_agent_accounts.py index 7a3d43f9de..eb78a3a7b8 100644 --- a/eth_bots/core/get_agent_accounts.py +++ b/eth_bots/core/get_agent_accounts.py @@ -14,8 +14,8 @@ from web3.contract.contract import Contract from elfpy import eth -from elfpy.bots import AgentConfig from elfpy.eth.accounts import EthAgent +from eth_bots.core import AgentConfig # pylint: disable=too-many-locals diff --git a/eth_bots/core/setup_experiment.py b/eth_bots/core/setup_experiment.py index 81a8e011f6..1a0ba631bf 100644 --- a/eth_bots/core/setup_experiment.py +++ b/eth_bots/core/setup_experiment.py @@ -10,11 +10,10 @@ from web3.contract.contract import Contract from elfpy import eth -from elfpy.bots import DEFAULT_USERNAME, EnvironmentConfig from elfpy.eth.accounts import EthAgent from elfpy.utils import logs from eth_bots import hyperdrive_interface -from eth_bots.core import crash_report +from eth_bots.core import DEFAULT_USERNAME, EnvironmentConfig, crash_report from eth_bots.core.get_agent_accounts import get_agent_accounts from eth_bots.eth_bots_config import get_eth_bots_config diff --git a/eth_bots/eth_bots_config.py b/eth_bots/eth_bots_config.py index 5e72ec2541..bf8aab5939 100644 --- a/eth_bots/eth_bots_config.py +++ b/eth_bots/eth_bots_config.py @@ -6,7 +6,7 @@ from fixedpointmath import FixedPoint from elfpy.agents.policies import Policies -from elfpy.bots import AgentConfig, Budget, EnvironmentConfig +from eth_bots.core import AgentConfig, Budget, EnvironmentConfig # You can import custom policies here. For example: from eth_bots.custom_policies.example_custom_policy import ExampleCustomPolicy