diff --git a/apps/indexer/src/indexer.ts b/apps/indexer/src/indexer.ts index 99c479183..2f2382941 100644 --- a/apps/indexer/src/indexer.ts +++ b/apps/indexer/src/indexer.ts @@ -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') @@ -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 @@ -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) +} diff --git a/apps/indexer/src/lib.ts b/apps/indexer/src/lib.ts index 6524b91f3..071ae8238 100644 --- a/apps/indexer/src/lib.ts +++ b/apps/indexer/src/lib.ts @@ -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) } -} \ No newline at end of file +} + +export function runPromisesInSeries(promiseFns: (() => Promise)[]): Promise { + // Start with a Promise to ensure compatibility with the accumulator's type + return promiseFns.reduce>((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()) +} diff --git a/bun.lockb b/bun.lockb index 60892f696..908aad89c 100755 Binary files a/bun.lockb and b/bun.lockb differ