From 1ab111a7171f5abb7be37d2e4d02c4ed4f7c9eec Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Thu, 5 Jun 2025 17:16:56 +0200 Subject: [PATCH 1/9] feat: add chain.blockTime --- src/celo/chainConfig.ts | 1 + src/clients/createClient.test.ts | 12 +++++++++++- src/clients/createClient.ts | 11 +++++++---- src/op-stack/chainConfig.ts | 1 + src/types/chain.ts | 2 ++ src/zksync/chainConfig.ts | 1 + 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/celo/chainConfig.ts b/src/celo/chainConfig.ts index 7ab857367a..1ffb46d758 100644 --- a/src/celo/chainConfig.ts +++ b/src/celo/chainConfig.ts @@ -4,6 +4,7 @@ import { formatters } from './formatters.js' import { serializers } from './serializers.js' export const chainConfig = { + blockTime: 2_000, contracts, formatters, serializers, diff --git a/src/clients/createClient.test.ts b/src/clients/createClient.test.ts index 36ce354ab9..b5487f34d7 100644 --- a/src/clients/createClient.test.ts +++ b/src/clients/createClient.test.ts @@ -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' @@ -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({ diff --git a/src/clients/createClient.ts b/src/clients/createClient.ts index cff5be076a..438d50d5d3 100644 --- a/src/clients/createClient.ts +++ b/src/clients/createClient.ts @@ -70,7 +70,7 @@ export type ClientConfig< name?: string | undefined /** * Frequency (in ms) for polling enabled actions & events. - * @default 4_000 + * @default chain.blockTime ?? 4_000 */ pollingInterval?: number | undefined /** @@ -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_ms = chain?.blockTime ?? 12_000 + const pollingInterval = + parameters.pollingInterval ?? Math.floor(blockTime_ms / 3) + const cacheTime = parameters.cacheTime ?? pollingInterval + const account = parameters.account ? parseAccount(parameters.account) : undefined diff --git a/src/op-stack/chainConfig.ts b/src/op-stack/chainConfig.ts index 44e19e46d4..1a45a826f7 100644 --- a/src/op-stack/chainConfig.ts +++ b/src/op-stack/chainConfig.ts @@ -3,6 +3,7 @@ import { formatters } from './formatters.js' import { serializers } from './serializers.js' export const chainConfig = { + blockTime: 2_000, contracts, formatters, serializers, diff --git a/src/types/chain.ts b/src/types/chain.ts index f3b57daaec..4cd9bf72f9 100644 --- a/src/types/chain.ts +++ b/src/types/chain.ts @@ -28,6 +28,8 @@ export type Chain< default: ChainBlockExplorer } | undefined + /** Block time in milliseconds. */ + blockTime?: number | undefined /** Collection of contracts */ contracts?: | Prettify< diff --git a/src/zksync/chainConfig.ts b/src/zksync/chainConfig.ts index 1e46b0163a..089013988f 100644 --- a/src/zksync/chainConfig.ts +++ b/src/zksync/chainConfig.ts @@ -3,6 +3,7 @@ import { serializers } from './serializers.js' import { getEip712Domain } from './utils/getEip712Domain.js' export const chainConfig = { + blockTime: 1_000, formatters, serializers, custom: { From 30c9f7270fe563bd1beb87629d596fbff0f94026 Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Thu, 5 Jun 2025 17:28:53 +0200 Subject: [PATCH 2/9] chore: up --- src/clients/createClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clients/createClient.ts b/src/clients/createClient.ts index 438d50d5d3..925975005a 100644 --- a/src/clients/createClient.ts +++ b/src/clients/createClient.ts @@ -70,7 +70,7 @@ export type ClientConfig< name?: string | undefined /** * Frequency (in ms) for polling enabled actions & events. - * @default chain.blockTime ?? 4_000 + * @default chain.blockTime / 3 */ pollingInterval?: number | undefined /** From 82d65b43c7eb13e4a7450c010a7f6064b238a76b Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Thu, 5 Jun 2025 17:30:59 +0200 Subject: [PATCH 3/9] chore: format --- src/clients/createClient.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/clients/createClient.ts b/src/clients/createClient.ts index 925975005a..e0280a3f8c 100644 --- a/src/clients/createClient.ts +++ b/src/clients/createClient.ts @@ -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 /** @@ -223,9 +223,9 @@ export function createClient(parameters: ClientConfig): Client { type = 'base', } = parameters - const blockTime_ms = chain?.blockTime ?? 12_000 + const blockTime = chain?.blockTime ?? 12_000 const pollingInterval = - parameters.pollingInterval ?? Math.floor(blockTime_ms / 3) + parameters.pollingInterval ?? Math.floor(blockTime / 3) const cacheTime = parameters.cacheTime ?? pollingInterval const account = parameters.account From 74614a3f0500d939060f98a30b0f600f6ca8ebcd Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Thu, 5 Jun 2025 17:32:04 +0200 Subject: [PATCH 4/9] Create wild-flowers-rush.md --- .changeset/wild-flowers-rush.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wild-flowers-rush.md diff --git a/.changeset/wild-flowers-rush.md b/.changeset/wild-flowers-rush.md new file mode 100644 index 0000000000..7a97dbfff5 --- /dev/null +++ b/.changeset/wild-flowers-rush.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Added `blockTime` to the `Chain` type. From 1c6d12d9b786e546d085f9736cd80eb7fab872cd Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Thu, 5 Jun 2025 17:32:49 +0200 Subject: [PATCH 5/9] Create good-melons-attack.md --- .changeset/good-melons-attack.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/good-melons-attack.md diff --git a/.changeset/good-melons-attack.md b/.changeset/good-melons-attack.md new file mode 100644 index 0000000000..2abe24339e --- /dev/null +++ b/.changeset/good-melons-attack.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Added `blockTime` to OP Stack (2s) & ZKsync (1s) chains. From 1859b368033ec1391aea58abb7783775eb848452 Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Thu, 5 Jun 2025 17:34:41 +0200 Subject: [PATCH 6/9] Update wild-flowers-rush.md --- .changeset/wild-flowers-rush.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/wild-flowers-rush.md b/.changeset/wild-flowers-rush.md index 7a97dbfff5..e3f6ce7bfb 100644 --- a/.changeset/wild-flowers-rush.md +++ b/.changeset/wild-flowers-rush.md @@ -2,4 +2,4 @@ "viem": patch --- -Added `blockTime` to the `Chain` type. +Added `blockTime` to the `Chain` type. Polling intervals are now influenced from this property (if set). From fd4f32399c01f16a029e52b7265eec8f559acbba Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Thu, 5 Jun 2025 17:48:59 +0200 Subject: [PATCH 7/9] chore: up --- src/utils/chain/extractChain.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/chain/extractChain.test.ts b/src/utils/chain/extractChain.test.ts index e69b5a8232..8cf208efb2 100644 --- a/src/utils/chain/extractChain.test.ts +++ b/src/utils/chain/extractChain.test.ts @@ -65,6 +65,7 @@ test('default', async () => { "url": "https://optimistic.etherscan.io", }, }, + "blockTime": 2000, "contracts": { "disputeGameFactory": { "1": { From 51f9bd770219a004b528b08c52a979de524c8d10 Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Thu, 5 Jun 2025 18:11:13 +0200 Subject: [PATCH 8/9] Update wild-flowers-rush.md --- .changeset/wild-flowers-rush.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/wild-flowers-rush.md b/.changeset/wild-flowers-rush.md index e3f6ce7bfb..0b1d4e8b98 100644 --- a/.changeset/wild-flowers-rush.md +++ b/.changeset/wild-flowers-rush.md @@ -1,5 +1,5 @@ --- -"viem": patch +"viem": minor --- Added `blockTime` to the `Chain` type. Polling intervals are now influenced from this property (if set). From 99f795449d0d8a222577de20749bd531c49b841b Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Thu, 5 Jun 2025 18:11:40 +0200 Subject: [PATCH 9/9] Update good-melons-attack.md --- .changeset/good-melons-attack.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/good-melons-attack.md b/.changeset/good-melons-attack.md index 2abe24339e..7081b907de 100644 --- a/.changeset/good-melons-attack.md +++ b/.changeset/good-melons-attack.md @@ -1,5 +1,5 @@ --- -"viem": patch +"viem": minor --- Added `blockTime` to OP Stack (2s) & ZKsync (1s) chains.