Example indexer using the NEAR Lake Framework for saving trade data from Tonic.
Prerequisites
- a working Rust installation
- a Postgres instance that you control
- Docker users: a compose file is included in the repo
- the Diesel CLI (
cargo install diesel_cli --no-default-features --features postgres
) - an AWS account with permission to fetch from the NEAR lake S3 bucket
Required IAM permissions
At a minimum, you need the following permissions
GetBucketLocation
ListBucket
GetObject
on the following resources
arn:aws:s3:::near-lake-data-mainnet
arn:aws:s3:::near-lake-data-mainnet/*
A basic policy would be
data "aws_iam_policy_document" "near_lake_reader_policy" {
statement {
sid = "AllowReadNearLakeBucket"
actions = [
"s3:GetBucketLocation",
"s3:ListBucket",
"s3:GetObject",
]
resources = [
"arn:aws:s3:::near-lake-data-mainnet",
"arn:aws:s3:::near-lake-data-mainnet/*"
]
}
}
resource "aws_iam_policy" "near_lake_reader_policy" {
name = "near-lake-reader-policy"
description = "Allow access to the NEAR Lake S3 bucket"
policy = data.aws_iam_policy_document.near_lake_reader_policy.json
}
Set required environment variables
export DATABASE_URL=postgres://postgres:test@localhost:5432/postgres
export TONIC_CONTRACT_ID=v1.orderbook.near
(Docker users only): Start dev postgres container
docker compose up -d
Run migrations
diesel migration run
Run indexer
When the indexer starts, it will check the database for the latest processed
block number. If none is found, it starts from block 0. You can pass the
--from-blockheight
flag to start at a specific block. The official Tonic
contract was deployed in block 66,296,455.
# if you have Just
just run --from-blockheight 66296455
# or
cargo run --release -- run --contract-ids $TONIC_CONTRACT_ID --from-blockheight 66296455
For all future runs, the flag can be omitted
# if you have Just
just run
# or
cargo run --release -- run --contract-ids $TONIC_CONTRACT_ID