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 f57733e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
88 changes: 65 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,76 @@
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 { ethers } from 'ethers'
import { ERC20Short__factory, L2Bridge__factory } from '../generated'
import { BaseClient } from './base_client'
import { AVG_BLOCK_TIME_2SECONDS, HOURS_12 } from '../services/monitor_withdrawals'
import * as Winston from 'winston'

const timeout = 120_000

describe('base provider tests', () => {
test('should fetch block logs', async () => {
const app = await App.getInstance()
const logger: Winston.Logger = Winston.createLogger({
format: Winston.format.simple(),
transports: [new Winston.transports.Console()],
})

const baseNetworkID = 8453
const baseProvider = new ethers.providers.JsonRpcProvider('https://base.llamarpc.com', baseNetworkID)
const adr: Address = Address

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)

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

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

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

test(
'getWstEthTotalSupply is 16388.426826708573275643 wsETH',
async () => {
const baseBlockNumber = 15_091_860
const balance = await baseClient.getWstEthTotalSupply(baseBlockNumber)
if (E.isLeft(balance)) {
throw balance.left
}

const latestBlock = await app.baseClient.getLatestL2Block()
if (E.isLeft(latestBlock)) {
throw latestBlock
}
expect(balance.right.dividedBy(ETH_DECIMALS)).toEqual(new BigNumber('16388.426826708573275643'))
},
timeout,
)

const blocksDto = await app.baseClient.getL2Logs(latestBlock.right.number, latestBlock.right.number)
if (E.isLeft(blocksDto)) {
throw blocksDto
}
test(
'getWithdrawalEvents fetches 21_601 blocks for getting withdrawal events',
async () => {
const currentBlock = 15_091_860

expect(blocksDto.right.length).toBeGreaterThan(1)
}, 120_000)
const pastBlock = currentBlock - Math.ceil(HOURS_12 / AVG_BLOCK_TIME_2SECONDS)

test('getWstEthTotalSupply is 1696.070092078019991932 wsETH', async () => {
const app = await App.getInstance()
expect(21_601).toEqual(currentBlock - pastBlock + 1)

const baseBlockNumber = 13_022_744
const balance = await app.baseClient.getWstEthTotalSupply(baseBlockNumber)
if (E.isLeft(balance)) {
throw balance.left
}
const withdrawalEvents = await baseClient.getWithdrawalEvents(pastBlock, currentBlock - 1)
if (E.isLeft(withdrawalEvents)) {
throw withdrawalEvents
}

expect(balance.right.dividedBy(ETH_DECIMALS)).toEqual(new BigNumber('11430.956916416032084584'))
}, 120_000)
expect(withdrawalEvents.right.length).toEqual(0)
},
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
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 f57733e

Please sign in to comment.