Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexers v2 #132

Merged
merged 37 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c56719d
Create indexers-v2 docker baseline
marcus-snx Oct 23, 2024
5efd639
Fix package.json name
marcus-snx Oct 23, 2024
5ef68ea
Create separate services for indexers
marcus-snx Oct 23, 2024
1103017
Remove service
marcus-snx Oct 23, 2024
4811eed
Add cannon config for base
marcus-snx Oct 23, 2024
8676c18
Fix docker exiting when no migrations
marcus-snx Oct 23, 2024
7907660
Make services explicit for indexers-v2
marcus-snx Oct 24, 2024
412b0d5
Refactor logic
marcus-snx Oct 25, 2024
d9078aa
Handle Exceptions
marcus-snx Oct 29, 2024
ca9d08d
Fix arbitrum mainnet network config
marcus-snx Oct 29, 2024
7275dc9
Consolidate contract data
marcus-snx Oct 29, 2024
cde7a83
Fix sdk version
marcus-snx Oct 29, 2024
3623f3c
Create entrypoint bash script
marcus-snx Oct 29, 2024
3136887
Add block range
marcus-snx Oct 29, 2024
a786170
Fix entrypoint script
marcus-snx Oct 29, 2024
226f8a7
Update network config files
marcus-snx Oct 29, 2024
992ea2b
Use sdk logger
marcus-snx Oct 31, 2024
91ab6cf
Improve arg and config parsing
marcus-snx Nov 5, 2024
b7bb3ca
Add rate limit config
marcus-snx Nov 5, 2024
ea8f205
Fix base mainnet config file
marcus-snx Nov 5, 2024
7889047
Add config for base mainnet parquet
marcus-snx Nov 5, 2024
2976d5a
Set default config
marcus-snx Nov 5, 2024
69d2ec6
Fix packages
marcus-snx Nov 5, 2024
978b140
Add env var to entrypoint
marcus-snx Nov 5, 2024
981affb
Add custom config to indexer service
marcus-snx Nov 5, 2024
ef9fb76
Remove migration deps in squid commands
marcus-snx Nov 5, 2024
b18a47e
Add parquet export support
marcus-snx Nov 6, 2024
d3eea6f
Fix patches paths in Dockerfile
marcus-snx Nov 6, 2024
fe8a67c
Remove required for config_name arg
marcus-snx Nov 6, 2024
2eeaa5d
Patch gen tools for no archive url
marcus-snx Nov 6, 2024
1bd0517
Fix blockrange
marcus-snx Nov 7, 2024
5f1190a
Update indexers-v2/requirements.txt
Tburm Nov 8, 2024
3646184
Remove contract renaming
marcus-snx Nov 11, 2024
fbe4130
Remove postgres target
marcus-snx Nov 11, 2024
b0dd1d3
Merge branch 'feat/indexers-v2' of github.com:Synthetixio/data into f…
marcus-snx Nov 11, 2024
2f4b52d
Remove deprecated syncIntervalBlocks
marcus-snx Nov 11, 2024
bbeef8a
Merge branch 'main' into feat/indexers-v2
marcus-snx Nov 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,42 @@ services:
limits:
cpus: "4.0"
memory: 8192M

arbitrum-mainnet-indexer:
build:
context: ./indexers-v2
dockerfile: Dockerfile
networks:
- data
depends_on:
- db
restart: always
environment:
DB_HOST: db
DB_PORT: 5432
DB_NAME: arbitrum_mainnet
DB_PASS: $PG_PASSWORD
GQL_PORT: 4350
RPC_ENDPOINT: wss://arbitrum-one-rpc.publicnode.com
NETWORK_NAME: arbitrum_mainnet

base-mainnet-indexer:
build:
context: ./indexers-v2
dockerfile: Dockerfile
networks:
- data
depends_on:
- db
restart: always
environment:
DB_HOST: db
DB_PORT: 5432
DB_NAME: base_mainnet
DB_PASS: $PG_PASSWORD
GQL_PORT: 4350
RPC_ENDPOINT: https://mainnet.base.org
NETWORK_NAME: base_mainnet

eth-mainnet-processor:
image: ghcr.io/synthetixio/data/indexer:${VERSION}-mainnet
Expand Down
16 changes: 16 additions & 0 deletions indexers-v2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:16-alpine

WORKDIR /app

COPY package*.json ./
COPY requirements.txt .

RUN apk add --no-cache python3 py3-pip
RUN apk add --no-cache build-base && npm ci && apk del build-base

COPY . .

RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

ENTRYPOINT ["sh", "-c", "python3 main.py --network_name \"$NETWORK_NAME\" --rpc_endpoint \"$RPC_ENDPOINT\" && npm run generate:processor && npm run build && (npm run generate:migration || true) && npm run start"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can simplify this entrypoint, potentially just make a shell script that calls this stuff in order. It would simplify the docker compose commands if we can just call a script with configurations

93 changes: 93 additions & 0 deletions indexers-v2/commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"$schema": "https://cdn.subsquid.io/schemas/commands.json",
"commands": {
"clean": {
"description": "delete all build artifacts",
"cmd": ["npx", "--yes", "rimraf", "lib"]
},
"generate": {
"description": "Generate a squid from an ABI file",
"cmd": ["squid-gen-abi"]
},
"squid-gen-abi": {
"description": "Generate a squid from an ABI file",
"cmd": ["squid-gen-abi"],
"hidden": true
},
"build": {
"description": "Build the squid project",
"deps": ["clean"],
"cmd": ["tsc"]
},
"up": {
"description": "Start a PG database",
"cmd": ["docker-compose", "up", "-d"]
},
"down": {
"description": "Drop a PG database",
"cmd": ["docker-compose", "down"]
},
"migration:apply": {
"description": "Apply the DB migrations",
"cmd": ["squid-typeorm-migration", "apply"]
},
"migration:generate": {
"description": "Generate a DB migration matching the TypeORM entities",
"deps": ["build", "migration:clean"],
"cmd": ["squid-typeorm-migration", "generate"],
},
"migration:clean": {
"description": "Clean the migrations folder",
"cmd": ["npx", "--yes", "rimraf", "./db/migrations"],
},
"migration": {
"deps": ["build"],
"cmd": ["squid-typeorm-migration", "generate"],
"hidden": true
},
"codegen": {
"description": "Generate TypeORM entities from the schema file",
"cmd": ["squid-typeorm-codegen"]
},
"typegen": {
"description": "Generate data access classes for an ABI file(s) in the ./abi folder",
"cmd": ["squid-evm-typegen", "./src/abi", {"glob": "./abi/*.json"}, "--multicall"]
},
"process": {
"description": "Load .env and start the squid processor",
"deps": ["build", "migration:apply"],
"cmd": ["node", "--require=dotenv/config", "lib/main.js"]
},
"process:prod": {
"description": "Start the squid processor",
"deps": ["migration:apply"],
"cmd": ["node", "lib/main.js"],
"hidden": true
},
"serve": {
"description": "Start the GraphQL API server",
"cmd": ["squid-graphql-server"]
},
"serve:prod": {
"description": "Start the GraphQL API server with caching and limits",
"cmd": ["squid-graphql-server",
"--dumb-cache", "in-memory",
"--dumb-cache-ttl", "1000",
"--dumb-cache-size", "100",
"--dumb-cache-max-age", "1000" ]
},
"check-updates": {
"cmd": ["npx", "--yes", "npm-check-updates", "--filter=/subsquid/", "--upgrade"],
"hidden": true
},
"bump": {
"description": "Bump @subsquid packages to the latest versions",
"deps": ["check-updates"],
"cmd": ["npm", "i", "-f"]
},
"open": {
"description": "Open a local browser window",
"cmd": ["npx", "--yes", "opener"]
}
}
}
143 changes: 143 additions & 0 deletions indexers-v2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import json
import sys
import os
import argparse
import yaml
from synthetix import Synthetix


def save_abi(abi, contract_name):
os.makedirs("abi", exist_ok=True)
with open(f"abi/{contract_name}.json", "w") as file:
json.dump(abi, file, indent=2)


def create_squidgen_config(rpc_url, archive_url, contracts_info):
config = {
"archive": archive_url,
"finalityConfirmation": 1,
"chain": {"url": rpc_url, "rateLimit": 10},
"target": {"type": "postgres"},
"contracts": [],
}

for contract in contracts_info:
name = contract["name"]
address = contract["address"]
contract_config = {
"name": name,
"address": address,
"abi": f"./abi/{name}.json",
"events": True,
"functions": False,
}
config["contracts"].append(contract_config)

return config


def create_squid_config(network_name):
return {
"manifestVersion": "subsquid.io/v0.1",
"name": network_name,
"version": 1,
"description": "A squid indexer generated from an ABI template",
"build": None,
"deploy": {
"addons": {"postgres": None},
"processor": {"cmd": ["node", "lib/main"]},
"api": {
"cmd": [
"npx",
"squid-graphql-server",
"--dumb-cache",
"in-memory",
"--dumb-cache-ttl",
"1000",
"--dumb-cache-size",
"100",
"--dumb-cache-max-age",
"1000",
]
},
},
}


def write_yaml(config, filename):
with open(filename, "w") as file:
yaml.dump(config, file, default_flow_style=False)


def load_network_config(path):
with open(f"{path}/network_config.yaml", "r") as file:
return yaml.safe_load(file)


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate Squid configuration files for a given network"
)
parser.add_argument("--network_name", type=str, help="Network name", required=True)
parser.add_argument("--rpc_endpoint", type=str, help="RPC URL", required=True)
args = parser.parse_args()

network_name = args.network_name
path = f"networks/{network_name}"
network_config = load_network_config(path)[network_name]

if not network_config:
print(f"Network '{network_name}' not found in {path}/network_config.yaml")
sys.exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using raise Exception("{message}") is a cleaner way to print and exit with code 1


rpc_endpoint = args.rpc_endpoint
if not rpc_endpoint:
print("RPC_ENDPOINT environment variable is not set")
sys.exit(1)
archive_url = network_config["archive_url"]

contracts = []

if "contracts_from_sdk" in network_config:
contracts_from_sdk = network_config["contracts_from_sdk"]

if "cannon_config" in network_config:
snx = Synthetix(
provider_rpc=rpc_endpoint,
network_id=network_config["network_id"],
cannon_config=network_config["cannon_config"],
)
else:
snx = Synthetix(
provider_rpc=rpc_endpoint,
network_id=network_config["network_id"],
)

for contract in contracts_from_sdk:
name = contract["name"]
package = contract["package"]
contract_data = snx.contracts[package][name]
if name == "buyback_snx":
name = "BuybackSnx"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this? I know the contract is named something weird, but we don't want to handle that logic here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcus-snx still need to fix this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah somehow I managed to discard this change. Thanks for the reminder!

save_abi(contract_data["abi"], name)
contracts.append({"name": name, "address": contract_data["address"]})

if "contracts_from_abi" in network_config:
contracts_from_abi = network_config["contracts_from_abi"]

for contract in contracts_from_abi:
name = contract["name"]
with open(f"{path}/abi/{name}.json", "r") as file:
contract_abi = json.load(file)
save_abi(contract_abi, name)
contracts.append({"name": name, "address": contract["address"]})

squidgen_config = create_squidgen_config(rpc_endpoint, archive_url, contracts)
write_yaml(squidgen_config, "squidgen.yaml")

squid_config = create_squid_config(args.network_name)
write_yaml(squid_config, "squid.yaml")

print(
f"squidgen.yaml, squid.yaml, and ABI files have been generated for {args.network_name}"
)
9 changes: 9 additions & 0 deletions indexers-v2/networks/arbitrum_mainnet/network_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
arbitrum_mainnet:
archive_url: "https://v2.archive.subsquid.io/network/arbitrum-one"
contracts:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This format is different from the base one

- ["CoreProxy", "system"]
- ["AccountProxy", "system"]
- ["SpotMarketProxy", "spotFactory"]
- ["PerpsMarketProxy", "perpsFactory"]
- ["PerpsAccountProxy", "perpsFactory"]
network_id: 42161
Loading
Loading