-
-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: scope filters to their respective transport
- Loading branch information
Showing
20 changed files
with
455 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"viem": patch | ||
--- | ||
|
||
Scoped filters to their respective transport. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,48 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
import { publicClient } from '../../_test/index.js' | ||
import { createHttpServer, publicClient } from '../../_test/index.js' | ||
|
||
import { createBlockFilter } from './createBlockFilter.js' | ||
import { createPublicClient, fallback, http } from '../../clients/index.js' | ||
|
||
test('default', async () => { | ||
expect(await createBlockFilter(publicClient)).toBeDefined() | ||
}) | ||
|
||
test('fallback client: scopes request', async () => { | ||
let count1 = 0 | ||
const server1 = await createHttpServer((_req, res) => { | ||
count1++ | ||
res.writeHead(200, { | ||
'Content-Type': 'application/json', | ||
}) | ||
res.end( | ||
JSON.stringify({ | ||
error: { code: -32004, message: 'method not supported' }, | ||
}), | ||
) | ||
}) | ||
|
||
let count2 = 0 | ||
const server2 = await createHttpServer((_req, res) => { | ||
count2++ | ||
res.writeHead(200, { | ||
'Content-Type': 'application/json', | ||
}) | ||
res.end(JSON.stringify({ result: '0x1' })) | ||
}) | ||
|
||
const fallbackClient = createPublicClient({ | ||
transport: fallback([http(server1.url), http(server2.url)], { | ||
rank: false, | ||
}), | ||
}) | ||
const filter = await createBlockFilter(fallbackClient) | ||
expect(filter).toBeDefined() | ||
expect(count1).toBe(1) | ||
expect(count2).toBe(1) | ||
|
||
await filter.request({ method: 'eth_getFilterChanges', params: [filter.id] }) | ||
expect(count1).toBe(1) | ||
expect(count2).toBe(2) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import type { PublicClient, Transport } from '../../clients/index.js' | ||
import type { Chain, Filter } from '../../types/index.js' | ||
import { createFilterRequestScope } from '../../utils/filters/createFilterRequestScope.js' | ||
|
||
export type CreateBlockFilterReturnType = Filter<'block'> | ||
|
||
export async function createBlockFilter<TChain extends Chain | undefined>( | ||
client: PublicClient<Transport, TChain>, | ||
): Promise<CreateBlockFilterReturnType> { | ||
const getRequest = createFilterRequestScope(client, { | ||
method: 'eth_newBlockFilter', | ||
}) | ||
const id = await client.request({ | ||
method: 'eth_newBlockFilter', | ||
}) | ||
return { id, type: 'block' } | ||
return { id, request: getRequest(id), type: 'block' } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,48 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
import { publicClient } from '../../_test/index.js' | ||
import { createHttpServer, publicClient } from '../../_test/index.js' | ||
|
||
import { createPendingTransactionFilter } from './createPendingTransactionFilter.js' | ||
import { createPublicClient, fallback, http } from '../../clients/index.js' | ||
|
||
test('default', async () => { | ||
expect(await createPendingTransactionFilter(publicClient)).toBeDefined() | ||
}) | ||
|
||
test('fallback client: scopes request', async () => { | ||
let count1 = 0 | ||
const server1 = await createHttpServer((_req, res) => { | ||
count1++ | ||
res.writeHead(200, { | ||
'Content-Type': 'application/json', | ||
}) | ||
res.end( | ||
JSON.stringify({ | ||
error: { code: -32004, message: 'method not supported' }, | ||
}), | ||
) | ||
}) | ||
|
||
let count2 = 0 | ||
const server2 = await createHttpServer((_req, res) => { | ||
count2++ | ||
res.writeHead(200, { | ||
'Content-Type': 'application/json', | ||
}) | ||
res.end(JSON.stringify({ result: '0x1' })) | ||
}) | ||
|
||
const fallbackClient = createPublicClient({ | ||
transport: fallback([http(server1.url), http(server2.url)], { | ||
rank: false, | ||
}), | ||
}) | ||
const filter = await createPendingTransactionFilter(fallbackClient) | ||
expect(filter).toBeDefined() | ||
expect(count1).toBe(1) | ||
expect(count2).toBe(1) | ||
|
||
await filter.request({ method: 'eth_getFilterChanges', params: [filter.id] }) | ||
expect(count1).toBe(1) | ||
expect(count2).toBe(2) | ||
}) |
Oops, something went wrong.