diff --git a/sdk/index.ts b/sdk/index.ts index ffc6ad1..f68fc4d 100644 --- a/sdk/index.ts +++ b/sdk/index.ts @@ -28,6 +28,7 @@ import { getChainId, getBotOwner, getBotId, + filterLog, } from "./utils"; import { GetAlerts, @@ -133,6 +134,7 @@ export { getChainId, getBotOwner, getBotId, + filterLog, BloomFilter, MOCK_JWT, }; diff --git a/sdk/transaction.event.ts b/sdk/transaction.event.ts index 0d8647a..6523bcd 100644 --- a/sdk/transaction.event.ts +++ b/sdk/transaction.event.ts @@ -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; @@ -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( diff --git a/sdk/utils.ts b/sdk/utils.ts index 399958b..7f7864c 100644 --- a/sdk/utils.ts +++ b/sdk/utils.ts @@ -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"; @@ -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; +};