Skip to content
Merged
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
8 changes: 8 additions & 0 deletions contracts/BlockInfo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract BlockInfo {
function blockInfo() external view returns (uint256 timestamp, uint256 blockNumber) {
return (block.timestamp, block.number);
}
}
1 change: 1 addition & 0 deletions src/all-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ await import('./gas.test.ts')
await import('./others.test.ts')
await import('./tracing-call-trace.test.ts')
await import('./tracing-prestate.test.ts')
await import('./block-info.test.ts')

globalThis.addEventListener('unload', () => {
cleanupTests()
Expand Down
71 changes: 71 additions & 0 deletions src/block-info.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { CallReturnType, decodeFunctionResult, encodeFunctionData } from 'viem'
import {
getByteCode,
getEnv,
memoizedTx,
sanitizeOpts as opts,
} from './util.ts'
import { expect } from '@std/expect'
import { BlockInfoAbi } from '../codegen/abi/BlockInfo.ts'

// Initialize test environment
const env = await getEnv()

const getBlockInfoReceipt = memoizedTx(
env,
() =>
env.serverWallet.deployContract({
abi: BlockInfoAbi,
bytecode: getByteCode('BlockInfo', env.evm),
}),
)

const getBlockInfoAddr = () =>
getBlockInfoReceipt().then((r) => r.contractAddress!)

Deno.test('eth_call with latest block tag', opts, async () => {
const address = await getBlockInfoAddr()

const latestBlock = await env.serverWallet.getBlock({ blockTag: 'latest' })

const callReturn: CallReturnType = await env.serverWallet.call({
to: address,
data: encodeFunctionData({
abi: BlockInfoAbi,
functionName: 'blockInfo',
}),
blockTag: 'latest',
})

const [blockTimestamp, blockNumber] = decodeFunctionResult({
abi: BlockInfoAbi,
functionName: 'blockInfo',
data: callReturn?.data!,
})
expect(blockNumber).toEqual(latestBlock.number)
expect(blockTimestamp).toEqual(latestBlock.timestamp)
})

Deno.test('eth_call with pending block tag', opts, async () => {
const address = await getBlockInfoAddr()

const latestBlock = await env.serverWallet.getBlock({ blockTag: 'latest' })

const callReturn: CallReturnType = await env.serverWallet.call({
to: address,
data: encodeFunctionData({
abi: BlockInfoAbi,
functionName: 'blockInfo',
}),
blockTag: 'pending',
})

const [blockTimestamp, blockNumber] = decodeFunctionResult({
abi: BlockInfoAbi,
functionName: 'blockInfo',
data: callReturn?.data!,
})

expect(blockNumber).toEqual(latestBlock.number + 1n)
expect(blockTimestamp).toBeGreaterThan(Number(latestBlock.timestamp))
})