Skip to content

Commit

Permalink
fix: scope filters to their respective transport
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Apr 11, 2023
1 parent 630f9cc commit fc39f7b
Show file tree
Hide file tree
Showing 20 changed files with 455 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-roses-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Scoped filters to their respective transport.
41 changes: 40 additions & 1 deletion src/actions/public/createBlockFilter.test.ts
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)
})
6 changes: 5 additions & 1 deletion src/actions/public/createBlockFilter.ts
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' }
}
46 changes: 46 additions & 0 deletions src/actions/public/createContractEventFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import { assertType, expect, test } from 'vitest'

import {
accounts,
createHttpServer,
initialBlockNumber,
publicClient,
usdcContractConfig,
} from '../../_test/index.js'
import { createPublicClient, fallback, http } from '../../clients/index.js'
import type { Requests } from '../../types/eip1193.js'

import { createContractEventFilter } from './createContractEventFilter.js'

const request = (() => {}) as unknown as Requests['request']

test('default', async () => {
const filter = await createContractEventFilter(publicClient, {
abi: usdcContractConfig.abi,
Expand All @@ -19,6 +24,7 @@ test('default', async () => {
type: 'event',
args: undefined,
eventName: undefined,
request,
})
expect(filter.id).toBeDefined()
expect(filter.type).toBe('event')
Expand Down Expand Up @@ -125,3 +131,43 @@ test('args: toBlock', async () => {
).id,
).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 createContractEventFilter(fallbackClient, {
abi: usdcContractConfig.abi,
})
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)
})
6 changes: 6 additions & 0 deletions src/actions/public/createContractEventFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
} from '../../types/index.js'
import { encodeEventTopics, numberToHex } from '../../utils/index.js'
import type { EncodeEventTopicsParameters } from '../../utils/index.js'
import { createFilterRequestScope } from '../../utils/filters/createFilterRequestScope.js'

export type CreateContractEventFilterParameters<
TAbi extends Abi | readonly unknown[] = Abi,
Expand Down Expand Up @@ -64,6 +65,10 @@ export async function createContractEventFilter<
toBlock,
}: CreateContractEventFilterParameters<TAbi, TEventName, TArgs>,
): Promise<CreateContractEventFilterReturnType<TAbi, TEventName, TArgs>> {
const getRequest = createFilterRequestScope(client, {
method: 'eth_newFilter',
})

const topics = eventName
? encodeEventTopics({
abi,
Expand All @@ -88,6 +93,7 @@ export async function createContractEventFilter<
args,
eventName,
id,
request: getRequest(id),
type: 'event',
} as unknown as CreateContractEventFilterReturnType<TAbi, TEventName, TArgs>
}
51 changes: 51 additions & 0 deletions src/actions/public/createEventFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { assertType, describe, expect, test } from 'vitest'

import {
accounts,
createHttpServer,
initialBlockNumber,
publicClient,
} from '../../_test/index.js'
import { createEventFilter } from './createEventFilter.js'
import type { Requests } from '../../types/eip1193.js'
import { createPublicClient, fallback, http } from '../../clients/index.js'

const event = {
default: {
Expand Down Expand Up @@ -49,11 +52,14 @@ const event = {
},
} as const

const request = (() => {}) as unknown as Requests['request']

describe('default', () => {
test('no args', async () => {
const filter = await createEventFilter(publicClient)
assertType<typeof filter>({
id: '0x',
request,
type: 'event',
})
expect(filter.id).toBeDefined()
Expand All @@ -77,6 +83,7 @@ describe('default', () => {
abi: [event.default],
eventName: 'Transfer',
id: '0x',
request,
type: 'event',
})
expect(filter.args).toBeUndefined()
Expand All @@ -100,6 +107,7 @@ describe('default', () => {
},
eventName: 'Transfer',
id: '0x',
request,
type: 'event',
})
expect(filter.args).toEqual({
Expand All @@ -122,6 +130,7 @@ describe('default', () => {
},
eventName: 'Transfer',
id: '0x',
request,
type: 'event',
})
expect(filter2.args).toEqual({
Expand All @@ -143,6 +152,7 @@ describe('default', () => {
},
eventName: 'Transfer',
id: '0x',
request,
type: 'event',
})
expect(filter3.args).toEqual({
Expand All @@ -162,6 +172,7 @@ describe('default', () => {
args: [accounts[0].address, accounts[1].address],
eventName: 'Transfer',
id: '0x',
request,
type: 'event',
})
expect(filter1.args).toEqual([accounts[0].address, accounts[1].address])
Expand All @@ -177,6 +188,7 @@ describe('default', () => {
args: [[accounts[0].address, accounts[1].address]],
eventName: 'Transfer',
id: '0x',
request,
type: 'event',
})
expect(filter2.args).toEqual([[accounts[0].address, accounts[1].address]])
Expand All @@ -192,6 +204,7 @@ describe('default', () => {
args: [null, accounts[0].address],
eventName: 'Transfer',
id: '0x',
request,
type: 'event',
})
expect(filter3.args).toEqual([null, accounts[0].address])
Expand Down Expand Up @@ -221,3 +234,41 @@ describe('default', () => {
})
})
})

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 createEventFilter(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)
})
7 changes: 7 additions & 0 deletions src/actions/public/createEventFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
} from '../../types/index.js'
import { encodeEventTopics, numberToHex } from '../../utils/index.js'
import type { EncodeEventTopicsParameters } from '../../utils/index.js'
import { createFilterRequestScope } from '../../utils/filters/createFilterRequestScope.js'

export type CreateEventFilterParameters<
TAbiEvent extends AbiEvent | undefined = undefined,
Expand Down Expand Up @@ -82,6 +83,10 @@ export async function createEventFilter<
_Args
> = {} as any,
): Promise<CreateEventFilterReturnType<TAbiEvent, _Abi, _EventName, _Args>> {
const getRequest = createFilterRequestScope(client, {
method: 'eth_newFilter',
})

let topics: LogTopic[] = []
if (event)
topics = encodeEventTopics({
Expand All @@ -102,11 +107,13 @@ export async function createEventFilter<
},
],
})

return {
abi: event ? [event] : undefined,
args,
eventName: event ? (event as AbiEvent).name : undefined,
id,
request: getRequest(id),
type: 'event',
} as unknown as CreateEventFilterReturnType<
TAbiEvent,
Expand Down
41 changes: 40 additions & 1 deletion src/actions/public/createPendingTransactionFilter.test.ts
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)
})
Loading

0 comments on commit fc39f7b

Please sign in to comment.