Seeking Help with viem's getFilterLogs Type Errors #646
-
I always encounter a type error when using the Here are the code snippets I tried: // doc: https://viem.sh/docs/contract/createContractEventFilter.html
export const wagmiAbi = [
{
inputs: [
{
indexed: true,
name: "from",
type: "address",
},
{ indexed: true, name: "to", type: "address" },
{
indexed: true,
name: "tokenId",
type: "uint256",
},
],
name: "Transfer",
type: "event",
},
] as const;
export const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
});
const filter = await publicClient.createContractEventFilter({
abi: wagmiAbi,
});
const logs = await publicClient.getFilterLogs({ filter });
// Type 'CreateContractEventFilterReturnType<readonly [{ readonly inputs: readonly [{ readonly indexed: true; readonly name: "from"; readonly type: "address"; }, { readonly indexed: true; readonly name: "to"; readonly type: "address"; }, { readonly indexed: true; readonly name: "tokenId"; readonly type: "uint256"; }]; readon...' is not assignable to type 'Filter<"event", readonly [{ readonly inputs: readonly [{ readonly indexed: true; readonly name: "from"; readonly type: "address"; }, { readonly indexed: true; readonly name: "to"; readonly type: "address"; }, { readonly indexed: true; readonly name: "tokenId"; readonly type: "uint256"; }]; readonly name: "Transfer";...'.
// Type 'CreateContractEventFilterReturnType<readonly [{ readonly inputs: readonly [{ readonly indexed: true; readonly name: "from"; readonly type: "address"; }, { readonly indexed: true; readonly name: "to"; readonly type: "address"; }, { readonly indexed: true; readonly name: "tokenId"; readonly type: "uint256"; }]; readon...' is not assignable to type '{ id: `0x${string}`; request: { (args: { method: "web3_clientVersion"; params?: never; }): Promise<string>; (args: { method: "web3_sha3"; params: [data: `0x${string}`]; }): Promise<string>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>;...'.
// Type 'CreateContractEventFilterReturnType<readonly [{ readonly inputs: readonly [{ readonly indexed: true; readonly name: "from"; readonly type: "address"; }, { readonly indexed: true; readonly name: "to"; readonly type: "address"; }, { readonly indexed: true; readonly name: "tokenId"; readonly type: "uint256"; }]; readon...' is not assignable to type '{ abi: readonly [{ readonly inputs: readonly [{ readonly indexed: true; readonly name: "from"; readonly type: "address"; }, { readonly indexed: true; readonly name: "to"; readonly type: "address"; }, { readonly indexed: true; readonly name: "tokenId"; readonly type: "uint256"; }]; readonly name: "Transfer"; readonly...'.
// Property 'eventName' is optional in type 'CreateContractEventFilterReturnType<readonly [{ readonly inputs: readonly [{ readonly indexed: true; readonly name: "from"; readonly type: "address"; }, { readonly indexed: true; readonly name: "to"; readonly type: "address"; }, { readonly indexed: true; readonly name: "tokenId"; readonly type: "uint256"; }]; readon...' but required in type '{ abi: readonly [{ readonly inputs: readonly [{ readonly indexed: true; readonly name: "from"; readonly type: "address"; }, { readonly indexed: true; readonly name: "to"; readonly type: "address"; }, { readonly indexed: true; readonly name: "tokenId"; readonly type: "uint256"; }]; readonly name: "Transfer"; readonly...'.ts(2322)
// getFilterLogs.d.ts(8, 5): The expected type comes from property 'filter' which is declared here on type 'GetFilterLogsParameters<readonly [{ readonly inputs: readonly [{ readonly indexed: true; readonly name: "from"; readonly type: "address"; }, { readonly indexed: true; readonly name: "to"; readonly type: "address"; }, { readonly indexed: true; readonly name: "tokenId"; readonly type: "uint256"; }]; readonly name: "Tr...' // doc: https://viem.sh/docs/actions/public/getFilterLogs.html#filter
const filter = await publicClient.createEventFilter({
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
event: parseAbiItem('event Transfer(address indexed, address indexed, uint256)'),
})
const logs = await publicClient.getFilterLogs({ filter });
// Type '{ id: `0x${string}`; request: { (args: { method: "web3_clientVersion"; params?: never; }): Promise<string>; (args: { method: "web3_sha3"; params: [data: `0x${string}`]; }): Promise<string>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>;...' is not assignable to type 'Filter<"event", Abi, never, any>'.
// Type '{ id: `0x${string}`; request: { (args: { method: "web3_clientVersion"; params?: never; }): Promise<string>; (args: { method: "web3_sha3"; params: [data: `0x${string}`]; }): Promise<string>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>;...' is not assignable to type 'Filter<"event", Abi, never, any>'.
// Type '{ id: `0x${string}`; request: { (args: { method: "web3_clientVersion"; params?: never; }): Promise<string>; (args: { method: "web3_sha3"; params: [data: `0x${string}`]; }): Promise<string>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>;...' is not assignable to type '{ id: `0x${string}`; request: { (args: { method: "web3_clientVersion"; params?: never; }): Promise<string>; (args: { method: "web3_sha3"; params: [data: `0x${string}`]; }): Promise<string>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>;...'. Two different types with this name exist, but they are unrelated.
// Type '{ id: `0x${string}`; request: { (args: { method: "web3_clientVersion"; params?: never; }): Promise<string>; (args: { method: "web3_sha3"; params: [data: `0x${string}`]; }): Promise<string>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>;...' is not assignable to type '{ abi: Abi; args?: never; eventName: never; }'.
// Property 'abi' is optional in type '{ id: `0x${string}`; request: { (args: { method: "web3_clientVersion"; params?: never; }): Promise<string>; (args: { method: "web3_sha3"; params: [data: `0x${string}`]; }): Promise<string>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>; (args: { ...; }): Promise<...>;...' but required in type '{ abi: Abi; args?: never; eventName: never; }'.ts(2322)
// getFilterLogs.d.ts(8, 5): The expected type comes from property 'filter' which is declared here on type 'GetFilterLogsParameters<Abi, never>' I always receive type errors related to the Please assist me in resolving these type errors and provide any suggestions to improve the code. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Works fine in this TypeScript Playground. What TypeScript version are you using and what does you |
Beta Was this translation helpful? Give feedback.
You'll need to set
strict
to true.