Skip to content
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
4 changes: 2 additions & 2 deletions assets/images/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 86 additions & 1 deletion hyperliquid/info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from hyperliquid.api import API
from hyperliquid.utils.types import Any, Callable, Meta, Subscription, cast
from hyperliquid.utils.types import Any, Callable, Meta, Optional, Subscription, cast
from hyperliquid.websocket_manager import WebsocketManager


Expand Down Expand Up @@ -137,6 +137,91 @@ def meta(self) -> Meta:
"""
return cast(Meta, self.post("/info", {"type": "meta"}))

def funding_history(self, coin: str, startTime: int, endTime: Optional[int] = None) -> Any:
"""Retrieve funding history for a given coin

POST /info

Args:
coin (str): Coin to retrieve funding history for.
startTime (int): Unix timestamp in milliseconds.
endTime (int): Unix timestamp in milliseconds.

Returns:
[
{
coin: str,
fundingRate: float string,
premium: float string,
time: int
},
...
]
"""
if endTime is not None:
return self.post(
"/info", {"type": "fundingHistory", "coin": coin, "startTime": startTime, "endTime": endTime}
)
return self.post("/info", {"type": "fundingHistory", "coin": coin, "startTime": startTime})

def l2_snapshot(self, coin: str) -> Any:
"""Retrieve L2 snapshot for a given coin

POST /info

Args:
coin (str): Coin to retrieve L2 snapshot for.

Returns:
{
coin: str,
levels: [
[
{
n: int,
px: float string,
sz: float string
},
...
],
...
],
time: int
}
"""
return self.post("/info", {"type": "l2Book", "coin": coin})

def candles_snapshot(self, coin: str, interval: str, startTime: int, endTime: int) -> Any:
"""Retrieve candles snapshot for a given coin

POST /info

Args:
coin (str): Coin to retrieve candles snapshot for.
interval (str): Candlestick interval.
startTime (int): Unix timestamp in milliseconds.
endTime (int): Unix timestamp in milliseconds.

Returns:
[
{
T: int,
c: float string,
h: float string,
i: str,
l: float string,
n: int,
o: float string,
s: string,
t: int,
v: float string
},
...
]
"""
req = {"coin": coin, "interval": interval, "startTime": startTime, "endTime": endTime}
return self.post("/info", {"type": "candleSnapshot", "req": req})

def subscribe(self, subscription: Subscription, callback: Callable[[Any], None]) -> int:
if self.ws_manager is None:
raise RuntimeError("Cannot call subscribe since skip_ws was used")
Expand Down
41 changes: 41 additions & 0 deletions tests/cassettes/test_get_candles_snapshot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
interactions:
- request:
body: '{"type": "candleSnapshot", "req": {"coin": "kPEPE", "interval": "1h", "startTime":
1684702007000, "endTime": 1684784807000}}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '124'
Content-Type:
- application/json
User-Agent:
- python-requests/2.28.2
method: POST
uri: https://api.hyperliquid.xyz/info
response:
body:
string: '[{"T":1684702799999,"c":"0.001604","h":"0.001616","i":"1h","l":"0.001601","n":709,"o":"0.001601","s":"kPEPE","t":1684699200000,"v":"520665683.0"},{"T":1684706399999,"c":"0.001591","h":"0.001603","i":"1h","l":"0.001584","n":802,"o":"0.001603","s":"kPEPE","t":1684702800000,"v":"682593287.0"},{"T":1684709999999,"c":"0.001605","h":"0.001609","i":"1h","l":"0.001591","n":424,"o":"0.001591","s":"kPEPE","t":1684706400000,"v":"316205596.0"},{"T":1684713599999,"c":"0.001596","h":"0.001608","i":"1h","l":"0.001592","n":308,"o":"0.001605","s":"kPEPE","t":1684710000000,"v":"235815170.0"},{"T":1684717199999,"c":"0.00155","h":"0.001598","i":"1h","l":"0.001532","n":1913,"o":"0.001596","s":"kPEPE","t":1684713600000,"v":"1943301529.0"},{"T":1684720799999,"c":"0.001545","h":"0.001555","i":"1h","l":"0.001535","n":990,"o":"0.001549","s":"kPEPE","t":1684717200000,"v":"727931405.0"},{"T":1684724399999,"c":"0.001542","h":"0.001555","i":"1h","l":"0.001525","n":917,"o":"0.001545","s":"kPEPE","t":1684720800000,"v":"745882596.0"},{"T":1684727999999,"c":"0.001542","h":"0.001547","i":"1h","l":"0.001534","n":455,"o":"0.001544","s":"kPEPE","t":1684724400000,"v":"317711025.0"},{"T":1684731599999,"c":"0.001548","h":"0.001557","i":"1h","l":"0.001536","n":672,"o":"0.001541","s":"kPEPE","t":1684728000000,"v":"491887591.0"},{"T":1684735199999,"c":"0.001543","h":"0.00155","i":"1h","l":"0.001537","n":428,"o":"0.001548","s":"kPEPE","t":1684731600000,"v":"269811863.0"},{"T":1684738799999,"c":"0.001563","h":"0.001576","i":"1h","l":"0.001541","n":1228,"o":"0.001543","s":"kPEPE","t":1684735200000,"v":"1075874400.0"},{"T":1684742399999,"c":"0.001564","h":"0.001568","i":"1h","l":"0.001555","n":449,"o":"0.001562","s":"kPEPE","t":1684738800000,"v":"278929296.0"},{"T":1684745999999,"c":"0.001559","h":"0.001575","i":"1h","l":"0.001557","n":681,"o":"0.001565","s":"kPEPE","t":1684742400000,"v":"447636970.0"},{"T":1684749599999,"c":"0.001572","h":"0.001586","i":"1h","l":"0.001557","n":915,"o":"0.00156","s":"kPEPE","t":1684746000000,"v":"736537598.0"},{"T":1684753199999,"c":"0.001573","h":"0.001578","i":"1h","l":"0.001567","n":429,"o":"0.001574","s":"kPEPE","t":1684749600000,"v":"225647917.0"},{"T":1684756799999,"c":"0.001583","h":"0.001591","i":"1h","l":"0.001561","n":847,"o":"0.001572","s":"kPEPE","t":1684753200000,"v":"734892082.0"},{"T":1684760399999,"c":"0.001571","h":"0.001586","i":"1h","l":"0.001567","n":696,"o":"0.001582","s":"kPEPE","t":1684756800000,"v":"461986408.0"},{"T":1684763999999,"c":"0.001575","h":"0.001586","i":"1h","l":"0.001559","n":1130,"o":"0.001571","s":"kPEPE","t":1684760400000,"v":"873852989.0"},{"T":1684767599999,"c":"0.001565","h":"0.001579","i":"1h","l":"0.001551","n":617,"o":"0.001577","s":"kPEPE","t":1684764000000,"v":"468121268.0"},{"T":1684771199999,"c":"0.00153","h":"0.001567","i":"1h","l":"0.001507","n":825,"o":"0.001567","s":"kPEPE","t":1684767600000,"v":"655465232.0"},{"T":1684774799999,"c":"0.001528","h":"0.001538","i":"1h","l":"0.001514","n":683,"o":"0.00153","s":"kPEPE","t":1684771200000,"v":"424010634.0"},{"T":1684778399999,"c":"0.00153","h":"0.001534","i":"1h","l":"0.001511","n":888,"o":"0.001527","s":"kPEPE","t":1684774800000,"v":"767341903.0"},{"T":1684781999999,"c":"0.001539","h":"0.001549","i":"1h","l":"0.001528","n":592,"o":"0.001531","s":"kPEPE","t":1684778400000,"v":"521321138.0"},{"T":1684785599999,"c":"0.00154","h":"0.001548","i":"1h","l":"0.001536","n":430,"o":"0.001538","s":"kPEPE","t":1684782000000,"v":"308219489.0"}]'
headers:
access-control-allow-origin:
- '*'
access-control-expose-headers:
- '*'
content-length:
- '3479'
content-type:
- application/json
date:
- Thu, 25 May 2023 02:38:24 GMT
vary:
- origin
- access-control-request-method
- access-control-request-headers
status:
code: 200
message: OK
version: 1
41 changes: 41 additions & 0 deletions tests/cassettes/test_get_funding_history[1684811870000].yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
interactions:
- request:
body: '{"type": "fundingHistory", "coin": "BTC", "startTime": 1681923833000, "endTime":
1684811870000}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '95'
Content-Type:
- application/json
User-Agent:
- python-requests/2.28.2
method: POST
uri: https://api.hyperliquid.xyz/info
response:
body:
string: '[{"coin":"BTC","fundingRate":"-0.00061334","premium":"-0.00091334","time":1683849600048},{"coin":"BTC","fundingRate":"-0.00074503","premium":"-0.00104503","time":1683878400388},{"coin":"BTC","fundingRate":"-0.00081798","premium":"-0.00111798","time":1683907200253},{"coin":"BTC","fundingRate":"-0.00044036","premium":"-0.00074036","time":1683936000278},{"coin":"BTC","fundingRate":"-0.00010343","premium":"-0.00040343","time":1683964800255},{"coin":"BTC","fundingRate":"-0.00013803","premium":"-0.00043803","time":1683993600054},{"coin":"BTC","fundingRate":"-0.0002227","premium":"-0.0005227","time":1684022400087},{"coin":"BTC","fundingRate":"-0.00021372","premium":"-0.00051372","time":1684051200271},{"coin":"BTC","fundingRate":"-0.00019454","premium":"-0.00049454","time":1684080000380},{"coin":"BTC","fundingRate":"-0.0000782","premium":"-0.0003782","time":1684108800374},{"coin":"BTC","fundingRate":"-0.00012763","premium":"-0.00042763","time":1684137600045},{"coin":"BTC","fundingRate":"-0.00012881","premium":"-0.00042881","time":1684166400409},{"coin":"BTC","fundingRate":"-0.00002537","premium":"-0.00032537","time":1684195200447},{"coin":"BTC","fundingRate":"-0.00012952","premium":"-0.00042952","time":1684224000126},{"coin":"BTC","fundingRate":"-0.00004628","premium":"-0.00034628","time":1684252800731},{"coin":"BTC","fundingRate":"-0.00002615","premium":"-0.00032615","time":1684281600067},{"coin":"BTC","fundingRate":"0.0001","premium":"-0.00004426","time":1684310400279},{"coin":"BTC","fundingRate":"0.0001","premium":"-0.00007651","time":1684339200024},{"coin":"BTC","fundingRate":"0.0001","premium":"-0.00004908","time":1684368000048},{"coin":"BTC","fundingRate":"-0.00000646","premium":"-0.00030646","time":1684396800038},{"coin":"BTC","fundingRate":"-0.0000067","premium":"-0.0003067","time":1684425600052},{"coin":"BTC","fundingRate":"0.00008923","premium":"-0.00021077","time":1684454400026},{"coin":"BTC","fundingRate":"0.0001","premium":"-0.00017735","time":1684483200158},{"coin":"BTC","fundingRate":"0.00009162","premium":"-0.00020838","time":1684512000396},{"coin":"BTC","fundingRate":"0.0000366","premium":"-0.0002634","time":1684540800029},{"coin":"BTC","fundingRate":"0.00006101","premium":"-0.00023899","time":1684569600111},{"coin":"BTC","fundingRate":"0.00000651","premium":"-0.00029349","time":1684598400345},{"coin":"BTC","fundingRate":"-0.00021389","premium":"-0.00051389","time":1684627200095},{"coin":"BTC","fundingRate":"-0.00012021","premium":"-0.00042021","time":1684656000013},{"coin":"BTC","fundingRate":"-0.00011985","premium":"-0.00041985","time":1684684800088},{"coin":"BTC","fundingRate":"-0.00015543","premium":"-0.00045543","time":1684713600066},{"coin":"BTC","fundingRate":"-0.0002855","premium":"-0.0005855","time":1684742400290},{"coin":"BTC","fundingRate":"-0.00026923","premium":"-0.00056923","time":1684771200159},{"coin":"BTC","fundingRate":"-0.00018991","premium":"-0.00048991","time":1684800000022}]'
headers:
access-control-allow-origin:
- '*'
access-control-expose-headers:
- '*'
content-length:
- '2958'
content-type:
- application/json
date:
- Thu, 25 May 2023 02:38:22 GMT
vary:
- origin
- access-control-request-method
- access-control-request-headers
status:
code: 200
message: OK
version: 1
40 changes: 40 additions & 0 deletions tests/cassettes/test_get_funding_history[None].yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
interactions:
- request:
body: '{"type": "fundingHistory", "coin": "BTC", "startTime": 1681923833000}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '69'
Content-Type:
- application/json
User-Agent:
- python-requests/2.28.2
method: POST
uri: https://api.hyperliquid.xyz/info
response:
body:
string: '[{"coin":"BTC","fundingRate":"-0.00061334","premium":"-0.00091334","time":1683849600048},{"coin":"BTC","fundingRate":"-0.00074503","premium":"-0.00104503","time":1683878400388},{"coin":"BTC","fundingRate":"-0.00081798","premium":"-0.00111798","time":1683907200253},{"coin":"BTC","fundingRate":"-0.00044036","premium":"-0.00074036","time":1683936000278},{"coin":"BTC","fundingRate":"-0.00010343","premium":"-0.00040343","time":1683964800255},{"coin":"BTC","fundingRate":"-0.00013803","premium":"-0.00043803","time":1683993600054},{"coin":"BTC","fundingRate":"-0.0002227","premium":"-0.0005227","time":1684022400087},{"coin":"BTC","fundingRate":"-0.00021372","premium":"-0.00051372","time":1684051200271},{"coin":"BTC","fundingRate":"-0.00019454","premium":"-0.00049454","time":1684080000380},{"coin":"BTC","fundingRate":"-0.0000782","premium":"-0.0003782","time":1684108800374},{"coin":"BTC","fundingRate":"-0.00012763","premium":"-0.00042763","time":1684137600045},{"coin":"BTC","fundingRate":"-0.00012881","premium":"-0.00042881","time":1684166400409},{"coin":"BTC","fundingRate":"-0.00002537","premium":"-0.00032537","time":1684195200447},{"coin":"BTC","fundingRate":"-0.00012952","premium":"-0.00042952","time":1684224000126},{"coin":"BTC","fundingRate":"-0.00004628","premium":"-0.00034628","time":1684252800731},{"coin":"BTC","fundingRate":"-0.00002615","premium":"-0.00032615","time":1684281600067},{"coin":"BTC","fundingRate":"0.0001","premium":"-0.00004426","time":1684310400279},{"coin":"BTC","fundingRate":"0.0001","premium":"-0.00007651","time":1684339200024},{"coin":"BTC","fundingRate":"0.0001","premium":"-0.00004908","time":1684368000048},{"coin":"BTC","fundingRate":"-0.00000646","premium":"-0.00030646","time":1684396800038},{"coin":"BTC","fundingRate":"-0.0000067","premium":"-0.0003067","time":1684425600052},{"coin":"BTC","fundingRate":"0.00008923","premium":"-0.00021077","time":1684454400026},{"coin":"BTC","fundingRate":"0.0001","premium":"-0.00017735","time":1684483200158},{"coin":"BTC","fundingRate":"0.00009162","premium":"-0.00020838","time":1684512000396},{"coin":"BTC","fundingRate":"0.0000366","premium":"-0.0002634","time":1684540800029},{"coin":"BTC","fundingRate":"0.00006101","premium":"-0.00023899","time":1684569600111},{"coin":"BTC","fundingRate":"0.00000651","premium":"-0.00029349","time":1684598400345},{"coin":"BTC","fundingRate":"-0.00021389","premium":"-0.00051389","time":1684627200095},{"coin":"BTC","fundingRate":"-0.00012021","premium":"-0.00042021","time":1684656000013},{"coin":"BTC","fundingRate":"-0.00011985","premium":"-0.00041985","time":1684684800088},{"coin":"BTC","fundingRate":"-0.00015543","premium":"-0.00045543","time":1684713600066},{"coin":"BTC","fundingRate":"-0.0002855","premium":"-0.0005855","time":1684742400290},{"coin":"BTC","fundingRate":"-0.00026923","premium":"-0.00056923","time":1684771200159},{"coin":"BTC","fundingRate":"-0.00018991","premium":"-0.00048991","time":1684800000022},{"coin":"BTC","fundingRate":"-0.00017541","premium":"-0.00047541","time":1684830233040},{"coin":"BTC","fundingRate":"-0.00015528","premium":"-0.00045528","time":1684857600079},{"coin":"BTC","fundingRate":"-0.0001105","premium":"-0.0004105","time":1684886400035},{"coin":"BTC","fundingRate":"-0.00018093","premium":"-0.00048093","time":1684915200002},{"coin":"BTC","fundingRate":"-0.00019988","premium":"-0.00049988","time":1684944000001},{"coin":"BTC","fundingRate":"-0.00017638","premium":"-0.00047638","time":1684972800233}]'
headers:
access-control-allow-origin:
- '*'
access-control-expose-headers:
- '*'
content-length:
- '3484'
content-type:
- application/json
date:
- Thu, 25 May 2023 02:38:22 GMT
vary:
- origin
- access-control-request-method
- access-control-request-headers
status:
code: 200
message: OK
version: 1
40 changes: 40 additions & 0 deletions tests/cassettes/test_get_l2_snapshot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
interactions:
- request:
body: '{"type": "l2Book", "coin": "DYDX"}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '34'
Content-Type:
- application/json
User-Agent:
- python-requests/2.28.2
method: POST
uri: https://api.hyperliquid.xyz/info
response:
body:
string: '{"coin":"DYDX","levels":[[{"n":1,"px":"1.9844","sz":"360.5"},{"n":1,"px":"1.9836","sz":"5740.8"},{"n":2,"px":"1.9826","sz":"12200.8"},{"n":1,"px":"1.9821","sz":"140.0"},{"n":1,"px":"1.9808","sz":"5314.3"},{"n":1,"px":"1.9804","sz":"6213.0"},{"n":1,"px":"1.9802","sz":"359.3"},{"n":1,"px":"1.9794","sz":"5339.6"},{"n":1,"px":"1.9784","sz":"6303.3"},{"n":1,"px":"1.976","sz":"6040.7"},{"n":1,"px":"1.9755","sz":"5583.9"},{"n":1,"px":"1.9746","sz":"5755.4"},{"n":1,"px":"1.9727","sz":"6032.7"},{"n":1,"px":"1.9718","sz":"6089.2"},{"n":1,"px":"1.9713","sz":"5706.0"},{"n":1,"px":"1.9694","sz":"5524.8"},{"n":1,"px":"1.9685","sz":"6029.6"},{"n":1,"px":"1.9679","sz":"6002.4"},{"n":1,"px":"1.9678","sz":"5727.3"},{"n":1,"px":"1.9677","sz":"6214.0"}],[{"n":2,"px":"1.9849","sz":"722.8"},{"n":2,"px":"1.9857","sz":"11745.3"},{"n":1,"px":"1.9867","sz":"5902.4"},{"n":1,"px":"1.987","sz":"6298.9"},{"n":1,"px":"1.9872","sz":"5861.3"},{"n":1,"px":"1.9889","sz":"6196.0"},{"n":1,"px":"1.9892","sz":"5495.8"},{"n":1,"px":"1.9921","sz":"5592.5"},{"n":1,"px":"1.9924","sz":"5794.2"},{"n":1,"px":"1.9933","sz":"5754.4"},{"n":1,"px":"1.9938","sz":"6318.0"},{"n":1,"px":"1.9966","sz":"6314.4"},{"n":1,"px":"1.9972","sz":"6336.3"},{"n":1,"px":"1.9973","sz":"5453.7"},{"n":1,"px":"1.9981","sz":"5777.1"},{"n":1,"px":"1.9999","sz":"5695.7"},{"n":1,"px":"2.0006","sz":"6191.9"},{"n":1,"px":"2.0016","sz":"5718.2"},{"n":1,"px":"2.0091","sz":"5584.2"},{"n":1,"px":"2.0118","sz":"5376.1"}]],"time":1684982303887}'
headers:
access-control-allow-origin:
- '*'
access-control-expose-headers:
- '*'
content-length:
- '1487'
content-type:
- application/json
date:
- Thu, 25 May 2023 02:38:23 GMT
vary:
- origin
- access-control-request-method
- access-control-request-headers
status:
code: 200
message: OK
version: 1
Loading