Skip to content

Commit

Permalink
wip: event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
gaboesquivel committed Feb 5, 2024
1 parent 960c390 commit 1e68b30
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
70 changes: 66 additions & 4 deletions apps/indexer/src/indexer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { stringify, parseEventLogs } from 'viem'
import { client } from './evm-client'
import { TestnetEasyAuction } from 'smartsale-abis'
import { runPromisesInSeries } from './lib'

export async function startIndexer() {
console.log('indexing starting')
Expand All @@ -13,10 +14,13 @@ export async function startIndexer() {
fromBlock: BigInt(TestnetEasyAuction.indexFromBlock),
toBlock: blockNumber,
})
const filteredlogs = logs.filter((log) => log.eventName !== 'OwnershipTransferred')
console.log(
stringify(parseEventLogs({ abi: TestnetEasyAuction.abi, logs: filteredlogs }), null, 2),
)
processLogs(logs)

// const filteredlogs = logs.filter((log) => log.eventName !== 'OwnershipTransferred')

// console.log(
// stringify(parseEventLogs({ abi: TestnetEasyAuction.abi, logs: filteredlogs }), null, 2),
// )
// await writeToFile(stringify(, null, 2), './logs.json')

// Watch for new event logs
Expand All @@ -27,6 +31,64 @@ export async function startIndexer() {
console.log(
stringify(parseEventLogs({ abi: TestnetEasyAuction.abi, logs: filteredlogs }), null, 2),
)
processLogs(logs)
},
})
}

async function processLogs(logs: any) {
const actions = logs.map((log: any) => {
const eventName = log.eventName.toString()
if (!isKeyOfEventHandlers(eventName)) return null
return async () => eventHandlers[eventName](log)
})

runPromisesInSeries(actions)
}

function isKeyOfEventHandlers(key: string) {
return key in eventHandlers
}

const eventHandlers: { [key: string]: (log: any) => void } = {
AuctionCleared: handleAuctionCleared,
CancellationSellOrder: handleCancellationSellOrder,
ClaimedFromOrder: handleClaimedFromOrder,
NewAuction: handleNewAuction,
NewSellOrder: handleNewSellOrder,
NewUser: handleNewUser,
OwnershipTransferred: handleOwnershipTransferred,
UserRegistration: handleUserRegistration,
}

function handleAuctionCleared(log: any) {
console.log('handleAuctionCleared', log)
}

function handleCancellationSellOrder(log: any) {
console.log('handleCancellationSellOrder', log)
}

function handleClaimedFromOrder(log: any) {
console.log('handleClaimedFromOrder', log)
}

function handleNewAuction(log: any) {
console.log('handleNewAuction', log)
}

function handleNewSellOrder(log: any) {
console.log('handleNewSellOrder', log)
}

function handleNewUser(log: any) {
console.log('handleNewUser', log)
}

function handleOwnershipTransferred(log: any) {
console.log('handleOwnershipTransferred', log)
}

function handleUserRegistration(log: any) {
console.log('handleUserRegistration', log)
}
22 changes: 16 additions & 6 deletions apps/indexer/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import fs from 'fs/promises'

export async function writeToFile(data:string, filePath:string) {
export async function writeToFile(data: string, filePath: string) {
try {
// Write the data to the specified file
// If the file does not exist, it will be created.
await fs.writeFile(filePath, data, 'utf8');
console.log('Logs have been written to the file successfully.');
await fs.writeFile(filePath, data, 'utf8')

console.log('Logs have been written to the file successfully.')
} catch (error) {
console.error('Error writing logs to file:', error);
console.error('Error writing logs to file:', error)
}
}
}

export function runPromisesInSeries<T>(promiseFns: (() => Promise<T>)[]): Promise<T | void> {
// Start with a Promise<void> to ensure compatibility with the accumulator's type
return promiseFns.reduce<Promise<T | void>>((prevPromise, currentPromiseFn) => {
// Chain the current promise to the accumulator after the previous one completes
// Here, we ignore the result of the previous promise, as we're focusing on chaining
// If collecting results is needed, an additional structure would be required
return prevPromise.then(() => currentPromiseFn())
}, Promise.resolve())
}
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit 1e68b30

Please sign in to comment.