Skip to content
Open
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
43 changes: 28 additions & 15 deletions packages/interfold-sdk/src/events/event-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,7 @@ export class EventListener implements SDKEventEmitter {
for (let i = 0; i < logs.length; i++) {
const log = logs[i]
if (!log) break
const event: InterfoldEvent<T> = {
type: eventType,
data: (log as unknown as { args: unknown }).args as T extends InterfoldEventTypeT
? InterfoldEventData[T]
: T extends RegistryEventTypeT
? RegistryEventData[T]
: unknown,
log,
timestamp: new Date(),
blockNumber: log.blockNumber ?? BigInt(0),
transactionHash: log.transactionHash ?? '0x',
}
emitter.emit(event)
emitter.emit(emitter.buildEvent(eventType, log))
}
},
})
Expand All @@ -129,6 +117,21 @@ export class EventListener implements SDKEventEmitter {
}
}

private buildEvent<T extends AllEventTypes>(eventType: T, log: Log): InterfoldEvent<T> {
return {
type: eventType,
data: (log as unknown as { args: unknown }).args as T extends InterfoldEventTypeT
? InterfoldEventData[T]
: T extends RegistryEventTypeT
? RegistryEventData[T]
: unknown,
log,
timestamp: new Date(),
blockNumber: log.blockNumber ?? BigInt(0),
transactionHash: log.transactionHash ?? '0x',
}
}

public async watchLogs(address: `0x${string}`, callback: (log: Log) => void): Promise<void> {
const watcherKey = `logs:${address}`

Expand Down Expand Up @@ -249,11 +252,21 @@ export class EventListener implements SDKEventEmitter {
while (this.isPolling) {
try {
const currentBlock = await this.publicClient.getBlockNumber()

if (currentBlock > this.lastBlockNumber) {
const fromBlock = this.lastBlockNumber + BigInt(1)

for (const eventType of this.listeners.keys()) {
const logs = await this.getHistoricalEvents(eventType, fromBlock, currentBlock)
Comment thread
leonschh marked this conversation as resolved.

for (const log of logs) {
this.emit(this.buildEvent(eventType, log))
}
}

this.lastBlockNumber = currentBlock
Comment thread
leonschh marked this conversation as resolved.
Comment thread
leonschh marked this conversation as resolved.
}

await sleep(this.config.pollingInterval || 5000)
} catch (error) {
console.error('Error during polling:', error)
Expand Down
Loading