From 39bbaad5a7d733150f71133313d0bca771a53293 Mon Sep 17 00:00:00 2001 From: perplover <184728147+perplover@users.noreply.github.com> Date: Mon, 14 Oct 2024 14:04:48 -0700 Subject: [PATCH 1/5] Add githooks for prepush codestyle --- githooks/pre-push | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 githooks/pre-push diff --git a/githooks/pre-push b/githooks/pre-push new file mode 100644 index 00000000..e718066a --- /dev/null +++ b/githooks/pre-push @@ -0,0 +1,22 @@ +#!/bin/sh + +# Run make lint +echo "Running code style checks..." +make codestyle + +# Check the exit status of make lint +if [ $? -ne 0 ]; then + echo "Codestyle checks failed. Please fix the issues before pushing." + exit 1 +fi + +# Check for unstaged changes +if ! git diff --exit-code > /dev/null 2>&1; then + echo "Codestyle checks have modified files. Please review and stage the changes before pushing." + git status --short + exit 1 +fi + +# If we get here, linting passed and there are no unstaged changes +echo "Codestyle passed and no unstaged changes. Proceeding with push." +exit 0 From abb0688d9bd5324274674479c39be23b8f4f48c6 Mon Sep 17 00:00:00 2001 From: perplover <184728147+perplover@users.noreply.github.com> Date: Mon, 14 Oct 2024 14:07:21 -0700 Subject: [PATCH 2/5] Run codestyle --- examples/basic_adding.py | 3 ++- examples/basic_agent.py | 2 +- examples/basic_builder_fee.py | 3 ++- examples/basic_leverage_adjustment.py | 3 ++- examples/basic_market_order.py | 3 ++- examples/basic_order.py | 3 ++- examples/basic_order_modify.py | 3 ++- examples/basic_order_with_cloid.py | 3 ++- examples/basic_schedule_cancel.py | 3 ++- examples/basic_set_referrer.py | 3 ++- examples/basic_spot_order.py | 2 +- examples/basic_spot_to_perp.py | 3 ++- examples/basic_spot_transfer.py | 3 ++- examples/basic_sub_account.py | 3 ++- examples/basic_tpsl.py | 3 ++- examples/basic_transfer.py | 3 ++- examples/basic_vault.py | 3 ++- examples/basic_vault_transfer.py | 3 ++- examples/basic_withdraw.py | 3 ++- examples/basic_ws.py | 3 ++- examples/cancel_open_orders.py | 3 ++- examples/example_utils.py | 5 +++-- examples/rounding.py | 3 ++- githooks/pre-push | 0 hyperliquid/exchange.py | 12 ++++++------ hyperliquid/info.py | 4 ++-- hyperliquid/utils/signing.py | 6 +++--- hyperliquid/utils/types.py | 2 +- tests/signing_test.py | 2 +- 29 files changed, 58 insertions(+), 37 deletions(-) mode change 100644 => 100755 githooks/pre-push diff --git a/examples/basic_adding.py b/examples/basic_adding.py index afdc8347..7f10b7a4 100644 --- a/examples/basic_adding.py +++ b/examples/basic_adding.py @@ -4,6 +4,8 @@ import threading import time +import example_utils + from hyperliquid.exchange import Exchange from hyperliquid.info import Info from hyperliquid.utils import constants @@ -21,7 +23,6 @@ UserEventsMsg, UserEventsSubscription, ) -import example_utils # How far from the best bid and offer this strategy ideally places orders. Currently set to .3% # i.e. if the best bid is $1000, this strategy will place a resting bid at $997 diff --git a/examples/basic_agent.py b/examples/basic_agent.py index f040cfc8..ca01fd0b 100644 --- a/examples/basic_agent.py +++ b/examples/basic_agent.py @@ -1,9 +1,9 @@ import eth_account +import example_utils from eth_account.signers.local import LocalAccount from hyperliquid.exchange import Exchange from hyperliquid.utils import constants -import example_utils def main(): diff --git a/examples/basic_builder_fee.py b/examples/basic_builder_fee.py index 872e138c..4eb38ccb 100644 --- a/examples/basic_builder_fee.py +++ b/examples/basic_builder_fee.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_leverage_adjustment.py b/examples/basic_leverage_adjustment.py index 993052e5..41e2772f 100644 --- a/examples/basic_leverage_adjustment.py +++ b/examples/basic_leverage_adjustment.py @@ -1,8 +1,9 @@ import json -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_market_order.py b/examples/basic_market_order.py index 81ffbc6a..f3db2c73 100644 --- a/examples/basic_market_order.py +++ b/examples/basic_market_order.py @@ -1,8 +1,9 @@ import time -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_order.py b/examples/basic_order.py index 5bd77aaf..e596a1cd 100644 --- a/examples/basic_order.py +++ b/examples/basic_order.py @@ -1,8 +1,9 @@ import json -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(base_url=constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_order_modify.py b/examples/basic_order_modify.py index 2d4f6160..f37d6d4e 100644 --- a/examples/basic_order_modify.py +++ b/examples/basic_order_modify.py @@ -1,6 +1,7 @@ +import example_utils + from hyperliquid.utils import constants from hyperliquid.utils.types import Cloid -import example_utils def main(): diff --git a/examples/basic_order_with_cloid.py b/examples/basic_order_with_cloid.py index beaf9798..31fed272 100644 --- a/examples/basic_order_with_cloid.py +++ b/examples/basic_order_with_cloid.py @@ -1,6 +1,7 @@ +import example_utils + from hyperliquid.utils import constants from hyperliquid.utils.types import Cloid -import example_utils def main(): diff --git a/examples/basic_schedule_cancel.py b/examples/basic_schedule_cancel.py index ff8db478..d46cda46 100644 --- a/examples/basic_schedule_cancel.py +++ b/examples/basic_schedule_cancel.py @@ -1,8 +1,9 @@ import time +import example_utils + from hyperliquid.utils import constants from hyperliquid.utils.signing import get_timestamp_ms -import example_utils def main(): diff --git a/examples/basic_set_referrer.py b/examples/basic_set_referrer.py index 10960258..d3f22464 100644 --- a/examples/basic_set_referrer.py +++ b/examples/basic_set_referrer.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_spot_order.py b/examples/basic_spot_order.py index 6e197ae3..e312541a 100644 --- a/examples/basic_spot_order.py +++ b/examples/basic_spot_order.py @@ -1,8 +1,8 @@ import json -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants PURR = "PURR/USDC" OTHER_COIN = "@8" diff --git a/examples/basic_spot_to_perp.py b/examples/basic_spot_to_perp.py index c3a92cbb..0361ed88 100644 --- a/examples/basic_spot_to_perp.py +++ b/examples/basic_spot_to_perp.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_spot_transfer.py b/examples/basic_spot_transfer.py index a9557974..01d4f380 100644 --- a/examples/basic_spot_transfer.py +++ b/examples/basic_spot_transfer.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_sub_account.py b/examples/basic_sub_account.py index e3c8d694..d274953a 100644 --- a/examples/basic_sub_account.py +++ b/examples/basic_sub_account.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + # This example shows how to create, query, and transfer funds to a subaccount. # To trade as a subaccount set vault_address to the subaccount's address. See basic_vault.py for an example. diff --git a/examples/basic_tpsl.py b/examples/basic_tpsl.py index 426c5391..fccb37db 100644 --- a/examples/basic_tpsl.py +++ b/examples/basic_tpsl.py @@ -1,8 +1,9 @@ import argparse -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): parser = argparse.ArgumentParser(description="basic_tpsl") diff --git a/examples/basic_transfer.py b/examples/basic_transfer.py index 1d07ff9c..0df92747 100644 --- a/examples/basic_transfer.py +++ b/examples/basic_transfer.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_vault.py b/examples/basic_vault.py index 5f96eb1f..6fc16a51 100644 --- a/examples/basic_vault.py +++ b/examples/basic_vault.py @@ -1,6 +1,7 @@ +import example_utils + from hyperliquid.exchange import Exchange from hyperliquid.utils import constants -import example_utils def main(): diff --git a/examples/basic_vault_transfer.py b/examples/basic_vault_transfer.py index 2f2e5ac9..d613e303 100644 --- a/examples/basic_vault_transfer.py +++ b/examples/basic_vault_transfer.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_withdraw.py b/examples/basic_withdraw.py index 82cf8f30..e8cf845c 100644 --- a/examples/basic_withdraw.py +++ b/examples/basic_withdraw.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/basic_ws.py b/examples/basic_ws.py index 85357089..5c70fbbe 100644 --- a/examples/basic_ws.py +++ b/examples/basic_ws.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, _ = example_utils.setup(constants.TESTNET_API_URL) diff --git a/examples/cancel_open_orders.py b/examples/cancel_open_orders.py index fbdb6d11..257d6b68 100644 --- a/examples/cancel_open_orders.py +++ b/examples/cancel_open_orders.py @@ -1,6 +1,7 @@ -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/examples/example_utils.py b/examples/example_utils.py index 36c1cd06..40b77d96 100644 --- a/examples/example_utils.py +++ b/examples/example_utils.py @@ -1,8 +1,9 @@ -import eth_account -from eth_account.signers.local import LocalAccount import json import os +import eth_account +from eth_account.signers.local import LocalAccount + from hyperliquid.exchange import Exchange from hyperliquid.info import Info diff --git a/examples/rounding.py b/examples/rounding.py index 584c5536..b7c692ec 100644 --- a/examples/rounding.py +++ b/examples/rounding.py @@ -9,9 +9,10 @@ """ import json -from hyperliquid.utils import constants import example_utils +from hyperliquid.utils import constants + def main(): address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) diff --git a/githooks/pre-push b/githooks/pre-push old mode 100644 new mode 100755 diff --git a/hyperliquid/exchange.py b/hyperliquid/exchange.py index d9f5f538..85c8b4a2 100644 --- a/hyperliquid/exchange.py +++ b/hyperliquid/exchange.py @@ -1,34 +1,34 @@ -import eth_account import logging import secrets +import eth_account from eth_account.signers.local import LocalAccount from hyperliquid.api import API from hyperliquid.info import Info from hyperliquid.utils.constants import MAINNET_API_URL from hyperliquid.utils.signing import ( - CancelRequest, CancelByCloidRequest, + CancelRequest, ModifyRequest, + OidOrCloid, OrderRequest, OrderType, OrderWire, - OidOrCloid, ScheduleCancelAction, float_to_usd_int, get_timestamp_ms, order_request_to_order_wire, order_wires_to_order_action, + sign_agent, sign_approve_builder_fee, sign_l1_action, + sign_spot_transfer_action, sign_usd_class_transfer_action, sign_usd_transfer_action, - sign_spot_transfer_action, sign_withdraw_from_bridge_action, - sign_agent, ) -from hyperliquid.utils.types import Any, List, Meta, SpotMeta, Optional, Tuple, Cloid, BuilderInfo +from hyperliquid.utils.types import Any, BuilderInfo, Cloid, List, Meta, Optional, SpotMeta, Tuple class Exchange(API): diff --git a/hyperliquid/info.py b/hyperliquid/info.py index 22fcc4f1..b63f2751 100644 --- a/hyperliquid/info.py +++ b/hyperliquid/info.py @@ -2,13 +2,13 @@ from hyperliquid.utils.types import ( Any, Callable, + Cloid, Meta, + Optional, SpotMeta, SpotMetaAndAssetCtxs, - Optional, Subscription, cast, - Cloid, ) from hyperliquid.websocket_manager import WebsocketManager diff --git a/hyperliquid/utils/signing.py b/hyperliquid/utils/signing.py index c0c467ab..2e063343 100644 --- a/hyperliquid/utils/signing.py +++ b/hyperliquid/utils/signing.py @@ -1,11 +1,11 @@ import time - from decimal import Decimal + +import msgpack from eth_account.messages import encode_structured_data from eth_utils import keccak, to_hex -import msgpack -from hyperliquid.utils.types import Literal, Optional, TypedDict, Union, Cloid, NotRequired +from hyperliquid.utils.types import Cloid, Literal, NotRequired, Optional, TypedDict, Union Tif = Union[Literal["Alo"], Literal["Ioc"], Literal["Gtc"]] Tpsl = Union[Literal["tp"], Literal["sl"]] diff --git a/hyperliquid/utils/types.py b/hyperliquid/utils/types.py index 1c71c279..d400453e 100644 --- a/hyperliquid/utils/types.py +++ b/hyperliquid/utils/types.py @@ -1,7 +1,7 @@ from __future__ import annotations +from typing import Any, Callable, Dict, List, Literal, NamedTuple, Optional, Tuple, TypedDict, Union, cast from typing_extensions import NotRequired -from typing import Any, Callable, Dict, List, NamedTuple, Optional, Tuple, Union, cast, Literal, TypedDict Any = Any Option = Optional diff --git a/tests/signing_test.py b/tests/signing_test.py index 1b55246e..4458290e 100644 --- a/tests/signing_test.py +++ b/tests/signing_test.py @@ -3,9 +3,9 @@ from eth_utils import to_hex from hyperliquid.utils.signing import ( - action_hash, OrderRequest, ScheduleCancelAction, + action_hash, construct_phantom_agent, float_to_int_for_hashing, order_request_to_order_wire, From a5367b0a5b1c020ab852e4a52f4f09562a1e5e32 Mon Sep 17 00:00:00 2001 From: perplover <184728147+perplover@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:24:10 -0700 Subject: [PATCH 3/5] Update readme to link git hooks --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index a120996f..0b27eda2 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,12 @@ make install Codestyle

+Add git pre-push hook to run codestyle checks: + +``` +git config --local core.hooksPath githooks +``` + Automatic formatting uses `pyupgrade`, `isort` and `black`. ```bash From aa20b4063f10c77c523a7eb68013b524f114b173 Mon Sep 17 00:00:00 2001 From: perplover <184728147+perplover@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:40:05 -0700 Subject: [PATCH 4/5] Move the codestyle check to pre-commit-hook --- .pre-commit-config.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e4a3daa4..f3b6bbb9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -34,3 +34,11 @@ repos: entry: poetry run black --config pyproject.toml types: [python] language: system + + - repo: local + hooks: + - id: codestyle + name: codestyle + entry: make codestyle + types: [python] + language: system From 174be41088350a7aa79f260e800e59e05d096db9 Mon Sep 17 00:00:00 2001 From: perplover <184728147+perplover@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:47:38 -0700 Subject: [PATCH 5/5] Move from pre-push to pre-commit hook --- README.md | 6 +++--- githooks/pre-push | 22 ---------------------- 2 files changed, 3 insertions(+), 25 deletions(-) delete mode 100755 githooks/pre-push diff --git a/README.md b/README.md index 0b27eda2..0cd398a6 100644 --- a/README.md +++ b/README.md @@ -69,10 +69,10 @@ make install

Codestyle

-Add git pre-push hook to run codestyle checks: +Install pre-commit hooks which will run isort, black and codestyle on your code: -``` -git config --local core.hooksPath githooks +```bash +make pre-commit-install ``` Automatic formatting uses `pyupgrade`, `isort` and `black`. diff --git a/githooks/pre-push b/githooks/pre-push deleted file mode 100755 index e718066a..00000000 --- a/githooks/pre-push +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh - -# Run make lint -echo "Running code style checks..." -make codestyle - -# Check the exit status of make lint -if [ $? -ne 0 ]; then - echo "Codestyle checks failed. Please fix the issues before pushing." - exit 1 -fi - -# Check for unstaged changes -if ! git diff --exit-code > /dev/null 2>&1; then - echo "Codestyle checks have modified files. Please review and stage the changes before pushing." - git status --short - exit 1 -fi - -# If we get here, linting passed and there are no unstaged changes -echo "Codestyle passed and no unstaged changes. Proceeding with push." -exit 0