Python용 NFT 인덱서 구축
frolvanya/near-lake-nft-indexer
: source code for this tutorial
목표
This tutorial ends with a working NFT indexer built on top NEAR Lake Framework for Python. The indexer is watching for nft_mint
Events and prints some relevant data:
receipt_id
of the Receipt where the mint has happened- 마켓플레이스
- NFT 소유자 계정 이름
- 마켓플레이스 내 NFT로의 링크
The final source code is available on the GitHub frolvanya/near-lake-nft-indexer
동기
NEAR Protocol had introduced a nice feature Events. The Events allow a contract developer to add standardized logs to the ExecutionOutcomes
thus allowing themselves or other developers to read those logs in more convenient manner via API or indexers.
The Events have a field standard
which aligns with NEPs. In this tutorial we'll be talking about NEP-171 Non-Fungible Token standard.
In this tutorial our goal is to show you how you can "listen" to the Events contracts emit and how you can benefit from them.
As the example we will be building an indexer that watches all the NFTs minted following the NEP-171 Events standard, assuming we're collectors who don't want to miss a thing. Our indexer should notice every single NFT minted and give us a basic set of data like: in what Receipt it was minted, and show us the link to a marketplace (we'll cover Paras and Mintbase in our example).
We will use Python version of NEAR Lake Framework in this tutorial. Though the concept is the same for Rust, but we want to show more people that it's not that complex to build your own indexer.
준비
Please, ensure you've the credentials set up as described on the Credentials page. Otherwise you won't be able to get the code working.
Let's create our project folder
mkdir lake-nft-indexer && cd lake-nft-indexer
touch main.py
NEAR Lake 프레임워크 설정
In the main.py
let's import stream
function and near_primitives
from near-lake-framework
:
from near_lake_framework import near_primitives, LakeConfig, streamer
Add the main function
async def main():
print("Starting NFT indexer")
Add the instantiation of LakeConfig
below:
config = LakeConfig.mainnet()
config.start_block_height = 69030747
config.aws_access_key_id = os.getenv("AWS_ACCESS_KEY_ID")
config.aws_secret_key = os.getenv("AWS_SECRET_ACCESS_KEY")
Just a few words on the config, function mainnet()
has set s3_bucket_name
, s3_region_name
for mainnet.
You can go to NEAR Explorer and get the most recent block height to set config.start_block_height
.
Let's call streamer
function with the config
stream_handle, streamer_messages_queue = streamer(config)
while True:
streamer_message = await streamer_messages_queue.get()
And an actual start of our indexer in the end of the main.py
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
The final main.py
at this moment should look like the following:
from near_lake_framework import LakeConfig, streamer, near_primitives
async def main():
print("Starting NFT indexer")
config = LakeConfig.mainnet()
config.start_block_height = 69030747
config.aws_access_key_id = os.getenv("AWS_ACCESS_KEY_ID")
config.aws_secret_key = os.getenv("AWS_SECRET_ACCESS_KEY")
stream_handle, streamer_messages_queue = streamer(config)
while True:
streamer_message = await streamer_messages_queue.get()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Now we need to create a callback function that we'll be called to handle StreamerMessage
our indexer receives.
async def handle_streamer_message(streamer_message: near_primitives.StreamerMessage):
pass