-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ts-js' of github.com-ikem:ikemHood/chainevents-contract…
…s into ts-js
- Loading branch information
Showing
21 changed files
with
672 additions
and
34 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
target | ||
.snfoundry_cache/ | ||
.snfoundry_cache/ | ||
.DS_Store | ||
src/.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
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
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,7 @@ | ||
export const events = { | ||
NewEventAdded: "NewEventAdded", //replace with event identify | ||
RegisteredForEvent: "RegisteredForEvent", | ||
EventAttendanceMark: "EventAttendanceMark", | ||
EndEventRegistration: "EndEventRegistration", | ||
RSVPForEvent: "RSVPForEvent" | ||
}; |
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
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,131 @@ | ||
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!; | ||
|
||
const eventDetails: NewEventAdded = { | ||
name: hexToAscii(FieldElement.toHex(data[0]).toString()), | ||
event_id: parseInt(uint256 | ||
.uint256ToBN({ | ||
low: FieldElement.toBigInt(data[1]), | ||
high: FieldElement.toBigInt(data[2]), | ||
}) | ||
.toString()), | ||
location: hexToAscii(FieldElement.toHex(data[3]).toString()), | ||
event_owner: FieldElement.toHex(data[4]).toString() | ||
}; | ||
|
||
//Debugging purposes | ||
console.log(eventDetails); | ||
|
||
const eventExists = await Event.findByEventId(eventDetails.event_id); | ||
if (eventExists) { | ||
console.log("Event already exists"); | ||
return; | ||
} | ||
await Event.create(eventDetails); | ||
} | ||
|
||
export async function handleRegisteredForEvent(event: starknet.IEvent) { | ||
const data = event.data!; | ||
|
||
const registeredForEvent: RegisteredForEvent = { | ||
event_id: parseInt(uint256 | ||
.uint256ToBN({ | ||
low: FieldElement.toBigInt(data[0]), | ||
high: FieldElement.toBigInt(data[1]), | ||
}) | ||
.toString()), | ||
event_name: hexToAscii(FieldElement.toHex(data[2]).toString()), | ||
user_address: FieldElement.toHex(data[3]).toString() | ||
}; | ||
|
||
console.log(registeredForEvent); | ||
|
||
const hasRegistered = await Event.isUserRegistered(registeredForEvent.event_id, registeredForEvent.user_address); | ||
if (hasRegistered) { | ||
console.log("User has already registered"); | ||
return; | ||
} | ||
await Event.registerUser(registeredForEvent.event_id, registeredForEvent.user_address); | ||
} | ||
|
||
export async function handleEventAttendanceMark(event: starknet.IEvent) { | ||
const data = event.data!; | ||
|
||
const eventAttendanceMark: EventAttendanceMark = { | ||
event_id: parseInt(uint256 | ||
.uint256ToBN({ | ||
low: FieldElement.toBigInt(data[0]), | ||
high: FieldElement.toBigInt(data[1]), | ||
}) | ||
.toString()), | ||
user_address: FieldElement.toHex(data[2]).toString() | ||
}; | ||
|
||
console.log(eventAttendanceMark); | ||
|
||
const hasMarkedAttendance = await Event.hasUserAttended(eventAttendanceMark.event_id, eventAttendanceMark.user_address); | ||
if (hasMarkedAttendance) { | ||
console.log("User has already marked attendance"); | ||
return; | ||
} | ||
await Event.markAttendance(eventAttendanceMark.event_id, eventAttendanceMark.user_address); | ||
} | ||
|
||
export async function handleEndEventRegistration(event: starknet.IEvent) { | ||
const data = event.data!; | ||
|
||
const endEventRegistration: EndEventRegistration = { | ||
event_id: parseInt(uint256 | ||
.uint256ToBN({ | ||
low: FieldElement.toBigInt(data[0]), | ||
high: FieldElement.toBigInt(data[1]), | ||
}) | ||
.toString()), | ||
event_name: hexToAscii(FieldElement.toHex(data[2]).toString()), | ||
event_owner: FieldElement.toHex(data[3]).toString() | ||
}; | ||
|
||
console.log(endEventRegistration); | ||
|
||
const eventExists = await Event.findByEventId(endEventRegistration.event_id); | ||
if (!eventExists) { | ||
console.log("Event does not exist"); | ||
return; | ||
} | ||
await Event.endRegistration(endEventRegistration.event_id); | ||
} | ||
|
||
export async function handleRSVPForEvent(event: starknet.IEvent) { | ||
const data = event.data!; | ||
|
||
const rsvpForEvent: RSVPForEvent = { | ||
event_id: parseInt(uint256 | ||
.uint256ToBN({ | ||
low: FieldElement.toBigInt(data[0]), | ||
high: FieldElement.toBigInt(data[1]), | ||
}) | ||
.toString()), | ||
attendee_address: FieldElement.toHex(data[2]).toString() | ||
}; | ||
|
||
console.log(rsvpForEvent); | ||
|
||
const hasRSVPed = await Event.hasUserRSVPed(rsvpForEvent.event_id, rsvpForEvent.attendee_address); | ||
if (hasRSVPed) { | ||
console.log("User has already RSVPed"); | ||
return; | ||
} | ||
await Event.addRSVP(rsvpForEvent.event_id, rsvpForEvent.attendee_address); | ||
} |
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,69 @@ | ||
import { StreamClient, v1alpha2 } from '@apibara/protocol'; | ||
import { | ||
FieldElement, | ||
Filter, | ||
v1alpha2 as starknet, | ||
StarkNetCursor, | ||
} from '@apibara/starknet'; | ||
import { events } from '../config/events'; | ||
import { | ||
handleNewEventAdded, | ||
handleRegisteredForEvent, | ||
handleEventAttendanceMark, | ||
handleEndEventRegistration, | ||
handleRSVPForEvent, | ||
} from './handlers'; | ||
|
||
const client = new StreamClient({ | ||
url: process.env.DNA_CLIENT_URL!, | ||
clientOptions: { | ||
'grpc.max_receive_message_length': 100 * 1024 * 1024, // 100MB | ||
}, | ||
token: process.env.DNA_TOKEN, | ||
}); | ||
|
||
// Create filter combining all event handlers | ||
const filter = Filter.create().withHeader({ weak: true }); | ||
|
||
// Map your events to handlers | ||
const eventHandlers: Record<string, (event: starknet.IEvent) => Promise<void>> = { | ||
[events.NewEventAdded]: handleNewEventAdded, | ||
[events.RegisteredForEvent]: handleRegisteredForEvent, | ||
[events.EventAttendanceMark]: handleEventAttendanceMark, | ||
[events.EndEventRegistration]: handleEndEventRegistration, | ||
[events.RSVPForEvent]: handleRSVPForEvent, | ||
}; | ||
|
||
// Add all events to filter | ||
Object.keys(eventHandlers).forEach((eventKey) => { | ||
filter.addEvent((event) => | ||
event.withKeys([FieldElement.fromBigInt(BigInt(eventKey))]) | ||
); | ||
}); | ||
|
||
// Start indexer function | ||
export async function startIndexer() { | ||
client.configure({ | ||
filter: filter.encode(), | ||
batchSize: 1, | ||
finality: v1alpha2.DataFinality.DATA_STATUS_FINALIZED, | ||
cursor: StarkNetCursor.createWithBlockNumber(0), | ||
}); | ||
|
||
for await (const message of client) { | ||
if (message.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 handler = eventHandlers[eventKey]; | ||
if (handler) { | ||
await handler(event.event); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.