hyperliquid-python-sdk-async 0.24.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Hyperliquid Labs Pte. Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21
+ OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,162 @@
1
+ Metadata-Version: 2.1
2
+ Name: hyperliquid-python-sdk-async
3
+ Version: 0.24.6
4
+ Summary: SDK for Hyperliquid API trading with Python.
5
+ Home-page: https://github.com/hyperliquid-dex/hyperliquid-python-sdk
6
+ License: MIT
7
+ Author: Hyperliquid
8
+ Author-email: hello@hyperliquid.xyz
9
+ Requires-Python: >=3.9,<4.0
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: Python :: 3.9
24
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
+ Requires-Dist: aiohttp (>=3.10,<4.0)
26
+ Requires-Dist: eth-account (>=0.13.5,<0.14.0)
27
+ Requires-Dist: eth-utils (>=5.2.0,<6.0.0)
28
+ Requires-Dist: msgpack (>=1.0.5,<2.0.0)
29
+ Requires-Dist: websockets (>=15.0.1,<16.0.0)
30
+ Project-URL: Repository, https://github.com/hyperliquid-dex/hyperliquid-python-sdk
31
+ Description-Content-Type: text/markdown
32
+
33
+ # hyperliquid-python-sdk
34
+
35
+ <div align="center">
36
+
37
+ [![Dependencies Status](https://img.shields.io/badge/dependencies-up%20to%20date-brightgreen.svg)](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/pulls?utf8=%E2%9C%93&q=is%3Apr%20author%3Aapp%2Fdependabot)
38
+
39
+ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
40
+ [![Security: bandit](https://img.shields.io/badge/security-bandit-green.svg)](https://github.com/PyCQA/bandit)
41
+ [![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/blob/master/.pre-commit-config.yaml)
42
+ [![Semantic Versions](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--versions-e10079.svg)](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/releases)
43
+ [![License](https://img.shields.io/pypi/l/hyperliquid-python-sdk)](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/blob/master/LICENSE.md)
44
+
45
+ SDK for Hyperliquid API trading with Python.
46
+
47
+ </div>
48
+
49
+ ## Installation
50
+ ```bash
51
+ pip install hyperliquid-python-sdk
52
+ ```
53
+ ## Configuration
54
+
55
+ - Set the public key as the `account_address` in examples/config.json.
56
+ - Set your private key as the `secret_key` in examples/config.json.
57
+ - See the example of loading the config in examples/example_utils.py
58
+
59
+ ### [Optional] Generate a new API key for an API Wallet
60
+ Generate and authorize a new API private key on https://app.hyperliquid.xyz/API, and set the API wallet's private key as the `secret_key` in examples/config.json. Note that you must still set the public key of the main wallet *not* the API wallet as the `account_address` in examples/config.json
61
+
62
+ ## Usage Examples
63
+ ```python
64
+ import asyncio
65
+
66
+ from hyperliquid.info import Info
67
+ from hyperliquid.utils import constants
68
+
69
+
70
+ async def main():
71
+ async with Info(constants.TESTNET_API_URL, skip_ws=True) as info:
72
+ user_state = await info.user_state("0xcd5051944f780a621ee62e39e493c489668acf4d")
73
+ print(user_state)
74
+
75
+
76
+ asyncio.run(main())
77
+ ```
78
+ See [examples](examples) for more complete examples. You can also checkout the repo and run any of the examples after configuring your private key e.g.
79
+ ```bash
80
+ cp examples/config.json.example examples/config.json
81
+ vim examples/config.json
82
+ python examples/basic_order.py
83
+ ```
84
+
85
+ ## Getting started with contributing to this repo
86
+
87
+ 1. Download `Poetry`: https://python-poetry.org/.
88
+ - Note that in the install script you might have to set `symlinks=True` in `venv.EnvBuilder`.
89
+ - Note that Poetry v2 is not supported, so you'll need to specify a specific version e.g. curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.4.1 python3 -
90
+
91
+ 2. Point poetry to correct version of python. For development we require python 3.10 exactly. Some dependencies have issues on 3.11, while older versions don't have correct typing support.
92
+ `brew install python@3.10 && poetry env use /opt/homebrew/Cellar/python@3.10/3.10.16/bin/python3.10`
93
+
94
+ 3. Install dependencies:
95
+
96
+ ```bash
97
+ make install
98
+ ```
99
+
100
+ ### Makefile usage
101
+
102
+ CLI commands for faster development. See `make help` for more details.
103
+
104
+ ```bash
105
+ check-safety Run safety checks on dependencies
106
+ cleanup Cleanup project
107
+ install Install dependencies from poetry.lock
108
+ install-types Find and install additional types for mypy
109
+ lint Alias for the pre-commit target
110
+ lockfile-update Update poetry.lock
111
+ lockfile-update-full Fully regenerate poetry.lock
112
+ poetry-download Download and install poetry
113
+ pre-commit Run linters + formatters via pre-commit, run "make pre-commit hook=black" to run only black
114
+ test Run tests with pytest
115
+ update-dev-deps Update development dependencies to latest versions
116
+ ```
117
+
118
+ ## Releases
119
+
120
+ You can see the list of available releases on the [GitHub Releases](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/releases) page.
121
+
122
+ We follow the [Semantic Versions](https://semver.org/) specification and use [`Release Drafter`](https://github.com/marketplace/actions/release-drafter). As pull requests are merged, a draft release is kept up-to-date listing the changes, ready to publish when you’re ready. With the categories option, you can categorize pull requests in release notes using labels.
123
+
124
+ ### List of labels and corresponding titles
125
+
126
+ | **Label** | **Title in Releases** |
127
+ | :-----------------------------------: | :---------------------: |
128
+ | `enhancement`, `feature` | Features |
129
+ | `bug`, `refactoring`, `bugfix`, `fix` | Fixes & Refactoring |
130
+ | `build`, `ci`, `testing` | Build System & CI/CD |
131
+ | `breaking` | Breaking Changes |
132
+ | `documentation` | Documentation |
133
+ | `dependencies` | Dependencies updates |
134
+
135
+ ### Building and releasing
136
+
137
+ Building a new version of the application contains steps:
138
+
139
+ - Bump the version of your package with `poetry version <version>`. You can pass the new version explicitly, or a rule such as `major`, `minor`, or `patch`. For more details, refer to the [Semantic Versions](https://semver.org/) standard.
140
+ - Make a commit to `GitHub`
141
+ - Create a `GitHub release`
142
+ - `poetry publish --build`
143
+
144
+ ## License
145
+
146
+ This project is licensed under the terms of the `MIT` license. See [LICENSE](LICENSE.md) for more details.
147
+
148
+ ```bibtex
149
+ @misc{hyperliquid-python-sdk,
150
+ author = {Hyperliquid},
151
+ title = {SDK for Hyperliquid API trading with Python.},
152
+ year = {2024},
153
+ publisher = {GitHub},
154
+ journal = {GitHub repository},
155
+ howpublished = {\url{https://github.com/hyperliquid-dex/hyperliquid-python-sdk}}
156
+ }
157
+ ```
158
+
159
+ ## Credits
160
+
161
+ This project was generated with [`python-package-template`](https://github.com/TezRomacH/python-package-template).
162
+
@@ -0,0 +1,129 @@
1
+ # hyperliquid-python-sdk
2
+
3
+ <div align="center">
4
+
5
+ [![Dependencies Status](https://img.shields.io/badge/dependencies-up%20to%20date-brightgreen.svg)](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/pulls?utf8=%E2%9C%93&q=is%3Apr%20author%3Aapp%2Fdependabot)
6
+
7
+ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
8
+ [![Security: bandit](https://img.shields.io/badge/security-bandit-green.svg)](https://github.com/PyCQA/bandit)
9
+ [![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/blob/master/.pre-commit-config.yaml)
10
+ [![Semantic Versions](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--versions-e10079.svg)](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/releases)
11
+ [![License](https://img.shields.io/pypi/l/hyperliquid-python-sdk)](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/blob/master/LICENSE.md)
12
+
13
+ SDK for Hyperliquid API trading with Python.
14
+
15
+ </div>
16
+
17
+ ## Installation
18
+ ```bash
19
+ pip install hyperliquid-python-sdk
20
+ ```
21
+ ## Configuration
22
+
23
+ - Set the public key as the `account_address` in examples/config.json.
24
+ - Set your private key as the `secret_key` in examples/config.json.
25
+ - See the example of loading the config in examples/example_utils.py
26
+
27
+ ### [Optional] Generate a new API key for an API Wallet
28
+ Generate and authorize a new API private key on https://app.hyperliquid.xyz/API, and set the API wallet's private key as the `secret_key` in examples/config.json. Note that you must still set the public key of the main wallet *not* the API wallet as the `account_address` in examples/config.json
29
+
30
+ ## Usage Examples
31
+ ```python
32
+ import asyncio
33
+
34
+ from hyperliquid.info import Info
35
+ from hyperliquid.utils import constants
36
+
37
+
38
+ async def main():
39
+ async with Info(constants.TESTNET_API_URL, skip_ws=True) as info:
40
+ user_state = await info.user_state("0xcd5051944f780a621ee62e39e493c489668acf4d")
41
+ print(user_state)
42
+
43
+
44
+ asyncio.run(main())
45
+ ```
46
+ See [examples](examples) for more complete examples. You can also checkout the repo and run any of the examples after configuring your private key e.g.
47
+ ```bash
48
+ cp examples/config.json.example examples/config.json
49
+ vim examples/config.json
50
+ python examples/basic_order.py
51
+ ```
52
+
53
+ ## Getting started with contributing to this repo
54
+
55
+ 1. Download `Poetry`: https://python-poetry.org/.
56
+ - Note that in the install script you might have to set `symlinks=True` in `venv.EnvBuilder`.
57
+ - Note that Poetry v2 is not supported, so you'll need to specify a specific version e.g. curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.4.1 python3 -
58
+
59
+ 2. Point poetry to correct version of python. For development we require python 3.10 exactly. Some dependencies have issues on 3.11, while older versions don't have correct typing support.
60
+ `brew install python@3.10 && poetry env use /opt/homebrew/Cellar/python@3.10/3.10.16/bin/python3.10`
61
+
62
+ 3. Install dependencies:
63
+
64
+ ```bash
65
+ make install
66
+ ```
67
+
68
+ ### Makefile usage
69
+
70
+ CLI commands for faster development. See `make help` for more details.
71
+
72
+ ```bash
73
+ check-safety Run safety checks on dependencies
74
+ cleanup Cleanup project
75
+ install Install dependencies from poetry.lock
76
+ install-types Find and install additional types for mypy
77
+ lint Alias for the pre-commit target
78
+ lockfile-update Update poetry.lock
79
+ lockfile-update-full Fully regenerate poetry.lock
80
+ poetry-download Download and install poetry
81
+ pre-commit Run linters + formatters via pre-commit, run "make pre-commit hook=black" to run only black
82
+ test Run tests with pytest
83
+ update-dev-deps Update development dependencies to latest versions
84
+ ```
85
+
86
+ ## Releases
87
+
88
+ You can see the list of available releases on the [GitHub Releases](https://github.com/hyperliquid-dex/hyperliquid-python-sdk/releases) page.
89
+
90
+ We follow the [Semantic Versions](https://semver.org/) specification and use [`Release Drafter`](https://github.com/marketplace/actions/release-drafter). As pull requests are merged, a draft release is kept up-to-date listing the changes, ready to publish when you’re ready. With the categories option, you can categorize pull requests in release notes using labels.
91
+
92
+ ### List of labels and corresponding titles
93
+
94
+ | **Label** | **Title in Releases** |
95
+ | :-----------------------------------: | :---------------------: |
96
+ | `enhancement`, `feature` | Features |
97
+ | `bug`, `refactoring`, `bugfix`, `fix` | Fixes & Refactoring |
98
+ | `build`, `ci`, `testing` | Build System & CI/CD |
99
+ | `breaking` | Breaking Changes |
100
+ | `documentation` | Documentation |
101
+ | `dependencies` | Dependencies updates |
102
+
103
+ ### Building and releasing
104
+
105
+ Building a new version of the application contains steps:
106
+
107
+ - Bump the version of your package with `poetry version <version>`. You can pass the new version explicitly, or a rule such as `major`, `minor`, or `patch`. For more details, refer to the [Semantic Versions](https://semver.org/) standard.
108
+ - Make a commit to `GitHub`
109
+ - Create a `GitHub release`
110
+ - `poetry publish --build`
111
+
112
+ ## License
113
+
114
+ This project is licensed under the terms of the `MIT` license. See [LICENSE](LICENSE.md) for more details.
115
+
116
+ ```bibtex
117
+ @misc{hyperliquid-python-sdk,
118
+ author = {Hyperliquid},
119
+ title = {SDK for Hyperliquid API trading with Python.},
120
+ year = {2024},
121
+ publisher = {GitHub},
122
+ journal = {GitHub repository},
123
+ howpublished = {\url{https://github.com/hyperliquid-dex/hyperliquid-python-sdk}}
124
+ }
125
+ ```
126
+
127
+ ## Credits
128
+
129
+ This project was generated with [`python-package-template`](https://github.com/TezRomacH/python-package-template).
@@ -0,0 +1,69 @@
1
+ import json
2
+ import logging
3
+ from json import JSONDecodeError
4
+
5
+ import aiohttp
6
+
7
+ from hyperliquid.utils.constants import MAINNET_API_URL
8
+ from hyperliquid.utils.error import ClientError, ServerError
9
+ from hyperliquid.utils.types import Any, Optional
10
+
11
+
12
+ class API:
13
+ def __init__(self, base_url: Optional[str] = None, timeout: Optional[float] = None, session=None):
14
+ self.base_url = base_url or MAINNET_API_URL
15
+ self.session: Optional[aiohttp.ClientSession] = session
16
+ self._owns_session = session is None
17
+ self._logger = logging.getLogger(__name__)
18
+ self.timeout = timeout
19
+
20
+ if self.session is not None:
21
+ self.session.headers.update({"Content-Type": "application/json"})
22
+
23
+ async def _ensure_session(self) -> aiohttp.ClientSession:
24
+ if self.session is None or self.session.closed:
25
+ timeout = aiohttp.ClientTimeout(total=self.timeout) if self.timeout is not None else None
26
+ self.session = aiohttp.ClientSession(timeout=timeout, headers={"Content-Type": "application/json"})
27
+ self._owns_session = True
28
+ return self.session
29
+
30
+ async def aclose(self) -> None:
31
+ if self.session is not None and not self.session.closed and self._owns_session:
32
+ await self.session.close()
33
+
34
+ async def __aenter__(self):
35
+ await self._ensure_session()
36
+ return self
37
+
38
+ async def __aexit__(self, exc_type, exc, tb) -> None:
39
+ await self.aclose()
40
+
41
+ async def post(self, url_path: str, payload: Any = None) -> Any:
42
+ payload = payload or {}
43
+ url = self.base_url + url_path
44
+ session = await self._ensure_session()
45
+
46
+ async with session.post(url, json=payload) as response:
47
+ await self._handle_exception(response)
48
+ try:
49
+ return await response.json()
50
+ except (ValueError, aiohttp.ContentTypeError):
51
+ return {"error": f"Could not parse JSON: {await response.text()}"}
52
+
53
+ async def _handle_exception(self, response: aiohttp.ClientResponse) -> None:
54
+ status_code = response.status
55
+ if status_code < 400:
56
+ return
57
+
58
+ text = await response.text()
59
+ if 400 <= status_code < 500:
60
+ try:
61
+ err = json.loads(text)
62
+ except JSONDecodeError as exc:
63
+ raise ClientError(status_code, None, text, None, response.headers) from exc
64
+ if err is None:
65
+ raise ClientError(status_code, None, text, None, response.headers)
66
+ error_data = err.get("data")
67
+ raise ClientError(status_code, err["code"], err["msg"], response.headers, error_data)
68
+
69
+ raise ServerError(status_code, text)