Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-commit hooks #589

Merged
merged 7 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/python_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ jobs:
run: |
poetry run isort --profile black .
git diff --exit-code || exit 1

pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/python_prepare
- name: Install Dependencies
run: |
pip install pre-commit
- name: Run Pre-Commit
run: |
pre-commit run --all-files
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-commit needs to be installed locally (see install_hooks.sh), but as a last resort, also run it in CI, so at least we won't miss it by accident if the key gets leaked.

9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repos:
- repo: https://github.com/kongzii/pre-commit-hooks
rev: peter/web3
hooks:
- id: detect-private-key
- id: check-added-large-files
args: ['--maxkb=5000']
- id: detect-web3-private-key
exclude: 'poetry.lock'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ This API can be built on top of to create your application. See [here](https://g

See the [Issues](https://github.com/gnosis/prediction-market-agent-tooling/issues) for ideas of things that need fixing or implementing. The team is also receptive to new issues and PRs.

We use `mypy` for static type checking, and `isort`, `black` and `autoflake` for linting. These all run as steps in CI.
We use `mypy` for static type checking, and `isort`, `black` and `autoflake` for linting, and `pre-commit` to minimise unwanted pushes to the public repositories. These all run as steps in CI, but `pre-commit` also needs to be installed locally using the provided `install_hooks.sh` script.
13 changes: 13 additions & 0 deletions install_hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Install pre-commit if not already installed
if ! command -v pre-commit &> /dev/null
then
echo "pre-commit is not installed. Installing now..."
brew install pre-commit
fi
Comment on lines +6 to +8
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add cross-platform support for pre-commit installation.

The script assumes brew is available, which is macOS specific. Consider adding support for other package managers:

 if ! command -v pre-commit &> /dev/null
 then
     echo "pre-commit is not installed. Installing now..."
-    brew install pre-commit
+    if command -v brew &> /dev/null; then
+        brew install pre-commit
+    elif command -v apt-get &> /dev/null; then
+        sudo apt-get install -y pre-commit
+    elif command -v pip &> /dev/null; then
+        pip install pre-commit
+    else
+        echo "Error: Could not find a package manager to install pre-commit"
+        exit 1
+    fi
 fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
echo "pre-commit is not installed. Installing now..."
brew install pre-commit
fi
echo "pre-commit is not installed. Installing now..."
if command -v brew &> /dev/null; then
brew install pre-commit
elif command -v apt-get &> /dev/null; then
sudo apt-get install -y pre-commit
elif command -v pip &> /dev/null; then
pip install pre-commit
else
echo "Error: Could not find a package manager to install pre-commit"
exit 1
fi
fi


# Install the hooks
pre-commit install

echo "Pre-commit hooks installed successfully."
13 changes: 8 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prediction_market_agent_tooling/gtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ def secretstr_to_v1_secretstr(s: SecretStr | None) -> SecretStrV1 | None:


def int_to_hexbytes(v: int) -> HexBytes:
# Example: 1 -> HexBytes("0x0000000000000000000000000000000000000000000000000000000000000001").
# Example: 1 -> HexBytes("0x0000000000000000000000000000000000000000000000000000000000000001"). # web3-private-key-ok
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like private keys don't have any strong format. I had to add this # web3-private-key-ok so we can tell the script that these aren't real private keys.

@gabrielfior please check out scripts/detect_web3_private_keys.py, if you know a better way of how to detect private keys, I'll be happy 😄

return HexBytes.fromhex(format(v, "064x"))
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ class FixedProductMarketMakersResponse(BaseModel):


class RealityQuestion(BaseModel):
# This `id` is in form of `0x79e32ae03fb27b07c89c0c568f80287c01ca2e57-0x2d362f435e7b5159794ff0b5457a900283fca41fe6301dc855a647595903db13`,
# This `id` is in form of `0x79e32ae03fb27b07c89c0c568f80287c01ca2e57-0x2d362f435e7b5159794ff0b5457a900283fca41fe6301dc855a647595903db13`, # web3-private-key-ok
# which I couldn't find how it is created, but based on how it looks like I assume it's composed of `answerId-questionId`.
# (Why is answer id as part of the question object? Because this question object is actually received from the answer object below).
# And because all the contract methods so far needed bytes32 input, when asked for question id, `questionId` field was the correct one to use so far.
Expand Down
2 changes: 1 addition & 1 deletion prediction_market_agent_tooling/markets/omen/omen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ def omen_remove_fund_market_tx(
amount_per_index_set = get_conditional_tokens_balance_for_market(
market, from_address, web3
)
# We fetch the minimum balance of outcome token - for ex, in this tx (https://gnosisscan.io/tx/0xc31c4e9bc6a60cf7db9991a40ec2f2a06e3539f8cb8dd81b6af893cef6f40cd7#eventlog) - event #460, this should yield 9804940144070370149. This amount matches what is displayed in the Omen UI.
# We fetch the minimum balance of outcome token - for ex, in this tx (https://gnosisscan.io/tx/0xc31c4e9bc6a60cf7db9991a40ec2f2a06e3539f8cb8dd81b6af893cef6f40cd7#eventlog) - event #460, this should yield 9804940144070370149. This amount matches what is displayed in the Omen UI. # web3-private-key-ok
# See similar logic from Olas
# https://github.com/valory-xyz/market-creator/blob/4bc47f696fb5ecb61c3b7ec8c001ff2ab6c60fcf/packages/valory/skills/market_creation_manager_abci/behaviours.py#L1308
amount_to_merge = min(amount_per_index_set.values())
Expand Down
2 changes: 1 addition & 1 deletion prediction_market_agent_tooling/tools/hexbytes_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BaseHex:
"0xd4e56740",
"0xd4e56740f876aef8",
"0xd4e56740f876aef8c010b86a40d5f567",
"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", # web3-private-key-ok
)

@classmethod
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pinatapy-vourhey = "^0.2.0"
hishel = "^0.0.31"
pytest-postgresql = "^6.1.1"
optuna = { version = "^4.1.0", optional = true}
eth-keys = "^0.6.1"

[tool.poetry.extras]
openai = ["openai"]
Expand Down
4 changes: 1 addition & 3 deletions scripts/store_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def main(
SAFE_ADDRESS=None,
)
market_address = Web3.to_checksum_address(api_keys.public_key)
dummy_transaction_hash = (
"0x3750ffa211dab39b4d0711eb27b02b56a17fa9d257ee549baa3110725fd1d41b"
)
dummy_transaction_hash = "0x3750ffa211dab39b4d0711eb27b02b56a17fa9d257ee549baa3110725fd1d41b" # web3-private-key-ok
contract_prediction = ContractPrediction(
tx_hashes=[HexBytes(dummy_transaction_hash)],
estimated_probability_bps=5454,
Expand Down
50 changes: 33 additions & 17 deletions tests/markets/omen/test_omen_subgraph_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,15 @@ def test_get_user_positions_1(
)
# Assert 3 conditionIds are included
expected_condition_ids = [
HexBytes("0x9c7711bee0902cc8e6838179058726a7ba769cc97d4d0ea47b31370d2d7a117b"),
HexBytes("0xe2bf80af2a936cdabeef4f511620a2eec46f1caf8e75eb5dc189372367a9154c"),
HexBytes("0x3f8153364001b26b983dd92191a084de8230f199b5ad0b045e9e1df61089b30d"),
HexBytes(
"0x9c7711bee0902cc8e6838179058726a7ba769cc97d4d0ea47b31370d2d7a117b" # web3-private-key-ok
),
HexBytes(
"0xe2bf80af2a936cdabeef4f511620a2eec46f1caf8e75eb5dc189372367a9154c" # web3-private-key-ok
),
HexBytes(
"0x3f8153364001b26b983dd92191a084de8230f199b5ad0b045e9e1df61089b30d" # web3-private-key-ok
),
]
unique_condition_ids: list[HexBytes] = sum(
[u.position.conditionIds for u in user_positions], []
Expand All @@ -162,7 +168,9 @@ def test_get_user_positions_1(

def test_get_answers(omen_subgraph_handler: OmenSubgraphHandler) -> None:
question_id = HexBytes.fromhex(
HexStr("0xdcb2691a9ec05e25a6e595a9972b482ea65b789d978b27c0c06ff97345fce919")
HexStr(
"0xdcb2691a9ec05e25a6e595a9972b482ea65b789d978b27c0c06ff97345fce919" # web3-private-key-ok
)
)
answers = omen_subgraph_handler.get_answers(question_id)
assert len(answers) == 1
Expand All @@ -174,7 +182,9 @@ def test_get_answers(omen_subgraph_handler: OmenSubgraphHandler) -> None:

def test_get_responses(omen_subgraph_handler: OmenSubgraphHandler) -> None:
question_id = HexBytes.fromhex(
HexStr("0xdcb2691a9ec05e25a6e595a9972b482ea65b789d978b27c0c06ff97345fce919")
HexStr(
"0xdcb2691a9ec05e25a6e595a9972b482ea65b789d978b27c0c06ff97345fce919" # web3-private-key-ok
)
)
responses = omen_subgraph_handler.get_responses(limit=None, question_id=question_id)
assert len(responses) == 1
Expand All @@ -190,23 +200,23 @@ def test_get_responses(omen_subgraph_handler: OmenSubgraphHandler) -> None:
None,
[
HexBytes(
"0x00f57ca97d4fc07c70c0900df502dacfca455dd435643fcfab44e122b7da8684"
"0x00f57ca97d4fc07c70c0900df502dacfca455dd435643fcfab44e122b7da8684" # web3-private-key-ok
)
],
[
HexBytes(
"0x00f57ca97d4fc07c70c0900df502dacfca455dd435643fcfab44e122b7da8684"
"0x00f57ca97d4fc07c70c0900df502dacfca455dd435643fcfab44e122b7da8684" # web3-private-key-ok
),
HexBytes(
"0xfa2f09d7375837e791c66f7ccee06d4fa7955812baf668883c2a5f939670ef33"
"0xfa2f09d7375837e791c66f7ccee06d4fa7955812baf668883c2a5f939670ef33" # web3-private-key-ok
),
],
[
HexBytes(
"0x00f57ca97d4fc07c70c0900df502dacfca455dd435643fcfab44e122b7da8684"
"0x00f57ca97d4fc07c70c0900df502dacfca455dd435643fcfab44e122b7da8684" # web3-private-key-ok
),
HexBytes(
"0xfa2f09d7375837e791c66f7ccee06d4fa7955812baf668883c2a5f939670ef33"
"0xfa2f09d7375837e791c66f7ccee06d4fa7955812baf668883c2a5f939670ef33" # web3-private-key-ok
),
]
* 100, # Multiply to test if API won't fail with many IDs in the list.
Expand All @@ -233,7 +243,9 @@ def test_get_market_with_condition_ids(
omen_subgraph_handler: OmenSubgraphHandler,
) -> None:
condition_ids = [
HexBytes("0x9c7711bee0902cc8e6838179058726a7ba769cc97d4d0ea47b31370d2d7a117b")
HexBytes(
"0x9c7711bee0902cc8e6838179058726a7ba769cc97d4d0ea47b31370d2d7a117b" # web3-private-key-ok
)
]
expected_market_title = (
"Will the Federal Reserve cut interest rates on 28 March 2024?"
Expand All @@ -251,8 +263,12 @@ def test_get_markets_from_multiple_user_positions(
omen_subgraph_handler: OmenSubgraphHandler,
) -> None:
condition_ids = [
HexBytes("0xe2bf80af2a936cdabeef4f511620a2eec46f1caf8e75eb5dc189372367a9154c"),
HexBytes("0x3f8153364001b26b983dd92191a084de8230f199b5ad0b045e9e1df61089b30d"),
HexBytes(
"0xe2bf80af2a936cdabeef4f511620a2eec46f1caf8e75eb5dc189372367a9154c" # web3-private-key-ok
),
HexBytes(
"0x3f8153364001b26b983dd92191a084de8230f199b5ad0b045e9e1df61089b30d" # web3-private-key-ok
),
]
user_positions = [
build_incomplete_user_position_from_condition_ids([condition_id])
Expand All @@ -268,7 +284,7 @@ def test_get_positions_by_condition_id(
omen_subgraph_handler: OmenSubgraphHandler,
) -> None:
condition_id = HexBytes(
"0xffe4bf3e61be010728813a2a61ef422fd7d07b410170b64a5dfced9549f2e057"
"0xffe4bf3e61be010728813a2a61ef422fd7d07b410170b64a5dfced9549f2e057" # web3-private-key-ok
)
positions = omen_subgraph_handler.get_positions(condition_id)
assert len(positions) == 2
Expand Down Expand Up @@ -365,7 +381,7 @@ def test_get_arbitrated_market_with_answer(
assert omen_subgraph_handler.get_questions(
limit=None,
question_id=HexBytes(
"0xfd9c313aca5b704d6d4920ab7dd4c6d1ebcdfa0242df8dc517a050643419285b"
"0xfd9c313aca5b704d6d4920ab7dd4c6d1ebcdfa0242df8dc517a050643419285b" # web3-private-key-ok
),
with_answers=True,
), "Should return it, because that questionId has an answer."
Expand All @@ -374,11 +390,11 @@ def test_get_arbitrated_market_with_answer(
def test_do_not_get_arbitrated_market_without_answer(
omen_subgraph_handler: OmenSubgraphHandler,
) -> None:
# If this test starts to fail, check if `0xfd9c313aca5b704d6d4920ab7dd4c6d1ebcdfa0242df8dc517a050643419285b` isn't arbitrated anymore, and if so simply delete this test.
# If this test starts to fail, check if `0xfd9c313aca5b704d6d4920ab7dd4c6d1ebcdfa0242df8dc517a050643419285b` isn't arbitrated anymore, and if so simply delete this test. # web3-private-key-ok
assert not omen_subgraph_handler.get_questions(
limit=None,
question_id=HexBytes(
"0xfd9c313aca5b704d6d4920ab7dd4c6d1ebcdfa0242df8dc517a050643419285b"
"0xfd9c313aca5b704d6d4920ab7dd4c6d1ebcdfa0242df8dc517a050643419285b" # web3-private-key-ok
),
with_answers=False,
), "Should not return anything, because that questionId has an answer."
4 changes: 1 addition & 3 deletions tests/tools/test_web3_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@


def test_private_key_to_public_key() -> None:
ganache_private_key_example = (
"0x94c589f92a38698b984605efbc0bff47208c43eac85ab6ea553cc9e17c4a49fe"
)
ganache_private_key_example = "0x94c589f92a38698b984605efbc0bff47208c43eac85ab6ea553cc9e17c4a49fe" # web3-private-key-ok
ganache_public_key_example = "0x4c24e51488429E013f259A7FB6Ac174c715BB66a"
actual_public_key = private_key_to_public_key(
SecretStr(ganache_private_key_example)
Expand Down
9 changes: 5 additions & 4 deletions tests_integration_with_local_chain/markets/omen/test_omen.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,15 @@ def get_position_balance_by_position_id(

@pytest.mark.parametrize(
"ipfs_hash",
["0x3750ffa211dab39b4d0711eb27b02b56a17fa9d257ee549baa3110725fd1d41b", HASH_ZERO],
[
"0x3750ffa211dab39b4d0711eb27b02b56a17fa9d257ee549baa3110725fd1d41b", # web3-private-key-ok
HASH_ZERO,
],
)
def test_add_predictions(local_web3: Web3, test_keys: APIKeys, ipfs_hash: str) -> None:
agent_result_mapping = OmenAgentResultMappingContract()
market_address = test_keys.public_key
dummy_transaction_hash = (
"0x3750ffa211dab39b4d0711eb27b02b56a17fa9d257ee549baa3110725fd1d41b"
)
dummy_transaction_hash = "0x3750ffa211dab39b4d0711eb27b02b56a17fa9d257ee549baa3110725fd1d41b" # web3-private-key-ok
stored_predictions = agent_result_mapping.get_predictions(
market_address, web3=local_web3
)
Expand Down
2 changes: 1 addition & 1 deletion tests_integration_with_local_chain/safe/test_constants.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ANVIL_PKEY1 = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
ANVIL_PKEY1 = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" # web3-private-key-ok
Loading