Skip to content

Commit

Permalink
fix(multiple bots): unify address format in event filtering
Browse files Browse the repository at this point in the history
to prevent possible incorrect logic if a hardcoded address is specified
in checksum format
  • Loading branch information
arwer13 committed May 28, 2024
1 parent f6c6467 commit b15400c
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 67 deletions.
12 changes: 3 additions & 9 deletions l2-bridge-arbitrum/src/agent-bridge-watcher.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
ethers,
BlockEvent,
TransactionEvent,
Finding,
FindingType,
FindingSeverity,
} from "forta-agent";
import { TransactionEvent, Finding } from "forta-agent";
import { formatAddress } from "forta-agent/dist/cli/utils";
import { L2_BRIDGE_EVENTS } from "./constants";

export const name = "BridgeWatcher";
Expand All @@ -27,7 +21,7 @@ export async function handleTransaction(txEvent: TransactionEvent) {

function handleL2BridgeEvents(txEvent: TransactionEvent, findings: Finding[]) {
L2_BRIDGE_EVENTS.forEach((eventInfo) => {
if (eventInfo.address in txEvent.addresses) {
if (formatAddress(eventInfo.address) in txEvent.addresses) {
const events = txEvent.filterLog(eventInfo.event, eventInfo.address);
events.forEach((event) => {
findings.push(
Expand Down
12 changes: 3 additions & 9 deletions l2-bridge-arbitrum/src/agent-governance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
ethers,
BlockEvent,
TransactionEvent,
Finding,
FindingType,
FindingSeverity,
} from "forta-agent";
import { formatAddress } from "forta-agent/dist/cli/utils";
import { TransactionEvent, Finding } from "forta-agent";
import { GOV_BRIDGE_EVENTS } from "./constants";

export const name = "GovBridgeBot";
Expand All @@ -27,7 +21,7 @@ export async function handleTransaction(txEvent: TransactionEvent) {

function handleGovBridgeEvents(txEvent: TransactionEvent, findings: Finding[]) {
GOV_BRIDGE_EVENTS.forEach((eventInfo) => {
if (eventInfo.address in txEvent.addresses) {
if (formatAddress(eventInfo.address) in txEvent.addresses) {
const events = txEvent.filterLog(eventInfo.event, eventInfo.address);
events.forEach((event) => {
findings.push(
Expand Down
4 changes: 2 additions & 2 deletions l2-bridge-arbitrum/src/agent-proxy-watcher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { formatAddress } from "forta-agent/dist/cli/utils";
import {
ethers,
BlockEvent,
Expand All @@ -11,7 +12,6 @@ import {
LIDO_PROXY_CONTRACTS,
LidoProxy,
} from "./constants";
import { ethersProvider } from "./ethers";

// Block interval tp fetch proxy params
const BLOCK_INTERVAL = 10;
Expand Down Expand Up @@ -52,7 +52,7 @@ function handleProxyAdminEvents(
findings: Finding[],
) {
PROXY_ADMIN_EVENTS.forEach((eventInfo) => {
if (eventInfo.address in txEvent.addresses) {
if (formatAddress(eventInfo.address) in txEvent.addresses) {
const events = txEvent.filterLog(eventInfo.event, eventInfo.address);
events.forEach((event) => {
findings.push(
Expand Down
5 changes: 2 additions & 3 deletions l2-bridge-arbitrum/src/agent-withdrawals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from "bignumber.js";

import { formatAddress } from "forta-agent/dist/cli/utils";
import {
ethers,
BlockEvent,
Expand All @@ -10,7 +10,6 @@ import {
} from "forta-agent";

import { Event } from "ethers";
import { ethersProvider } from "./ethers";

import L2_BRIDGE_ABI from "./abi/L2Bridge.json";
import {
Expand Down Expand Up @@ -159,7 +158,7 @@ export async function handleTransaction(txEvent: TransactionEvent) {
}

function handleWithdrawalEvent(txEvent: TransactionEvent, findings: Finding[]) {
if (L2_ERC20_TOKEN_GATEWAY in txEvent.addresses) {
if (formatAddress(L2_ERC20_TOKEN_GATEWAY) in txEvent.addresses) {
const events = txEvent.filterLog(
WITHDRAWAL_INITIATED_EVENT,
L2_ERC20_TOKEN_GATEWAY,
Expand Down
9 changes: 6 additions & 3 deletions l2-bridge-balance/src/agent-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
BridgeParamLDO,
BridgeParamWstETH,
ETH_DECIMALS,
LDO_ADDRESS,
LDO_L1_ADDRESS,
WSTETH_ADDRESS,
} from "./constants";
import ERC20_SHORT_ABI from "./abi/ERC20Short.json";
Expand Down Expand Up @@ -41,7 +41,6 @@ export async function handleBlock(blockEvent: BlockEvent) {
findings,
BRIDGE_PARAMS_WSTETH.Optimism,
),

handleBridgeBalanceLDO(blockEvent, findings, BRIDGE_PARAMS_LDO.Arbitrum),
handleBridgeBalanceLDO(blockEvent, findings, BRIDGE_PARAMS_LDO.Optimism),
]);
Expand Down Expand Up @@ -101,7 +100,11 @@ async function handleBridgeBalanceLDO(
findings: Finding[],
networkParams: BridgeParamLDO,
) {
const LDO = new ethers.Contract(LDO_ADDRESS, ERC20_SHORT_ABI, ethersProvider);
const LDO = new ethers.Contract(
LDO_L1_ADDRESS,
ERC20_SHORT_ABI,
ethersProvider,
);
const l1Balance = new BigNumber(
String(await LDO.functions.balanceOf(networkParams.l1Gateway)),
);
Expand Down
4 changes: 1 addition & 3 deletions l2-bridge-balance/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import BigNumber from "bignumber.js";
import config from "./config/bot-config.json";

// COMMON CONSTS

// 1 ETH
export const ETH_DECIMALS = new BigNumber(10).pow(18);

Expand Down Expand Up @@ -37,7 +35,7 @@ export const BRIDGE_PARAMS_WSTETH: BridgeParamsWstETH = {
},
};

export const LDO_ADDRESS = "0x5a98fcbea516cf06857215779fd812ca3bef1b32";
export const LDO_L1_ADDRESS = "0x5a98fcbea516cf06857215779fd812ca3bef1b32";

export interface BridgeParamLDO {
name: string;
Expand Down
5 changes: 3 additions & 2 deletions l2-bridge-base/src/services/event_watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { filterLog, Finding } from 'forta-agent'
import { Logger } from 'winston'
import { elapsedTime } from '../utils/time'
import { getUniqueKey } from '../utils/finding.helpers'
import { formatAddress } from 'forta-agent/dist/cli/utils'

export class EventWatcher {
private readonly name: string
Expand All @@ -25,12 +26,12 @@ export class EventWatcher {
const addresses: string[] = []

for (const l2log of l2logs) {
addresses.push(l2log.address)
addresses.push(formatAddress(l2log.address))
}

const findings: Finding[] = []
for (const eventToFinding of this.eventsToFinding) {
const ind = addresses.indexOf(eventToFinding.address)
const ind = addresses.indexOf(formatAddress(eventToFinding.address))
if (ind >= 0) {
const filteredEvents = filterLog(l2logs, eventToFinding.event, eventToFinding.address)

Expand Down
3 changes: 2 additions & 1 deletion l2-bridge-ethereum/src/agent-bridge-watcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Finding, TransactionEvent } from "forta-agent";
import { formatAddress } from "forta-agent/dist/cli/utils";
import { L1_BRIDGE_EVENTS } from "./constants";

export const name = "BridgeWatcher";
Expand All @@ -20,7 +21,7 @@ export async function handleTransaction(txEvent: TransactionEvent) {

function handleL1BridgeEvents(txEvent: TransactionEvent, findings: Finding[]) {
L1_BRIDGE_EVENTS.forEach((eventInfo) => {
if (eventInfo.address in txEvent.addresses) {
if (formatAddress(eventInfo.address) in txEvent.addresses) {
const events = txEvent.filterLog(eventInfo.event, eventInfo.address);
events.forEach((event) => {
findings.push(
Expand Down
9 changes: 5 additions & 4 deletions l2-bridge-ethereum/src/agent-proxy-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FindingType,
TransactionEvent,
} from "forta-agent";
import { formatAddress } from "forta-agent/dist/cli/utils";
import {
ARBITRUM_GATEWAY_SET_EVENT,
ARBITRUM_L1_GATEWAY_ROUTER,
Expand Down Expand Up @@ -57,7 +58,7 @@ function handleProxyAdminEvents(
findings: Finding[],
) {
PROXY_ADMIN_EVENTS.forEach((eventInfo) => {
if (eventInfo.address in txEvent.addresses) {
if (formatAddress(eventInfo.address) in txEvent.addresses) {
const events = txEvent.filterLog(eventInfo.event, eventInfo.address);
events.forEach((event) => {
findings.push(
Expand All @@ -80,7 +81,7 @@ function handleThirdPartyProxyAdminEvents(
findings: Finding[],
) {
THIRD_PARTY_PROXY_EVENTS.forEach((eventInfo) => {
if (eventInfo.address in txEvent.addresses) {
if (formatAddress(eventInfo.address) in txEvent.addresses) {
const events = txEvent.filterLog(eventInfo.event, eventInfo.address);
events.forEach((event) => {
findings.push(
Expand All @@ -96,7 +97,7 @@ function handleThirdPartyProxyAdminEvents(
});
}
});
if (ARBITRUM_L1_GATEWAY_ROUTER in txEvent.addresses) {
if (formatAddress(ARBITRUM_L1_GATEWAY_ROUTER) in txEvent.addresses) {
const events = txEvent.filterLog(
ARBITRUM_GATEWAY_SET_EVENT,
ARBITRUM_L1_GATEWAY_ROUTER,
Expand All @@ -117,7 +118,7 @@ function handleThirdPartyProxyAdminEvents(
});
}

if (LINEA_L1_CROSS_DOMAIN_MESSENGER in txEvent.addresses) {
if (formatAddress(LINEA_L1_CROSS_DOMAIN_MESSENGER) in txEvent.addresses) {
const events = txEvent.filterLog(
LINEA_CUSTOM_CONTRACT_SET_EVENT,
LINEA_L1_CROSS_DOMAIN_MESSENGER,
Expand Down
4 changes: 2 additions & 2 deletions l2-bridge-ethereum/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const handleBlock: HandleBlock = async (
}),
);

runs.forEach((r: PromiseSettledResult<any>, index: number) => {
runs.forEach((r: PromiseSettledResult<unknown>, index: number) => {
if (r.status == "rejected") {
blockFindings.push(
errorToFinding(r.reason, subAgents[index], "handleBlock"),
Expand Down Expand Up @@ -199,7 +199,7 @@ const handleTransaction: HandleTransaction = async (
}),
);

runs.forEach((r: PromiseSettledResult<any>, index: number) => {
runs.forEach((r: PromiseSettledResult<unknown>, index: number) => {
if (r.status == "rejected") {
txFindings.push(
errorToFinding(r.reason, subAgents[index], "handleBlock"),
Expand Down
3 changes: 2 additions & 1 deletion l2-bridge-linea/src/services/event_watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { filterLog, Finding } from 'forta-agent'
import { Logger } from 'winston'
import { elapsedTime } from '../utils/time'
import { getUniqueKey } from '../utils/finding.helpers'
import { formatAddress } from 'forta-agent/dist/cli/utils'

export class EventWatcher {
private readonly name: string
Expand All @@ -30,7 +31,7 @@ export class EventWatcher {

const findings: Finding[] = []
for (const eventToFinding of this.eventsToFinding) {
const ind = addresses.indexOf(eventToFinding.address)
const ind = addresses.indexOf(formatAddress(eventToFinding.address))
if (ind >= 0) {
const filteredEvents = filterLog(logs, eventToFinding.event, eventToFinding.address)

Expand Down
3 changes: 2 additions & 1 deletion l2-bridge-mantle/src/services/event_watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { filterLog, Finding } from 'forta-agent'
import { getUniqueKey } from '../utils/finding.helpers'
import { elapsedTime } from '../utils/time'
import { Logger } from 'winston'
import { formatAddress } from 'forta-agent/dist/cli/utils'

export class EventWatcher {
private readonly name: string
Expand All @@ -30,7 +31,7 @@ export class EventWatcher {

const findings: Finding[] = []
for (const eventToFinding of this.eventsToFinding) {
const ind = addresses.indexOf(eventToFinding.address)
const ind = addresses.indexOf(formatAddress(eventToFinding.address))
if (ind >= 0) {
const filteredEvents = filterLog(l2logs, eventToFinding.event, eventToFinding.address)

Expand Down
7 changes: 4 additions & 3 deletions l2-bridge-mantle/src/services/monitor_withdrawals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NetworkError } from '../utils/error'
import { elapsedTime } from '../utils/time'
import { Logger } from 'winston'
import { getUniqueKey } from '../utils/finding.helpers'
import { formatAddress } from 'forta-agent/dist/cli/utils'

const ETH_DECIMALS = new BigNumber(10).pow(18)
// 10k wstETH
Expand Down Expand Up @@ -42,7 +43,7 @@ export class MonitorWithdrawals {
}

public async initialize(currentBlock: number): Promise<E.Either<NetworkError, MonitorWithdrawalsInitResp>> {
// 48 hours
// TODO: Fix actually on Mantle block time is not constant and the check logic must be updated
const pastBlock = currentBlock - Math.ceil(HOURS_48 / AVG_BLOCK_TIME_2SECONDS)

const withdrawalEvents = await this.withdrawalsClient.getWithdrawalEvents(pastBlock, currentBlock - 1)
Expand Down Expand Up @@ -145,7 +146,7 @@ export class MonitorWithdrawals {

for (const log of logs) {
logIndexToLogs.set(log.logIndex, log)
addresses.push(log.address.toLowerCase())
addresses.push(formatAddress(log.address))
}

for (const blockDto of blocksDto) {
Expand All @@ -154,7 +155,7 @@ export class MonitorWithdrawals {

const out: WithdrawalRecord[] = []
if (this.l2Erc20TokenGatewayAddress in addresses) {
const events = filterLog(logs, this.withdrawalInitiatedEvent, this.l2Erc20TokenGatewayAddress.toLowerCase())
const events = filterLog(logs, this.withdrawalInitiatedEvent, formatAddress(this.l2Erc20TokenGatewayAddress))

for (const event of events) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
12 changes: 3 additions & 9 deletions l2-bridge-optimism/src/agent-bridge-watcher.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
ethers,
BlockEvent,
TransactionEvent,
Finding,
FindingType,
FindingSeverity,
} from "forta-agent";
import { TransactionEvent, Finding } from "forta-agent";
import { formatAddress } from "forta-agent/dist/cli/utils";
import { L2_BRIDGE_EVENTS } from "./constants";

export const name = "BridgeWatcher";
Expand All @@ -27,7 +21,7 @@ export async function handleTransaction(txEvent: TransactionEvent) {

function handleL2BridgeEvents(txEvent: TransactionEvent, findings: Finding[]) {
L2_BRIDGE_EVENTS.forEach((eventInfo) => {
if (eventInfo.address in txEvent.addresses) {
if (formatAddress(eventInfo.address) in txEvent.addresses) {
const events = txEvent.filterLog(eventInfo.event, eventInfo.address);
events.forEach((event) => {
findings.push(
Expand Down
12 changes: 3 additions & 9 deletions l2-bridge-optimism/src/agent-governance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
ethers,
BlockEvent,
TransactionEvent,
Finding,
FindingType,
FindingSeverity,
} from "forta-agent";
import { TransactionEvent, Finding } from "forta-agent";
import { formatAddress } from "forta-agent/dist/cli/utils";
import { GOV_BRIDGE_EVENTS } from "./constants";

export const name = "GovBridgeBot";
Expand All @@ -27,7 +21,7 @@ export async function handleTransaction(txEvent: TransactionEvent) {

function handleGovBridgeEvents(txEvent: TransactionEvent, findings: Finding[]) {
GOV_BRIDGE_EVENTS.forEach((eventInfo) => {
if (eventInfo.address in txEvent.addresses) {
if (formatAddress(eventInfo.address) in txEvent.addresses) {
const events = txEvent.filterLog(eventInfo.event, eventInfo.address);
events.forEach((event) => {
findings.push(
Expand Down
3 changes: 2 additions & 1 deletion l2-bridge-optimism/src/agent-proxy-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FindingType,
FindingSeverity,
} from "forta-agent";
import { formatAddress } from "forta-agent/dist/cli/utils";
import {
PROXY_ADMIN_EVENTS,
LIDO_PROXY_CONTRACTS,
Expand Down Expand Up @@ -52,7 +53,7 @@ function handleProxyAdminEvents(
findings: Finding[],
) {
PROXY_ADMIN_EVENTS.forEach((eventInfo) => {
if (eventInfo.address in txEvent.addresses) {
if (formatAddress(eventInfo.address) in txEvent.addresses) {
const events = txEvent.filterLog(eventInfo.event, eventInfo.address);
events.forEach((event) => {
findings.push(
Expand Down
4 changes: 2 additions & 2 deletions l2-bridge-optimism/src/agent-withdrawals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from "bignumber.js";

import { formatAddress } from "forta-agent/dist/cli/utils";
import {
ethers,
BlockEvent,
Expand Down Expand Up @@ -131,7 +131,7 @@ export async function handleTransaction(txEvent: TransactionEvent) {
}

function handleWithdrawalEvent(txEvent: TransactionEvent, findings: Finding[]) {
if (L2_ERC20_TOKEN_GATEWAY in txEvent.addresses) {
if (formatAddress(L2_ERC20_TOKEN_GATEWAY) in txEvent.addresses) {
const events = txEvent.filterLog(
WITHDRAWAL_INITIATED_EVENT,
L2_ERC20_TOKEN_GATEWAY,
Expand Down
Loading

0 comments on commit b15400c

Please sign in to comment.