Skip to content

Commit

Permalink
Adapting to Lastet Near Lake Framework (#9)
Browse files Browse the repository at this point in the history
1. Drop CI python version to 3.11 (near lake framework doesn't work with 3.12)
2. Load .env in Makefile
3. [Minor] Fix enumeration in Readme
4. 🔑 Adapt main file to use latest near-lake-framework
5. Use >= on requirements file (also version bump near-lake-framework to 0.0.8)
6. Add AWS env details to readme (closes #7)
  • Loading branch information
bh2smith authored May 29, 2024
1 parent 55bdaa4 commit ab2bf55
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python 3.12
- name: Setup Python 3.11
uses: actions/setup-python@v5
with:
python-version: '3.12'
python-version: '3.11'
- name: Install Requirements
run:
pip install -r requirements.txt
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
include .env

export $(shell sed 's/=.*//' .env)

# Configuration
VENV_NAME ?= .venv
PYTHON := $(VENV_NAME)/bin/python3
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
Picks up events emitted from the gas station contract used for generating signed foreign chain transactions and calls the multichain relayer `/send_funding_and_user_signed_txns` endpoint locally

# Run
1. ensure you have https://github.com/near/multichain-relayer-server running on localhost:3030
1. Ensure you have https://github.com/near/multichain-relayer-server running on localhost:3030
2. `make install` create virtual environment and install `requirements.txt`
2. update the config.toml with the appropriate values
3. `make run` runs the `gas_station_event_indexer.py` script
3. Update the config.toml with the appropriate values
4. `make run` runs the `gas_station_event_indexer.py` script

*Note that* you will also have to populate an environment file
```shell
cp .env.example .env
```
containing AWS credentials for reading from [Near Lake](https://docs.near.org/tools/realtime#near-lake-indexer).
43 changes: 8 additions & 35 deletions gas_station_event_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import json
import os
from dataclasses import dataclass, fields
from enum import Enum
from typing import Optional, Any

import requests
import toml
from dataclasses_json import DataClassJsonMixin
from near_lake_framework import near_primitives, LakeConfig, streamer
from near_lake_framework import near_primitives, LakeConfig, streamer, Network

REQUEST_TIMEOUT = 10
ParsedLog = dict[str, Any]
Expand Down Expand Up @@ -52,32 +51,6 @@ def send_to_service(self) -> None:
print(f"HTTP Request failed: {str(e)}")


class Network(Enum):
"""
Representing Near Lake Framework networks.
TODO - this Enum actually belongs in the `near_lake_framework.enums` module.
"""

MAINNET = "mainnet"
TESTNET = "testnet"

@staticmethod
def from_string(value: str) -> Network:
try:
return Network(value.lower())
except ValueError as err:
valid_values = [v.value for v in Network]
raise ValueError(
f"Unknown network: {value}. Valid values are: {valid_values}"
) from err

def __str__(self) -> str:
return self.value

def __repr__(self) -> str:
return self.value


@dataclass
class Config:
"""
Expand Down Expand Up @@ -126,7 +99,7 @@ def fetch_latest_block(
)

# Parse the JSON response to get the latest final block height
latest_final_block = response.json()["result"]["header"]["height"]
latest_final_block: int = response.json()["result"]["header"]["height"]

return latest_final_block

Expand Down Expand Up @@ -197,7 +170,7 @@ def process_log(log: str, receipt: near_primitives.Receipt) -> bool:
def process_receipt_if_gas_station_contract(
receipt: near_primitives.Receipt, parsed_log: ParsedLog
) -> bool:
if not receipt.receiver_id.endswith(config.contract_id):
if not receipt.receiver_id.endswith(CONFIG.contract_id):
return False

try:
Expand Down Expand Up @@ -226,24 +199,24 @@ async def handle_streamer_message(


async def main() -> None:
latest_final_block = fetch_latest_block(network=config.network)
latest_final_block = fetch_latest_block(network=CONFIG.network)
lake_config = LakeConfig(
s3_bucket_name=f"near-lake-data-{config.network}",
s3_region_name="eu-central-1",
network=CONFIG.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"],
)
print(f"Latest final block: {latest_final_block} on network: {config.network}")
print(f"Latest final block: {latest_final_block} on network: {CONFIG.network}")

_stream_handle, streamer_messages_queue = streamer(lake_config)
while True:
streamer_message = await streamer_messages_queue.get()
await handle_streamer_message(streamer_message)


CONFIG = Config.from_toml()

if __name__ == "__main__":
config = Config.from_toml()
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
asyncio==3.4.3
near-lake-framework==0.0.7
requests==2.31.0
toml==0.10.2
asyncio>=3.4.3
near-lake-framework>=0.0.8
requests>=2.32.0
toml>=0.10.2

black>=24.4.0
pylint>=3.1.0
Expand Down

0 comments on commit ab2bf55

Please sign in to comment.