-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Vladimir Bobrikov <[email protected]>
- Loading branch information
1 parent
4411c00
commit 14cff04
Showing
81 changed files
with
842 additions
and
127 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
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 |
---|---|---|
|
@@ -29,4 +29,6 @@ | |
|
||
# Docs | ||
!docs/** | ||
docs/_build | ||
docs/_build | ||
|
||
dipdup_indexer |
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,187 @@ | ||
--- | ||
title: "Quickstart on EVM" | ||
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" | ||
network: "ethereum" | ||
--- | ||
|
||
# Quickstart | ||
|
||
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. | ||
|
||
Let's create an indexer for the [USDt token contract](https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7). Our goal is to save all token transfers to the database and then calculate some statistics of its holders' activity. | ||
|
||
## Install DipDup | ||
|
||
A modern Linux/macOS distribution with Python 3.11 installed is required to run DipDup. | ||
|
||
We have a [convenient installer](https://dipdup.io/install.py) that will install DipDup and all the required dependencies for you. It uses pipx to install DipDup as a CLI application and make it available everywhere for the current user. Run the following command in your terminal: | ||
|
||
```shell [Terminal] | ||
curl -Lsf https://dipdup.io/install.py | python3 | ||
``` | ||
|
||
If you don't want to use our script, install DipDup manually using commands from the snippet below. You can use any Python package manager you like, but we recommend [PDM](https://pdm.fming.dev/latest/) and provide some useful scripts for it. Here are some spells to get you started: | ||
|
||
```shell [Terminal] | ||
# pipx | ||
pipx install dipdup datamodel-code-generator | ||
|
||
# PDM | ||
pdm init --python 3.11 --lib # use "">=3.11,<3.12" for requires-python | ||
pdm venv create | ||
pdm add "dipdup>=7.0.0rc4,<8" --venv | ||
$(pdm venv activate) | ||
|
||
# Poetry | ||
poetry init --python ">=3.11,<3.12" | ||
poetry add "dipdup>=7.0.0rc4,<8" | ||
poetry shell | ||
|
||
# pip + venv | ||
python -m venv .venv | ||
. .venv/bin/activate | ||
echo "dipdup>=7.0.0rc4,<8" > requirements.txt | ||
pip install -r requirements.txt -e . | ||
``` | ||
|
||
## Create a project | ||
|
||
DipDup CLI has a built-in project generator. Run the following command in your terminal: | ||
|
||
```shell [Terminal] | ||
dipdup new | ||
``` | ||
|
||
For educational purposes, we'll create a project from scratch, so choose `[none]` network and `demo_blank` template. | ||
|
||
::banner{type="note"} | ||
Want to skip a tutorial? Choose `EVM-compatible` and `demo_evm_events` instead! | ||
:: | ||
|
||
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](1.getting_started/3.config.md) section; for now just replace its content with the following: | ||
|
||
```yaml [dipdup.yaml] | ||
{{ #include ../src/demo_evm_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_evm_events` as a package name; yours may differ. | ||
|
||
Run the following command: | ||
|
||
```shell [Terminal] | ||
dipdup init | ||
``` | ||
|
||
DipDup will create a Python package `demo_evm_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_evm_events [.] | ||
├── abi | ||
│ ├── eth_usdt/abi.json | ||
│ └── eth_usdt/events.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 | ||
│ └── on_transfer.py | ||
├── hasura | ||
├── hooks | ||
│ ├── on_index_rollback.py | ||
│ ├── on_reindex.py | ||
│ ├── on_restart.py | ||
│ └── on_synchronized.py | ||
├── models | ||
│ └── __init__.py | ||
├── sql | ||
├── types | ||
│ └── eth_usdt/evm_events/transfer.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 custom ORM based on Tortoise ORM as an abstraction layer. | ||
|
||
First, you need to define a model class. 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 implement this model in DipDup: | ||
|
||
```python [models/__init__.py] | ||
{{ #include ../src/demo_evm_events/models/__init__.py }} | ||
``` | ||
|
||
## 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_evm_events/handlers/on_transfer.py }} | ||
``` | ||
|
||
And that's all! We can run the indexer now. | ||
|
||
## Next steps | ||
|
||
Run the indexer in-memory: | ||
|
||
```bash | ||
dipdup run | ||
``` | ||
|
||
Store data in SQLite database: | ||
|
||
```bash | ||
dipdup -c . -c configs/dipdup.sqlite.yaml run | ||
``` | ||
|
||
Or spawn a docker-compose stack: | ||
|
||
```bash | ||
cd deploy | ||
cp .env.default .env | ||
# Edit .env 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 a query to check the data: | ||
|
||
```bash | ||
sqlite3 demo_evm_events.sqlite 'SELECT * FROM holder LIMIT 10' | ||
``` | ||
|
||
If you run a Compose stack, check open `http://127.0.0.1:8080` in your browser to see the Hasura console (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
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.