• Stars
    star
    124
  • Rank 282,797 (Top 6 %)
  • Language
    Rust
  • License
    GNU General Publi...
  • Created almost 4 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Watch NEAR network and store all the data from NEAR blockchain to PostgreSQL database

NEAR Indexer for Explorer

NEAR Indexer for Explorer is built on top of NEAR Lake Framework to watch the network and store all the events in the PostgreSQL database.

Shared Public Access

NEAR runs the indexer and maintains it for NEAR Explorer, NEAR Wallet, and some other internal services. It proved to be a great source of data for various analysis and services, so we decided to give a shared read-only public access to the data:

WARNING: We may evolve the data schemas, so make sure you follow the release notes of this repository.

NOTE: Please, keep in mind that the access to the database is shared across everyone in the world, so it is better to make sure you limit the amount of queries and individual queries are efficient.

Self-hosting

The final setup consists of the following components:

  • PostgreSQL database (you can run it locally or in the cloud), which can hold the whole history of the blockchain (as of August 2022, mainnet takes 3TB of data in PostgreSQL storage, and testnet takes 1TB)
  • NEAR Indexer for Explorer binary that operates as a NEAR Lake Framework based indexer, it requires AWS S3 credentials

Prepare Development Environment

Before you proceed, make sure you have the following software installed:

  • Rust compiler of the version that is mentioned in rust-toolchain file in the root of nearcore project.

  • libpq-dev dependency

    On Debian/Ubuntu:

    $ sudo apt install libpq-dev

Prepare Database

Setup PostgreSQL database, create a database with the regular tools, and note the connection string (database host, credentials, and the database name).

Clone this repository and open the project folder

$ git clone https://github.com/near/near-indexer-for-explorer.git
$ cd near-indexer-for-explorer

You need to provide credentials via .env file for:

  • database

    (replace user, password, host and db_name with yours)

    $ echo "DATABASE_URL=postgres://user:password@host/db_name" > .env
  • AWS S3 (permission to read from buckets):

    $ echo "AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE" >> .env
    $ echo "AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" >> .env

Then you need to apply migrations to create necessary database structure. For this you'll need diesel-cli, you can install it like so:

$ cargo install diesel_cli --no-default-features --features "postgres"

And apply migrations

$ cd database && diesel migration run

If you have the DB with some data collected, and you need to apply the next migration, we highly recommend to read the migration contents.
Some migrations have the explanations what should be done, e.g. [1], [2], [3].
General advice is to add CONCURRENTLY option to all indexes creation and apply such changes manually.

Compile NEAR Indexer for Explorer

$ cargo build --release

Run NEAR Indexer for Explorer

Command to run NEAR Indexer for Explorer have to include the chain-id and start options:

You can choose NEAR Indexer for Explorer start options:

  • from-latest - start indexing blocks from the latest finalized block
  • from-interruption - start indexing blocks from the block NEAR Indexer was interrupted last time but earlier for <number_of_blocks> if provided
  • from-genesis - download and store accounts/access keys in genesis file and start indexing from the genesis block
  • from-block --height <block_height> - start indexing blocks from the specific block height

Storing genesis file

When starting Indexer for Explorer with from-genesis, the entire genesis file will be loaded in to memory before iterating the stored accounts/access keys. As of writing this, mainnet and betanet both have relatively small genesis files (<1GB), but the testnet file size is around 5GB. Therefore, if you intend to store the testnet genesis records, make sure that your system has sufficient RAM to hande the memory load.

Strict mode

NEAR Indexer for Explorer works in strict mode by default. In strict mode, the Indexer will ensure parent data exists before storing children, infinitely retrying until this condition is met. This is necessary as a parent (i.e. block) may still be processing while a child (i.e. receipt) is ready to be stored. This scenario will likely occur if you have not stored the genesis file or do not have all data prior to the block you start indexing from. In this case, you can disable strict mode to store data prior to the block you are concerned about, and then re-enable it once you have passed this block.

To disable strict mode provide the following command arugment:

--non-strict-mode

Concurrency

By default NEAR Indexer for Explorer processes only a single block at a time. You can adjust this with the --concurrency argument (when the blocks are mostly empty, it is fine to go with as many as 100 blocks of concurrency).

Starting

So final command to run NEAR Indexer for Explorer can look like:

$ ./target/release/indexer-explorer \
  --non-strict-mode \
  --concurrency 1 \
  mainnet \
  from-latest

After the network is synced, you should see logs of every block height currently received by NEAR Indexer for Explorer.

Troubleshoot NEAR Indexer for Explorer

Refer to a separate TROBLESHOOTING.md document.

Database structure

database structure

Creating read-only PostgreSQL user

We highly recommend using a separate read-only user to access the data to avoid unexcepted corruption of the indexed data.

We use public schema for all tables. By default, new users have the possibility to create new tables/views/etc there. If you want to restrict that, you have to revoke these rights:

REVOKE CREATE ON SCHEMA PUBLIC FROM PUBLIC;
REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA PUBLIC FROM PUBLIC;
ALTER DEFAULT PRIVILEGES IN SCHEMA PUBLIC GRANT SELECT ON TABLES TO PUBLIC;

After that, you could create read-only user in PostgreSQL:

CREATE ROLE readonly;
GRANT USAGE ON SCHEMA public TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public to readonly;
-- Put here your limit or just ignore this command
ALTER ROLE readonly SET statement_timeout = '30s';

CREATE USER explorer with login password 'password';
GRANT readonly TO explorer;
$ PGPASSWORD="password" psql -h 127.0.0.1 -U explorer databasename

Deployments

Both indexer-explorer and circulating-supply binaries are run within Docker, their Dockerfiles can be found within their respective directories/workspaces. Docker images are built using Google Cloud Build and then deployed to Google Cloud Run. The following commands can be used to build the Docker images:

$ docker build -f ./indexer/Dockerfile .
$ docker build -f ./circulating-supply/Dockerfile .

Deprecated features

The tables account_changes and/or assets__fungible_token_events can be still enabled by features on the compile stage:

cargo build --release --features "account_changes fungible_token_events"

Note, we no longer support these tables. We highly recommend you to use Enhanced API instead.

More Repositories

1

nearcore

Reference client for NEAR Protocol
Rust
2,301
star
2

near-sdk-rs

Rust library for writing NEAR smart contracts
Rust
432
star
3

borsh

Binary Object Representation Serializer for Hashing
430
star
4

near-api-js

JavaScript library to interact with NEAR Protocol via RPC API
TypeScript
386
star
5

create-near-app

Create a starter app hooked up to the NEAR blockchain
JavaScript
347
star
6

core-contracts

Core contracts: reference staking pool, lockup, voting, whitelist, multisig.
Rust
316
star
7

borsh-rs

Rust implementation of Binary Object Representation Serializer for Hashing
Rust
273
star
8

near-wallet

Web wallet for NEAR Protocol which stores keys in browser's localStorage
JavaScript
209
star
9

near-sdk-js

Tools for building NEAR smart contracts in JavaScript
TypeScript
191
star
10

NEPs

The Near Enhancement Proposals repository
JavaScript
190
star
11

near-cli

General purpose command line tools for interacting with NEAR Protocol
JavaScript
190
star
12

assemblyscript-json

JSON encoder / decoder for AssemblyScript
TypeScript
168
star
13

docs

NEAR Protocol Documentation
SCSS
139
star
14

wallet-selector

This is a wallet selector modal that allows users to interact with NEAR dApps with a selection of available wallets.
TypeScript
133
star
15

near-sdk-as

Tools for building NEAR smart contracts in AssemblyScript
TypeScript
114
star
16

nearup

Public scripts to launch NEAR Protocol betanet and testnet node
Python
104
star
17

borsh-js

TypeScript/JavaScript implementation of Binary Object Representation Serializer for Hashing
JavaScript
89
star
18

stakewars-iii

Stake Wars: Episode 3 challenges and place to report issues
88
star
19

near-cli-rs

near CLI is your human-friendly companion that helps to interact with NEAR Protocol from command line.
Rust
82
star
20

near-explorer

NEAR blockchain explorer
TypeScript
82
star
21

near-workspaces-rs

Write tests once, run them both on NEAR TestNet and a controlled NEAR Sandbox local environment via Rust
Rust
81
star
22

bounties

Specs for technical and non-technical work that earns NEAR tokens
74
star
23

near-linkdrop

Contract to drop tokens via link
Rust
55
star
24

near-api-py

Python API to interact with NEAR via RPC API
Python
51
star
25

near-discovery

The homebase for Near Builders
HTML
48
star
26

near-jsonrpc-client-rs

Lower-level API for interfacing with the NEAR Protocol via JSONRPC.
Rust
46
star
27

near-lake-framework-rs

Library to connect to the NEAR Lake S3 and stream the data
Rust
45
star
28

near-lake-indexer

Watch NEAR network and store all the events as JSON files on AWS S3
Rust
45
star
29

borsh-go

Go implementation of Binary Object Representation Serializer for Hashing
Go
44
star
30

near-sandbox

Easily run a local NEAR blockchain
TypeScript
42
star
31

near-workspaces-js

Write tests once, run them both on NEAR TestNet and a controlled NEAR Sandbox local environment
TypeScript
42
star
32

ecosystem

Community-sourced and curated data for the NEAR Ecosystem.
Python
39
star
33

near-sdk-contract-tools

Helpful functions and macros for developing smart contracts on NEAR Protocol.
Rust
39
star
34

data-availability

NEAR as data availability!
Rust
39
star
35

community

Coordination repository of Near Community
36
star
36

awesome-near

Curated list of resources: examples, libraries, projects
TypeScript
36
star
37

near-seed-phrase

Utilities to work with NEAR Protocol key pairs based on BIP39 seed phrases
JavaScript
35
star
38

near-contract-helper

Micro-service used by NEAR Wallet to store & send recovery methods
JavaScript
35
star
39

near-evm

Obsolete EVM contract experiments. Find current development at: https://github.com/aurora-is-near/aurora-engine
Rust
33
star
40

fast-auth-signer

TypeScript
29
star
41

mpc

Rust
28
star
42

cargo-near

Cargo extension for building Rust smart contracts on NEAR
Rust
28
star
43

pagoda-relayer-rs

Rust Reference Implementation of Relayer for NEP-366 Meta Transactions
Rust
28
star
44

wiki

NEAR Wiki
JavaScript
27
star
45

DX

Developer Experience building on NEAR
26
star
46

near-lake-framework-js

JS Library to connect to the NEAR Lake S3 and stream the data
TypeScript
25
star
47

borsh-construct-py

Python implementation of Binary Object Representation Serializer for Hashing
Python
25
star
48

bos-web-engine

Improved execution layer for NEAR decentralized frontend components
TypeScript
24
star
49

corgis

simple solution for corgi NFT
JavaScript
22
star
50

near-api-swift

Interact with NEAR blockchain from iOS and OS X apps using Swift
Swift
22
star
51

wasmer

🚀 The leading WebAssembly Runtime supporting WASI and Emscripten
Rust
21
star
52

near-analytics

Python
20
star
53

sdk-docs

The book about near-sdk-rs
JavaScript
20
star
54

neardevhub-widgets

NEAR DevHub UI hosted on NEAR BOS
JavaScript
20
star
55

finite-wasm

Cheating a little to solve the halting problem at scale
WebAssembly
20
star
56

near-api-kotlin

Kotlin
20
star
57

bos-loader

Rust
18
star
58

near-enhanced-api-server

Rust
18
star
59

read-rpc

Read-only NEAR RPC centralized-like performant solution
Rust
16
star
60

queryapi

Near Indexing as a Service
Rust
15
star
61

near-redpacket

NEAR Redpacket based on NEAR Linkdrop
CSS
14
star
62

near-vscode

https://marketplace.visualstudio.com/items?itemName=near-protocol.near-discovery-ide
JavaScript
14
star
63

near-discovery-components

This is a repository that holds the source code of all NEAR discovery components that the team maintains for near.org.
JavaScript
13
star
64

neardevhub-contract

NEAR DevHub contract
Rust
11
star
65

near-sdk-js-template-project

A starting point to write, build and test JavaScript smart contract
JavaScript
11
star
66

devx

This is the home of NEAR collective developer experience plans and roadmap.
11
star
67

node-docs

NEAR Nodes documentation
CSS
11
star
68

stakewars-iv

Shell
11
star
69

abi

NEAR contract schema and tooling
10
star
70

rainbow-bridge-lib

JavaScript
10
star
71

rainbow-bridge-sol

Solidity
10
star
72

borshj

Borsh binary serialization format support for Java.
Java
10
star
73

near-memory-tracker

near-memory-tracker
Rust
10
star
74

devrel

The space for DevRel
9
star
75

units-js

Easily parse and format NEAR Tokens and gas units
TypeScript
9
star
76

rainbow-bridge-rs

Rust
9
star
77

cargo-near-new-project-template

temp project to become part of `cargo near new` command
Rust
9
star
78

near-microindexers

Rust
8
star
79

near-api-unity

Port of https://github.com/near/near-api-js to Unity
C#
8
star
80

near-wallet-roadmap

near-wallet-roadmap
8
star
81

near-drop-demo

JavaScript
7
star
82

near-indexer-events

Rust
7
star
83

multichain-gas-station-contract

Rust
7
star
84

near-sdk-abi

ABI utilities used for generating Rust SDK cross-contract calls
Rust
6
star
85

repro-near-funcall

Repro near function call actions with local near-vm-runner-standalone
JavaScript
6
star
86

as-base64

Encode and Decode base64 strings in AssemblyScript
WebAssembly
6
star
87

near-indexer-for-wallet

Rust
6
star
88

near-ledger-js

Connect to NEAR Ledger app from browser
JavaScript
6
star
89

boilerplate-template-keypom

A github template repository of an end-to-end application that demonstrates minimal UI to build a lazy-minted NFT link drop using Keypom
TypeScript
6
star
90

near-abi-rs

NEAR smart contract ABI primitives
Rust
6
star
91

near-workspaces

Write tests once, run them both on NEAR TestNet and a controlled NEAR Sandbox local environment
6
star
92

near-abi-client-rs

Library to generate Rust client code from NEAR ABI
Rust
5
star
93

discovery-docs

NEAR Discovery Documentation
JavaScript
5
star
94

local

5
star
95

near-api-helper

Cloudflare worker that can batch RPC calls
JavaScript
5
star
96

boilerplate-template-rs

TypeScript
5
star
97

near-blake2

Pure Rust implementation of the BLAKE2 hash function family.
Rust
5
star
98

wasm_sizer

Python
5
star
99

near-abi-client-js

Library to generate JavaScript/TypeScript client code from NEAR ABI
TypeScript
5
star
100

nayduck

Test Infra
Python
5
star