From 762c38dcb76032864fbd298b54b7eb4678f355f6 Mon Sep 17 00:00:00 2001 From: Marc Doerflinger Date: Fri, 15 Nov 2024 14:05:59 +0000 Subject: [PATCH] refactor instance processing --- src/instance_processor.ts | 63 +++++++++++++++++++++------------------ src/main.ts | 28 +++++------------ 2 files changed, 41 insertions(+), 50 deletions(-) diff --git a/src/instance_processor.ts b/src/instance_processor.ts index 589872f..2a56052 100644 --- a/src/instance_processor.ts +++ b/src/instance_processor.ts @@ -35,36 +35,41 @@ export default class InstanceProcessor { } } - async processInstanceServiceEvents(instanceEvents: Array): Promise> { - return instanceEvents.map(event => { - logger.info(`Processing instance service event ${event.tx_hash} - ${event.event_name} - ${event.data}`); - const data = this.decodeIInstanceServiceEvent(event); - if (data === null || data === undefined) { - logger.error(`Failed to decode event ${event.tx_hash} - ${event.event_name} - ${event.data}`); - return null as unknown as Instance; - } - if (data.name !== 'LogInstanceServiceInstanceCreated') { - return null as unknown as Instance; - } + async processInstanceServiceEvent(event: DecodedLogEntry, instances: Map): Promise> { + if (event.event_name !== 'LogInstanceServiceInstanceCreated') { + throw new Error(`Invalid event type ${event.event_name}`); + } - logger.debug(`args: ${JSON.stringify(data.args)}`); - const nftId = data.args[0] as BigInt; - const instanceAddress = data.args[1] as string; - return { - nftId, - instanceAddress, - created: { - blockNumber: event.block_number, - txHash: event.tx_hash, - from: event.tx_from - }, - modified: { - blockNumber: event.block_number, - txHash: event.tx_hash, - from: event.tx_from - } - } as Instance; - }).filter(event => event !== null); + logger.debug(`Processing instance service instance created event`); + + const data = this.decodeIInstanceServiceEvent(event); + if (data === null || data === undefined) { + logger.error(`Failed to decode event ${event.tx_hash} - ${event.event_name} - ${event.data}`); + return instances; + } + if (data.name !== 'LogInstanceServiceInstanceCreated') { + throw new Error(`Invalid event name ${data.name}`); + } + + logger.debug(`args: ${JSON.stringify(data.args)}`); + const nftId = data.args[0] as BigInt; + const instanceAddress = data.args[1] as string; + const instance = { + nftId, + instanceAddress, + created: { + blockNumber: event.block_number, + txHash: event.tx_hash, + from: event.tx_from + }, + modified: { + blockNumber: event.block_number, + txHash: event.tx_hash, + from: event.tx_from + } + } as Instance; + instances.set(nftId, instance); + return instances; } diff --git a/src/main.ts b/src/main.ts index 3ad8535..bb123b1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -28,33 +28,19 @@ class Main { this.dune = new DuneApi(); this.nftProcessor = new NftProcessor(prisma); this.instanceProcessor = new InstanceProcessor(prisma); - } public async main(): Promise { const gifEvents = await this.dune.getLatestResult(DUNE_QUERY_ID_GIF_EVENTS, 0); const { nfts, instances } = await this.parseGifEvents(gifEvents); - // const nftTransferEvents = await this.dune.getLatestResult(DUNE_QUERY_ID_NFT_TRANSFER_EVENTS, 0); - - // let nfts = await this.nftProcessor.processNftRegistrationEvents(nftRegistrationEvents); - // nfts = await this.nftProcessor.processNftTransferEvents(nftTransferEvents, nfts); - // await this.nftProcessor.persistNfts(nfts); - - // // print one log per event - const nftIterator = nfts.values(); - for (const nft of nftIterator) { + for (const nft of nfts.values()) { logger.info(`NFT: ${nft.nftId} - ${ObjectType[nft.objectType]} - ${nft.objectAddress} - ${nft.owner}`); }; - // const instanceEvents = await this.dune.getLatestResult(DUNE_QUERY_ID_INSTANCE_SERVICE_EVENTS, 0); - // const instances = await this.instanceProcessor.processInstanceServiceEvents(instanceEvents); - // await this.instanceProcessor.persistInstances(instances); - - // print one log per event - // instances.forEach(event => { - // logger.info(`Instance: ${event.nftId} - ${event.instanceAddress}`); - // }); + for (const instance of instances.values()) { + logger.info(`Instance: ${instance.nftId} - ${instance.instanceAddress}`); + } } async parseGifEvents(gifEvents: Array) @@ -74,9 +60,9 @@ class Main { case 'LogRegistryObjectRegistered': await this.nftProcessor.processNftRegistrationEvent(event, nfts); break; - // // Transfer - // // LogRegistryObjectRegistered - // // LogInstanceServiceInstanceCreated + case 'LogInstanceServiceInstanceCreated': + await this.instanceProcessor.processInstanceServiceEvent(event, instances); + break; // // LogApplicationServiceApplicationCreated // // LogPolicyServicePolicyCreated // // LogPolicyServicePolicyPremiumCollected