Skip to main content

NEAR Lake Framework for Python

Available in programming languages: Rust | Javascript | Python3

NEAR Lake Framework is a small library companion to NEAR Lake. It allows you to build your own indexer that subscribes to the stream of blocks from the NEAR Lake data source and create your own logic to process the NEAR Protocol data.

PyPI version MIT or Apache 2.0 licensed


Official NEAR Lake Framework launch announcement has been published on the NEAR Gov Forum Greetings from the Data Platform Team! We are happy and proud to announce an MVP release of a brand new word in indexer building approach - NEAR Lake Framework.


Example

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())

Try it yourself as follows

pip3 install -r requirements.txt
python3 example/run.py

How to use

Dependencies

Install near-lake-framework

$ pip3 install near-lake-framework

Credentials

To be able to access the data from NEAR Lake you need to provide credentials. Please, see the Credentials article

Configuration

Everything should be configured before the start of your indexer application via LakeConfig struct.

Available parameters:

  • network: Network - provide network
  • s3_bucket_name: str - provide the AWS S3 bucket name (near-lake-testnet, near-lake-mainnet or yours if you run your own NEAR Lake)
  • s3_region_name: str - provide the region for AWS S3 bucket
  • start_block_height: BlockHeight - block height to start the stream from
  • blocks_preload_pool_size: int - provide the number of blocks to preload (default: 200)

Cost estimates

TL;DR approximately $18.15 per month (for AWS S3 access, paid directly to AWS) for the reading of fresh blocks

Explanation:

Assuming NEAR Protocol produces accurately 1 block per second (which is really not, the average block production time is 1.3s). A full day consists of 86400 seconds, that's the max number of blocks that can be produced.

According the Amazon S3 prices list requests are charged for $0.005 per 1000 requests and get is charged for $0.0004 per 1000 requests.

Calculations (assuming we are following the tip of the network all the time):

86400 blocks per day * 5 requests for each block / 1000 requests * $0.0004 per 1k requests = $0.173 * 30 days = $5.19

Note: 5 requests for each block means we have 4 shards (1 file for common block data and 4 separate files for each shard)

And a number of list requests we need to perform for 30 days:

86400 blocks per day / 1000 requests * $0.005 per 1k list requests = $0.432 * 30 days = $12.96

$5.19 + $12.96 = $18.15

The price depends on the number of shards

Real-life Example

Pagoda, a leading blockchain infrastructure company, utilizes the NEAR Lake Framework for Python in their Gas Station Event Indexer. This real-world implementation showcases the framework's capabilities in efficiently processing and indexing blockchain data.

Publishing to PyPi

Follow this guide to safely publish PyPi package

Metadata

Release files for near-lake-framework 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for near-lake-framework 0.1.3
File Size Uploaded Provenance
near_lake_framework-0.1.3.tar.gz 12.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for near-lake-framework 0.1.3
File Interpreter ABI Platform
near_lake_framework-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 25.0 kB

Release files / near_lake_framework-0.1.3.tar.gz

Download URL near_lake_framework-0.1.3.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3789a1788a2dd8096bdaa84b7c3c2c70ed10cae61f3ad37ea66573d11712ef2d
BLAKE2b-256 checksum
How to use checksums
65f116aa722b58af283668472298d9c635e628d6f1173578fb470894c67d82d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.6

Release files / near_lake_framework-0.1.3-py3-none-any.whl

Download URL near_lake_framework-0.1.3-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d0c53a6b04db43c5e6e0340f7036a8c375379e33670cbf0ea94e12c3ababa36
BLAKE2b-256 checksum
How to use checksums
cd250f3ee85e382d612895ab5c396e2c0698286909fcf52b50a8aa650d4745af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.6

Security for near-lake-framework 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page