Skip to content

Commit

Permalink
refactor instance processing
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Nov 15, 2024
1 parent d20d7c6 commit 762c38d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 50 deletions.
63 changes: 34 additions & 29 deletions src/instance_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,41 @@ export default class InstanceProcessor {
}
}

async processInstanceServiceEvents(instanceEvents: Array<DecodedLogEntry>): Promise<Array<Instance>> {
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<BigInt, Instance>): Promise<Map<BigInt, Instance>> {
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;
}


Expand Down
28 changes: 7 additions & 21 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,19 @@ class Main {
this.dune = new DuneApi();
this.nftProcessor = new NftProcessor(prisma);
this.instanceProcessor = new InstanceProcessor(prisma);

}

public async main(): Promise<void> {
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<DecodedLogEntry>)
Expand All @@ -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
Expand Down

0 comments on commit 762c38d

Please sign in to comment.