forked from keep-starknet-strange/zkramp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Apibara indexers (keep-starknet-strange#47)
* 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
Showing
12 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"deno.enablePaths": ["indexers"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const ESCROW_ADDRESS = '0x0' | ||
export const STARTING_BLOCK = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Oops, something went wrong.