Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/big-humans-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

support block overrides on simulate/call
12 changes: 12 additions & 0 deletions src/actions/public/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '~contracts/generated.js'
import {
baycContractConfig,
multicall3ContractConfig,
usdcContractConfig,
wagmiContractConfig,
} from '~test/src/abis.js'
Expand Down Expand Up @@ -51,6 +52,7 @@ const wagmiContractAddress = '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2'
const name4bytes = '0x06fdde03'
const mint4bytes = '0x1249c58b'
const mintWithParams4bytes = '0xa0712d68'
const getCurrentBlockTimestamp4bytes = '0x0f28c97d'
const fourTwenty =
'00000000000000000000000000000000000000000000000000000000000001a4'
const sixHundred =
Expand Down Expand Up @@ -200,6 +202,16 @@ test('args: override', async () => {
)
})

test.skip('args: blockOverrides', async () => {
// TODO: don't skip once block overrides are supported in Anvil.
const { data } = await call(client, {
data: getCurrentBlockTimestamp4bytes,
code: multicall3ContractConfig.bytecode,
blockOverrides: { time: 420n },
})
expect(data).toMatchInlineSnapshot(fourTwenty)
})

test.skip('args: blobs', async () => {
// TODO: migrate to `client` once 4844 is supported in Anvil.
const blobs = toBlobs({ data: stringToHex(blobData) })
Expand Down
39 changes: 32 additions & 7 deletions src/actions/public/call.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Address, parseAbi } from 'abitype'
import * as BlockOverrides from 'ox/BlockOverrides'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice :D


import type { Account } from '../../accounts/types.js'
import {
Expand Down Expand Up @@ -91,6 +92,8 @@ export type CallParameters<
factoryData?: Hex | undefined
/** State overrides for the call. */
stateOverride?: StateOverride | undefined
/** Block overrides for the call. */
blockOverrides?: BlockOverrides.BlockOverrides | undefined
} & (
| {
/** The balance of the account at a block number. */
Expand Down Expand Up @@ -172,6 +175,7 @@ export async function call<chain extends Chain | undefined>(
to,
value,
stateOverride,
blockOverrides,
...rest
} = args
const account = account_ ? parseAccount(account_) : undefined
Expand Down Expand Up @@ -212,6 +216,8 @@ export async function call<chain extends Chain | undefined>(
const block = blockNumberHex || blockTag

const rpcStateOverride = serializeStateOverride(stateOverride)
const rpcBlockOverrides =
blockOverrides && BlockOverrides.toRpc(blockOverrides)

const chainFormat = client.chain?.formatters?.transactionRequest?.format
const format = chainFormat || formatTransactionRequest
Expand All @@ -233,7 +239,12 @@ export async function call<chain extends Chain | undefined>(
value,
} as TransactionRequest) as TransactionRequest

if (batch && shouldPerformMulticall({ request }) && !rpcStateOverride) {
if (
batch &&
shouldPerformMulticall({ request }) &&
!rpcStateOverride &&
!rpcBlockOverrides
) {
try {
return await scheduleMulticall(client, {
...request,
Expand All @@ -252,12 +263,26 @@ export async function call<chain extends Chain | undefined>(
const response = await client.request({
method: 'eth_call',
params: rpcStateOverride
? [
request as ExactPartial<RpcTransactionRequest>,
block,
rpcStateOverride,
]
: [request as ExactPartial<RpcTransactionRequest>, block],
? rpcBlockOverrides
? [
request as ExactPartial<RpcTransactionRequest>,
block,
rpcStateOverride,
rpcBlockOverrides,
]
: [
request as ExactPartial<RpcTransactionRequest>,
block,
rpcStateOverride,
]
: rpcBlockOverrides
? [
request as ExactPartial<RpcTransactionRequest>,
block,
{},
rpcBlockOverrides,
]
: [request as ExactPartial<RpcTransactionRequest>, block],
})
if (response === '0x') return { data: undefined }
return { data: response }
Expand Down
6 changes: 6 additions & 0 deletions src/types/eip1193.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,12 @@ export type PublicRpcSchema = [
block: BlockNumber | BlockTag | BlockIdentifier,
stateOverrideSet: RpcStateOverride,
]
| [
transaction: ExactPartial<TransactionRequest>,
block: BlockNumber | BlockTag | BlockIdentifier,
stateOverrideSet: RpcStateOverride,
blockOverrides: BlockOverrides.Rpc,
]
ReturnType: Hex
},
/**
Expand Down
Loading