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
5 changes: 5 additions & 0 deletions .changeset/good-melons-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": minor
---

Added `blockTime` to OP Stack (2s) & ZKsync (1s) chains.
5 changes: 5 additions & 0 deletions .changeset/wild-flowers-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": minor
---

Added `blockTime` to the `Chain` type. Polling intervals are now influenced from this property (if set).
1 change: 1 addition & 0 deletions src/celo/chainConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { formatters } from './formatters.js'
import { serializers } from './serializers.js'

export const chainConfig = {
blockTime: 2_000,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
blockTime: 2_000,
blockTime: 1_000,

celo's block time is 1 second https://specs.celo.org/deployments.html?highlight=second#mainnet

contracts,
formatters,
serializers,
Expand Down
12 changes: 11 additions & 1 deletion src/clients/createClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertType, describe, expect, test, vi } from 'vitest'

import { anvilMainnet } from '../../test/src/anvil.js'
import { localhost, mainnet } from '../chains/index.js'
import { base, localhost, mainnet } from '../chains/index.js'
import type { EIP1193RequestFn, EIP1474Methods } from '../types/eip1193.js'
import { getAction } from '../utils/getAction.js'
import { type Client, createClient } from './createClient.js'
Expand Down Expand Up @@ -455,6 +455,16 @@ describe('config', () => {
})
})

test('behavior: cacheTime, pollingInterval based on chain.blockTime', () => {
const client = createClient({
chain: base,
transport: http(),
})

expect(client.cacheTime).toEqual(Math.floor(base.blockTime / 3))
expect(client.pollingInterval).toEqual(Math.floor(base.blockTime / 3))
})

describe('extends', () => {
test('default', async () => {
const client = createClient({
Expand Down
13 changes: 8 additions & 5 deletions src/clients/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type ClientConfig<
| undefined
/**
* Time (in ms) that cached data will remain in memory.
* @default 4_000
* @default chain.blockTime / 3
*/
cacheTime?: number | undefined
/**
Expand All @@ -70,7 +70,7 @@ export type ClientConfig<
name?: string | undefined
/**
* Frequency (in ms) for polling enabled actions & events.
* @default 4_000
* @default chain.blockTime / 3
*/
pollingInterval?: number | undefined
/**
Expand Down Expand Up @@ -216,15 +216,18 @@ export function createClient<
export function createClient(parameters: ClientConfig): Client {
const {
batch,
cacheTime = parameters.pollingInterval ?? 4_000,
chain,
ccipRead,
key = 'base',
name = 'Base Client',
pollingInterval = 4_000,
type = 'base',
} = parameters

const chain = parameters.chain
const blockTime = chain?.blockTime ?? 12_000
const pollingInterval =
parameters.pollingInterval ?? Math.floor(blockTime / 3)
const cacheTime = parameters.cacheTime ?? pollingInterval

const account = parameters.account
? parseAccount(parameters.account)
: undefined
Expand Down
1 change: 1 addition & 0 deletions src/op-stack/chainConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { formatters } from './formatters.js'
import { serializers } from './serializers.js'

export const chainConfig = {
blockTime: 2_000,
contracts,
formatters,
serializers,
Expand Down
2 changes: 2 additions & 0 deletions src/types/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export type Chain<
default: ChainBlockExplorer
}
| undefined
/** Block time in milliseconds. */
blockTime?: number | undefined
/** Collection of contracts */
contracts?:
| Prettify<
Expand Down
1 change: 1 addition & 0 deletions src/utils/chain/extractChain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ test('default', async () => {
"url": "https://optimistic.etherscan.io",
},
},
"blockTime": 2000,
"contracts": {
"disputeGameFactory": {
"1": {
Expand Down
1 change: 1 addition & 0 deletions src/zksync/chainConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { serializers } from './serializers.js'
import { getEip712Domain } from './utils/getEip712Domain.js'

export const chainConfig = {
blockTime: 1_000,
formatters,
serializers,
custom: {
Expand Down
Loading