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

feat: apibara setup #212

Merged
merged 6 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions land-registry-indexer/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
STARTING_BLOCK=0
LAND_REGISTRY_ADDRESS=0x5a4054a1b1389dcd48b650637977280d32f1ad8b3027bc6c7eb606bf7e28bf5
DATABASE_URL=postgresql://username:password@localhost:5432/land_registry
APIBARA_URL=
11 changes: 11 additions & 0 deletions land-registry-indexer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .
RUN npm run build

CMD ["npm", "start"]
18 changes: 18 additions & 0 deletions land-registry-indexer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Land Registry Indexer

An Apibara indexer for tracking and storing Land Registry smart contract events on StarkNet. This indexer maintains a complete history of land registrations, transfers, verifications, and marketplace activities in a PostgreSQL database.

## Features

- Tracks all Land Registry contract events
- Stores event data in a normalized PostgreSQL database
- Maintains relationships between lands, inspectors, and listings
- Provides complete history of all land-related transactions
- Supports Docker deployment

## Prerequisites

- Node.js >= 18
- PostgreSQL >= 14
- Docker and Docker Compose (for containerized deployment)
- StarkNet node access (via Apibara)
27 changes: 27 additions & 0 deletions land-registry-indexer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3.8'

services:
postgres:
image: postgres:14
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
POSTGRES_DB: ${POSTGRES_DB:-land_registry}
volumes:
- postgres_data:/var/lib/postgresql/data
- ./schema.sql:/docker-entrypoint-initdb.d/schema.sql
ports:
- "5432:5432"

indexer:
build: .
environment:
- DATABASE_URL=postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/${POSTGRES_DB:-land_registry}
- LAND_REGISTRY_ADDRESS=${LAND_REGISTRY_ADDRESS}
- STARTING_BLOCK=${STARTING_BLOCK:-0}
- APIBARA_URL=${APIBARA_URL}
depends_on:
- postgres

volumes:
postgres_data:
24 changes: 24 additions & 0 deletions land-registry-indexer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "land-registry-indexer",
"version": "1.0.0",
"description": "Apibara indexer for Land Registry events",
"main": "src/index.ts",
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "ts-node src/index.ts"
},
"dependencies": {
"@apibara/indexer": "^0.3.0",
"@apibara/protocol": "^0.4.0",
"@apibara/starknet": "^0.3.0",
"dotenv": "^16.0.3",
"pg": "^8.11.0",
"typescript": "^5.0.4"
},
"devDependencies": {
"@types/node": "^20.2.5",
"@types/pg": "^8.10.1",
"ts-node": "^10.9.1"
}
}
100 changes: 100 additions & 0 deletions land-registry-indexer/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
CREATE TABLE IF NOT EXISTS lands (
land_id VARCHAR PRIMARY KEY,
owner_address VARCHAR NOT NULL,
location_latitude NUMERIC,
location_longitude NUMERIC,
area NUMERIC,
land_use VARCHAR,
status VARCHAR,
inspector_address VARCHAR,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS land_transfers (
id SERIAL PRIMARY KEY,
land_id VARCHAR NOT NULL REFERENCES lands(land_id),
from_address VARCHAR NOT NULL,
to_address VARCHAR NOT NULL,
transaction_hash VARCHAR NOT NULL,
block_number BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL
);

CREATE TABLE IF NOT EXISTS land_verifications (
id SERIAL PRIMARY KEY,
land_id VARCHAR NOT NULL REFERENCES lands(land_id),
inspector_address VARCHAR NOT NULL,
transaction_hash VARCHAR NOT NULL,
block_number BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL
);

CREATE TABLE IF NOT EXISTS land_updates (
id SERIAL PRIMARY KEY,
land_id VARCHAR NOT NULL REFERENCES lands(land_id),
area NUMERIC,
land_use VARCHAR,
transaction_hash VARCHAR NOT NULL,
block_number BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL
);

CREATE TABLE IF NOT EXISTS inspectors (
address VARCHAR PRIMARY KEY,
is_active BOOLEAN DEFAULT true,
added_at TIMESTAMP NOT NULL,
removed_at TIMESTAMP
);

CREATE TABLE IF NOT EXISTS inspector_assignments (
id SERIAL PRIMARY KEY,
land_id VARCHAR NOT NULL REFERENCES lands(land_id),
inspector_address VARCHAR NOT NULL REFERENCES inspectors(address),
transaction_hash VARCHAR NOT NULL,
block_number BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL
);

-- CREATE TABLE IF NOT EXISTS fee_updates (
-- id SERIAL PRIMARY KEY,
-- old_fee NUMERIC NOT NULL,
-- new_fee NUMERIC NOT NULL,
-- transaction_hash VARCHAR NOT NULL,
-- block_number BIGINT NOT NULL,
-- timestamp TIMESTAMP NOT NULL
-- );

CREATE TABLE IF NOT EXISTS listings (
id SERIAL PRIMARY KEY,
land_id VARCHAR NOT NULL REFERENCES lands(land_id),
seller_address VARCHAR NOT NULL,
price NUMERIC NOT NULL,
status VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
transaction_hash VARCHAR NOT NULL,
block_number BIGINT NOT NULL
);

CREATE TABLE IF NOT EXISTS listing_price_updates (
id SERIAL PRIMARY KEY,
listing_id INTEGER NOT NULL REFERENCES listings(id),
old_price NUMERIC NOT NULL,
new_price NUMERIC NOT NULL,
transaction_hash VARCHAR NOT NULL,
block_number BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL
);

CREATE TABLE IF NOT EXISTS land_sales (
id SERIAL PRIMARY KEY,
listing_id INTEGER NOT NULL REFERENCES listings(id),
land_id VARCHAR NOT NULL REFERENCES lands(land_id),
seller_address VARCHAR NOT NULL,
buyer_address VARCHAR NOT NULL,
price NUMERIC NOT NULL,
transaction_hash VARCHAR NOT NULL,
block_number BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL
);
16 changes: 16 additions & 0 deletions land-registry-indexer/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Configuration settings for the Land Registry Indexer
*
* This module loads environment variables from a .env file and provides
* configuration constants used throughout the application.
*/

import dotenv from 'dotenv';
dotenv.config();

export const config = {
startingBlock: Number(process.env.STARTING_BLOCK || 0),
landRegistryAddress: process.env.LAND_REGISTRY_ADDRESS || '',
pgConnection: process.env.DATABASE_URL || '',
apibaraUrl: process.env.APIBARA_URL || '',
};
Loading
Loading