Skip to content

Commit cf2024d

Browse files
authored
add function get_user_funding_history (#19)
1 parent 2e9f1ce commit cf2024d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

hyperliquid/info.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,31 @@ def funding_history(self, coin: str, startTime: int, endTime: Optional[int] = No
196196
)
197197
return self.post("/info", {"type": "fundingHistory", "coin": coin, "startTime": startTime})
198198

199+
def user_funding_history(self, user: str, startTime: int, endTime: Optional[int] = None) -> Any:
200+
"""Retrieve a user's funding history
201+
202+
POST /info
203+
204+
Args:
205+
user (str): Address of the user in 42-character hexadecimal format.
206+
startTime (int): Start time in milliseconds, inclusive.
207+
endTime (int, optional): End time in milliseconds, inclusive. Defaults to current time.
208+
209+
Returns:
210+
List[Dict]: A list of funding history records, where each record contains:
211+
- user (str): User address.
212+
- type (str): Type of the record, e.g., "userFunding".
213+
- startTime (int): Unix timestamp of the start time in milliseconds.
214+
- endTime (int): Unix timestamp of the end time in milliseconds.
215+
"""
216+
if endTime is not None:
217+
return self.post(
218+
"/info", {"type": "userFunding", "user": user, "startTime": startTime, "endTime": endTime}
219+
)
220+
return self.post(
221+
"/info", {"type": "userFunding", "user": user, "startTime": startTime}
222+
)
223+
199224
def l2_snapshot(self, coin: str) -> Any:
200225
"""Retrieve L2 snapshot for a given coin
201226

tests/info_test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,39 @@ def test_get_candles_snapshot():
8787
assert len(response) == 24
8888
for key in ["T", "c", "h", "i", "l", "n", "o", "s", "t", "v"]:
8989
assert key in response[0].keys()
90+
91+
92+
@pytest.mark.vcr()
93+
def test_user_funding_history_with_end_time():
94+
info = Info(skip_ws=True)
95+
response = info.user_funding_history(
96+
user="0xb7b6f3cea3f66bf525f5d8f965f6dbf6d9b017b2",
97+
startTime=1681923833000,
98+
endTime=1682010233000
99+
)
100+
assert isinstance(response, list), "The answer should be a list"
101+
for record in response:
102+
assert 'delta' in record, "There must be a key 'delta'"
103+
assert 'hash' in record, "There must be a key 'hash'"
104+
assert 'time' in record, "There must be a key 'time'"
105+
delta = record['delta']
106+
for key in ['coin', 'fundingRate', 'szi', 'type', 'usdc']:
107+
assert key in delta, f"В 'delta' There must be a key '{key}'"
108+
assert delta['type'] == 'funding', "The type must be 'funding'"
109+
110+
@pytest.mark.vcr()
111+
def test_user_funding_history_without_end_time():
112+
info = Info(skip_ws=True)
113+
response = info.user_funding_history(
114+
user="0xb7b6f3cea3f66bf525f5d8f965f6dbf6d9b017b2",
115+
startTime=1681923833000
116+
)
117+
assert isinstance(response, list), "The answer must be a list"
118+
for record in response:
119+
assert 'delta' in record, "There must be a key 'delta'"
120+
assert 'hash' in record, "There must be a key 'hash'"
121+
assert 'time' in record, "There must be a key 'time'"
122+
delta = record['delta']
123+
for key in ['coin', 'fundingRate', 'szi', 'type', 'usdc']:
124+
assert key in delta, f"В 'delta' There must be a '{key}'"
125+
assert delta['type'] == 'funding', "The type must be 'funding'"

0 commit comments

Comments
 (0)