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

#516 indexer and backend for DAO contract #528

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions .github/workflows/data-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ jobs:
pnpm install
fi

# - name: Prettier Format Check
# run: pnpm format:check
- name: Build indexer-v2-db
run: pnpm -w run build:indexer-v2-db # it's simpler to do this here for moment

- name: Build
run: pnpm build

- name: TypeScript Check
run: pnpm ts:check

- name: Build
run: pnpm build
# - name: Prettier Format Check
# run: pnpm format:check
64 changes: 64 additions & 0 deletions .github/workflows/indexer-v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Indexer v2 App

on:
push:
paths:
- ".github/workflows/indexer-v2.yml"
- "apps/indexer-v2/**"
- "package.json"
pull_request:
branches:
- main
paths:
- "apps/indexer-v2/**"

jobs:
check-app:
runs-on: ubuntu-latest

defaults:
run:
working-directory: ./apps/indexer-v2

steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'

- name: Install pnpm
uses: pnpm/action-setup@v4

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

- name: Setup pnpm cache
uses: actions/cache@v4
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-

- name: Install Dependencies
run: |
if [ -f "pnpm-lock.yaml" ]; then
pnpm install --frozen-lockfile
else
pnpm install
fi

- name: Build
run: pnpm build:all

# - name: TypeScript Check
# run: pnpm ts:check

# - name: Prettier Format Check
# run: pnpm format:check
4 changes: 3 additions & 1 deletion apps/data-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "tsc",
"build:index": "tsc",
"build:all_repo": "pnpm run build:all",
"build:all": "pnpm -w run build:indexer-prisma && pnpm -w run build:prisma-db && pnpm build",
"build:all": "pnpm -w run build:indexer-prisma && pnpm -w run build:prisma-db && pnpm -w run build:indexer-v2-db && pnpm build",
"build:prisma": "",
"start": "ts-node src/index.ts",
"start:dev": "ts-node-dev src/index.ts",
Expand Down Expand Up @@ -41,6 +41,7 @@
"cors": "^2.8.5",
"data-backend": "link:",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.37.0",
"fastify": "^5.1.0",
"fastify-plugin": "^5.0.1",
"fastify-socket.io": "^5.1.0",
Expand All @@ -49,6 +50,7 @@
"graphql": "^16.9.0",
"helmet": "^7.2.0",
"indexer-prisma": "workspace:*",
"indexer-v2-db": "workspace:*",
"jsonwebtoken": "^9.0.2",
"nodemailer": "^6.9.16",
"pg": "^8.13.1",
Expand Down
57 changes: 57 additions & 0 deletions apps/data-backend/src/routes/indexer/dao.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { FastifyInstance, RouteOptions } from 'fastify';
import { HTTPStatus } from '../../utils/http';
import { isValidStarknetAddress } from '../../utils/starknet';
import { eq } from 'drizzle-orm';
import { db } from 'indexer-v2-db/dist';
import { daoCreation, daoProposal } from 'indexer-v2-db/dist/schema';

interface DaoParams {
dao_address: string;
}

async function daoServiceRoute(fastify: FastifyInstance, options: RouteOptions) {
// Get tips by sender
fastify.get('/daos', async (request, reply) => {
try {
const daos = await db.select().from(daoCreation).execute();

reply.status(HTTPStatus.OK).send({
data: daos,
});
} catch (error) {
console.error('Error fetching daos:', error);
reply.status(HTTPStatus.InternalServerError).send({ message: 'Internal server error.' });
}
});

// Get tips by recipient
fastify.get<{
Params: DaoParams;
}>('/daos/:dao_address/proposals/', async (request, reply) => {
try {
const { dao_address } = request.params;
if (!isValidStarknetAddress(dao_address)) {
reply.status(HTTPStatus.BadRequest).send({
code: HTTPStatus.BadRequest,
message: 'Invalid dao address',
});
return;
}

const daoProposals = await db
.select()
.from(daoProposal)
.where(eq(daoProposal.contractAddress, dao_address))
.execute();

reply.status(HTTPStatus.OK).send({
data: daoProposals,
});
} catch (error) {
console.error('Error fetching dao proposals', error);
reply.status(HTTPStatus.InternalServerError).send({ message: 'Internal server error.' });
}
});
}

export default daoServiceRoute;
4 changes: 2 additions & 2 deletions apps/indexer-v2/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
POSTGRES_CONNECTION_STRING=postgres://postgres:postgres@localhost:5432/postgres
DNA_TOKEN_ADDRESS=fdfsfsd
INDEXER_V2_DATABASE_URL=postgres://postgres:postgres@localhost:5435/indexer
DNA_TOKEN_ADDRESS=
48 changes: 20 additions & 28 deletions apps/indexer-v2/README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
# Indexer V2
# indexer-v2

## How to run
A new indexer that use the apibara v2 (currently in preview version)
He currently used a new database, see `.env.example`

```
docker compose up -d
```
## How generate ABI files

Run indexer

```
docker run -it --env-file ./.env dao-indexer run /app/main.ts --tls-accept-invalid-certificates=true --allow-env-from-env POSTGRES_CONNECTION_STRING,AUTH_TOKEN
The more simple to generate ABI files is to use the `abi-wan-kanabi` tool that will convert json contract class file
into typescript file.

```shell
npx abi-wan-kanabi --input ../../onchain/cairo/afk/target/dev/[contract_name].contract_class.json --output indexers/abi/[contract_name].abi.ts
```

```
docker run -it --env-file ./.env dao-indexer run /app/main.ts --tls-accept-invalid-certificates=true --allow-env-from-env POSTGRES_CONNECTION_STRING
```
## How to run

## Install
### Dev

Example:
https://github.com/apibara/typescript-sdk/tree/main/examples/cli
In dev, we can run all indexers or chose one indexer to run.

# Todo
```shell
pnpm dev
pnpm dev --indexer index_name
```

- Fix script in Typescript: indexer script operation failed
├╴at /build/source/script/src/script.rs:339:22
├╴failed to run indexer event loop
╰╴error: TypeError: No such file or di
### Prod

In production, we can only run one indexer at time.

- Saved DAO AA Created in db
- Saved DAO Proposal Created in db
- Saved DAO Proposal Voted in db
- Saved DAO Proposal Executed in db
- Saved DAO Proposal Whitelisted in db
- Saved DAO Proposal Ended in db
- Saved DAO Proposal Failed in db
- Saved DAO Proposal Succeeded in db
```shell
pnpm start --indexer index_name
```
4 changes: 4 additions & 0 deletions apps/indexer-v2/apibara.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { defineConfig } from 'apibara/config';

export default defineConfig({
runtimeConfig: {
streamUrl: 'https://starknet-sepolia.preview.apibara.org',
startingCursor: {
orderKey: 500_000,
},
pgLiteDBPath: 'memory://persistence',
},
presets: {
Expand Down
Loading