Skip to content

Commit

Permalink
expose filterLog as utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebrabbani committed Dec 11, 2023
1 parent 643126e commit f16191b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
2 changes: 2 additions & 0 deletions sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
getChainId,
getBotOwner,
getBotId,
filterLog,
} from "./utils";
import {
GetAlerts,
Expand Down Expand Up @@ -133,6 +134,7 @@ export {
getChainId,
getBotOwner,
getBotId,
filterLog,
BloomFilter,
MOCK_JWT,
};
32 changes: 2 additions & 30 deletions sdk/transaction.event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Trace } from "./trace";
import { Transaction } from "./transaction";
import { ethers } from "ethers";
import _ from "lodash";
import { filterLog } from "./utils";

export interface TxEventBlock {
hash: string;
Expand Down Expand Up @@ -66,36 +67,7 @@ export class TransactionEvent {
eventAbi: string | string[],
contractAddress?: string | string[]
): LogDescription[] {
eventAbi = _.isArray(eventAbi) ? eventAbi : [eventAbi];
let logs = this.logs;
// filter logs by contract address, if provided
if (contractAddress) {
contractAddress = _.isArray(contractAddress)
? contractAddress
: [contractAddress];
const contractAddressMap: { [address: string]: boolean } = {};
contractAddress.forEach((address) => {
contractAddressMap[address.toLowerCase()] = true;
});
logs = logs.filter(
(log) => contractAddressMap[log.address.toLowerCase()]
);
}
// parse logs
const results: LogDescription[] = [];
const iface = new ethers.utils.Interface(eventAbi);
for (const log of logs) {
try {
const parsedLog = iface.parseLog(log);
results.push(
Object.assign(parsedLog, {
address: log.address,
logIndex: log.logIndex,
})
);
} catch (e) {} // TODO see if theres a better way to handle 'no matching event' error
}
return results;
return filterLog(this.logs, eventAbi, contractAddress)
}

filterFunction(
Expand Down
36 changes: 35 additions & 1 deletion sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Alert } from "./alert";
import { AlertEvent } from "./alert.event";
import { BlockEvent } from "./block.event";
import { Trace } from "./trace";
import { TransactionEvent } from "./transaction.event";
import { LogDescription, TransactionEvent } from "./transaction.event";
import { Transaction } from "./transaction";
import { Log, Receipt } from "./receipt";
import { TxEventBlock } from "./transaction.event";
Expand Down Expand Up @@ -257,3 +257,37 @@ export const getFortaApiHeaders = () => {
headers,
};
};

export const filterLog = (
logs: Log[],
eventAbi: string | string[],
contractAddress?: string | string[]
): LogDescription[] => {
eventAbi = _.isArray(eventAbi) ? eventAbi : [eventAbi];
// filter logs by contract address, if provided
if (contractAddress) {
contractAddress = _.isArray(contractAddress)
? contractAddress
: [contractAddress];
const contractAddressMap: { [address: string]: boolean } = {};
contractAddress.forEach((address) => {
contractAddressMap[address.toLowerCase()] = true;
});
logs = logs.filter((log) => contractAddressMap[log.address.toLowerCase()]);
}
// parse logs
const results: LogDescription[] = [];
const iface = new ethers.utils.Interface(eventAbi);
for (const log of logs) {
try {
const parsedLog = iface.parseLog(log);
results.push(
Object.assign(parsedLog, {
address: log.address,
logIndex: log.logIndex,
})
);
} catch (e) {} // TODO see if theres a better way to handle 'no matching event' error
}
return results;
};

0 comments on commit f16191b

Please sign in to comment.