-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ankr #137
Closed
Closed
Ankr #137
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f6b5c12
add ankr support
AlexTheLion123 11a8766
add blockchain enum
AlexTheLion123 6d8033a
make sync instead of async
AlexTheLion123 e2eac93
add docstrings
AlexTheLion123 9fdd332
add request_id
AlexTheLion123 0b7f1e1
update changelog
AlexTheLion123 1f8bf78
add url
AlexTheLion123 6385f0e
update imports
AlexTheLion123 b748c4e
Merge branch 'master' into ankr
AlexTheLion123 f29c50d
update changelog
AlexTheLion123 b1a8f58
Merge branch 'master' into ankr
AlexTheLion123 a7d67c7
update comment
AlexTheLion123 75a2ef7
Merge branch 'master' into ankr
AlexTheLion123 3910153
refix merge
AlexTheLion123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import requests | ||
import json | ||
from enum import Enum | ||
|
||
from eth_defi.event_reader.block_header import BlockHeader | ||
|
||
|
||
class AnkrSupportedBlockchain(Enum): | ||
eth = "eth" | ||
bsc = "bsc" | ||
polygon = "polygon" | ||
fantom = "fantom" | ||
arbitrum = "arbitrum" | ||
avalanche = "avalanche" | ||
syscoin = "syscoin" | ||
|
||
|
||
def make_block_request_ankr(endpoint_url: str = "https://rpc.ankr.com/multichain/79258ce7f7ee046decc3b5292a24eb4bf7c910d7e39b691384c7ce0cfb839a01/", start_block: int | str = "latest", end_block: int | str = "latest", blockchain: AnkrSupportedBlockchain | None = None, request_id: int = 1) -> list[dict]: | ||
"""Fetch blocks from Ankr API | ||
|
||
:param endpoint_url: URL of Ankr API endpoint. Should be multichain endpoint. | ||
|
||
:param start_block: Block number to start fetching from. Can be an int or "latest". | ||
|
||
:param end_block: Block number to end fetching at. Can be an int or "latest". | ||
|
||
:param blockchain: Blockchain to fetch blocks from. Must be of type AnkrSupportedBlockchain. | ||
|
||
:return: List of blocks in JSON format. | ||
""" | ||
if start_block == "latest": | ||
end_block = "latest" | ||
|
||
assert type(start_block) == int or start_block == "latest", "start_block must be an int or 'latest'" | ||
assert type(end_block) == int or end_block == "latest", "end_block must be an int or None" | ||
assert isinstance(blockchain, AnkrSupportedBlockchain), "blockchain must be of type AnkrSupportedBlockchain" | ||
|
||
headers = { | ||
"accept": "application/json", | ||
"content-type": "application/json", | ||
} | ||
|
||
data = { | ||
"jsonrpc": "2.0", | ||
"method": "ankr_getBlocks", | ||
"params": { | ||
"blockchain": blockchain.value, | ||
"fromBlock": start_block, | ||
"toBlock": end_block, | ||
"includeTxs": False, | ||
"includeLogs": False, | ||
}, | ||
"id": request_id, | ||
} | ||
|
||
result = requests.post(endpoint_url, headers=headers, json=data) | ||
|
||
j = result.json() | ||
|
||
blocks = j["result"]["blocks"] | ||
|
||
return blocks | ||
|
||
|
||
def extract_timestamps_ankr_get_block( | ||
endpoint_url: str, | ||
start_block: int | None = None, | ||
end_block: int | None = None, | ||
max_blocks_at_once: int = 30, | ||
) -> list[int]: | ||
"""Extract timestamps from Ankr API | ||
|
||
:param endpoint_url: URL of Ankr API endpoint. Should be multichain endpoint. | ||
|
||
:param start_block: Block number to start fetching from. Can be an int or None. | ||
|
||
:param end_block: Block number to end fetching at. Can be an int or None. | ||
|
||
:param max_blocks_at_once: Maximum number of blocks to fetch at once. Default is 30. | ||
|
||
:return: List of timestamps in int format. | ||
""" | ||
timestamps = [] | ||
|
||
for i in range(start_block, end_block + 1, max_blocks_at_once): | ||
blocks = make_block_request_ankr(endpoint_url, i, min(i + max_blocks_at_once - 1, end_block)) | ||
timestamps.extend([int(x["timestamp"], 16) for x in blocks]) | ||
|
||
return timestamps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Test chain reogranisation monitor and connect to live Ankr network.""" | ||
|
||
import os | ||
|
||
import pytest | ||
from web3 import HTTPProvider | ||
|
||
from eth_defi.chain import has_ankr_support | ||
from eth_defi.event_reader.reorganisation_monitor import AnkrReogranisationMonitor, AnkrSupportedBlockchain | ||
|
||
pytestmark = pytest.mark.skipif( | ||
os.environ.get("JSON_RPC_ANKR_PRIVATE") is None, | ||
reason="Set JSON_RPC_ANKR_PRIVATE environment variable to a privately configured Polygon node with GraphQL turned on", | ||
) | ||
|
||
|
||
def test_ankr_last_block(): | ||
"""Get last block num using Ankr API.""" | ||
|
||
provider = HTTPProvider(os.environ["JSON_RPC_ANKR_PRIVATE"]) | ||
assert has_ankr_support(provider) | ||
|
||
reorg_mon = AnkrReogranisationMonitor(provider=provider, blockchain=AnkrSupportedBlockchain.arbitrum) | ||
block_number = reorg_mon.get_last_block_live() | ||
assert block_number > 30_000_000 | ||
|
||
|
||
def test_ankr_block_headers(): | ||
"""Download block headers using Ankr API.""" | ||
|
||
provider = HTTPProvider(os.environ["JSON_RPC_ANKR_PRIVATE"]) | ||
assert has_ankr_support(provider) | ||
|
||
reorg_mon = AnkrReogranisationMonitor(provider=provider, blockchain=AnkrSupportedBlockchain.arbitrum) | ||
|
||
start_block, end_block = reorg_mon.load_initial_block_headers(block_count=5) | ||
|
||
block = reorg_mon.get_block_by_number(start_block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we always download the same blocks in the test, you can actually check real block numbers and timestamps here. They will always stay stable as long as the start block is the same. |
||
assert block.block_number > 0 | ||
assert block.block_hash.startswith("0x") | ||
assert block.timestamp > 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must use Web3 instance, not direct
endpoint_url
, because otherwise the function won't be compatible or in line with the rest of the library.For making custom RPC requests, see
anvil.py
for examples.If you need to fish out the
endpoint_url
from web3 object you need to do:As the provider may also use
WebsocketProvider
and in that case we should fail with an exception until a special support is added.