generated from checkpoint-labs/checkpoint-template
-
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.
- Loading branch information
Ioan Alexandru Oara
authored
Nov 7, 2023
0 parents
commit 1c8269f
Showing
21 changed files
with
5,701 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,2 @@ | ||
DATABASE_URL=mysql://root:default_password@localhost:3306/checkpoint | ||
|
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,23 @@ | ||
{ | ||
"parser": "@typescript-eslint/parser", | ||
"extends": [ | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"plugins": [ | ||
"prettier", | ||
"@typescript-eslint" | ||
], | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"no-console": "off", | ||
"prettier/prettier": "error", | ||
"@typescript-eslint/explicit-function-return-type": "off", | ||
"@typescript-eslint/ban-ts-ignore": "off", | ||
"@typescript-eslint/camelcase": "off", | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"@typescript-eslint/ban-ts-comment": "off" | ||
} | ||
} |
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,21 @@ | ||
name: Build and Run Tests | ||
on: [push] | ||
jobs: | ||
build-test: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: '18' | ||
cache: 'yarn' | ||
- name: Set up MySQL | ||
run: | | ||
sudo /etc/init.d/mysql start | ||
mysql -e 'CREATE DATABASE checkpoint;' -uroot -proot | ||
- run: yarn install | ||
- run: yarn lint | ||
- run: yarn build | ||
- run: yarn test | ||
env: | ||
DATABASE_URL: 'mysql://root:[email protected]:3306/checkpoint' |
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,11 @@ | ||
.DS_Store | ||
node_modules | ||
dist | ||
build | ||
.env | ||
coverage | ||
|
||
# Remove some common IDE working directories | ||
.idea | ||
.vscode | ||
*.log |
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": true, | ||
"singleQuote": true, | ||
"printWidth": 100, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"arrowParens": "avoid" | ||
} |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Snapshot Labs | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,65 @@ | ||
# Checkpoint starter template | ||
|
||
This is a template to quickly get started to use [Checkpoint](https://docs.checkpoint.fyi) | ||
to expose a GraphQL API to query data from your StarkNet contracts. | ||
|
||
## Getting started | ||
|
||
This starter project contains logic to index events from a StarkNet Poster contract that is defined in the | ||
[starknet-poster](https://github.com/snapshot-labs/starknet-poster/blob/master/contracts/Poster.cairo) repository. | ||
|
||
Create a copy of this repository by clicking **'Use this template'** button or clicking [this | ||
link](https://github.com/snapshot-labs/checkpoint-template/generate). | ||
|
||
**Requirements** | ||
|
||
- Node.js (>= 16.x.x) | ||
- Docker with `docker-compose` | ||
- Yarn | ||
|
||
> You can also use npm, just make sure to replace the subsequent 'yarn' commands with their npm equivalent. | ||
After cloning this project, run the following command to install dependencies: | ||
|
||
```bash | ||
yarn # or 'npm install' | ||
``` | ||
|
||
Next, you'll need a MySQL server running and a connection string available as environment variable `DATABASE_URL`. | ||
You can use `docker-compose` to set up default MySQL server in container: | ||
|
||
```bash | ||
docker-compose up -d | ||
``` | ||
|
||
> For local development, you can create a .env file from the .env.example file and the application will read the values on startup. | ||
Next, start up the server: | ||
|
||
```bash | ||
yarn dev # for local development or else `yarn start` for production build. | ||
``` | ||
|
||
This will expose a GraphQL API endpoint locally at http://localhost:3000. You can easily interact with this endpoint using the graphiql interface by visiting http://localhost:3000 in your browser. | ||
|
||
To fetch a list of Post's try the following query: | ||
|
||
```graphql | ||
query { | ||
posts { | ||
id | ||
author | ||
content | ||
tag | ||
created_at_block | ||
created_at | ||
tx_hash | ||
} | ||
} | ||
``` | ||
|
||
To learn more about the different ways you can query the GraphQL API, visit the Checkpoint documentation [here](https://docs.checkpoint.fyi/core-concepts/entity-schema). | ||
|
||
## License | ||
|
||
MIT |
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,17 @@ | ||
version: '3.8' | ||
services: | ||
mysql: | ||
image: mysql:8.0 | ||
command: --default-authentication-plugin=mysql_native_password | ||
cap_add: | ||
- SYS_NICE | ||
ports: | ||
- '3306:3306' | ||
environment: | ||
- MYSQL_ROOT_PASSWORD=default_password | ||
- MYSQL_DATABASE=checkpoint | ||
volumes: | ||
- mysql:/var/lib/mysql | ||
volumes: | ||
mysql: | ||
driver: local |
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,20 @@ | ||
/* | ||
* For a detailed explanation regarding each configuration property and type check, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
export default { | ||
clearMocks: true, | ||
collectCoverage: true, | ||
coverageDirectory: 'coverage', | ||
coverageProvider: 'v8', | ||
|
||
// An array of regexp pattern strings used to skip coverage collection | ||
coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/dist/'], | ||
|
||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
setupFiles: ['dotenv/config'], | ||
testPathIgnorePatterns: ['/node_modules/', '<rootDir>/dist/'], | ||
moduleFileExtensions: ['js', 'ts'] | ||
}; |
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,44 @@ | ||
{ | ||
"name": "@snapshot-labs/checkpoint-template", | ||
"version": "1.0.0", | ||
"description": "Checkpoint starter template", | ||
"main": "dist/src/index.js", | ||
"repository": "[email protected]:snapshot-labs/checkpoint-template.git", | ||
"scripts": { | ||
"lint": "eslint src/ test/ --ext .ts --fix", | ||
"build": "tsc -p tsconfig.build.json", | ||
"dev": "nodemon src/index.ts", | ||
"start": "node dist/src/index.js", | ||
"test": "jest" | ||
}, | ||
"author": "Snapshot Labs", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@ethersproject/address": "^5.6.0", | ||
"@ethersproject/bignumber": "^5.6.1", | ||
"@snapshot-labs/checkpoint": "^0.1.0-beta.25", | ||
"@snapshot-labs/sx": "^0.1.0-beta.44", | ||
"cors": "^2.8.5", | ||
"dotenv": "^16.0.1", | ||
"express": "^4.18.1", | ||
"starknet": "^5.19.3" | ||
}, | ||
"devDependencies": { | ||
"@types/bn.js": "^5.1.0", | ||
"@types/express": "^4.17.13", | ||
"@types/jest": "^27.5.1", | ||
"@types/mysql": "^2.15.21", | ||
"@types/node": "^20.8.10", | ||
"@typescript-eslint/eslint-plugin": "^5.25.0", | ||
"@typescript-eslint/parser": "^5.25.0", | ||
"eslint": "^8.15.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^28.1.0", | ||
"jest-mock-extended": "^2.0.6", | ||
"nodemon": "^2.0.16", | ||
"prettier": "^2.6.2", | ||
"ts-jest": "^28.0.2", | ||
"ts-node": "^10.7.0", | ||
"typescript": "^4.9.3" | ||
} | ||
} |
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,6 @@ | ||
[ | ||
{ | ||
"contract": "0x04d10712e72b971262f5df09506bbdbdd7f729724030fa909e8c8e7ac2fd0012", | ||
"blocks": [185778, 185780, 220016, 220138, 221984] | ||
} | ||
] |
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,16 @@ | ||
{ | ||
"network_node_url": "https://starknet-goerli.infura.io/v3/46a5dd9727bf48d4a132672d3f376146", | ||
"sources": [ | ||
{ | ||
"contract": "0x04d10712e72b971262f5df09506bbdbdd7f729724030fa909e8c8e7ac2fd0012", | ||
"start": 185778, | ||
"deploy_fn": "handleDeploy", | ||
"events": [ | ||
{ | ||
"name": "new_post", | ||
"fn": "handleNewPost" | ||
} | ||
] | ||
} | ||
] | ||
} |
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,43 @@ | ||
import 'dotenv/config'; | ||
import express from 'express'; | ||
import cors from 'cors'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import Checkpoint, { LogLevel } from '@snapshot-labs/checkpoint'; | ||
import config from './config.json'; | ||
import * as writers from './writers'; | ||
import checkpointBlocks from './checkpoints.json'; | ||
|
||
const dir = __dirname.endsWith('dist/src') ? '../' : ''; | ||
const schemaFile = path.join(__dirname, `${dir}../src/schema.gql`); | ||
const schema = fs.readFileSync(schemaFile, 'utf8'); | ||
|
||
const checkpointOptions = { | ||
logLevel: LogLevel.Info | ||
// prettifyLogs: true, // uncomment in local dev | ||
}; | ||
|
||
// Initialize checkpoint | ||
// @ts-ignore | ||
const checkpoint = new Checkpoint(config, writers, schema, checkpointOptions); | ||
|
||
// resets the entities already created in the database | ||
// ensures data is always fresh on each re-run | ||
checkpoint | ||
.reset() | ||
.then(() => checkpoint.seedCheckpoints(checkpointBlocks)) | ||
.then(() => { | ||
// start the indexer | ||
checkpoint.start(); | ||
}); | ||
|
||
const app = express(); | ||
app.use(express.json({ limit: '4mb' })); | ||
app.use(express.urlencoded({ limit: '4mb', extended: false })); | ||
app.use(cors({ maxAge: 86400 })); | ||
|
||
// mount Checkpoint's GraphQL API on path / | ||
app.use('/', checkpoint.graphql); | ||
|
||
const PORT = process.env.PORT || 3000; | ||
app.listen(PORT, () => console.log(`Listening at http://localhost:${PORT}`)); |
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,11 @@ | ||
scalar Text | ||
|
||
type Post { | ||
id: String! | ||
author: String! | ||
content: Text! | ||
tag: String | ||
tx_hash: String! | ||
created_at: Int! | ||
created_at_block: Int! | ||
} |
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,16 @@ | ||
import { getAddress } from '@ethersproject/address'; | ||
import { BigNumber } from '@ethersproject/bignumber'; | ||
import { shortStringArrToStr } from '@snapshot-labs/sx/dist/utils/strings'; | ||
|
||
export const toAddress = bn => { | ||
try { | ||
return getAddress(BigNumber.from(bn).toHexString()); | ||
} catch (e) { | ||
return bn; | ||
} | ||
}; | ||
|
||
export const hexStrArrToStr = (data, start: number, length: number | bigint): string => { | ||
const dataSlice = data.slice(start, start + Number(length)); | ||
return shortStringArrToStr(dataSlice.map(m => BigInt(m))); | ||
}; |
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,53 @@ | ||
import { hexStrArrToStr, toAddress } from './utils'; | ||
import type { CheckpointWriter } from '@snapshot-labs/checkpoint'; | ||
|
||
export async function handleDeploy() { | ||
// Run logic as at the time Contract was deployed. | ||
} | ||
|
||
// This decodes the new_post events data and stores successfully | ||
// decoded information in the `posts` table. | ||
// | ||
// See here for the original logic used to create post transactions: | ||
// https://gist.github.com/perfectmak/417a4dab69243c517654195edf100ef9#file-index-ts | ||
export async function handleNewPost({ block, tx, event, mysql }: Parameters<CheckpointWriter>[0]) { | ||
if (!block || !event) return; | ||
|
||
const author = toAddress(event.data[0]); | ||
let content = ''; | ||
let tag = ''; | ||
const contentLength = BigInt(event.data[1]); | ||
const tagLength = BigInt(event.data[2 + Number(contentLength)]); | ||
const timestamp = block.timestamp; | ||
const blockNumber = block.block_number; | ||
|
||
// parse content bytes | ||
try { | ||
content = hexStrArrToStr(event.data, 2, contentLength); | ||
} catch (e) { | ||
console.error(`failed to decode content on block [${blockNumber}]: ${e}`); | ||
return; | ||
} | ||
|
||
// parse tag bytes | ||
try { | ||
tag = hexStrArrToStr(event.data, 3 + Number(contentLength), tagLength); | ||
} catch (e) { | ||
console.error(`failed to decode tag on block [${blockNumber}]: ${e}`); | ||
return; | ||
} | ||
|
||
// post object matches fields of Post type in schema.gql | ||
const post = { | ||
id: `${author}/${tx.transaction_hash}`, | ||
author, | ||
content, | ||
tag, | ||
tx_hash: tx.transaction_hash, | ||
created_at: timestamp, | ||
created_at_block: blockNumber | ||
}; | ||
|
||
// table names are `lowercase(TypeName)s` and can be interacted with sql | ||
await mysql.queryAsync('INSERT IGNORE INTO posts SET ?', [post]); | ||
} |
Oops, something went wrong.