Skip to content

Commit

Permalink
feat: Apibara indexers (keep-starknet-strange#47)
Browse files Browse the repository at this point in the history
* feat: Init apibara indexer repo

* fix: rename indexer to indexers

* fix: use docker compose instead of dockerfile

* feat: locked & unlocked indexers

* fix: remove init.sql file

* style: format with deno fmt

* ci: indexers CI job

* fix: move eventId to indexer

* fix: deno fmt format error

* fix: remove .env file from indexers
  • Loading branch information
ugur-eren authored and chachaleo committed Sep 10, 2024
1 parent cb14243 commit 64d2b9a
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/indexers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Indexers

on:
push:
paths: [indexers/**]
pull_request:
paths: [indexers/**]

permissions: read-all

jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- name: Check formatting
run: deno fmt --check
working-directory: indexers

- name: Lint
run: deno lint
working-directory: indexers
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enablePaths": ["indexers"]
}
2 changes: 2 additions & 0 deletions indexers/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AUTH_TOKEN=dna_your_apibara_token
POSTGRES_CONNECTION_STRING=postgresql://admin:password@postgres:5432/zkramp
175 changes: 175 additions & 0 deletions indexers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
8 changes: 8 additions & 0 deletions indexers/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 120,
"useTabs": false,
"tabWidth": 2,
"trailingComma": "all"
}
19 changes: 19 additions & 0 deletions indexers/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"lint": {
"include": ["src/"],
"rules": {
"tags": ["recommended"],
"include": ["ban-untagged-todo"],
"exclude": ["no-unused-vars"]
}
},
"fmt": {
"useTabs": false,
"lineWidth": 120,
"indentWidth": 2,
"semiColons": false,
"singleQuote": true,
"proseWrap": "preserve",
"include": ["src/"]
}
}
30 changes: 30 additions & 0 deletions indexers/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3.8'

services:
zkramp-locked-indexer:
environment:
- AUTH_TOKEN=${AUTH_TOKEN}
- POSTGRES_CONNECTION_STRING=${POSTGRES_CONNECTION_STRING}
image: quay.io/apibara/sink-postgres:latest
command: 'run ./indexer/escrow-locked.indexer.ts --connection-string ${POSTGRES_CONNECTION_STRING} -A ${AUTH_TOKEN}'
volumes:
- ./src:/indexer
networks:
- indexer
restart: on-failure

zkramp-unlocked-indexer:
environment:
- AUTH_TOKEN=${AUTH_TOKEN}
- POSTGRES_CONNECTION_STRING=${POSTGRES_CONNECTION_STRING}
image: quay.io/apibara/sink-postgres:latest
command: 'run ./indexer/escrow-unlocked.indexer.ts --connection-string ${POSTGRES_CONNECTION_STRING} -A ${AUTH_TOKEN}'
volumes:
- ./src:/indexer
networks:
- indexer
restart: on-failure

networks:
indexer:
driver: bridge
56 changes: 56 additions & 0 deletions indexers/src/escrow-locked.indexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { apibara, starknet } from './utils/deps.ts'
import { ESCROW_ADDRESS, STARTING_BLOCK } from './utils/constants.ts'
import { getCommonValues } from './utils/helpers.ts'

const filter = {
header: {
weak: true,
},
events: [
{
fromAddress: ESCROW_ADDRESS,
keys: [starknet.hash.getSelectorFromName('Locked')],
includeReceipt: false,
},
],
}

export const config = {
streamUrl: 'https://mainnet.starknet.a5a.ch',
startingBlock: STARTING_BLOCK,
network: 'starknet',
finality: 'DATA_STATUS_ACCEPTED',
filter,
sinkType: 'postgres',
sinkOptions: {
tableName: 'indexer_locked',
},
}

export default function transform({ header, events }: apibara.Block) {
return (events ?? [])
.map(({ event, transaction }) => {
if (!event.data || !event.keys) return null

const eventId = `${transaction.meta.hash}_${event.index ?? 0}`

const tokenAddress = event.keys[1]
const [fromAddress, amountLow, amountHigh] = event.data

const amount = starknet.uint256.uint256ToBN({
low: amountLow,
high: amountHigh,
})

return {
...getCommonValues(header!, event, transaction),

id: eventId,

token_address: tokenAddress,
from_address: fromAddress,
amount: amount.toString(),
}
})
.filter(Boolean)
}
57 changes: 57 additions & 0 deletions indexers/src/escrow-unlocked.indexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { apibara, starknet } from './utils/deps.ts'
import { ESCROW_ADDRESS, STARTING_BLOCK } from './utils/constants.ts'
import { getCommonValues } from './utils/helpers.ts'

const filter = {
header: {
weak: true,
},
events: [
{
fromAddress: ESCROW_ADDRESS,
keys: [starknet.hash.getSelectorFromName('UnLocked')],
includeReceipt: false,
},
],
}

export const config = {
streamUrl: 'https://mainnet.starknet.a5a.ch',
startingBlock: STARTING_BLOCK,
network: 'starknet',
finality: 'DATA_STATUS_ACCEPTED',
filter,
sinkType: 'postgres',
sinkOptions: {
tableName: 'indexer_unlocked',
},
}

export default function transform({ header, events }: apibara.Block) {
return (events ?? [])
.map(({ event, transaction }) => {
if (!event.data || !event.keys) return null

const eventId = `${transaction.meta.hash}_${event.index ?? 0}`

const tokenAddress = event.keys[1]
const [fromAddress, toAddress, amountLow, amountHigh] = event.data

const amount = starknet.uint256.uint256ToBN({
low: amountLow,
high: amountHigh,
})

return {
...getCommonValues(header!, event, transaction),

id: eventId,

token_address: tokenAddress,
from_address: fromAddress,
to_address: toAddress,
amount: amount.toString(),
}
})
.filter(Boolean)
}
2 changes: 2 additions & 0 deletions indexers/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ESCROW_ADDRESS = '0x0'
export const STARTING_BLOCK = 0
4 changes: 4 additions & 0 deletions indexers/src/utils/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as starknet from 'https://esm.sh/[email protected]'
export * as viem from 'https://esm.sh/[email protected]'

export * as apibara from 'https://esm.sh/@apibara/[email protected]/starknet'
Loading

0 comments on commit 64d2b9a

Please sign in to comment.