Skip to content

Commit

Permalink
fix: l2-bridge-base added test for getWithdrawalEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyWh1te committed May 29, 2024
1 parent b15400c commit 0aca5a2
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 57 deletions.
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
24 changes: 1 addition & 23 deletions l2-bridge-base/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,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
93 changes: 70 additions & 23 deletions l2-bridge-base/src/clients/base_client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,81 @@
import { App } from '../app'
import * as E from 'fp-ts/Either'
import { ETH_DECIMALS } from '../utils/constants'
import { Address, ETH_DECIMALS } from '../utils/constants'
import BigNumber from 'bignumber.js'
import { AVG_BLOCK_TIME_2SECONDS, HOURS_12 } from '../services/monitor_withdrawals'
import { App } from '../app'
import * as Winston from 'winston'
import { ethers } from 'forta-agent'
import { ERC20Short__factory, L2Bridge__factory } from '../generated'
import { BaseClient } from './base_client'

const timeout = 120_000

describe('base provider tests', () => {
test('should fetch block logs', async () => {
const app = await App.getInstance()
const app = App.getInstance()

test(
'should fetch block logs',
async () => {
const latestBlock = await app.baseClient.getLatestL2Block()
if (E.isLeft(latestBlock)) {
throw latestBlock
}

const blocksDto = await app.baseClient.getL2Logs(latestBlock.right.number, latestBlock.right.number)
if (E.isLeft(blocksDto)) {
throw blocksDto
}

expect(blocksDto.right.length).toBeGreaterThan(1)
},
timeout,
)

test(
'getWithdrawalEvents fetches 21_601 blocks for getting withdrawal events',
async () => {
const currentBlock = 15_091_860

const pastBlock = currentBlock - Math.ceil(HOURS_12 / AVG_BLOCK_TIME_2SECONDS)

expect(21_601).toEqual(currentBlock - pastBlock + 1)

const withdrawalEvents = await app.baseClient.getWithdrawalEvents(pastBlock, currentBlock - 1)
if (E.isLeft(withdrawalEvents)) {
throw withdrawalEvents
}

expect(withdrawalEvents.right.length).toEqual(0)
},
timeout,
)

const latestBlock = await app.baseClient.getLatestL2Block()
if (E.isLeft(latestBlock)) {
throw latestBlock
}
test(
'getWstEthTotalSupply is 16388.426826708573275643 wsETH',
async () => {
const logger: Winston.Logger = Winston.createLogger({
format: Winston.format.simple(),
transports: [new Winston.transports.Console()],
})

const blocksDto = await app.baseClient.getL2Logs(latestBlock.right.number, latestBlock.right.number)
if (E.isLeft(blocksDto)) {
throw blocksDto
}
const baseNetworkID = 8453
let defaultL2RPCURL = 'https://base.drpc.org'

expect(blocksDto.right.length).toBeGreaterThan(1)
}, 120_000)
const baseProvider = new ethers.providers.JsonRpcProvider(defaultL2RPCURL, baseNetworkID)
const adr: Address = Address

test('getWstEthTotalSupply is 1696.070092078019991932 wsETH', async () => {
const app = await App.getInstance()
const l2Bridge = L2Bridge__factory.connect(adr.BASE_L2ERC20_TOKEN_BRIDGE_ADDRESS, baseProvider)
const bridgedWSthEthRunner = ERC20Short__factory.connect(adr.BASE_WSTETH_ADDRESS, baseProvider)
const baseClient = new BaseClient(baseProvider, l2Bridge, logger, bridgedWSthEthRunner)

const baseBlockNumber = 13_022_744
const balance = await app.baseClient.getWstEthTotalSupply(baseBlockNumber)
if (E.isLeft(balance)) {
throw balance.left
}
const baseBlockNumber = 15_091_860
const balance = await baseClient.getWstEthTotalSupply(baseBlockNumber)
if (E.isLeft(balance)) {
throw balance.left
}

expect(balance.right.dividedBy(ETH_DECIMALS)).toEqual(new BigNumber('11430.956916416032084584'))
}, 120_000)
expect(balance.right.dividedBy(ETH_DECIMALS)).toEqual(new BigNumber('16388.426826708573275643'))
},
timeout,
)
})
2 changes: 1 addition & 1 deletion l2-bridge-base/src/clients/base_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class BaseClient implements IBaseClient, IMonitorWithdrawalsClient, IL2Br
batchRequests.push(i)
}

const chunkSize = 125
const chunkSize = 10_000
const out: WithdrawalInitiatedEvent[] = []

const batchPromises: Promise<WithdrawalInitiatedEvent[]>[] = []
Expand Down
2 changes: 1 addition & 1 deletion l2-bridge-base/src/clients/eth_provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import BigNumber from 'bignumber.js'

describe('eth provider tests', () => {
test('getBalanceByBlockHash is 10786.9163900726000737487 wsETH', async () => {
const app = await App.getInstance()
const app = App.getInstance()
const adr = Address

const blockNumber = 19_619_102
Expand Down
4 changes: 2 additions & 2 deletions l2-bridge-base/src/services/monitor_withdrawals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { dateTimeFormat, elapsedTime } from '../utils/time'
import { getUniqueKey } from '../utils/finding.helpers'

// 12 hours
const HOURS_12 = 60 * 60 * 12
const AVG_BLOCK_TIME_2SECONDS: number = 2 //s
export const HOURS_12 = 60 * 60 * 12
export const AVG_BLOCK_TIME_2SECONDS: number = 2 //s
const ETH_DECIMALS = new BigNumber(10).pow(18)
// 10k wstETH
const MAX_WITHDRAWALS_10K_WstEth = 10_000
Expand Down

0 comments on commit 0aca5a2

Please sign in to comment.