Skip to content

Commit 997d5aa

Browse files
committed
Added MATIC ABI
Installed file-store-csv dependency Removed old dependency Removed old ABI Added code for table(s), db Modified processor to save Transfers
1 parent 90b3a73 commit 997d5aa

32 files changed

+6714
-10192
lines changed

.dockerignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ DB_NAME=squid
22
DB_PORT=23798
33
GQL_PORT=4350
44
# JSON-RPC node endpoint, both wss and https endpoints are accepted
5-
RPC_ENDPOINT=
5+
RPC_ENDPOINT="https://rpc.ankr.com/eth"

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66

77
# IDE files
88
/.idea
9+
/data/*
10+
!*.py
11+
!*.ipynb
12+
!*.lock
13+
!*.toml

.gitpod.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This configuration file was automatically generated by Gitpod.
2+
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
3+
# and commit this file to your remote git repository to share the goodness with others.
4+
5+
# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart
6+
github:
7+
prebuilds:
8+
# enable for the master/default branch (defaults to true)
9+
master: true
10+
# enable for all branches in this repo (defaults to false)
11+
branches: false
12+
# enable for pull requests coming from this repo (defaults to true)
13+
pullRequests: true
14+
# add a check to pull requests (defaults to true)
15+
addCheck: true
16+
# add a "Review in Gitpod" button as a comment to pull requests (defaults to false)
17+
addComment: false
18+
19+
tasks:
20+
- init: |
21+
npm i
22+
npm i -g @subsquid/cli
23+
gp sync-done setup
24+
exit
25+
- name: Python setup
26+
command: |
27+
cd data
28+
curl -sSL https://install.python-poetry.org | python3 -
29+
poetry install
30+
gp sync-done py-setup
31+
exit
32+
- name: Squid procesor
33+
command: |
34+
gp open src/processor.ts
35+
gp sync-await setup
36+
sqd process
37+
- name: Python data analysis
38+
command: |
39+
gp open data/analysis.py
40+
gp sync-await py-setup
41+
cd data
42+
poetry run python analysis.py

Makefile

Lines changed: 0 additions & 37 deletions
This file was deleted.

README.md

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,77 @@
1-
# Gravatar squid
1+
# MATIC data analytics - A squid example for CSV storage
22

3-
This squid has been migrated from the [Gravatar subgraph](https://github.com/graphprotocol/example-subgraph). For a step-by-step migration guide, see the [migration docs page](https://docs.subsquid.io/migrate/migrate-subgraph/).
3+
<p align="center">
4+
<img src="assets/white-woodmark.svg">
5+
</p>
46

5-
## Quickstart
7+
<div align="center">
68

7-
```bash
8-
# 1. Update the Squid SDK and install dependencies
9-
npm run update
10-
npm ci
9+
[Twitter](https://twitter.com/subsquid) | [Discord](https://discord.gg/subsquid) | [LinkedIn](https://linkedin.com/subsquid) | [Telegram](https://t.me/HydraDevs) | [GitHub](https://github.com/subsquid) | [YouTube](https://www.youtube.com/@subsquid)
1110

12-
# 2. Compile typescript files
13-
make build
11+
</div>
1412

15-
# 3. Start target Postgres database and detach
16-
make up
13+
## Quick-start
1714

18-
# 4. Start the processor
19-
make process
15+
<div align="center">
2016

21-
# 5. The command above will block the terminal
22-
# being busy with fetching the chain data,
23-
# transforming and storing it in the target database.
24-
#
25-
# To start the graphql server open the separate terminal
26-
# and run. The GraphQL playground will be available at localhost:4350/graphql
27-
make serve
28-
```
17+
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io#https://github.com/RaekwonIII/local-csv-indexing.git)
18+
19+
</div>
20+
21+
## Introduction
22+
23+
In this article I describe how to use Subsquid's indexing framework for data analytics prototyping.
24+
I have built an indexer that processes MATIC transactions on Ethereum mainnet, and dumps them on a local CSV files.
25+
I have then developed a simple Python script to demonstrate how to import these CSVs into a Pandas DataFrame, and perform aggregation operations on this data.
26+
27+
This project is a squid that indexes blockchain information generated by the Transfers of [MATIC](https://etherscan.io/token/0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0) tokens on Ethereum mainnet.
28+
The indexer writes it to multiple files, divided in chunks of configurable size, in the CSV format.
29+
30+
It also contains a simple Python script (in the `data` folder), which reads the CSV files, imports the data in a [Pandas](http://pandas.pydata.org/) DataFrame, aggregates the data (albeit rather trivially) and plots a bar chart.
31+
32+
MATIC is the native token of the [Polygon project](https://polygon.technology/). It is defined by an ERC-20 standard smart contract, and the tokens are transferred via the contract's `transfer` function, which emits a `Transfer` event.
33+
Such event is exactly what the squid ETL is indexing, and eventually writing to CSV files using [Subsqduid's `file-store`](https://www.npmjs.com/package/@subsquid/file-store) and [`file-store-csv` libraries](https://www.npmjs.com/package/@subsquid/file-store-csv).
34+
35+
The `analysis.py` Python script is using Pandas to read the data stored in all CSVs, and create a [DataFrame](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html). Then, using the `groupby` function, it calculates the daily total value of transfers, and it creates a bar chart, using [`matplotlib` Python library](https://matplotlib.org/).
36+
37+
<p align="center">
38+
<img src="assets/plot.png">
39+
</p>
40+
41+
The project is relatively simple, because its purpose is purely demonstrative. Its intent is to showcase the capabilities of Subsquid SDK.
2942

30-
## Dev flow
43+
### What is a Squid?
44+
45+
> A squid is a project that extracts and transforms on-chain data in order to present it as a GraphQL API. Squids are developed using the Subsquid SDK, which provides extensive tooling to define data schemas, data transfomation rules, and the shape of the resulting API.
46+
47+
We recommend that you read Subsquid docs to understand how it works: https://docs.subsquid.io/
48+
49+
## Prerequisites
50+
51+
- Node 16.x
52+
- Docker
53+
- NPM
54+
55+
## Quick-start local indexing
56+
57+
1. Clone the repository
58+
2. Install dependencies (in a console window): `npm i`
59+
3. Build the project `sqd build`
60+
4. Launch the database container `sqd up`
61+
5. Launch the processor `sqd process`
62+
6. Launch the GraphQL server (in a separate console window) `sqd serve`
63+
7. Access the GraphiQL Playground, by running `sqd open http://localhost:4350/graphql` <!-- markdown-link-check-disable-line -->
64+
65+
## Key components
66+
67+
* The `schema.graphql` file is used to define the database and API schemas. A command line tool will automatically generate code from it, which you can find in `src/model/generated`
68+
* The `db/migrations` folder contains automatically files with SQL statements to modify the database (create, alter, delete tables), similarly to any ORM database interface.
69+
* The `src/abi` folder contains facade TypeScript code, automatically generated by a command line tool from one, or multiple smart contract ABI(s). This code is used to programmatically interface with the smart contract(s) and decode events and function calls.
70+
* The main logic of this project is defined in `src/processor.ts`. The `EvmBatchProcessor` class is configured and used to perform request to [Subsquid's Archive for Ethereum blockchain](https://app.subsquid.io/archives), to obtain necessary data. Then some custom logic is implemented to process this data in batches, and save it on the database with the custom defined structure.
71+
72+
[Subsquid documentation](https://docs.subsquid.io/) has dedicated sections and pages describing each of these concepts, it is advised to consult them, before starting to develop your own squid.
73+
74+
## Development flow
3175

3276
### 1. Define database schema
3377

@@ -61,18 +105,17 @@ npx squid-typeorm-migration generate
61105

62106
See [docs on schema updates](https://docs.subsquid.io/develop-a-squid/schema-file/schema-updates/) for more details.
63107

64-
65108
### 4. Import ABI contract and generate interfaces to decode events
66109

67110
It is necessary to import the respective ABI definition to decode EVM logs.
68111

69-
To generate a type-safe facade class to decode EVM logs, use `squid-evm-typegen(1)`. For example, for Gravatar we fetch the public ABI by the address `0x2E645469f354BB4F5c8a05B3b30A929361cf77eC` and tell the `evm-typegen` to use `Gravity` as the base name:
112+
To generate a type-safe facade class to decode EVM logs, place the ABI in the `assets` folder and use `squid-evm-typegen(1)`, e.g.:
70113

71114
```bash
72-
npx squid-evm-typegen src/abi 0x2E645469f354BB4F5c8a05B3b30A929361cf77eC#Gravity --clean
115+
npx squid-evm-typegen src/abi assets/ERC721.json#erc721
73116
```
74-
It generates the files `abi.support.ts`, `Gravity.abi.ts` and `Gravity.ts` in the destination folder `src/abi`.
75117

118+
For more details about `squid-evm-typegen` read the [docs page](https://docs.subsquid.io/develop-a-squid/typegen/squid-evm-typegen/)
76119

77120
## Project conventions
78121

@@ -83,14 +126,15 @@ The layout of `lib` must reflect `src`.
83126
* All TypeORM classes must be exported by `src/model/index.ts` (`lib/model` module).
84127
* Database schema must be defined in `schema.graphql`.
85128
* Database migrations must reside in `db/migrations` and must be plain js files.
86-
* `sqd(1)` and `squid-*(1)` executables consult `.env` file environment variables, in particular, to establish a database connection.
129+
* `sqd(1)` and `squid-*(1)` executables consult `.env` file for a number of environment variables.
87130

88131
## GraphQL server extensions
89132

90133
It is possible to extend `squid-graphql-server(1)` with custom
91134
[type-graphql](https://typegraphql.com) resolvers and to add request validation. See [the docs](https://docs.subsquid.io/develop-a-squid/graphql-api/custom-resolvers/) for more details.
92135

136+
## Learn More
93137

94-
## Disclaimer
138+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
95139

96-
This is alpha-quality software. The Squid SDK may introduce breaking changes in future versions.
140+
To learn React, check out the [React documentation](https://reactjs.org/).

assets/plot.png

47.1 KB
Loading

assets/primary-logo.svg

Lines changed: 1 addition & 0 deletions
Loading

assets/white-on-gradient-circle.svg

Lines changed: 1 addition & 0 deletions
Loading

assets/white-woodmark.svg

Lines changed: 1 addition & 0 deletions
Loading

commands.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "https://cdn.subsquid.io/schemas/commands.json",
3+
"commands": {
4+
"clean": {
5+
"description": "delete all build artifacts",
6+
"cmd": ["npx", "--yes", "rimraf", "lib"]
7+
},
8+
"build": {
9+
"description": "Build the squid project",
10+
"deps": ["clean"],
11+
"cmd": ["tsc"]
12+
},
13+
"typegen": {
14+
"description": "Generate data access classes for an ABI file(s) in the ./abi folder",
15+
"cmd": ["squid-evm-typegen", "./src/abi", {"glob": "./abi/*.json"}]
16+
},
17+
"process": {
18+
"description": "Load .env and start the squid processor",
19+
"deps": ["build"],
20+
"cmd": ["node", "--require=dotenv/config", "lib/processor.js"]
21+
},
22+
"process:prod": {
23+
"description": "Start the squid processor",
24+
"cmd": ["node", "lib/processor.js"],
25+
"hidden": true
26+
},
27+
"check-updates": {
28+
"cmd": ["npx", "--yes", "npm-check-updates", "--filter=/subsquid/", "--upgrade"],
29+
"hidden": true
30+
},
31+
"bump": {
32+
"description": "Bump @subsquid packages to the latest versions",
33+
"deps": ["check-updates"],
34+
"cmd": ["npm", "i", "-f"]
35+
},
36+
"open": {
37+
"description": "Open a local browser window",
38+
"cmd": ["npx", "--yes", "opener"]
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)