-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee5ceb9
commit 50291a7
Showing
199 changed files
with
21,865 additions
and
904 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
!**/pdm.lock | ||
!**/README.md | ||
!**/.keep | ||
!**/py.typed | ||
|
||
# Add Python code | ||
!**/*.py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
--- | ||
title: "Quickstart" | ||
description: "This page will guide you through the steps to get your first selective indexer up and running in a few minutes without getting too deep into the details." | ||
navigation.icon: "stars" | ||
--- | ||
|
||
# Quickstart | ||
|
||
::banner{type="warning"} | ||
Substrate support is in early preview stage. API and features may change in the future. | ||
:: | ||
|
||
This page will guide you through the steps to get your first selective indexer up and running in a few minutes without getting too deep into the details. | ||
|
||
A selective blockchain indexer is an application that extracts and organizes specific blockchain data from multiple data sources, rather than processing all blockchain data. It allows users to index only relevant entities, reducing storage and computational requirements compared to full node indexing, and query data more efficiently for specific use cases. Think of it as a customizable filter that captures and stores only the blockchain data you need, making data retrieval faster and more resource-efficient. DipDup is a framework that helps you implement such an indexer. | ||
|
||
Let's create an indexer for the balance transfers in AssetHub network. Our goal is to save all transfers to the database and then calculate some statistics of its holders' activity. | ||
|
||
## Install DipDup | ||
|
||
A modern Linux/macOS distribution with Python 3.12 installed is required to run DipDup. | ||
|
||
The recommended way to install DipDup CLI is [pipx](https://pipx.pypa.io/stable/). We also provide a convenient helper script that installs all necessary tools. Run the following command in your terminal: | ||
|
||
{{ #include _curl-spell.md }} | ||
|
||
See the [Installation](../docs/1.getting-started/1.installation.md) page for all options. | ||
|
||
After installation, run the following command to switch to the preview branch: | ||
|
||
```shell [Terminal] | ||
dipdup self install -f -r feat/substrate | ||
``` | ||
|
||
## Create a project | ||
|
||
DipDup CLI has a built-in project generator. Run the following command in your terminal: | ||
|
||
```shell [Terminal] | ||
dipdup new | ||
``` | ||
|
||
Choose `From template`, then `Substrate` network and `demo_substrate_events` template. | ||
|
||
::banner{type="note"} | ||
Want to skip a tutorial and start from scratch? Choose `Blank` at the first step instead and proceed to the [Config](../docs/1.getting-started/3.config.md) section. | ||
:: | ||
|
||
Follow the instructions; the project will be created in the new directory. | ||
|
||
## Write a configuration file | ||
|
||
In the project root, you'll find a file named `dipdup.yaml`. It's the main configuration file of your indexer. We will discuss it in detail in the [Config](../docs/1.getting-started/3.config.md) section; now it has the following content: | ||
|
||
```yaml [dipdup.yaml] | ||
{{ #include ../src/demo_substrate_events/dipdup.yaml }} | ||
``` | ||
|
||
## Generate types and stubs | ||
|
||
Now it's time to generate typeclasses and callback stubs based on definitions from config. Examples below use `demo_substrate_events` as a package name; yours may differ. | ||
|
||
Run the following command: | ||
|
||
```shell [Terminal] | ||
dipdup init | ||
``` | ||
|
||
DipDup will create a Python package `demo_substrate_events` with everything you need to start writing your indexer. Use `package tree` command to see the generated structure: | ||
|
||
```shell [Terminal] | ||
$ dipdup package tree | ||
demo_substrate_events [/home/droserasprout/git/dipdup/src/demo_substrate_events] | ||
├── abi | ||
│ ├── assethub/v1000000.json | ||
│ ├── assethub/v1001002.json | ||
│ ├── ... | ||
│ └── assethub/v9430.json | ||
├── configs | ||
│ ├── dipdup.compose.yaml | ||
│ ├── dipdup.sqlite.yaml | ||
│ ├── dipdup.swarm.yaml | ||
│ └── replay.yaml | ||
├── deploy | ||
│ ├── .env.default | ||
│ ├── Dockerfile | ||
│ ├── compose.sqlite.yaml | ||
│ ├── compose.swarm.yaml | ||
│ ├── compose.yaml | ||
│ ├── sqlite.env.default | ||
│ └── swarm.env.default | ||
├── graphql | ||
├── handlers | ||
│ ├── batch.py | ||
│ └── on_transfer.py | ||
├── hasura | ||
├── hooks | ||
│ ├── on_index_rollback.py | ||
│ ├── on_reindex.py | ||
│ ├── on_restart.py | ||
│ └── on_synchronized.py | ||
├── models | ||
│ └── __init__.py | ||
├── sql | ||
├── types | ||
│ ├── assethub/substrate_events/assets_transferred/__init__.py | ||
│ ├── assethub/substrate_events/assets_transferred/v601.py | ||
│ └── assethub/substrate_events/assets_transferred/v700.py | ||
└── py.typed | ||
``` | ||
|
||
That's a lot of files and directories! But don't worry, we will need only `models` and `handlers` sections in this guide. | ||
|
||
## Define data models | ||
|
||
DipDup supports storing data in SQLite, PostgreSQL and TimescaleDB databases. We use modified [Tortoise ORM](https://tortoise.github.io/) library as an abstraction layer. | ||
|
||
First, you need to define a model class. DipDup uses model definitions both for database schema and autogenerated GraphQL API. Our schema will consist of a single model `Holder` with the following fields: | ||
|
||
| | | | ||
| ----------- | ----------------------------------- | | ||
| `address` | account address | | ||
| `balance` | token amount held by the account | | ||
| `turnover` | total amount of transfer/mint calls | | ||
| `tx_count` | number of transfers/mints | | ||
| `last_seen` | time of the last transfer/mint | | ||
|
||
Here's how to define this model in DipDup: | ||
|
||
```python [models/__init__.py] | ||
{{ #include ../src/demo_substrate_events/models/__init__.py }} | ||
``` | ||
|
||
Using ORM is not a requirement; DipDup provides helpers to run SQL queries/scripts directly, see [Database](1.getting-started/5.database.md) page. | ||
|
||
## Implement handlers | ||
|
||
Everything's ready to implement an actual indexer logic. | ||
|
||
Our task is to index all the balance updates. Put some code to the `on_transfer` handler callback to process matched logs: | ||
|
||
```python [handlers/on_transfer.py] | ||
{{ #include ../src/demo_substrate_events/handlers/on_transfer.py }} | ||
``` | ||
|
||
And that's all! We can run the indexer now. | ||
|
||
## Next steps | ||
|
||
Run the indexer in memory: | ||
|
||
```shell | ||
dipdup run | ||
``` | ||
|
||
Store data in SQLite database (defaults to /tmp, set `SQLITE_PATH` env variable): | ||
|
||
```shell | ||
dipdup -c . -c configs/dipdup.sqlite.yaml run | ||
``` | ||
|
||
Or spawn a Compose stack with PostgreSQL and Hasura: | ||
|
||
```shell | ||
cd deploy | ||
cp .env.default .env | ||
# Edit .env file before running | ||
docker-compose up | ||
``` | ||
|
||
DipDup will fetch all the historical data and then switch to realtime updates. You can check the progress in the logs. | ||
|
||
If you use SQLite, run this query to check the data: | ||
|
||
```bash | ||
sqlite3 /tmp/demo_substrate_events.sqlite 'SELECT * FROM holder LIMIT 10' | ||
``` | ||
|
||
If you run a Compose stack, open `http://127.0.0.1:8080` in your browser to see the Hasura console (an exposed port may differ). You can use it to explore the database and build GraphQL queries. | ||
|
||
Congratulations! You've just created your first DipDup indexer. Proceed to the Getting Started section to learn more about DipDup configuration and features. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.