Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.

Commit 3a0c2c3

Browse files
committed
added page key for getting asset transfers
1 parent 690d770 commit 3a0c2c3

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

alchemy_sdk_py/api.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from typing import Tuple
23
import requests
34
import json
45
from decimal import Decimal
@@ -33,17 +34,20 @@ def get_current_block() -> int:
3334
return result
3435

3536

36-
def get_asset_transfers(node_address: str, block_start: int, block_end: int) -> list:
37+
def get_asset_transfers(
38+
from_address: str, from_block: int, to_block: int, max_count=1000, page_key=None
39+
) -> Tuple[list, str]:
3740
"""
3841
params:
39-
node_address: wallet address associated with the OCR node
42+
from_address: Address to look for transactions from
4043
block_start: INT
4144
block_end: INT
42-
returns: A dictionary, internal/external asset transfers for the address
45+
returns: [list, str]
46+
A Tuple, index 0 is the list of transfers, index 1 is the page key or None
4347
"""
44-
from_block = str(hex(block_start))
45-
to_block = str(hex(block_end))
46-
address = node_address.lower()
48+
from_block = str(hex(from_block))
49+
to_block = str(hex(to_block))
50+
address = from_address.lower()
4751
payload = {
4852
"id": 1,
4953
"jsonrpc": "2.0",
@@ -53,16 +57,21 @@ def get_asset_transfers(node_address: str, block_start: int, block_end: int) ->
5357
"fromBlock": from_block,
5458
"toBlock": to_block,
5559
"fromAddress": address,
56-
"category": ["external", "internal"],
60+
"category": ["external", "internal", "erc20", "erc721", "specialnft"],
5761
"excludeZeroValue": False,
62+
"maxCount": hex(max_count),
5863
}
5964
],
6065
}
66+
if page_key:
67+
payload["params"][0]["pageKey"] = page_key
6168
response = requests.post(BASE_URL, json=payload, headers=HEADERS)
62-
response = json.loads(response.text)
63-
result = response["result"]
69+
json_response = response.json()
70+
result = json_response["result"]
6471
transfers = result["transfers"]
65-
return transfers
72+
if "pageKey" in result:
73+
return transfers, result["pageKey"]
74+
return transfers, None
6675

6776

6877
def get_transaction_receipt(transaction_hash: str):
@@ -79,8 +88,8 @@ def get_transaction_receipt(transaction_hash: str):
7988
"params": [transaction_hash],
8089
}
8190
response = requests.post(BASE_URL, json=payload, headers=HEADERS)
82-
response = json.loads(response.text)
83-
result = response["result"]
91+
json_respnse = response.json()
92+
result = json_respnse["result"]
8493
return result
8594

8695

tests/test_api.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from alchemy_sdk_py import set_api_key, get_current_block
1+
from alchemy_sdk_py import set_api_key, get_current_block, get_asset_transfers
22
import os
33

44

@@ -17,3 +17,29 @@ def test_get_current_block():
1717

1818
# Assert
1919
assert current_block > 0
20+
21+
22+
def test_get_asset_transfers_page_key():
23+
# Arrange
24+
start_block = 0
25+
end_block = 16271807
26+
address = "0x165Ff6730D449Af03B4eE1E48122227a3328A1fc"
27+
28+
# Act
29+
_, page_key = get_asset_transfers(address, start_block, end_block)
30+
31+
# Assert
32+
assert page_key is not None
33+
34+
35+
def test_get_asset_transfers_page_key_is_none():
36+
# Arrange
37+
start_block = 16271807
38+
end_block = 16271807
39+
address = "0x165Ff6730D449Af03B4eE1E48122227a3328A1fc"
40+
41+
# Act
42+
_, page_key = get_asset_transfers(address, start_block, end_block)
43+
44+
# Assert
45+
assert page_key is None

0 commit comments

Comments
 (0)