Skip to content

Commit 06673e0

Browse files
authored
feat: support block overrides on readContract and multicall actions (#3922)
* chore: enable call block overrides test * feat: support block overrides on readContract * feat: support block overrides on multicall
1 parent 450c14c commit 06673e0

File tree

8 files changed

+79
-5
lines changed

8 files changed

+79
-5
lines changed

.changeset/dry-bobcats-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"viem": patch
3+
---
4+
5+
Added support for `blockOverrides` on `readContract`.

.changeset/sharp-goats-drive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"viem": patch
3+
---
4+
5+
Added support for `blockOverrides` on `multicall`.

src/actions/public/call.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,13 @@ test('args: override', async () => {
237237
)
238238
})
239239

240-
test.skip('args: blockOverrides', async () => {
241-
// TODO: don't skip once block overrides are supported in Anvil.
240+
test('args: blockOverrides', async () => {
242241
const { data } = await call(client, {
243242
data: getCurrentBlockTimestamp4bytes,
244243
code: multicall3ContractConfig.bytecode,
245244
blockOverrides: { time: 420n },
246245
})
247-
expect(data).toMatchInlineSnapshot(fourTwenty)
246+
expect(data).toMatchInlineSnapshot(`"0x${fourTwenty}"`)
248247
})
249248

250249
test.skip('args: blobs', async () => {

src/actions/public/multicall.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { describe, expect, test, vi } from 'vitest'
88
import { Delegation, ErrorsExample, GH434 } from '~contracts/generated.js'
99
import {
1010
baycContractConfig,
11+
multicall3ContractConfig,
1112
usdcContractConfig,
1213
wagmiContractConfig,
1314
} from '~test/src/abis.js'
@@ -449,6 +450,36 @@ test('args: stateOverride', async () => {
449450
`)
450451
})
451452

453+
test('args: blockOverrides', async () => {
454+
expect(
455+
await multicall(client, {
456+
batchSize: 2,
457+
contracts: [
458+
{
459+
...multicall3ContractConfig,
460+
functionName: 'getCurrentBlockTimestamp',
461+
},
462+
{
463+
...multicall3ContractConfig,
464+
functionName: 'getCurrentBlockTimestamp',
465+
},
466+
],
467+
blockOverrides: { time: 420n },
468+
}),
469+
).toMatchInlineSnapshot(`
470+
[
471+
{
472+
"result": 420n,
473+
"status": "success",
474+
},
475+
{
476+
"result": 420n,
477+
"status": "success",
478+
},
479+
]
480+
`)
481+
})
482+
452483
test('args: authorizationList', async () => {
453484
const { contractAddress } = await deploy(client, {
454485
abi: Delegation.abi,

src/actions/public/multicall.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export type MulticallParameters<
4444
} = {},
4545
> = Pick<
4646
CallParameters,
47-
'authorizationList' | 'blockNumber' | 'blockTag' | 'stateOverride'
47+
| 'authorizationList'
48+
| 'blockNumber'
49+
| 'blockOverrides'
50+
| 'blockTag'
51+
| 'stateOverride'
4852
> & {
4953
/** The account to use for the multicall. */
5054
account?: Address | undefined
@@ -135,6 +139,7 @@ export async function multicall<
135139
authorizationList,
136140
allowFailure = true,
137141
blockNumber,
142+
blockOverrides,
138143
blockTag,
139144
stateOverride,
140145
} = parameters
@@ -233,6 +238,7 @@ export async function multicall<
233238
args: [calls],
234239
authorizationList,
235240
blockNumber,
241+
blockOverrides,
236242
blockTag,
237243
functionName: 'aggregate3',
238244
stateOverride,

src/actions/public/readContract.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
SoladyAccount07,
77
SoladyAccountFactory07,
88
} from '~contracts/generated.js'
9-
import { baycContractConfig, wagmiContractConfig } from '~test/src/abis.js'
9+
import {
10+
baycContractConfig,
11+
multicall3ContractConfig,
12+
wagmiContractConfig,
13+
} from '~test/src/abis.js'
1014
import { accounts, address } from '~test/src/constants.js'
1115
import {
1216
deploy,
@@ -151,6 +155,16 @@ describe('wagmi', () => {
151155
}),
152156
).toEqual(fakeName)
153157
})
158+
159+
test('args: blockOverrides', async () => {
160+
expect(
161+
await readContract(client, {
162+
...multicall3ContractConfig,
163+
functionName: 'getCurrentBlockTimestamp',
164+
blockOverrides: { time: 420n },
165+
}),
166+
).toEqual(420n)
167+
})
154168
})
155169

156170
describe('bayc', () => {

src/actions/public/readContract.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export type ReadContractParameters<
4545
| 'account'
4646
| 'authorizationList'
4747
| 'blockNumber'
48+
| 'blockOverrides'
4849
| 'blockTag'
4950
| 'factory'
5051
| 'factoryData'

src/constants/abis.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ export const multicall3Abi = [
4141
stateMutability: 'view',
4242
type: 'function',
4343
},
44+
{
45+
inputs: [],
46+
name: 'getCurrentBlockTimestamp',
47+
outputs: [
48+
{
49+
internalType: 'uint256',
50+
name: 'timestamp',
51+
type: 'uint256',
52+
},
53+
],
54+
stateMutability: 'view',
55+
type: 'function',
56+
},
4457
] as const
4558

4659
export const batchGatewayAbi = [

0 commit comments

Comments
 (0)