Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes and refactoring across multiple bots #551

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('Steth.srv functional tests', () => {
const drpcURL = `https://eth.drpc.org`
const mainnet = 1
const ethProvider = new ethers.providers.JsonRpcProvider(drpcURL, mainnet)
const quickNode = 'https://docs-demo.quiknode.pro'

test(
'LOW-STAKING-LIMIT',
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('Steth.srv functional tests', () => {
async () => {
const app = await App.getInstance()
const txHash = '0x11a48020ae69cf08bd063f1fbc8ecf65bd057015aaa991bf507dbc598aadb68e'

const ethProvider = new ethers.providers.JsonRpcProvider(quickNode, mainnet)
const trx = await ethProvider.getTransaction(txHash)
const receipt = await trx.wait()

Expand Down Expand Up @@ -139,6 +140,7 @@ describe('Steth.srv functional tests', () => {
'Insurance fund',
async () => {
const app = await App.getInstance()
const ethProvider = new ethers.providers.JsonRpcProvider(quickNode, mainnet)
const txHash = '0x91c7c2f33faf3b5fb097138c1d49c1d4e83f99e1c3b346b3cad35a5928c03b3a'

const trx = await ethProvider.getTransaction(txHash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,10 @@ describe('StethOperationSrv', () => {

const expectedShareRateErrFinding = {
alertId: 'LIDO-INVARIANT-ERROR',
description: `Prev.shareRate(19061448) = 1.1549004556051977e+27
Curr.shareRate(19061449) = 1.3098009112103954e+27
Diff: 1.5490045560519778e+26`,
description:
`Prev.shareRate(19061448) = 1.1549004556051977e+27 \n` +
`Curr.shareRate(19061449) = 1.3098009112103954e+27 \n` +
`Diff: 1.5490045560519778e+26`,
name: '🚨🚨🚨 Share rate unexpected has changed',
severity: Finding.Severity.CRITICAL,
type: Finding.FindingType.SUSPICIOUS,
Expand Down Expand Up @@ -936,9 +937,10 @@ Diff: 1.5490045560519778e+26`,

const expectedShareRateErrFinding = {
alertId: 'LIDO-INVARIANT-ERROR',
description: `Prev.shareRate(19061448) = 1.1549004556051977e+27
Curr.shareRate(19061449) = 1e+27
Diff: -1.5490045560519778e+26`,
description:
`Prev.shareRate(19061448) = 1.1549004556051977e+27 \n` +
`Curr.shareRate(19061449) = 1e+27 \n` +
`Diff: -1.5490045560519778e+26`,
name: '🚨🚨🚨 Share rate unexpected has changed',
severity: Finding.Severity.CRITICAL,
type: Finding.FindingType.SUSPICIOUS,
Expand Down
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
22 changes: 15 additions & 7 deletions l2-bridge-base/src/agent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { App } from './app'
import * as E from 'fp-ts/Either'
import { Finding } from 'forta-agent'
import { ethers } from 'ethers'
import BigNumber from 'bignumber.js'

describe('agent-base functional test', () => {
test('should process app on 13_022_644 and 13_022_720 (25 l2 blocks)', async () => {
const app = await App.getInstance()
test('should process app on latest 25 l2 blocks', async () => {
const l1rpcURL = 'https://eth.drpc.org'
const app = App.getInstance()

const l1Block = 19_632_237
const l2StartBlock = 13_022_720
const l2EndBlock = 13_022_745
const l2BlocksDto = await app.baseClient.fetchL2Blocks(l2StartBlock, l2EndBlock)
const ehtProvider = new ethers.providers.JsonRpcProvider(l1rpcURL)
const l1Block = await ehtProvider.getBlockNumber()

const l2EndBlock = await app.baseClient.getLatestL2Block()
if (E.isLeft(l2EndBlock)) {
throw l2EndBlock
}

const l2StartBlock = l2EndBlock.right.number - 25
const l2BlocksDto = await app.baseClient.fetchL2Blocks(l2StartBlock, l2EndBlock.right.number)

for (const proxyWatcher of app.proxyWatchers) {
const err = await proxyWatcher.initialize(l2StartBlock)
if (err !== null) {
throw null
throw err
}
}

Expand Down
13 changes: 3 additions & 10 deletions l2-bridge-base/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ export function initialize(): Initialize {
}

return async function (): Promise<InitializeResponse | void> {
const app = await App.getInstance()

const token = await App.getJwt()
if (E.isLeft(token)) {
app.logger.error(token.left)

process.exit(1)
}
const app = App.getInstance()

const latestL2Block = await app.baseClient.getLatestL2Block()
if (E.isLeft(latestL2Block)) {
Expand Down Expand Up @@ -84,7 +77,7 @@ export const handleBlock = (): HandleBlock => {
}

isHandleBLockRunning = true
const app = await App.getInstance()
const app = App.getInstance()

const findings: Finding[] = []
const findingsAsync = await app.findingsRW.read()
Expand Down Expand Up @@ -148,7 +141,7 @@ export const handleBlock = (): HandleBlock => {

export const healthCheck = (): HealthCheck => {
return async function (): Promise<string[] | void> {
const app = await App.getInstance()
const app = App.getInstance()

if (!app.healthChecker.isHealth()) {
return ['There is too much network errors']
Expand Down
29 changes: 3 additions & 26 deletions l2-bridge-base/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers, fetchJwt, getJsonRpcUrl } from 'forta-agent'
import { getJsonRpcUrl } from 'forta-agent'
import { ethers } from 'ethers'
import { BaseClient } from './clients/base_client'
import { EventWatcher } from './services/event_watcher'
import { getL2BridgeEvents } from './utils/events/bridge_events'
Expand All @@ -14,8 +15,6 @@ import { FindingsRW } from './utils/mutex'
import * as Winston from 'winston'
import { Logger } from 'winston'
import { BorderTime, HealthChecker, MaxNumberErrorsPerBorderTime } from './services/health-checker/health-checker.srv'
import { verifyJwt } from 'forta-agent/dist/sdk/jwt'
import * as E from 'fp-ts/Either'
import { BridgeBalanceSrv } from './services/bridge_balance'
import { ETHProvider } from './clients/eth_provider_client'

Expand All @@ -39,29 +38,7 @@ export class App {

private constructor() {}

public static async getJwt(): Promise<E.Either<Error, string>> {
let token: string
try {
token = await fetchJwt({})
} catch (e) {
return E.left(new Error(`Could not fetch jwt. Cause ${e}`))
}

if (process.env.NODE_ENV === 'production') {
try {
const isTokenOk = await verifyJwt(token)
if (!isTokenOk) {
return E.left(new Error(`Token verification failed`))
}
} catch (e) {
return E.left(new Error(`Token verification failed`))
}
}

return E.right(token)
}

public static async getInstance(): Promise<Container> {
public static getInstance(): Container {
if (!App.instance) {
const logger: Winston.Logger = Winston.createLogger({
format: Winston.format.simple(),
Expand Down
Loading
Loading