From d7eba069720ed2d453d3c09858430c8fd54e14a6 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 09:44:57 +0100 Subject: [PATCH 01/30] Add Github Acions --- .github/workflows/python_ci.yaml | 33 ++++++++++++++ poetry.lock | 77 +++++++++++++++++++++++++++++++- pyproject.toml | 1 + 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/python_ci.yaml diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml new file mode 100644 index 00000000..85327903 --- /dev/null +++ b/.github/workflows/python_ci.yaml @@ -0,0 +1,33 @@ +name: Python CI + +on: + pull_request: + push: + branches: [main] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Set up Python 3.9 + uses: actions/setup-python@v2 + with: + python-version: 3.9 + + - name: Install Poetry + run: curl -sSL https://install.python-poetry.org | python3 - + + - name: Install dependencies + run: poetry install + + - name: Run mypy + run: poetry run mypy + + - name: Run pytest + run: poetry run pytest + + - name: Check with black + run: poetry run black --check . diff --git a/poetry.lock b/poetry.lock index e2a0c3fd..60ad0c3c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -330,6 +330,53 @@ files = [ {file = "bitarray-2.9.2.tar.gz", hash = "sha256:a8f286a51a32323715d77755ed959f94bef13972e9a2fe71b609e40e6d27957e"}, ] +[[package]] +name = "black" +version = "23.12.1" +description = "The uncompromising code formatter." +category = "main" +optional = false +python-versions = ">=3.8" +files = [ + {file = "black-23.12.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:e0aaf6041986767a5e0ce663c7a2f0e9eaf21e6ff87a5f95cbf3675bfd4c41d2"}, + {file = "black-23.12.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:c88b3711d12905b74206227109272673edce0cb29f27e1385f33b0163c414bba"}, + {file = "black-23.12.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a920b569dc6b3472513ba6ddea21f440d4b4c699494d2e972a1753cdc25df7b0"}, + {file = "black-23.12.1-cp310-cp310-win_amd64.whl", hash = "sha256:3fa4be75ef2a6b96ea8d92b1587dd8cb3a35c7e3d51f0738ced0781c3aa3a5a3"}, + {file = "black-23.12.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:8d4df77958a622f9b5a4c96edb4b8c0034f8434032ab11077ec6c56ae9f384ba"}, + {file = "black-23.12.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:602cfb1196dc692424c70b6507593a2b29aac0547c1be9a1d1365f0d964c353b"}, + {file = "black-23.12.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9c4352800f14be5b4864016882cdba10755bd50805c95f728011bcb47a4afd59"}, + {file = "black-23.12.1-cp311-cp311-win_amd64.whl", hash = "sha256:0808494f2b2df923ffc5723ed3c7b096bd76341f6213989759287611e9837d50"}, + {file = "black-23.12.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:25e57fd232a6d6ff3f4478a6fd0580838e47c93c83eaf1ccc92d4faf27112c4e"}, + {file = "black-23.12.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:2d9e13db441c509a3763a7a3d9a49ccc1b4e974a47be4e08ade2a228876500ec"}, + {file = "black-23.12.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6d1bd9c210f8b109b1762ec9fd36592fdd528485aadb3f5849b2740ef17e674e"}, + {file = "black-23.12.1-cp312-cp312-win_amd64.whl", hash = "sha256:ae76c22bde5cbb6bfd211ec343ded2163bba7883c7bc77f6b756a1049436fbb9"}, + {file = "black-23.12.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:1fa88a0f74e50e4487477bc0bb900c6781dbddfdfa32691e780bf854c3b4a47f"}, + {file = "black-23.12.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:a4d6a9668e45ad99d2f8ec70d5c8c04ef4f32f648ef39048d010b0689832ec6d"}, + {file = "black-23.12.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b18fb2ae6c4bb63eebe5be6bd869ba2f14fd0259bda7d18a46b764d8fb86298a"}, + {file = "black-23.12.1-cp38-cp38-win_amd64.whl", hash = "sha256:c04b6d9d20e9c13f43eee8ea87d44156b8505ca8a3c878773f68b4e4812a421e"}, + {file = "black-23.12.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:3e1b38b3135fd4c025c28c55ddfc236b05af657828a8a6abe5deec419a0b7055"}, + {file = "black-23.12.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:4f0031eaa7b921db76decd73636ef3a12c942ed367d8c3841a0739412b260a54"}, + {file = "black-23.12.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:97e56155c6b737854e60a9ab1c598ff2533d57e7506d97af5481141671abf3ea"}, + {file = "black-23.12.1-cp39-cp39-win_amd64.whl", hash = "sha256:dd15245c8b68fe2b6bd0f32c1556509d11bb33aec9b5d0866dd8e2ed3dba09c2"}, + {file = "black-23.12.1-py3-none-any.whl", hash = "sha256:78baad24af0f033958cad29731e27363183e140962595def56423e626f4bee3e"}, + {file = "black-23.12.1.tar.gz", hash = "sha256:4ce3ef14ebe8d9509188014d96af1c456a910d5b5cbf434a09fef7e024b3d0d5"}, +] + +[package.dependencies] +click = ">=8.0.0" +mypy-extensions = ">=0.4.3" +packaging = ">=22.0" +pathspec = ">=0.9.0" +platformdirs = ">=2" +tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} +typing-extensions = {version = ">=4.0.1", markers = "python_version < \"3.11\""} + +[package.extras] +colorama = ["colorama (>=0.4.3)"] +d = ["aiohttp (>=3.7.4)", "aiohttp (>=3.7.4,!=3.9.0)"] +jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] +uvloop = ["uvloop (>=0.15.2)"] + [[package]] name = "certifi" version = "2023.11.17" @@ -2180,6 +2227,34 @@ files = [ [package.dependencies] regex = ">=2022.3.15" +[[package]] +name = "pathspec" +version = "0.12.1" +description = "Utility library for gitignore style pattern matching of file paths." +category = "main" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08"}, + {file = "pathspec-0.12.1.tar.gz", hash = "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712"}, +] + +[[package]] +name = "platformdirs" +version = "4.1.0" +description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." +category = "main" +optional = false +python-versions = ">=3.8" +files = [ + {file = "platformdirs-4.1.0-py3-none-any.whl", hash = "sha256:11c8f37bcca40db96d8144522d925583bdb7a31f7b0e37e3ed4318400a8e2380"}, + {file = "platformdirs-4.1.0.tar.gz", hash = "sha256:906d548203468492d432bcb294d4bc2fff751bf84971fbb2c10918cc206ee420"}, +] + +[package.extras] +docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.1)", "sphinx-autodoc-typehints (>=1.24)"] +test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4)", "pytest-cov (>=4.1)", "pytest-mock (>=3.11.1)"] + [[package]] name = "pluggy" version = "1.3.0" @@ -3562,4 +3637,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.10" -content-hash = "506edc1c6c31c736b7d2db7053512d63106409eaa386fa9b6eacce40b915367d" +content-hash = "b9181ce05277d770342667c4f5459e987ca4fe19dbf07af07a93efd30284c9a2" diff --git a/pyproject.toml b/pyproject.toml index 19546216..07e6b6c0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ typer = "^0.9.0" web3 = "^6.14.0" mypy = "^1.8.0" types-requests = "^2.31.0.20240106" +black = "^23.12.1" [build-system] requires = ["poetry-core"] From a24af59c4f935413d468a097c8ecd1b35d69c293 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 09:49:18 +0100 Subject: [PATCH 02/30] Run in paralel --- .github/workflows/python_ci.yaml | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 85327903..f85f39f7 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -6,28 +6,35 @@ on: branches: [main] jobs: - build: + setup: runs-on: ubuntu-latest - steps: - uses: actions/checkout@v2 - - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - - name: Install Poetry run: curl -sSL https://install.python-poetry.org | python3 - - - name: Install dependencies run: poetry install + mypy: + needs: setup + runs-on: ubuntu-latest + steps: - name: Run mypy run: poetry run mypy + pytest: + needs: setup + runs-on: ubuntu-latest + steps: - name: Run pytest run: poetry run pytest - + black: + needs: setup + runs-on: ubuntu-latest + steps: - name: Check with black run: poetry run black --check . From 338a9fdb56833ca370e7ed9d6abc57b8c5f509ec Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 10:06:55 +0100 Subject: [PATCH 03/30] Test --- .github/workflows/actions/python_prepare.yaml | 13 +++++++++++++ .github/workflows/python_ci.yaml | 19 ++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/actions/python_prepare.yaml diff --git a/.github/workflows/actions/python_prepare.yaml b/.github/workflows/actions/python_prepare.yaml new file mode 100644 index 00000000..12c7d414 --- /dev/null +++ b/.github/workflows/actions/python_prepare.yaml @@ -0,0 +1,13 @@ +name: "Prepare Python environment" +runs: + using: "composite" + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.9 + uses: actions/setup-python@v2 + with: + python-version: 3.9 + - name: Install Poetry + run: curl -sSL https://install.python-poetry.org | python3 - + - name: Install dependencies + run: poetry install diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index f85f39f7..8156f663 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -6,23 +6,12 @@ on: branches: [main] jobs: - setup: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Set up Python 3.9 - uses: actions/setup-python@v2 - with: - python-version: 3.9 - - name: Install Poetry - run: curl -sSL https://install.python-poetry.org | python3 - - - name: Install dependencies - run: poetry install - mypy: needs: setup runs-on: ubuntu-latest steps: + - name: Prepare Python environment + uses: actions/python_prepare@v1 - name: Run mypy run: poetry run mypy @@ -30,11 +19,15 @@ jobs: needs: setup runs-on: ubuntu-latest steps: + - name: Prepare Python environment + uses: actions/python_prepare@v1 - name: Run pytest run: poetry run pytest black: needs: setup runs-on: ubuntu-latest steps: + - name: Prepare Python environment + uses: actions/python_prepare@v1 - name: Check with black run: poetry run black --check . From c6450d907bfbf6ec87792782f1851dbceacd644e Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 10:07:36 +0100 Subject: [PATCH 04/30] fix --- .github/workflows/python_ci.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 8156f663..4e71da1e 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -7,7 +7,6 @@ on: jobs: mypy: - needs: setup runs-on: ubuntu-latest steps: - name: Prepare Python environment @@ -16,7 +15,6 @@ jobs: run: poetry run mypy pytest: - needs: setup runs-on: ubuntu-latest steps: - name: Prepare Python environment @@ -24,7 +22,6 @@ jobs: - name: Run pytest run: poetry run pytest black: - needs: setup runs-on: ubuntu-latest steps: - name: Prepare Python environment From 3f3606a26c809f77e3ea9f2f7a757b7703df6d25 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 10:13:16 +0100 Subject: [PATCH 05/30] maybe now --- .../python_prepare/action.yaml} | 1 + .github/workflows/python_ci.yaml | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) rename .github/{workflows/actions/python_prepare.yaml => actions/python_prepare/action.yaml} (87%) diff --git a/.github/workflows/actions/python_prepare.yaml b/.github/actions/python_prepare/action.yaml similarity index 87% rename from .github/workflows/actions/python_prepare.yaml rename to .github/actions/python_prepare/action.yaml index 12c7d414..2d3adb5f 100644 --- a/.github/workflows/actions/python_prepare.yaml +++ b/.github/actions/python_prepare/action.yaml @@ -1,4 +1,5 @@ name: "Prepare Python environment" +description: "Set up Python and install dependencies" runs: using: "composite" steps: diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 4e71da1e..c4e69ede 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Prepare Python environment - uses: actions/python_prepare@v1 + - uses: ./.github/actions/python_prepare - name: Run mypy run: poetry run mypy @@ -18,13 +18,13 @@ jobs: runs-on: ubuntu-latest steps: - name: Prepare Python environment - uses: actions/python_prepare@v1 + - uses: ./.github/actions/python_prepare - name: Run pytest run: poetry run pytest black: runs-on: ubuntu-latest steps: - name: Prepare Python environment - uses: actions/python_prepare@v1 + - uses: ./.github/actions/python_prepare - name: Check with black run: poetry run black --check . From 14ab8a3e7516860bd8055074a134d1a6b51f8d96 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 10:13:48 +0100 Subject: [PATCH 06/30] now? --- .github/workflows/python_ci.yaml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index c4e69ede..2efa4a35 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -9,22 +9,19 @@ jobs: mypy: runs-on: ubuntu-latest steps: - - name: Prepare Python environment - - uses: ./.github/actions/python_prepare + - uses: ./.github/actions/python_prepare - name: Run mypy run: poetry run mypy pytest: runs-on: ubuntu-latest steps: - - name: Prepare Python environment - - uses: ./.github/actions/python_prepare + - uses: ./.github/actions/python_prepare - name: Run pytest run: poetry run pytest black: runs-on: ubuntu-latest steps: - - name: Prepare Python environment - - uses: ./.github/actions/python_prepare + - uses: ./.github/actions/python_prepare - name: Check with black run: poetry run black --check . From f896c665afcdcc3511d464e78d59477211dfad3f Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 10:16:15 +0100 Subject: [PATCH 07/30] now? --- .github/actions/python_prepare/action.yaml | 1 - .github/workflows/python_ci.yaml | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/actions/python_prepare/action.yaml b/.github/actions/python_prepare/action.yaml index 2d3adb5f..b57fb82c 100644 --- a/.github/actions/python_prepare/action.yaml +++ b/.github/actions/python_prepare/action.yaml @@ -3,7 +3,6 @@ description: "Set up Python and install dependencies" runs: using: "composite" steps: - - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 2efa4a35..4fa0a88c 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -9,6 +9,7 @@ jobs: mypy: runs-on: ubuntu-latest steps: + - uses: actions/checkout@v2 - uses: ./.github/actions/python_prepare - name: Run mypy run: poetry run mypy @@ -16,12 +17,14 @@ jobs: pytest: runs-on: ubuntu-latest steps: + - uses: actions/checkout@v2 - uses: ./.github/actions/python_prepare - name: Run pytest run: poetry run pytest black: runs-on: ubuntu-latest steps: + - uses: actions/checkout@v2 - uses: ./.github/actions/python_prepare - name: Check with black run: poetry run black --check . From 2df8734a5a086a39103cc9caed2f91acd9b9dc27 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 10:18:42 +0100 Subject: [PATCH 08/30] add shell --- .github/actions/python_prepare/action.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/actions/python_prepare/action.yaml b/.github/actions/python_prepare/action.yaml index b57fb82c..b0091617 100644 --- a/.github/actions/python_prepare/action.yaml +++ b/.github/actions/python_prepare/action.yaml @@ -8,6 +8,8 @@ runs: with: python-version: 3.9 - name: Install Poetry + shell: bash run: curl -sSL https://install.python-poetry.org | python3 - - name: Install dependencies + shell: bash run: poetry install From 0344cbb9f29097e95ac933a6cfe55d0015fb59cd Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 10:30:46 +0100 Subject: [PATCH 09/30] maybe fix --- prediction_market_agent/tools/google_search.py | 2 +- tests/test_manifold.py | 2 ++ tests/test_tools.py | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/prediction_market_agent/tools/google_search.py b/prediction_market_agent/tools/google_search.py index 9fa718c5..a954bfb4 100644 --- a/prediction_market_agent/tools/google_search.py +++ b/prediction_market_agent/tools/google_search.py @@ -5,7 +5,7 @@ def google_search(query: str) -> list[str]: - params = {"q": query, "api_key": utils.get_keys().serp, "num": 4} + params = {"q": query, "api_key": utils.get_serp_api_key(), "num": 4} search = serpapi.GoogleSearch(params) urls = [result["link"] for result in search.get_dict()["organic_results"]] return urls diff --git a/tests/test_manifold.py b/tests/test_manifold.py index d31dec84..9f07a79a 100644 --- a/tests/test_manifold.py +++ b/tests/test_manifold.py @@ -1,8 +1,10 @@ +import pytest from prediction_market_agent import utils from prediction_market_agent.markets import manifold from prediction_market_agent.tools.gtypes import mana_type +@pytest.mark.skipif(utils.get_manifold_api_key() is None, reason="No Manifold API key") def test_manifold() -> None: market = manifold.pick_binary_market() print(market.question) diff --git a/tests/test_tools.py b/tests/test_tools.py index 33c4ac9c..fd33fcab 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -1,7 +1,10 @@ +import pytest +from prediction_market_agent import utils from prediction_market_agent.tools.google_search import GoogleSearchTool from prediction_market_agent.tools.web_scrape import _summary, web_scrape +@pytest.mark.skipif(utils.get_serp_api_key() is None, reason="No Serp API key") def test_search_and_scrape() -> None: objective = "In 2024, will Israel conduct a military strike against Iran?" search = GoogleSearchTool().fn(query=objective) From 11cb5d0961e17930247d212bce3f0e989473b9b8 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 11:09:16 +0100 Subject: [PATCH 10/30] fix? --- prediction_market_agent/markets/manifold.py | 5 ++++- prediction_market_agent/utils.py | 22 ++++++++++----------- tests/test_manifold.py | 5 ++++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/prediction_market_agent/markets/manifold.py b/prediction_market_agent/markets/manifold.py index 278eda7f..eb44942e 100644 --- a/prediction_market_agent/markets/manifold.py +++ b/prediction_market_agent/markets/manifold.py @@ -2,6 +2,7 @@ import typing as t from prediction_market_agent import utils from prediction_market_agent.tools.gtypes import Mana, mana_type +from prediction_market_agent.tools.utils import check_not_none from prediction_market_agent.data_models.market_data_models import ManifoldMarket """ @@ -74,4 +75,6 @@ def place_bet(amount: Mana, market_id: str, outcome: bool, api_key: str) -> None market = pick_binary_market() print(market.question) print("Placing bet on market:", market.question) - place_bet(mana_type(2), market.id, True, utils.get_manifold_api_key()) + place_bet( + mana_type(2), market.id, True, check_not_none(utils.get_manifold_api_key()) + ) diff --git a/prediction_market_agent/utils.py b/prediction_market_agent/utils.py index 47dd176b..6962eeb1 100644 --- a/prediction_market_agent/utils.py +++ b/prediction_market_agent/utils.py @@ -10,33 +10,31 @@ ) -def get_api_key(name: str) -> str: +def get_api_key(name: str) -> t.Optional[str]: dotenv.load_dotenv() - key = os.getenv(name) - if not key: - raise Exception(f"No API key found. Please set env var '{name}'.") - return key + return os.getenv(name) -def get_manifold_api_key() -> str: +def get_manifold_api_key() -> t.Optional[str]: return get_api_key("MANIFOLD_API_KEY") -def get_serp_api_key() -> str: +def get_serp_api_key() -> t.Optional[str]: return get_api_key("SERP_API_KEY") -def get_openai_api_key() -> str: +def get_openai_api_key() -> t.Optional[str]: return get_api_key("OPENAI_API_KEY") -def get_bet_from_address() -> ChecksumAddress: +def get_bet_from_address() -> t.Optional[ChecksumAddress]: address = get_api_key("BET_FROM_ADDRESS") - return verify_address(address) + return verify_address(address) if address else None -def get_bet_from_private_key() -> PrivateKey: - return PrivateKey(get_api_key("BET_FROM_PRIVATE_KEY")) +def get_bet_from_private_key() -> t.Optional[PrivateKey]: + private_key = get_api_key("BET_FROM_PRIVATE_KEY") + return PrivateKey(private_key) if private_key else None def verify_address(address: str) -> ChecksumAddress: diff --git a/tests/test_manifold.py b/tests/test_manifold.py index 9f07a79a..27acf56f 100644 --- a/tests/test_manifold.py +++ b/tests/test_manifold.py @@ -2,6 +2,7 @@ from prediction_market_agent import utils from prediction_market_agent.markets import manifold from prediction_market_agent.tools.gtypes import mana_type +from prediction_market_agent.tools.utils import check_not_none @pytest.mark.skipif(utils.get_manifold_api_key() is None, reason="No Manifold API key") @@ -9,4 +10,6 @@ def test_manifold() -> None: market = manifold.pick_binary_market() print(market.question) print("Placing bet on market:", market.question) - manifold.place_bet(mana_type(2), market.id, True, utils.get_manifold_api_key()) + manifold.place_bet( + mana_type(2), market.id, True, check_not_none(utils.get_manifold_api_key()) + ) From 90bed31c0c8359ea30ed998c661622d894eb38a8 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 11:24:54 +0100 Subject: [PATCH 11/30] Remove if __name__ ... --- .../ai_models/llama_ai_models.py | 14 ----------- .../ai_models/openai_ai_models.py | 7 ------ prediction_market_agent/markets/manifold.py | 10 -------- prediction_market_agent/markets/omen.py | 4 ---- .../tools/betting_strategies.py | 24 ------------------- prediction_market_agent/tools/gnosis_rpc.py | 4 ---- .../tools/web_scrape_structured.py | 8 ------- 7 files changed, 71 deletions(-) diff --git a/prediction_market_agent/ai_models/llama_ai_models.py b/prediction_market_agent/ai_models/llama_ai_models.py index 8a3b69c8..c5f1f27f 100644 --- a/prediction_market_agent/ai_models/llama_ai_models.py +++ b/prediction_market_agent/ai_models/llama_ai_models.py @@ -62,17 +62,3 @@ def construct_llama_prompt(messages: list[Message]) -> str: ) for message in messages ) - - -if __name__ == "__main__": - from dotenv import load_dotenv - - load_dotenv() - - model = ChatReplicateLLamaModel() - messages = [ - Message(role=LlamaRole.user.value, content="Hello!"), - Message(role=LlamaRole.assistant.value, content="Bonjour!"), - ] - completion = model.complete(messages) - print(completion) diff --git a/prediction_market_agent/ai_models/openai_ai_models.py b/prediction_market_agent/ai_models/openai_ai_models.py index 4905865e..07686af8 100644 --- a/prediction_market_agent/ai_models/openai_ai_models.py +++ b/prediction_market_agent/ai_models/openai_ai_models.py @@ -90,10 +90,3 @@ def complete(self, messages: list[Message]) -> Optional[str]: ) completion = response.choices[0].message.content return completion - - -if __name__ == "__main__": - model = ChatOpenAIModel() - messages = [Message(role="user", content="Hello, how are you?")] - completion = model.complete(messages) - print(completion) diff --git a/prediction_market_agent/markets/manifold.py b/prediction_market_agent/markets/manifold.py index eb44942e..a672871d 100644 --- a/prediction_market_agent/markets/manifold.py +++ b/prediction_market_agent/markets/manifold.py @@ -68,13 +68,3 @@ def place_bet(amount: Mana, market_id: str, outcome: bool, api_key: str) -> None raise Exception( f"Placing bet failed: {response.status_code} {response.reason} {response.text}" ) - - -if __name__ == "__main__": - # A test run - market = pick_binary_market() - print(market.question) - print("Placing bet on market:", market.question) - place_bet( - mana_type(2), market.id, True, check_not_none(utils.get_manifold_api_key()) - ) diff --git a/prediction_market_agent/markets/omen.py b/prediction_market_agent/markets/omen.py index 3f1db206..6d5a9f40 100644 --- a/prediction_market_agent/markets/omen.py +++ b/prediction_market_agent/markets/omen.py @@ -474,7 +474,3 @@ def omen_sell_outcome_tx( ) nonce = Nonce(nonce + ONE_NONCE) # Increase after each tx. check_tx_receipt(withdraw_receipt) - - -if __name__ == "__main__": - pprint(get_omen_binary_markets(3)) diff --git a/prediction_market_agent/tools/betting_strategies.py b/prediction_market_agent/tools/betting_strategies.py index cd032589..6662eb98 100644 --- a/prediction_market_agent/tools/betting_strategies.py +++ b/prediction_market_agent/tools/betting_strategies.py @@ -210,27 +210,3 @@ def get_kelly_criterion_bet( ) ) return wei_to_xdai(kelly_bet_wei), outcome_index - - -if __name__ == "__main__": - market_address = "0xa3e47bb771074b33f2e279b9801341e9e0c9c6d7" - market = get_market(market_address) - - est_p_yes = Probability(0.1) - mov_bet = get_market_moving_bet( - market=market, - target_p_yes=est_p_yes, - verbose=True, - ) - kelly_bet = get_kelly_criterion_bet( - market=market, - estimated_p_yes=est_p_yes, - max_bet=xdai_type(10), # This significantly changes the outcome - ) - - print( - f"Market moving bet: {mov_bet[0]:.2f} on {market.get_outcome_str(mov_bet[1])}" - ) - print( - f"Kelly criterion bet: {kelly_bet[0]:.2f} on {market.get_outcome_str(kelly_bet[1])}" - ) diff --git a/prediction_market_agent/tools/gnosis_rpc.py b/prediction_market_agent/tools/gnosis_rpc.py index acc0c546..8bf8302f 100644 --- a/prediction_market_agent/tools/gnosis_rpc.py +++ b/prediction_market_agent/tools/gnosis_rpc.py @@ -17,7 +17,3 @@ def get_balance(address: str) -> Wei: ).json() balance = Wei(int(response["result"], 16)) # Convert hex value to int. return balance - - -if __name__ == "__main__": - print(get_balance("0xf3318C420e5e30C12786C4001D600e9EE1A7eBb1")) diff --git a/prediction_market_agent/tools/web_scrape_structured.py b/prediction_market_agent/tools/web_scrape_structured.py index 2c186ac0..a69ad142 100644 --- a/prediction_market_agent/tools/web_scrape_structured.py +++ b/prediction_market_agent/tools/web_scrape_structured.py @@ -94,11 +94,3 @@ def pretty_html_from_page_content(page_content_html: str, remove_a_links: bool) ).prettify() page_content_body_text_clean = prettify_html(page_content_body_text) return page_content_body_text_clean - - -if __name__ == "__main__": - print( - web_scrape_structured( - "https://ambcrypto.com/predictions/gnosis-price-prediction" - ) - ) From 54eca9ab7a238f31d8c23feb3eb1658559908000 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 11:47:13 +0100 Subject: [PATCH 12/30] add omen tests --- tests/test_omen.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/test_omen.py diff --git a/tests/test_omen.py b/tests/test_omen.py new file mode 100644 index 00000000..597db55e --- /dev/null +++ b/tests/test_omen.py @@ -0,0 +1,43 @@ +import time +from prediction_market_agent.utils import get_keys +from prediction_market_agent.markets.all_markets import omen + + +def test_omen_pick_binary_market() -> None: + market = omen.pick_binary_market() + assert market.outcomes == [ + "Yes", + "No", + ], "Omen binary market should have two outcomes, Yes and No." + + +def test_omen_get_market() -> None: + market = omen.get_market("0xa3e47bb771074b33f2e279b9801341e9e0c9c6d7") + assert ( + market.question + == "Will Bethesda's 'Indiana Jones and the Great Circle' be released by January 25, 2024?" + ), "Omen market question doesn't match the expected value." + + +def test_omen_buy_and_sell_outcome() -> None: + # Tests both buying and selling, so we are back at the square one in the wallet (minues fees). + market = omen.pick_binary_market() + amount = omen.xDai("0.001") + keys = get_keys() + omen.binary_omen_buy_outcome_tx( + amount=amount, + from_address=keys.bet_from_address, + from_private_key=keys.bet_from_private_key, + market=market, + binary_outcome=True, + auto_deposit=True, + ) + time.sleep(3.14) # Wait for the transaction to be mined. + omen.binary_omen_sell_outcome_tx( + amount=amount, + from_address=keys.bet_from_address, + from_private_key=keys.bet_from_private_key, + market=market, + binary_outcome=True, + auto_withdraw=True, + ) From b35bf936e92bae0a5727dcb7b462e160dffc853a Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 11:47:40 +0100 Subject: [PATCH 13/30] Get rid of optionality in apikeys --- prediction_market_agent/utils.py | 40 ++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/prediction_market_agent/utils.py b/prediction_market_agent/utils.py index 6962eeb1..bed3b387 100644 --- a/prediction_market_agent/utils.py +++ b/prediction_market_agent/utils.py @@ -8,6 +8,7 @@ HexAddress, HexStr, ) +from prediction_market_agent.tools.utils import check_not_none def get_api_key(name: str) -> t.Optional[str]: @@ -54,11 +55,40 @@ def __init__( bet_from_address: t.Optional[ChecksumAddress] = None, bet_from_private_key: t.Optional[PrivateKey] = None, ): - self.manifold = manifold - self.serp = serp - self.openai = openai - self.bet_from_address = bet_from_address - self.bet_from_private_key = bet_from_private_key + self._manifold = manifold + self._serp = serp + self._openai = openai + self._bet_from_address = bet_from_address + self._bet_from_private_key = bet_from_private_key + + @property + def manifold(self) -> str: + return check_not_none( + self._manifold, "MANIFOLD_API_KEY missing in the environment." + ) + + @property + def serp(self) -> str: + return check_not_none(self._serp, "SERP_API_KEY missing in the environment.") + + @property + def openai(self) -> str: + return check_not_none( + self._openai, "OPENAI_API_KEY missing in the environment." + ) + + @property + def bet_from_address(self) -> ChecksumAddress: + return check_not_none( + self._bet_from_address, "BET_FROM_ADDRESS missing in the environment." + ) + + @property + def bet_from_private_key(self) -> PrivateKey: + return check_not_none( + self._bet_from_private_key, + "BET_FROM_PRIVATE_KEY missing in the environment.", + ) def get_keys() -> APIKeys: From 3e8d53804d8dcbedb5434b2ec539bceb09fbca41 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 11:47:51 +0100 Subject: [PATCH 14/30] fix type --- prediction_market_agent/data_models/market_data_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prediction_market_agent/data_models/market_data_models.py b/prediction_market_agent/data_models/market_data_models.py index 90b812bb..7aa6103b 100644 --- a/prediction_market_agent/data_models/market_data_models.py +++ b/prediction_market_agent/data_models/market_data_models.py @@ -56,7 +56,7 @@ class OmenMarket(Market): collateralToken: HexAddress outcomes: list[str] outcomeTokenAmounts: list[OmenOutcomeToken] - outcomeTokenMarginalPrices: list[xDai] + outcomeTokenMarginalPrices: t.Optional[list[xDai]] fee: t.Optional[Wei] @property From ebacc79fe366bf4c7fb205464feed1c9c19c8b46 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 11:48:03 +0100 Subject: [PATCH 15/30] add missing function --- prediction_market_agent/markets/omen.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/prediction_market_agent/markets/omen.py b/prediction_market_agent/markets/omen.py index 6d5a9f40..03001376 100644 --- a/prediction_market_agent/markets/omen.py +++ b/prediction_market_agent/markets/omen.py @@ -33,6 +33,9 @@ OmenOutcomeToken, ) +OMEN_TRUE_OUTCOME = "Yes" +OMEN_FALSE_OUTCOME = "No" + with open( os.path.join( os.path.dirname(os.path.realpath(__file__)), "../abis/omen_fpmm.abi.json" @@ -111,7 +114,7 @@ def get_omen_markets(first: int, outcomes: list[str]) -> list[OmenMarket]: def get_omen_binary_markets(limit: int) -> list[OmenMarket]: - return get_omen_markets(limit, ["Yes", "No"]) + return get_omen_markets(limit, [OMEN_TRUE_OUTCOME, OMEN_FALSE_OUTCOME]) def pick_binary_market() -> OmenMarket: @@ -403,7 +406,7 @@ def binary_omen_buy_outcome_tx( from_address=from_address, from_private_key=from_private_key, market=market, - outcome="Yes" if binary_outcome else "No", + outcome=OMEN_TRUE_OUTCOME if binary_outcome else OMEN_FALSE_OUTCOME, auto_deposit=auto_deposit, ) @@ -474,3 +477,21 @@ def omen_sell_outcome_tx( ) nonce = Nonce(nonce + ONE_NONCE) # Increase after each tx. check_tx_receipt(withdraw_receipt) + + +def binary_omen_sell_outcome_tx( + amount: xDai, + from_address: ChecksumAddress, + from_private_key: PrivateKey, + market: OmenMarket, + binary_outcome: bool, + auto_withdraw: bool, +) -> None: + omen_sell_outcome_tx( + amount=amount, + from_address=from_address, + from_private_key=from_private_key, + market=market, + outcome=OMEN_TRUE_OUTCOME if binary_outcome else OMEN_FALSE_OUTCOME, + auto_withdraw=auto_withdraw, + ) From 2953ecd1f21680a29bb341c2da791b4eb64bbbe7 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 12:25:23 +0100 Subject: [PATCH 16/30] tset manifold --- tests/test_manifold.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/test_manifold.py b/tests/test_manifold.py index 27acf56f..1f3ea938 100644 --- a/tests/test_manifold.py +++ b/tests/test_manifold.py @@ -2,14 +2,10 @@ from prediction_market_agent import utils from prediction_market_agent.markets import manifold from prediction_market_agent.tools.gtypes import mana_type -from prediction_market_agent.tools.utils import check_not_none @pytest.mark.skipif(utils.get_manifold_api_key() is None, reason="No Manifold API key") def test_manifold() -> None: market = manifold.pick_binary_market() - print(market.question) print("Placing bet on market:", market.question) - manifold.place_bet( - mana_type(2), market.id, True, check_not_none(utils.get_manifold_api_key()) - ) + manifold.place_bet(mana_type(0.01), market.id, True, utils.get_keys().manifold) From abddcd5fe4936514ea76e9873548d14091b4684d Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 12:25:29 +0100 Subject: [PATCH 17/30] test tools --- tests/test_tools.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_tools.py b/tests/test_tools.py index fd33fcab..236d5ab8 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -8,5 +8,7 @@ def test_search_and_scrape() -> None: objective = "In 2024, will Israel conduct a military strike against Iran?" search = GoogleSearchTool().fn(query=objective) - scraping = web_scrape(objective, search[0]) - print(_summary(objective, scraping)) + assert ( + "https://www.cnn.com/middleeast/live-news/israel-hamas-war-gaza-news-01-20-24/index.html" + in search + ) From abde912075abe8d73a5e96b644a6a6d6676f7096 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 12:54:13 +0100 Subject: [PATCH 18/30] betting strategy tests --- tests/tools/test_betting_strategies.py | 79 ++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tests/tools/test_betting_strategies.py diff --git a/tests/tools/test_betting_strategies.py b/tests/tools/test_betting_strategies.py new file mode 100644 index 00000000..b1e7c762 --- /dev/null +++ b/tests/tools/test_betting_strategies.py @@ -0,0 +1,79 @@ +import pytest +import numpy as np +from prediction_market_agent.markets.all_markets import omen +from prediction_market_agent.tools.gtypes import ( + Probability, + xdai_type, + wei_type, + usd_type, + HexAddress, + OmenOutcomeToken, + xDai, +) +from prediction_market_agent.tools.betting_strategies import ( + get_kelly_criterion_bet, + get_market_moving_bet, +) + +TEST_OMEN_MARKET = omen.OmenMarket( + id=HexAddress("0x76a7a3487f85390dc568f3fce01e0a649cb39651"), + title="Will Plex launch a store for movies and TV shows by 26 January 2024?", + collateralVolume=4369016776639073062, + usdVolume=usd_type("4.369023756584789670441178585394842"), + collateralToken=HexAddress("0xe91d153e0b41518a2ce8dd3d7944fa863463a97d"), + outcomes=["Yes", "No"], + outcomeTokenAmounts=[ + OmenOutcomeToken(7277347438897016099), + OmenOutcomeToken(13741270543921756242), + ], + outcomeTokenMarginalPrices=[ + xdai_type("0.6537666061181695741160552853310822"), + xdai_type("0.3462333938818304258839447146689178"), + ], + fee=wei_type(20000000000000000), +) + + +@pytest.mark.parametrize( + "wanted_p_yes_on_the_market, expected_buying_xdai_amount, expected_buying_outcome", + [ + (Probability(0.1), xdai_type(25.32), "No"), + (Probability(0.9), xdai_type(18.1), "Yes"), + ], +) +def test_get_market_moving_bet_0( + wanted_p_yes_on_the_market: Probability, + expected_buying_xdai_amount: xDai, + expected_buying_outcome: str, +) -> None: + xdai_amount, outcome_index = get_market_moving_bet( + market=TEST_OMEN_MARKET, + target_p_yes=wanted_p_yes_on_the_market, + verbose=True, + ) + assert np.isclose( + float(xdai_amount), + float(expected_buying_xdai_amount), + atol=2.0, # We don't expect it to be 100% accurate, but close enough. + ), f"To move this martket to ~{wanted_p_yes_on_the_market}% for yes, the amount should be {expected_buying_xdai_amount}xDai, according to aiomen website." + assert outcome_index == TEST_OMEN_MARKET.get_outcome_index( + expected_buying_outcome + ), f"The buying outcome index should `{expected_buying_outcome}`." + + +@pytest.mark.parametrize( + "est_p_yes, expected_outcome", + [ + (Probability(0.1), "No"), + (Probability(0.9), "Yes"), + ], +) +def test_kelly_criterion_bet_0(est_p_yes: Probability, expected_outcome: str) -> None: + xdai_amount, outcome_index = get_kelly_criterion_bet( + market=TEST_OMEN_MARKET, + estimated_p_yes=est_p_yes, + max_bet=xdai_type(10), # This significantly changes the outcome. + ) + # Kelly estimates the best bet for maximizing the expected value of the logarithm of the wealth. + # We don't know the real best xdai_amount, but at least we know which outcome index makes sense. + assert outcome_index == TEST_OMEN_MARKET.get_outcome_index(expected_outcome) From 0a90ff609ccd7ba166826cf0caf50ed3cf14ef38 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 12:54:19 +0100 Subject: [PATCH 19/30] usd type --- prediction_market_agent/tools/gtypes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/prediction_market_agent/tools/gtypes.py b/prediction_market_agent/tools/gtypes.py index 2962b17c..d3a00cd1 100644 --- a/prediction_market_agent/tools/gtypes.py +++ b/prediction_market_agent/tools/gtypes.py @@ -21,6 +21,10 @@ Mana = NewType("Mana", Decimal) # Manifold's "currency" +def usd_type(amount: Union[str, int, float, Decimal]) -> USD: + return USD(Decimal(amount)) + + def wei_type(amount: Union[str, int]) -> Wei: return Wei(int(amount)) From c1f064c9e64a6ff19acefe1a2c11b82434b561a9 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 13:06:16 +0100 Subject: [PATCH 20/30] fix mypy --- prediction_market_agent/data_models/market_data_models.py | 8 ++++++-- prediction_market_agent/tools/betting_strategies.py | 8 ++++++-- tests/test_omen.py | 3 ++- tests/tools/test_betting_strategies.py | 8 +++++--- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/prediction_market_agent/data_models/market_data_models.py b/prediction_market_agent/data_models/market_data_models.py index 7aa6103b..601e319e 100644 --- a/prediction_market_agent/data_models/market_data_models.py +++ b/prediction_market_agent/data_models/market_data_models.py @@ -80,8 +80,12 @@ def collateral_token_contract_address_checksummed(self) -> ChecksumAddress: return Web3.to_checksum_address(self.collateral_token_contract_address) @property - def outcomeTokenProbabilities(self) -> list[Probability]: - return [Probability(float(x)) for x in self.outcomeTokenMarginalPrices] + def outcomeTokenProbabilities(self) -> t.Optional[list[Probability]]: + return ( + [Probability(float(x)) for x in self.outcomeTokenMarginalPrices] + if self.outcomeTokenMarginalPrices is not None + else None + ) def get_outcome_index(self, outcome: str) -> int: try: diff --git a/prediction_market_agent/tools/betting_strategies.py b/prediction_market_agent/tools/betting_strategies.py index 6662eb98..9d14d34f 100644 --- a/prediction_market_agent/tools/betting_strategies.py +++ b/prediction_market_agent/tools/betting_strategies.py @@ -45,7 +45,9 @@ def get_market_moving_bet( dx = (new_product - fixed_product) / na_y """ amounts = market.outcomeTokenAmounts - prices = market.outcomeTokenProbabilities + prices = check_not_none( + market.outcomeTokenProbabilities, "No probabilities, is marked closed?" + ) if len(amounts) != 2 or len(prices) != 2: raise ValueError("Only binary markets are supported.") @@ -191,7 +193,9 @@ def get_kelly_criterion_bet( if len(market.outcomeTokenAmounts) != 2: raise ValueError("Only binary markets are supported.") - current_p_yes = market.outcomeTokenProbabilities[0] + current_p_yes = check_not_none( + market.outcomeTokenProbabilities, "No probabilities, is marked closed?" + )[0] outcome_index: OutcomeIndex = 0 if estimated_p_yes > current_p_yes else 1 estimated_p_win = estimated_p_yes if outcome_index == 0 else 1 - estimated_p_yes diff --git a/tests/test_omen.py b/tests/test_omen.py index 597db55e..883bf65c 100644 --- a/tests/test_omen.py +++ b/tests/test_omen.py @@ -1,6 +1,7 @@ import time from prediction_market_agent.utils import get_keys from prediction_market_agent.markets.all_markets import omen +from prediction_market_agent.tools.gtypes import xdai_type def test_omen_pick_binary_market() -> None: @@ -22,7 +23,7 @@ def test_omen_get_market() -> None: def test_omen_buy_and_sell_outcome() -> None: # Tests both buying and selling, so we are back at the square one in the wallet (minues fees). market = omen.pick_binary_market() - amount = omen.xDai("0.001") + amount = xdai_type(0.001) keys = get_keys() omen.binary_omen_buy_outcome_tx( amount=amount, diff --git a/tests/tools/test_betting_strategies.py b/tests/tools/test_betting_strategies.py index b1e7c762..d36a18f9 100644 --- a/tests/tools/test_betting_strategies.py +++ b/tests/tools/test_betting_strategies.py @@ -8,6 +8,8 @@ usd_type, HexAddress, OmenOutcomeToken, + HexStr, + Wei, xDai, ) from prediction_market_agent.tools.betting_strategies import ( @@ -16,11 +18,11 @@ ) TEST_OMEN_MARKET = omen.OmenMarket( - id=HexAddress("0x76a7a3487f85390dc568f3fce01e0a649cb39651"), + id=HexAddress(HexStr("0x76a7a3487f85390dc568f3fce01e0a649cb39651")), title="Will Plex launch a store for movies and TV shows by 26 January 2024?", - collateralVolume=4369016776639073062, + collateralVolume=Wei(4369016776639073062), usdVolume=usd_type("4.369023756584789670441178585394842"), - collateralToken=HexAddress("0xe91d153e0b41518a2ce8dd3d7944fa863463a97d"), + collateralToken=HexAddress(HexStr("0xe91d153e0b41518a2ce8dd3d7944fa863463a97d")), outcomes=["Yes", "No"], outcomeTokenAmounts=[ OmenOutcomeToken(7277347438897016099), From a16db245f8c093e21de4f7d72a1d506b1d189491 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 13:21:45 +0100 Subject: [PATCH 21/30] remove skipifs --- tests/test_manifold.py | 2 -- tests/test_tools.py | 3 --- 2 files changed, 5 deletions(-) diff --git a/tests/test_manifold.py b/tests/test_manifold.py index 1f3ea938..6c89f160 100644 --- a/tests/test_manifold.py +++ b/tests/test_manifold.py @@ -1,10 +1,8 @@ -import pytest from prediction_market_agent import utils from prediction_market_agent.markets import manifold from prediction_market_agent.tools.gtypes import mana_type -@pytest.mark.skipif(utils.get_manifold_api_key() is None, reason="No Manifold API key") def test_manifold() -> None: market = manifold.pick_binary_market() print("Placing bet on market:", market.question) diff --git a/tests/test_tools.py b/tests/test_tools.py index 236d5ab8..ed05b363 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -1,10 +1,7 @@ -import pytest -from prediction_market_agent import utils from prediction_market_agent.tools.google_search import GoogleSearchTool from prediction_market_agent.tools.web_scrape import _summary, web_scrape -@pytest.mark.skipif(utils.get_serp_api_key() is None, reason="No Serp API key") def test_search_and_scrape() -> None: objective = "In 2024, will Israel conduct a military strike against Iran?" search = GoogleSearchTool().fn(query=objective) From 5102e7848534d5b708eae9da4380b1e348e243d5 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 14:00:22 +0100 Subject: [PATCH 22/30] add secrets --- .github/workflows/python_ci.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 4fa0a88c..cef63aad 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -5,6 +5,13 @@ on: push: branches: [main] +env: + MANIFOLD_API_KEY: ${{ secrets. MANIFOLD_API_KEY}} + SERP_API_KEY: ${{ secrets. SERP_API_KEY}} + OPENAI_API_KEY: ${{ secrets. OPENAI_API_KEY}} + BET_FROM_ADDRESS: ${{ secrets. BET_FROM_ADDRESS}} + BET_FROM_PRIVATE_KEY: ${{ secrets. BET_FROM_PRIVATE_KEY}} + jobs: mypy: runs-on: ubuntu-latest @@ -21,6 +28,7 @@ jobs: - uses: ./.github/actions/python_prepare - name: Run pytest run: poetry run pytest + black: runs-on: ubuntu-latest steps: From 58010f27a409f5ce6460c073de926ea0ae0e088b Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 14:01:58 +0100 Subject: [PATCH 23/30] Fix typo --- .github/workflows/python_ci.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index cef63aad..72357b63 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -6,11 +6,11 @@ on: branches: [main] env: - MANIFOLD_API_KEY: ${{ secrets. MANIFOLD_API_KEY}} - SERP_API_KEY: ${{ secrets. SERP_API_KEY}} - OPENAI_API_KEY: ${{ secrets. OPENAI_API_KEY}} - BET_FROM_ADDRESS: ${{ secrets. BET_FROM_ADDRESS}} - BET_FROM_PRIVATE_KEY: ${{ secrets. BET_FROM_PRIVATE_KEY}} + MANIFOLD_API_KEY: ${{ secrets.MANIFOLD_API_KEY}} + SERP_API_KEY: ${{ secrets.SERP_API_KEY}} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY}} + BET_FROM_ADDRESS: ${{ secrets.BET_FROM_ADDRESS}} + BET_FROM_PRIVATE_KEY: ${{ secrets.BET_FROM_PRIVATE_KEY}} jobs: mypy: From 441a6ff499ff346b03dffe7e751a060c94b5f220 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 14:20:51 +0100 Subject: [PATCH 24/30] maybe fix --- .github/workflows/python_ci.yaml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 72357b63..2b49e98a 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -5,13 +5,6 @@ on: push: branches: [main] -env: - MANIFOLD_API_KEY: ${{ secrets.MANIFOLD_API_KEY}} - SERP_API_KEY: ${{ secrets.SERP_API_KEY}} - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY}} - BET_FROM_ADDRESS: ${{ secrets.BET_FROM_ADDRESS}} - BET_FROM_PRIVATE_KEY: ${{ secrets.BET_FROM_PRIVATE_KEY}} - jobs: mypy: runs-on: ubuntu-latest @@ -27,6 +20,12 @@ jobs: - uses: actions/checkout@v2 - uses: ./.github/actions/python_prepare - name: Run pytest + env: + MANIFOLD_API_KEY: ${{ secrets.MANIFOLD_API_KEY}} + SERP_API_KEY: ${{ secrets.SERP_API_KEY}} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY}} + BET_FROM_ADDRESS: ${{ secrets.BET_FROM_ADDRESS}} + BET_FROM_PRIVATE_KEY: ${{ secrets.BET_FROM_PRIVATE_KEY}} run: poetry run pytest black: From 6495a616ec6bbc026f4595a456dc258261993596 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 14:27:13 +0100 Subject: [PATCH 25/30] add shell --- .github/workflows/python_ci.yaml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 2b49e98a..2c0a4b6b 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -21,11 +21,12 @@ jobs: - uses: ./.github/actions/python_prepare - name: Run pytest env: - MANIFOLD_API_KEY: ${{ secrets.MANIFOLD_API_KEY}} - SERP_API_KEY: ${{ secrets.SERP_API_KEY}} - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY}} - BET_FROM_ADDRESS: ${{ secrets.BET_FROM_ADDRESS}} - BET_FROM_PRIVATE_KEY: ${{ secrets.BET_FROM_PRIVATE_KEY}} + MANIFOLD_API_KEY: ${{ secrets.MANIFOLD_API_KEY }} + SERP_API_KEY: ${{ secrets.SERP_API_KEY }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + BET_FROM_ADDRESS: ${{ secrets.BET_FROM_ADDRESS }} + BET_FROM_PRIVATE_KEY: ${{ secrets.BET_FROM_PRIVATE_KEY }} + shell: bash run: poetry run pytest black: From 6faa352cd991f75c9f3fcf11a01a1cab470c212a Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 14:46:15 +0100 Subject: [PATCH 26/30] skip paid tests --- .github/workflows/python_ci.yaml | 26 +++++++++++++++++++------- tests/__init__.py | 0 tests/test_omen.py | 3 +++ tests/test_tools.py | 3 +++ tests/utils.py | 3 +++ 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/utils.py diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 2c0a4b6b..6eabfd2a 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -4,6 +4,14 @@ on: pull_request: push: branches: [main] + workflow_dispatch: + +env: + MANIFOLD_API_KEY: ${{ secrets.MANIFOLD_API_KEY }} + SERP_API_KEY: ${{ secrets.SERP_API_KEY }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + BET_FROM_ADDRESS: ${{ secrets.BET_FROM_ADDRESS }} + BET_FROM_PRIVATE_KEY: ${{ secrets.BET_FROM_PRIVATE_KEY }} jobs: mypy: @@ -20,15 +28,19 @@ jobs: - uses: actions/checkout@v2 - uses: ./.github/actions/python_prepare - name: Run pytest - env: - MANIFOLD_API_KEY: ${{ secrets.MANIFOLD_API_KEY }} - SERP_API_KEY: ${{ secrets.SERP_API_KEY }} - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - BET_FROM_ADDRESS: ${{ secrets.BET_FROM_ADDRESS }} - BET_FROM_PRIVATE_KEY: ${{ secrets.BET_FROM_PRIVATE_KEY }} - shell: bash run: poetry run pytest + pytest-paid: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./.github/actions/python_prepare + - name: Run pytest + run: poetry run pytest + if: github.event_name == 'workflow_dispatch' + # TODO: Remove once the environment variables are fixed in the new repository. + continue-on-error: true + black: runs-on: ubuntu-latest steps: diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_omen.py b/tests/test_omen.py index 883bf65c..b33980a6 100644 --- a/tests/test_omen.py +++ b/tests/test_omen.py @@ -1,4 +1,6 @@ import time +import pytest +from tests.utils import RUN_PAID_TESTS from prediction_market_agent.utils import get_keys from prediction_market_agent.markets.all_markets import omen from prediction_market_agent.tools.gtypes import xdai_type @@ -20,6 +22,7 @@ def test_omen_get_market() -> None: ), "Omen market question doesn't match the expected value." +@pytest.mark.skipif(not RUN_PAID_TESTS, reason="This test costs money to run.") def test_omen_buy_and_sell_outcome() -> None: # Tests both buying and selling, so we are back at the square one in the wallet (minues fees). market = omen.pick_binary_market() diff --git a/tests/test_tools.py b/tests/test_tools.py index ed05b363..146d08a0 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -1,7 +1,10 @@ +import pytest +from tests.utils import RUN_PAID_TESTS from prediction_market_agent.tools.google_search import GoogleSearchTool from prediction_market_agent.tools.web_scrape import _summary, web_scrape +@pytest.mark.skipif(not RUN_PAID_TESTS, reason="This test costs money to run.") def test_search_and_scrape() -> None: objective = "In 2024, will Israel conduct a military strike against Iran?" search = GoogleSearchTool().fn(query=objective) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..f2cf1cdc --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,3 @@ +import os + +RUN_PAID_TESTS = os.environ.get("RUN_PAID_TESTS", "0") == "1" From 04a0e801d32d1e0d0871271aba526a5624e2ea26 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 14:47:26 +0100 Subject: [PATCH 27/30] add forgoten var --- .github/workflows/python_ci.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 6eabfd2a..4ed70cdb 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -37,6 +37,8 @@ jobs: - uses: ./.github/actions/python_prepare - name: Run pytest run: poetry run pytest + env: + RUN_PAID_TESTS: "1" if: github.event_name == 'workflow_dispatch' # TODO: Remove once the environment variables are fixed in the new repository. continue-on-error: true From 31656341503fde30fef4c88d5ea26e911bc5edc1 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 14:49:45 +0100 Subject: [PATCH 28/30] fix test --- tests/test_manifold.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_manifold.py b/tests/test_manifold.py index 6c89f160..e7979d90 100644 --- a/tests/test_manifold.py +++ b/tests/test_manifold.py @@ -1,8 +1,11 @@ +import pytest +from tests.utils import RUN_PAID_TESTS from prediction_market_agent import utils from prediction_market_agent.markets import manifold from prediction_market_agent.tools.gtypes import mana_type +@pytest.mark.skipif(not RUN_PAID_TESTS, reason="This test costs money to run.") def test_manifold() -> None: market = manifold.pick_binary_market() print("Placing bet on market:", market.question) From b3ede3442201d16ebed963b25533c848d0fb86b4 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 25 Jan 2024 14:53:35 +0100 Subject: [PATCH 29/30] revert --- prediction_market_agent/tools/google_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prediction_market_agent/tools/google_search.py b/prediction_market_agent/tools/google_search.py index a954bfb4..9fa718c5 100644 --- a/prediction_market_agent/tools/google_search.py +++ b/prediction_market_agent/tools/google_search.py @@ -5,7 +5,7 @@ def google_search(query: str) -> list[str]: - params = {"q": query, "api_key": utils.get_serp_api_key(), "num": 4} + params = {"q": query, "api_key": utils.get_keys().serp, "num": 4} search = serpapi.GoogleSearch(params) urls = [result["link"] for result in search.get_dict()["organic_results"]] return urls From fabcaddba3c14e9e381d2b9654eff484dd930d83 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Fri, 26 Jan 2024 08:19:23 +0100 Subject: [PATCH 30/30] remove one test --- tests/test_tools.py | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 tests/test_tools.py diff --git a/tests/test_tools.py b/tests/test_tools.py deleted file mode 100644 index 146d08a0..00000000 --- a/tests/test_tools.py +++ /dev/null @@ -1,14 +0,0 @@ -import pytest -from tests.utils import RUN_PAID_TESTS -from prediction_market_agent.tools.google_search import GoogleSearchTool -from prediction_market_agent.tools.web_scrape import _summary, web_scrape - - -@pytest.mark.skipif(not RUN_PAID_TESTS, reason="This test costs money to run.") -def test_search_and_scrape() -> None: - objective = "In 2024, will Israel conduct a military strike against Iran?" - search = GoogleSearchTool().fn(query=objective) - assert ( - "https://www.cnn.com/middleeast/live-news/israel-hamas-war-gaza-news-01-20-24/index.html" - in search - )