import type { Address } from 'abitype' import { expectTypeOf, test } from 'vitest' import { decodeEventLog } from './decodeEventLog.js' test('named', async () => { const event = decodeEventLog({ abi: [ { inputs: [ { indexed: true, name: 'from', type: 'address', }, { indexed: true, name: 'to', type: 'address', }, { indexed: false, name: 'tokenId', type: 'uint256', }, ], name: 'Transfer', type: 'event', }, { inputs: [ { indexed: true, name: 'from', type: 'address', }, { indexed: true, name: 'to', type: 'address', }, { indexed: false, name: 'value', type: 'uint256', }, ], name: 'Foo', type: 'event', }, ], data: '0x0000000000000000000000000000000000000000000000000000000000000001', eventName: 'Transfer', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }) expectTypeOf(event).toEqualTypeOf<{ args: { from: Address to: Address tokenId: bigint } eventName: 'Transfer' }>() }) test('named (strict = false)', async () => { const event = decodeEventLog({ abi: [ { inputs: [ { indexed: true, name: 'from', type: 'address', }, { indexed: true, name: 'to', type: 'address', }, { indexed: false, name: 'tokenId', type: 'uint256', }, ], name: 'Transfer', type: 'event', }, { inputs: [ { indexed: true, name: 'from', type: 'address', }, { indexed: true, name: 'to', type: 'address', }, { indexed: false, name: 'value', type: 'uint256', }, ], name: 'Foo', type: 'event', }, ], data: '0x0000000000000000000000000000000000000000000000000000000000000001', eventName: 'Transfer', strict: false, topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }) expectTypeOf(event).toEqualTypeOf<{ args: { from?: Address to?: Address tokenId?: bigint } eventName: 'Transfer' }>() }) test('unnamed', async () => { const event = decodeEventLog({ abi: [ { inputs: [ { indexed: true, type: 'address', }, { indexed: true, type: 'address', }, { indexed: false, type: 'uint256', }, ], name: 'Transfer', type: 'event', }, ], data: '0x0000000000000000000000000000000000000000000000000000000000000001', eventName: 'Transfer', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }) expectTypeOf(event).toEqualTypeOf<{ args: readonly [`0x${string}`, `0x${string}`, bigint] eventName: 'Transfer' }>() }) test('unnamed (strict = false)', async () => { const event = decodeEventLog({ abi: [ { inputs: [ { indexed: true, type: 'address', }, { indexed: true, type: 'address', }, { indexed: false, type: 'uint256', }, ], name: 'Transfer', type: 'event', }, ], data: '0x0000000000000000000000000000000000000000000000000000000000000001', eventName: 'Transfer', strict: false, topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }) expectTypeOf(event).toEqualTypeOf<{ args: | readonly [] | readonly [`0x${string}`, `0x${string}`, bigint] | readonly [`0x${string}`, `0x${string}`] | readonly [`0x${string}`] eventName: 'Transfer' }>() }) test('unknown eventName', async () => { const event = decodeEventLog({ abi: [ { inputs: [ { indexed: true, name: 'from', type: 'address', }, { indexed: true, name: 'to', type: 'address', }, { indexed: false, name: 'tokenId', type: 'uint256', }, ], name: 'Transfer', type: 'event', }, { inputs: [ { indexed: true, name: 'from', type: 'address', }, { indexed: true, name: 'to', type: 'address', }, { indexed: false, name: 'value', type: 'uint256', }, ], name: 'Foo', type: 'event', }, ], data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }) expectTypeOf(event).toEqualTypeOf< | { args: { from: Address to: Address tokenId: bigint } eventName: 'Transfer' } | { args: { from: Address to: Address value: bigint } eventName: 'Foo' } >() }) test('unknown eventName (strict = false)', async () => { const event = decodeEventLog({ abi: [ { inputs: [ { indexed: true, name: 'from', type: 'address', }, { indexed: true, name: 'to', type: 'address', }, { indexed: false, name: 'tokenId', type: 'uint256', }, ], name: 'Transfer', type: 'event', }, { inputs: [ { indexed: true, name: 'from', type: 'address', }, { indexed: true, name: 'to', type: 'address', }, { indexed: false, name: 'value', type: 'uint256', }, ], name: 'Foo', type: 'event', }, ], data: '0x0000000000000000000000000000000000000000000000000000000000000001', strict: false, topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }) expectTypeOf(event).toEqualTypeOf< | { args: { from?: Address to?: Address tokenId?: bigint } eventName: 'Transfer' } | { args: { from?: Address to?: Address value?: bigint } eventName: 'Foo' } >() }) test('no abi', async () => { const event = decodeEventLog({ abi: [], data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }) expectTypeOf(event).toEqualTypeOf() })