Skip to content

Commit b55ec5a

Browse files
authored
feat: add chain.blockTime (#3702)
* feat: add chain.blockTime * chore: up * chore: format * Create wild-flowers-rush.md * Create good-melons-attack.md * Update wild-flowers-rush.md * chore: up * Update wild-flowers-rush.md * Update good-melons-attack.md
1 parent 835b02a commit b55ec5a

File tree

9 files changed

+35
-6
lines changed

9 files changed

+35
-6
lines changed

.changeset/good-melons-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"viem": minor
3+
---
4+
5+
Added `blockTime` to OP Stack (2s) & ZKsync (1s) chains.

.changeset/wild-flowers-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"viem": minor
3+
---
4+
5+
Added `blockTime` to the `Chain` type. Polling intervals are now influenced from this property (if set).

src/celo/chainConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { formatters } from './formatters.js'
44
import { serializers } from './serializers.js'
55

66
export const chainConfig = {
7+
blockTime: 2_000,
78
contracts,
89
formatters,
910
serializers,

src/clients/createClient.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { assertType, describe, expect, test, vi } from 'vitest'
22

33
import { anvilMainnet } from '../../test/src/anvil.js'
4-
import { localhost, mainnet } from '../chains/index.js'
4+
import { base, localhost, mainnet } from '../chains/index.js'
55
import type { EIP1193RequestFn, EIP1474Methods } from '../types/eip1193.js'
66
import { getAction } from '../utils/getAction.js'
77
import { type Client, createClient } from './createClient.js'
@@ -455,6 +455,16 @@ describe('config', () => {
455455
})
456456
})
457457

458+
test('behavior: cacheTime, pollingInterval based on chain.blockTime', () => {
459+
const client = createClient({
460+
chain: base,
461+
transport: http(),
462+
})
463+
464+
expect(client.cacheTime).toEqual(Math.floor(base.blockTime / 3))
465+
expect(client.pollingInterval).toEqual(Math.floor(base.blockTime / 3))
466+
})
467+
458468
describe('extends', () => {
459469
test('default', async () => {
460470
const client = createClient({

src/clients/createClient.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export type ClientConfig<
4343
| undefined
4444
/**
4545
* Time (in ms) that cached data will remain in memory.
46-
* @default 4_000
46+
* @default chain.blockTime / 3
4747
*/
4848
cacheTime?: number | undefined
4949
/**
@@ -70,7 +70,7 @@ export type ClientConfig<
7070
name?: string | undefined
7171
/**
7272
* Frequency (in ms) for polling enabled actions & events.
73-
* @default 4_000
73+
* @default chain.blockTime / 3
7474
*/
7575
pollingInterval?: number | undefined
7676
/**
@@ -216,15 +216,18 @@ export function createClient<
216216
export function createClient(parameters: ClientConfig): Client {
217217
const {
218218
batch,
219-
cacheTime = parameters.pollingInterval ?? 4_000,
219+
chain,
220220
ccipRead,
221221
key = 'base',
222222
name = 'Base Client',
223-
pollingInterval = 4_000,
224223
type = 'base',
225224
} = parameters
226225

227-
const chain = parameters.chain
226+
const blockTime = chain?.blockTime ?? 12_000
227+
const pollingInterval =
228+
parameters.pollingInterval ?? Math.floor(blockTime / 3)
229+
const cacheTime = parameters.cacheTime ?? pollingInterval
230+
228231
const account = parameters.account
229232
? parseAccount(parameters.account)
230233
: undefined

src/op-stack/chainConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { formatters } from './formatters.js'
33
import { serializers } from './serializers.js'
44

55
export const chainConfig = {
6+
blockTime: 2_000,
67
contracts,
78
formatters,
89
serializers,

src/types/chain.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export type Chain<
2828
default: ChainBlockExplorer
2929
}
3030
| undefined
31+
/** Block time in milliseconds. */
32+
blockTime?: number | undefined
3133
/** Collection of contracts */
3234
contracts?:
3335
| Prettify<

src/utils/chain/extractChain.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ test('default', async () => {
6565
"url": "https://optimistic.etherscan.io",
6666
},
6767
},
68+
"blockTime": 2000,
6869
"contracts": {
6970
"disputeGameFactory": {
7071
"1": {

src/zksync/chainConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { serializers } from './serializers.js'
33
import { getEip712Domain } from './utils/getEip712Domain.js'
44

55
export const chainConfig = {
6+
blockTime: 1_000,
67
formatters,
78
serializers,
89
custom: {

0 commit comments

Comments
 (0)