Skip to content

Latest commit

History

History
301 lines (230 loc) 路 11.2 KB

README.md

File metadata and controls

301 lines (230 loc) 路 11.2 KB

Tezos Indexer by Baking Bad

Made With License: MIT

TzKT is a lightweight Tezos blockchain indexer with an advanced API created by the Baking Bad team with huge support from the Tezos Foundation.

The indexer fetches raw data from the Tezos node, then processes it and stores in the database in such a way as to provide effective access to the blockchain data. For example, getting operations by hash, or getting all operations of the particular account, or getting detailed baking rewards, etc. None of this can be accessed via node RPC, but TzKT indexer makes this data (and much more) available.

Features:

  • More detailed data. TzKT not only collects blockchain data, but also processes and extends it with unique properties or even entities. For example, TzKT was the first indexer introduced synthetic operation types such as "migration" or "revelation penalty", which fill in the gaps in account history (because this data is missed in the blockchain), and the only indexer that correctly distinguishes smart contracts among all contracts.
  • Micheline-to-JSON conversion TzKT automatically converts raw Micheline JSON to human-readable JSON, so it's extremely handy to work with transaction parameters, contract storages, bigmaps keys, etc.
  • Data quality comes first! You will never see an incorrect account balance, or total rolls, or missed operations, etc. TzKT was built by professionals who know Tezos from A to Z (or, in other words, from tz to KT 馃樇).
  • Advanced API. TzKT provides a REST-like API, so you don't have to connect to the database directly. In addition to basic data access TzKT API has a lot of cool features such as deep filtering, sorting, data selection, exporting .csv statements, calculating historical data (at any block) such as balances or BigMap keys, injecting historical quotes and metadata, optimized caching and much more. See the complete API documentation.
  • WebSocket API. TzKT allows to subscribe to real-time blockchain data, such as new blocks or new operations, etc. via WebSocket. TzKT uses SignalR, which is very easy to use and for which there are many client libraries for different languages.
  • Low resource consumption. TzKT is fairly lightweight. The indexer consumes up to 128MB of RAM, and the API up to 256MB-1024MB, depending on the network and configured cache size.
  • No local node needed. TzKT indexer works well even with remote RPC node. By default it uses tezos.giganode.io, the most performant public RPC node in Tezos, which is more than enough for most cases.
  • Quick start. Indexer bootstrap takes ~15 minutes by using snapshots publicly available for all supported networks. Of course, you can run full synchronization from scratch as well.
  • Validation and diagnostics. TzKT indexer validates all incoming data so you will never get to the wrong chain and will never commit corrupted data. Also, the indexer performs self-diagnostics after each block, which guarantees the correct commiting.
  • Flexibility and scalability. There is no requirement to run all TzKT components (database, indexer, API) together and on the same machine. This allows flexible optimization, because you can optimize each component separately and according to your needs. Or you can run all the components on the same machine as well, which is much cheaper.
  • PostgreSQL. TzKT uses the world's most advanced open source database, that gives a lot of possibilities such as removing unused indexes to reduce storage usage or adding specific indexes to increase performance of specific queries. You can configure replication, clustering, partitioning and much more. You can use a lot of plugins to enable cool features like GraphQL. This is a really powerful database.
  • Friendly support. We are always happy to help everyone and are open to discussions and feature requests. Feel free to contact us.

Installation (docker)

First of all, install git, make, docker, docker-compose, then run the following commands:

git clone https://github.com/baking-bad/tzkt.git
cd tzkt/

make init #run this command if you want to restore the DB from the latest snapshot
make start

curl http://127.0.0.1:5000/v1/head

make stop

Installation (from source)

This is the preferred way, because you have more control over each TzKT component (database, indexer, API). This guide is for Ubuntu 20.04, but if you are using a different OS, the installation process will probably differ only in the "Install packages" step.

Install packages

Install Git

sudo apt update
sudo apt install git

Install .NET 5.0 SDK

wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

sudo apt update
sudo apt install -y apt-transport-https
sudo apt update
sudo apt install -y dotnet-sdk-5.0

Install Postgresql 13

wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt update
sudo apt -y install postgresql-13 postgresql-client-13

Install Tzkt Indexer and API for mainnet

Prepare database

Create an empty database and its user

sudo -u postgres psql

postgres=# create database tzkt_db;
postgres=# create user tzkt with encrypted password 'qwerty';
postgres=# grant all privileges on database tzkt_db to tzkt;
postgres=# \q

Download fresh snapshot

wget "https://tzkt-snapshots.s3.eu-central-1.amazonaws.com/tzkt_v1.6_mainnet.backup" -O /tmp/tzkt_db.backup

Restore database from the snapshot

// full mainnet restoring takes ~30 min (depending on hardware)
sudo -u postgres pg_restore -c --if-exists -v -d tzkt_db -1 /tmp/tzkt_db.backup

// for Docker use you may need to add "-U tzkt", like this
sudo -u postgres pg_restore -c --if-exists -v -d tzkt_db -U tzkt -1 /tmp/tzkt_db.backup

Clone, build, configure and run Tzkt Indexer

Clone

cd ~
git clone https://github.com/baking-bad/tzkt.git

Build indexer

cd ~/tzkt/Tzkt.Sync/
dotnet publish -o ~/tzkt-sync

Configure indexer

Edit configuration file ~/tzkt-sync/appsettings.json with your favorite text editor. What you need is to specify TezosNode.Endpoint and ConnectionStrings.DefaultConnection.

Like this:

{
  "Protocols": {
    "Diagnostics": false,
    "Validation": true
  },

  "TezosNode": {
    "Endpoint": "https://mainnet-tezos.giganode.io/",
    "Timeout": 60
  },

  "Quotes": {
    "Async": true,
    "Provider": {
      "Name": "TzktQuotes"
    }
  },

  "ConnectionStrings": {
    "DefaultConnection": "host=localhost;port=5432;database=tzkt_db;username=tzkt;password=qwerty;"
  },

  "HealthChecks": {
    "Enabled": true,
    "Delay": 10,
    "Period": 10,
    "FilePath": "sync.health"
  },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

Run indexer

cd ~/tzkt-sync
dotnet Tzkt.Sync.dll

// info: Microsoft.Hosting.Lifetime[0]
//       Application started. Press Ctrl+C to shut down.
// info: Microsoft.Hosting.Lifetime[0]
//       Hosting environment: Production
// info: Microsoft.Hosting.Lifetime[0]
//       Content root path: /home/tzkt/tzkt-sync
// warn: Tzkt.Sync.Services.Observer[0]
//       Observer is started
// info: Tzkt.Sync.Services.Observer[0]
//       Applied 776913
// info: Tzkt.Sync.Services.Observer[0]
//       Applied 776914
// ....

That's it. If you want to run the indexer as a daemon, take a look at this guide: https://docs.microsoft.com/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1#create-the-service-file.

Build, configure and run Tzkt API for the mainnet indexer

Suppose you have already created database tzkt_db, database user tzkt and cloned Tzkt repo to ~/tzkt.

Build API

cd ~/tzkt/Tzkt.Api/
dotnet publish -o ~/tzkt-api

Configure API

Edit configuration file ~/tzkt-api/appsettings.json with your favorite text editor. What you need is to specify ConnectionStrings.DefaultConnection, a connection string for the database created above.

Like this:

{
  "Cache": {
    "LoadRate": 0.75,
    "MaxAccounts": 32000
  },

  "Websocket": {
    "Enabled": true,
    "MaxConnections": 1000,
    "MaxOperationSubscriptions": 50,
    "MaxBigMapSubscriptions": 50,
    "MaxAccountsSubscriptions": 50
  },

  "ConnectionStrings": {
    "DefaultConnection": "host=localhost;port=5432;database=tzkt_db;username=tzkt;password=qwerty;"
  },

  "Home": {
    "Enabled": false,
    "UpdatePeriod": 30
  },

  "HealthChecks": {
    "Enabled": true,
    "Endpoint": "/health"
  },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
     
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      }
    }
  },

  "AllowedHosts": "*"
}

Run API

cd ~/tzkt-api
dotnet Tzkt.Api.dll

// info: Tzkt.Api.Services.Metadata.AccountMetadataService[0]
//       Accounts metadata not found
// info: Tzkt.Api.Services.Sync.SyncWorker[0]
//       Sync worker initialized with level 776917 and blocks time 60s
// info: Tzkt.Api.Services.Sync.SyncWorker[0]
//       Syncronization started
// info: Microsoft.Hosting.Lifetime[0]
//       Now listening on: http://localhost:5000
// info: Microsoft.Hosting.Lifetime[0]
//       Now listening on: https://localhost:5001
// info: Microsoft.Hosting.Lifetime[0]
//       Application started. Press Ctrl+C to shut down.
// info: Microsoft.Hosting.Lifetime[0]
//       Hosting environment: Production
// info: Microsoft.Hosting.Lifetime[0]
//       Content root path: /home/tzkt/tzkt-api
// ....

That's it. By default API is available on ports 5000 (HTTP) and 5001 (HTTPS). If you want to use HTTPS, you also need to configure certificates. If you want to run API on a different port, add the "Kestrel" section to the appsettings.json (see example below).

Install Tzkt Indexer and API for testnets

In general the steps are the same as for the mainnet, you just need to use different database, different snapshot and different appsettings (RPC endpoint). Here are some presets for testnets:

Have a question?

Feel free to contact us via:

Cheers! 馃嵑