Skip to content

Commit

Permalink
Fix: Get the correct most recent extension installed/uninstalled log
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubcolony authored and rdig committed Jun 5, 2023
1 parent 7ae51e5 commit c43ecc3
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/trackExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Extension, getExtensionHash, getLogs } from '@colony/colony-js';
import { BigNumber } from 'ethers';
import { Log } from '@ethersproject/providers';

import networkClient from '~networkClient';
import {
Expand Down Expand Up @@ -112,8 +113,10 @@ const trackExtensionEvents = async (
* If installation count is 0, that means the extension has been uninstalled
*/
if (installationsCount <= 0) {
const mostRecentUninstalledLog =
extensionUninstalledLogs[extensionUninstalledLogs.length - 1];
const mostRecentUninstalledLog = getMostRecentLog(
extensionUninstalledLogs,
colony,
);
/**
* Short circuit if there's no log or it happened before the latest processed block
* (meaning it's already reflected in the db)
Expand Down Expand Up @@ -148,13 +151,18 @@ const trackExtensionEvents = async (
await extensionSpecificEventsListener(extensionAddress, extensionHash);

// Store the most recent installation in the db
const mostRecentInstalledLog =
extensionInstalledLogs[extensionInstalledLogs.length - 1];
const mostRecentInstalledLog = getMostRecentLog(
extensionInstalledLogs,
colony,
);
if (!mostRecentInstalledLog) {
return;
}

const event = await mapLogToContractEvent(
mostRecentInstalledLog,
networkClient.interface,
);

if (!event) {
return;
}
Expand Down Expand Up @@ -194,3 +202,15 @@ const trackExtensionEvents = async (
);
}
};

// Util returning the most recent extension installed/uninstalled log for a given colony address
const getMostRecentLog = (
logs: Log[],
colonyAddress: string,
): Log | undefined => {
const colonyFilteredLogs = logs.filter(
(log) =>
networkClient.interface.parseLog(log).args.colony === colonyAddress,
);
return colonyFilteredLogs[colonyFilteredLogs.length - 1];
};

0 comments on commit c43ecc3

Please sign in to comment.