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

Fix - APIBara integration breaking app #66

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
4 changes: 4 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ DB_PASS=
DB_URL=
DB_SSL=
JWT_SECRET=""

# gotten from https://www.apibara.com/
DNA_TOKEN=""
DNA_CLIENT_URL="dns:///sepolia.starknet.a5a.ch"
7 changes: 7 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@
> Copy `.env.example` to `.env` file and update the postgresql credentials for production environment also set NODE_ENV to production if you want to run production, you can either use DB URL or set DB host,username,password and port with SSL for your DB connection.

### STEP 4
> Get your Apibara DNA Token:
> 1. Create an account at [Apibara](https://www.apibara.com/)
> 2. Click on "New Indexer" to create an indexer
> 3. Copy the DNA token provided
> 4. Paste the token in your `.env` file as `DNA_TOKEN`

### STEP 5
> Start project with `npm start` on production environment and `npm run dev` on development environment.
7 changes: 7 additions & 0 deletions backend/config/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const events = {
NewEventAdded: "0x1", //replace with event identify
RegisteredForEvent: "0x2",
EventAttendanceMark: "0x3",
EndEventRegistration: "0x4",
RSVPForEvent: "0x5"
};
7 changes: 0 additions & 7 deletions backend/config/events.ts

This file was deleted.

37 changes: 15 additions & 22 deletions backend/indexer/handlers.ts → backend/indexer/handlers.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import {
NewEventAdded,
RegisteredForEvent,
EventAttendanceMark,
EndEventRegistration,
RSVPForEvent
} from "./types";
import { FieldElement, v1alpha2 as starknet } from '@apibara/starknet';
import Event from "../models/Event.js";
import { uint256 } from 'starknet';
import { hexToAscii } from "../utils/tohexAscii.js";

export async function handleNewEventAdded(event: starknet.IEvent) {
const data = event.data!;
export async function handleNewEventAdded(event) {
const data = event.data;

const eventDetails: NewEventAdded = {
const eventDetails = {
name: hexToAscii(FieldElement.toHex(data[0]).toString()),
event_id: parseInt(uint256
.uint256ToBN({
Expand All @@ -36,10 +29,10 @@ export async function handleNewEventAdded(event: starknet.IEvent) {
await Event.create(eventDetails);
}

export async function handleRegisteredForEvent(event: starknet.IEvent) {
const data = event.data!;
export async function handleRegisteredForEvent(event) {
const data = event.data;

const registeredForEvent: RegisteredForEvent = {
const registeredForEvent = {
event_id: parseInt(uint256
.uint256ToBN({
low: FieldElement.toBigInt(data[0]),
Expand All @@ -60,10 +53,10 @@ export async function handleRegisteredForEvent(event: starknet.IEvent) {
await Event.registerUser(registeredForEvent.event_id, registeredForEvent.user_address);
}

export async function handleEventAttendanceMark(event: starknet.IEvent) {
const data = event.data!;
export async function handleEventAttendanceMark(event) {
const data = event.data;

const eventAttendanceMark: EventAttendanceMark = {
const eventAttendanceMark = {
event_id: parseInt(uint256
.uint256ToBN({
low: FieldElement.toBigInt(data[0]),
Expand All @@ -83,10 +76,10 @@ export async function handleEventAttendanceMark(event: starknet.IEvent) {
await Event.markAttendance(eventAttendanceMark.event_id, eventAttendanceMark.user_address);
}

export async function handleEndEventRegistration(event: starknet.IEvent) {
const data = event.data!;
export async function handleEndEventRegistration(event) {
const data = event.data;

const endEventRegistration: EndEventRegistration = {
const endEventRegistration = {
event_id: parseInt(uint256
.uint256ToBN({
low: FieldElement.toBigInt(data[0]),
Expand All @@ -107,10 +100,10 @@ export async function handleEndEventRegistration(event: starknet.IEvent) {
await Event.endRegistration(endEventRegistration.event_id);
}

export async function handleRSVPForEvent(event: starknet.IEvent) {
const data = event.data!;
export async function handleRSVPForEvent(event) {
const data = event.data;

const rsvpForEvent: RSVPForEvent = {
const rsvpForEvent = {
event_id: parseInt(uint256
.uint256ToBN({
low: FieldElement.toBigInt(data[0]),
Expand Down
12 changes: 6 additions & 6 deletions backend/indexer/index.ts → backend/indexer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import {
v1alpha2 as starknet,
StarkNetCursor,
} from '@apibara/starknet';
import { events } from '../config/events';
import { events } from '../config/events.js';
import {
handleNewEventAdded,
handleRegisteredForEvent,
handleEventAttendanceMark,
handleEndEventRegistration,
handleRSVPForEvent,
} from './handlers';
} from './handlers.js';

const client = new StreamClient({
url: process.env.DNA_CLIENT_URL!,
url: process.env.DNA_CLIENT_URL,
clientOptions: {
'grpc.max_receive_message_length': 100 * 1024 * 1024, // 100MB
},
Expand All @@ -26,7 +26,7 @@ const client = new StreamClient({
const filter = Filter.create().withHeader({ weak: true });

// Map your events to handlers
const eventHandlers: Record<string, (event: starknet.IEvent) => Promise<void>> = {
const eventHandlers = {
[events.NewEventAdded]: handleNewEventAdded,
[events.RegisteredForEvent]: handleRegisteredForEvent,
[events.EventAttendanceMark]: handleEventAttendanceMark,
Expand All @@ -52,12 +52,12 @@ export async function startIndexer() {

for await (const message of client) {
if (message.message === 'data') {
const { data } = message.data!;
const { data } = message.data;
for (const item of data) {
const block = starknet.Block.decode(item);
for (const event of block.events) {
if (!event.event) continue;
const eventKey = FieldElement.toHex(event.event.keys![0]);
const eventKey = FieldElement.toHex(event.event.keys[0]);
const handler = eventHandlers[eventKey];
if (handler) {
await handler(event.event);
Expand Down
28 changes: 0 additions & 28 deletions backend/indexer/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
export const up = function(knex) {
return knex.schema.createTable("event_attendance", function (table) {
table.increments("id").primary();
table.string("event_id").notNullable();
Expand All @@ -18,6 +18,6 @@ exports.up = function(knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
export const down = function(knex) {
return knex.schema.dropTableIfExists("event_attendance");
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
export const up = function(knex) {
return knex.schema.createTable("event_registrations", function (table) {
table.increments("id").primary();
table.string("event_id").notNullable();
Expand All @@ -19,6 +19,6 @@ exports.up = function(knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
export const down = function(knex) {
return knex.schema.dropTableIfExists("event_registrations");
};
4 changes: 2 additions & 2 deletions backend/migrations/20241124121820_create_event_rsvps_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
export const up = function(knex) {
return knex.schema.createTable("event_rsvps", function (table) {
table.increments("id").primary();
table.string("event_id").notNullable();
Expand All @@ -18,6 +18,6 @@ exports.up = function(knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
export const down = function(knex) {
return knex.schema.dropTableIfExists("event_rsvps");
};