-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Logger & Example Run Script (#13)
* add logging * update readme with latest example script * add reqeusts type stubs
- Loading branch information
Showing
10 changed files
with
139 additions
and
37 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ build/ | |
__pycache__/ | ||
.vscode | ||
*.egg-info | ||
.mypy_cache | ||
.mypy_cache | ||
.venv/ |
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
Empty file.
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,33 @@ | ||
import asyncio | ||
import logging | ||
import os | ||
|
||
from near_lake_framework import LakeConfig, streamer, Network | ||
from near_lake_framework.utils import fetch_latest_block | ||
|
||
# Suppress warning logs from specific dependencies | ||
logging.getLogger("near_lake_framework").setLevel(logging.INFO) | ||
|
||
|
||
async def main(): | ||
network = Network.TESTNET | ||
latest_final_block = fetch_latest_block(network=network) | ||
config = LakeConfig( | ||
network, | ||
start_block_height=latest_final_block, | ||
# These fields must be set! | ||
aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"], | ||
aws_secret_key=os.environ["AWS_SECRET_ACCESS_KEY"], | ||
) | ||
|
||
stream_handle, streamer_messages_queue = streamer(config) | ||
while True: | ||
streamer_message = await streamer_messages_queue.get() | ||
print( | ||
f"Received Block #{streamer_message.block.header.height} from Lake Framework" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
loop = asyncio.new_event_loop() | ||
loop.run_until_complete(main()) |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
asyncio==3.4.3 | ||
dataclasses==0.6 | ||
dataclasses-json==0.5.7 | ||
botocore==1.24.21 | ||
aiobotocore==2.3.0 | ||
asyncio>=3.4.3 | ||
dataclasses>=0.6 | ||
dataclasses-json>=0.6.6 | ||
botocore>=1.34.70 | ||
aiobotocore>=2.13.0 | ||
requests>=2.32.2 | ||
|
||
types-botocore>=1.0.2 | ||
types-aiobotocore>=2.13.0 | ||
types-requests>=2.32.0.20240523 |
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
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,37 @@ | ||
import json | ||
|
||
import requests | ||
|
||
from near_lake_framework import Network, near_primitives | ||
|
||
|
||
def fetch_latest_block( | ||
network: Network = Network.MAINNET, timeout: int = 10 | ||
) -> near_primitives.BlockHeight: | ||
""" | ||
Define the RPC endpoint for the NEAR network | ||
""" | ||
url = f"https://rpc.{network}.near.org" | ||
|
||
# Define the payload for fetching the latest block | ||
payload = json.dumps( | ||
{ | ||
"jsonrpc": "2.0", | ||
"id": "dontcare", | ||
"method": "block", | ||
"params": {"finality": "final"}, | ||
} | ||
) | ||
|
||
# Define the headers for the HTTP request | ||
headers = {"Content-Type": "application/json"} | ||
|
||
# Send the HTTP request to the NEAR RPC endpoint | ||
response = requests.request( | ||
"POST", url, headers=headers, data=payload, timeout=timeout | ||
) | ||
|
||
# Parse the JSON response to get the latest final block height | ||
latest_final_block: int = response.json()["result"]["header"]["height"] | ||
|
||
return latest_final_block |