import { Abi, AbiEvent, AbiParameters, Address, Hash, Hex } from 'ox' import { describe, expect, test } from 'vp/test' import { wagmiContractConfig } from '../../../test/constants/abis.js' import { address } from '../../../test/constants/addresses.js' import { anvilMainnet } from '../../../test/prool.js' describe('assertArgs', () => { test('behavior: integer number and bigint arguments', () => { const abiEvent = AbiEvent.from( 'event Deposit(uint8 indexed tier, uint256 amount)', ) const args = AbiEvent.decode(abiEvent, { data: AbiParameters.encode(AbiParameters.from('uint256'), [5n]), topics: [ AbiEvent.getSelector(abiEvent), AbiParameters.encode(AbiParameters.from('uint8'), [1]), ], }) AbiEvent.assertArgs(abiEvent, args, { amount: 5, tier: 1n } as never) AbiEvent.assertArgs(abiEvent, args, { amount: [4, 5], tier: [1n] } as never) expect(() => AbiEvent.assertArgs(abiEvent, args, { tier: 2n } as never)) .toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Deposit(uint8 indexed tier, uint256 amount) Expected Arguments: tier: 1 amount: 5 Given Arguments: tier: 2] `) expect(() => AbiEvent.assertArgs(abiEvent, args, { amount: 5.5 } as never)) .toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Deposit(uint8 indexed tier, uint256 amount) Expected Arguments: tier: 1 amount: 5 Given Arguments: amount: 5.5] `) expect( AbiEvent.extractLogs( [abiEvent], [ { data: AbiParameters.encode(AbiParameters.from('uint256'), [5n]), topics: [ AbiEvent.getSelector(abiEvent), AbiParameters.encode(AbiParameters.from('uint8'), [1]), ], }, ], { args: { amount: 5, tier: 1n } as never }, ).map((x) => x.args), ).toMatchInlineSnapshot(` [ { "amount": 5n, "tier": 1, }, ] `) }) test('behavior: non-indexed `string` and `bytes`', () => { const abiEvent = AbiEvent.from( 'event Message(string indexed topic, string text, bytes data, bytes32 key)', ) const args = AbiEvent.decode(abiEvent, { data: AbiParameters.encode(AbiParameters.from('string, bytes, bytes32'), [ 'hello', '0xabcd', `0x${'ab'.repeat(32)}`, ]), topics: [ AbiEvent.getSelector(abiEvent), Hash.keccak256(Hex.fromString('greeting')), ], }) AbiEvent.assertArgs(abiEvent, args, { data: '0xABCD', key: `0x${'AB'.repeat(32)}`, text: 'hello', topic: 'greeting', }) expect(() => AbiEvent.assertArgs(abiEvent, args, { text: 'goodbye' })) .toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Message(string indexed topic, string text, bytes data, bytes32 key) Expected Arguments: topic: 0xb6b19d010a6e0901db334da92e3eed820e0244fba35be613232ddf8d2ee39391 text: hello data: 0xabcd key: 0xabababababababababababababababababababababababababababababababab Given Arguments: text: goodbye] `) expect(() => AbiEvent.assertArgs(abiEvent, args, { data: '0xab' })) .toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Message(string indexed topic, string text, bytes data, bytes32 key) Expected Arguments: topic: 0xb6b19d010a6e0901db334da92e3eed820e0244fba35be613232ddf8d2ee39391 text: hello data: 0xabcd key: 0xabababababababababababababababababababababababababababababababab Given Arguments: data: 0xab] `) }) test('default', () => { const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) AbiEvent.assertArgs(abiEvent, args, { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', value: 1n, }) }) test('default', () => { const abiEvent = AbiEvent.from( 'event Transfer(address indexed, address indexed, uint256)', ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) AbiEvent.assertArgs(abiEvent, args, [ '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', 1n, ]) }) test('error: args mismatch, named', () => { const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', ], }) expect(() => AbiEvent.assertArgs(abiEvent, args, { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', value: 1n, }), ).toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Transfer(address indexed from, address indexed to, uint256 value) Expected Arguments: from: 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC to: 0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad value: 1 Given Arguments: from: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad to: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac value: 1] `) }) test('error: args mismatch, unnamed', () => { const abiEvent = AbiEvent.from( 'event Transfer(address indexed, address indexed, uint256)', ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(() => AbiEvent.assertArgs(abiEvent, args, [ '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', ]), ).toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Transfer(address indexed, address indexed, uint256) Expected Arguments: 0: 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC 1: 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC 2: 1 Given Arguments: 0: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad] `) }) test('behavior: named + unnamed inputs', () => { const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed, uint256)', ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(abiEvent), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', ], }) AbiEvent.assertArgs(abiEvent, args, { 1: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', 2: 1n, from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', }) expect(() => AbiEvent.assertArgs(abiEvent, args, { 1: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', }), ).toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Transfer(address indexed from, address indexed, uint256) Expected Arguments: 1: 0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad 2: 1 from: 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC Given Arguments: 1: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac] `) }) test('error: args mismatch, positional and keyed', () => { const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(abiEvent), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', ], }) expect(() => // @ts-expect-error AbiEvent.assertArgs(abiEvent, args, [ '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', ]), ).toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Transfer(address indexed from, address indexed to, uint256 value) Expected Arguments: from: 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC to: 0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad value: 1 Given Arguments: 0: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac] `) expect(() => AbiEvent.assertArgs( abiEvent, [ '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', 1n, ], { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac' }, ), ).toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Transfer(address indexed from, address indexed to, uint256 value) Expected Arguments: 0: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac 1: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad 2: 1 Given Arguments: from: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac] `) }) test('behavior: filter without values', () => { const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const args = [ '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', 1n, ] AbiEvent.assertArgs(abiEvent, args, {}) AbiEvent.assertArgs(abiEvent, args, { from: undefined, to: null }) }) test('error: args mismatch, string', () => { const abiEvent = AbiEvent.from('event Transfer(string indexed a)') const args = AbiEvent.decode(abiEvent, { topics: [ '0x7cebee4ee226a36ff8751d9d69bb8265f5138c825f8c25d7ebdd60d972ffe5be', '0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8', ], }) expect(() => AbiEvent.assertArgs(abiEvent, args, { a: '0xdeadbeef', }), ).toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Transfer(string indexed a) Expected Arguments: a: 0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8 Given Arguments: a: 0xdeadbeef] `) }) test('error: input not found, named', () => { const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(() => AbiEvent.assertArgs(abiEvent, args, { // @ts-expect-error a: 'b', from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', value: 1n, }), ).toThrowErrorMatchingInlineSnapshot( `[AbiEvent.InputNotFoundError: Parameter "a" not found on \`event Transfer(address indexed from, address indexed to, uint256 value)\`.]`, ) }) test('error: input not found, unnamed', () => { const abiEvent = AbiEvent.from( 'event Transfer(address indexed, address indexed, uint256)', ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(() => // @ts-expect-error AbiEvent.assertArgs(abiEvent, args, [ '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', 1n, 1n, ]), ).toThrowErrorMatchingInlineSnapshot( `[AbiEvent.InputNotFoundError: Parameter "3" not found on \`event Transfer(address indexed, address indexed, uint256)\`.]`, ) }) test('error: provided args, but no actual event args', () => { const abiEvent = AbiEvent.from('event Transfer()') const args = AbiEvent.decode(abiEvent, { topics: [ '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', ], }) expect(() => // @ts-expect-error AbiEvent.assertArgs(abiEvent, args, { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678az', to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', value: 1n, }), ).toThrowErrorMatchingInlineSnapshot(` [AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. Event: event Transfer() Expected Arguments: None Given Arguments: from: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678az to: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac value: 1] `) }) }) describe('decode', () => { test('behavior: named', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const decoded = AbiEvent.decode(transfer, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(decoded).toMatchInlineSnapshot(` { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "value": 1n, } `) }) test('behavior: unnamed', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed, address indexed, uint256)', ) const decoded = AbiEvent.decode(transfer, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(decoded).toMatchInlineSnapshot(` [ "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", 1n, ] `) }) test('behavior: named + unnamed', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256)', ) const decoded = AbiEvent.decode(transfer, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(decoded).toMatchInlineSnapshot(` { "2": 1n, "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", } `) }) test('behavior: named + unnamed', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed, uint256 value)', ) const decoded = AbiEvent.decode(transfer, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(decoded).toMatchInlineSnapshot(` { "1": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "value": 1n, } `) }) test('behavior: named + unnamed', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed, uint256 value, uint256)', ) const decoded = AbiEvent.decode(transfer, { data: '0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001', topics: [ '0x9ed053bb818ff08b8353cd46f78db1f0799f31c9e4458fdb425c10eccd2efc44', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(decoded).toMatchInlineSnapshot(` { "1": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "3": 1n, "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "value": 1n, } `) }) test('behavior: string inputs', () => { const transfer = AbiEvent.from('event Transfer(string indexed a)') const decoded = AbiEvent.decode(transfer, { topics: [ '0x7cebee4ee226a36ff8751d9d69bb8265f5138c825f8c25d7ebdd60d972ffe5be', '0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8', ], }) expect(decoded).toMatchInlineSnapshot(` { "a": "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8", } `) }) test('behavior: unnamed, non-indexed input before indexed input (array mode)', () => { // Declaration order (uint256, bool indexed) differs from indexed-subset // order (bool is the only indexed input). Args must still come back in // declaration order, not indexed-subset order. const foo = AbiEvent.from('event Foo(uint256, bool indexed)') const decoded = AbiEvent.decode(foo, { data: '0x0000000000000000000000000000000000000000000000000000000000000111', topics: [ '0x79c52e97493a8f32348c3cf1ebfe4a8dfaeb083ca12cddd87b5d9f7c00d3ccaa', '0x0000000000000000000000000000000000000000000000000000000000000001', ], }) expect(decoded).toMatchInlineSnapshot(` [ 273n, true, ] `) }) test('behavior: named + unnamed, indexed input interleaved with non-indexed (mixed mode)', () => { // `a` (indexed) and the trailing unnamed `address` (indexed) are not a // contiguous prefix of declaration order — a non-indexed `bool` sits // between them. All three values must be present and at the correct // declaration-order keys; none may be silently dropped or overwritten. const foo = AbiEvent.from( 'event Foo(uint256 indexed a, bool, address indexed)', ) const decoded = AbiEvent.decode(foo, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xbf5963a29fe665781e91d832989689110056ee09e32656ea57265dbb1084006e', '0x0000000000000000000000000000000000000000000000000000000000000aaa', '0x00000000000000000000000000cccccccccccccccccccccccccccccccccccccc', ], }) expect(decoded).toMatchInlineSnapshot(` { "1": true, "2": "0x00CCCCCCccCcCccCCcccCCcccCcCcCccCcCcCCcc", "a": 2730n, } `) }) test('error: topics mismatch, named', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address to, uint256 indexed value)', ) expect(() => AbiEvent.decode(transfer, { topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }), ).toThrowErrorMatchingInlineSnapshot( `[AbiEvent.TopicsMismatchError: Expected a topic for indexed event parameter "value" for "event Transfer(address indexed from, address to, uint256 indexed value)".]`, ) }) test('error: topics mismatch, unnamed', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed, address, uint256 indexed)', ) expect(() => AbiEvent.decode(transfer, { topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }), ).toThrowErrorMatchingInlineSnapshot( `[AbiEvent.TopicsMismatchError: Expected a topic for indexed event parameter for "event Transfer(address indexed, address, uint256 indexed)".]`, ) }) test('error: data mismatch', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address to, uint256 value)', ) expect(() => AbiEvent.decode(transfer, { data: '0x0000000000000000000000000000000000000000000000000000000023c34600', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', '0x00000000000000000000000070e8a65d014918798ba424110d5df658cde1cc58', ], }), ).toThrowErrorMatchingInlineSnapshot( ` [AbiEvent.DataMismatchError: Data size of 32 bytes is too small for non-indexed event parameters. Non-indexed Parameters: (address to, uint256 value) Data: 0x0000000000000000000000000000000000000000000000000000000023c34600 (32 bytes)] `, ) }) test('error: data mismatch', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address to, uint256 value)', ) expect(() => AbiEvent.decode(transfer, { topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', '0x00000000000000000000000070e8a65d014918798ba424110d5df658cde1cc58', ], }), ).toThrowErrorMatchingInlineSnapshot( ` [AbiEvent.DataMismatchError: Data size of 0 bytes is too small for non-indexed event parameters. Non-indexed Parameters: (address to, uint256 value) Data: 0x (0 bytes)] `, ) }) test('error: invalid data size', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) expect(() => AbiEvent.decode(transfer, { data: '0x01', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }), ).toThrowErrorMatchingInlineSnapshot( ` [AbiEvent.DataMismatchError: Data size of 1 bytes is too small for non-indexed event parameters. Non-indexed Parameters: (uint256 value) Data: 0x01 (1 bytes)] `, ) }) test('error: invalid boolean', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, bool sender)', ) expect(() => AbiEvent.decode(transfer, { data: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', topics: [ '0x3da3cd3cf420c78f8981e7afeefa0eab1f0de0eb56e78ad9ba918ed01c0b402f', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }), ).toThrowErrorMatchingInlineSnapshot( ` [Bytes.InvalidBytesBooleanError: Bytes value \`221,242,82,173,27,226,200,155,105,194,176,104,252,55,141,170,149,43,167,241,99,196,161,22,40,245,90,77,245,35,179,239\` is not a valid boolean. The bytes array must contain a single byte of either a \`0\` or \`1\` value.] `, ) }) test('behavior: canonical indexed `address`, `bool`, `intN` and `uintN` topics', () => { const abiEvent = AbiEvent.from( 'event Foo(address indexed a, bool indexed b, int8 indexed c, uint8 d)', ) expect( AbiEvent.decode(abiEvent, { data: '0x00000000000000000000000000000000000000000000000000000000000000ff', topics: [ AbiEvent.getSelector(abiEvent), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x0000000000000000000000000000000000000000000000000000000000000001', '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80', ], }), ).toMatchInlineSnapshot(` { "a": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "b": true, "c": -128, "d": 255, } `) const abiEvent_2 = AbiEvent.from( 'event Foo(bool indexed a, uint8 indexed b, int256 indexed c)', ) expect( AbiEvent.decode(abiEvent_2, { topics: [ AbiEvent.getSelector(abiEvent_2), '0x0000000000000000000000000000000000000000000000000000000000000000', '0x00000000000000000000000000000000000000000000000000000000000000ff', '0x8000000000000000000000000000000000000000000000000000000000000000', ], }), ).toMatchInlineSnapshot(` { "a": false, "b": 255, "c": -57896044618658097711785492504343953926634992332820282019728792003956564819968n, } `) }) test('error: non-canonical indexed `address` topic', () => { const abiEvent = AbiEvent.from('event Foo(address indexed a)') const topic = '0xdeadbeef0000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' expect(() => AbiParameters.decode([{ type: 'address' }], topic), ).toThrowError(Address.InvalidAddressError) expect(() => AbiEvent.decode(abiEvent, { topics: [AbiEvent.getSelector(abiEvent), topic], }), ).toThrowErrorMatchingInlineSnapshot(` [Address.InvalidAddressError: Address "0xdeadbeef0000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac" is invalid. Details: Address is not a 20 byte (40 hexadecimal character) value.] `) }) test('error: non-canonical indexed `bool` topic', () => { const abiEvent = AbiEvent.from('event Foo(bool indexed a)') expect(() => AbiEvent.decode(abiEvent, { topics: [ AbiEvent.getSelector(abiEvent), '0x0000000000000000000000000000000000000000000000000000000000000002', ], }), ).toThrowErrorMatchingInlineSnapshot(` [Bytes.InvalidBytesBooleanError: Bytes value \`2\` is not a valid boolean. The bytes array must contain a single byte of either a \`0\` or \`1\` value.] `) expect(() => AbiEvent.decode(abiEvent, { topics: [ AbiEvent.getSelector(abiEvent), '0x0000000000000000000000000000000000000000000000000000000000000011', ], }), ).toThrowErrorMatchingInlineSnapshot(` [Bytes.InvalidBytesBooleanError: Bytes value \`17\` is not a valid boolean. The bytes array must contain a single byte of either a \`0\` or \`1\` value.] `) }) test('error: indexed `intN` and `uintN` topics out of range', () => { const uint8 = AbiEvent.from('event Foo(uint8 indexed a)') expect(() => AbiEvent.decode(uint8, { topics: [ AbiEvent.getSelector(uint8), '0x0000000000000000000000000000000000000000000000000000000000000100', ], }), ).toThrowErrorMatchingInlineSnapshot( `[Hex.IntegerOutOfRangeError: Number \`256\` is not in safe 8-bit unsigned integer range (\`0\` to \`255\`)]`, ) const int8 = AbiEvent.from('event Foo(int8 indexed a)') expect(() => AbiEvent.decode(int8, { topics: [ AbiEvent.getSelector(int8), '0x0000000000000000000000000000000000000000000000000000000000000080', ], }), ).toThrowErrorMatchingInlineSnapshot( `[Hex.IntegerOutOfRangeError: Number \`128\` is not in safe 8-bit signed integer range (\`-128\` to \`127\`)]`, ) expect(() => AbiEvent.decode(int8, { topics: [ AbiEvent.getSelector(int8), '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f', ], }), ).toThrowErrorMatchingInlineSnapshot( `[Hex.IntegerOutOfRangeError: Number \`-129\` is not in safe 8-bit signed integer range (\`-128\` to \`127\`)]`, ) const uint64 = AbiEvent.from('event Foo(uint64 indexed a)') expect(() => AbiEvent.decode(uint64, { topics: [ AbiEvent.getSelector(uint64), '0x8000000000000000000000000000000000000000000000000000000000000000', ], }), ).toThrowErrorMatchingInlineSnapshot( `[Hex.IntegerOutOfRangeError: Number \`57896044618658097711785492504343953926634992332820282019728792003956564819968\` is not in safe 64-bit unsigned integer range (\`0\` to \`18446744073709551615\`)]`, ) }) test('error: indexed topic shorter than 32 bytes', () => { const abiEvent = AbiEvent.from('event Foo(uint256 indexed a)') expect(() => AbiEvent.decode(abiEvent, { topics: [AbiEvent.getSelector(abiEvent), '0x01'], }), ).toThrowErrorMatchingInlineSnapshot(` [AbiParameters.DataSizeTooSmallError: Data size of 1 bytes is too small for given parameters. Params: (uint256 indexed a) Data: 0x01 (1 bytes)] `) }) test('error: event selector topic mismatch', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, bool sender)', ) expect(() => AbiEvent.decode(transfer, { topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], }), ).toThrowErrorMatchingInlineSnapshot( ` [AbiEvent.SelectorTopicMismatchError: topics[0]="0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" does not match the expected topics[0]="0x3da3cd3cf420c78f8981e7afeefa0eab1f0de0eb56e78ad9ba918ed01c0b402f". Event: event Transfer(address indexed from, address indexed to, bool sender) Selector: 0x3da3cd3cf420c78f8981e7afeefa0eab1f0de0eb56e78ad9ba918ed01c0b402f] `, ) }) test('behavior: overload with abi', () => { const abi = Abi.from([ 'event Transfer(address indexed from, address indexed to, uint256 value)', ]) const log = { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], } as const // Test both overload patterns const abiEvent = AbiEvent.fromAbi(abi, 'Transfer') const decoded1 = AbiEvent.decode(abiEvent, log) const decoded2 = AbiEvent.decode(abi, 'Transfer', log) expect(decoded1).toEqual(decoded2) expect(decoded2).toMatchInlineSnapshot(` { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "value": 1n, } `) }) test('behavior: overload with hex selector', () => { const abi = Abi.from([ 'event Transfer(address indexed from, address indexed to, uint256 value)', ]) const log = { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], } as const // Test with hex selector const decoded = AbiEvent.decode( abi, '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', log, ) expect(decoded).toMatchInlineSnapshot(` { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "value": 1n, } `) }) test('behavior: anonymous event', () => { const abiEvent = AbiEvent.from({ type: 'event', name: 'Transfer', anonymous: true, inputs: [ { name: 'from', type: 'address', indexed: true }, { name: 'to', type: 'address', indexed: true }, { name: 'value', type: 'uint256', indexed: false }, ], }) const decoded = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', ], }) expect(decoded).toMatchInlineSnapshot(` { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad", "value": 1n, } `) }) test('behavior: with overloads, zero-input and named', () => { const abi = Abi.from(['event Foo()', 'event Foo(address indexed who)']) const log = { topics: [ AbiEvent.getSelector('event Foo(address indexed who)'), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], } as const expect(AbiEvent.decode(abi, 'Foo', log)).toMatchInlineSnapshot(` { "who": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", } `) expect(AbiEvent.decode(AbiEvent.fromAbi(abi, 'Foo'), log)) .toMatchInlineSnapshot(` { "who": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", } `) expect( AbiEvent.decode(abi, 'Foo', { topics: [AbiEvent.getSelector('event Foo()')], }), ).toMatchInlineSnapshot(`undefined`) }) test('behavior: with overloads, anonymous and named', () => { const abi = Abi.from([ { anonymous: true, inputs: [], name: 'Foo', type: 'event' }, { inputs: [{ indexed: true, name: 'who', type: 'address' }], name: 'Foo', type: 'event', }, ]) const log = { topics: [ AbiEvent.getSelector('event Foo(address indexed who)'), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], } as const expect(AbiEvent.decode(abi, 'Foo', log)).toMatchInlineSnapshot(` { "who": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", } `) expect(AbiEvent.decode(AbiEvent.fromAbi(abi, 'Foo'), log)) .toMatchInlineSnapshot(` { "who": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", } `) // Without a selector match, the log decodes with the event picked by name. expect(AbiEvent.decode(abi, 'Foo', { topics: [] })).toMatchInlineSnapshot( `undefined`, ) }) test('behavior: with overloads, two named', () => { const abi = Abi.from([ 'event Deposit(address indexed who, uint256 amount)', 'event Deposit(address indexed who, uint256 amount, bytes32 indexed ref)', ]) const [deposit, depositWithRef] = abi expect( AbiEvent.decode(abi, 'Deposit', { data: '0x0000000000000000000000000000000000000000000000000000000000000005', topics: [ AbiEvent.getSelector(depositWithRef), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x1111111111111111111111111111111111111111111111111111111111111111', ], }), ).toMatchInlineSnapshot(` { "amount": 5n, "ref": "0x1111111111111111111111111111111111111111111111111111111111111111", "who": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", } `) expect( AbiEvent.decode(abi, 'Deposit', { data: '0x0000000000000000000000000000000000000000000000000000000000000005', topics: [ AbiEvent.getSelector(deposit), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }), ).toMatchInlineSnapshot(` { "amount": 5n, "who": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", } `) }) test('error: with overloads, selector topic matches no overload', () => { const abi = Abi.from([ 'event Deposit(address indexed who, uint256 amount)', 'event Deposit(address indexed who, uint256 amount, bytes32 indexed ref)', ]) expect(() => AbiEvent.decode(abi, 'Deposit', { data: '0x0000000000000000000000000000000000000000000000000000000000000005', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }), ).toThrowErrorMatchingInlineSnapshot(` [AbiEvent.SelectorTopicMismatchError: topics[0]="0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" does not match the expected topics[0]="0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c". Event: event Deposit(address indexed who, uint256 amount) Selector: 0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c] `) }) }) describe('decodeLog', () => { test('behavior: decodes event from selector topic', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const decoded = AbiEvent.decodeLog([transfer], { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(decoded).toMatchInlineSnapshot(` { "args": { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "value": 1n, }, "event": { "hash": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "inputs": [ { "indexed": true, "name": "from", "type": "address", }, { "indexed": true, "name": "to", "type": "address", }, { "name": "value", "type": "uint256", }, ], "name": "Transfer", "type": "event", }, } `) }) test('behavior: decodes an anonymous event when every event is anonymous', () => { const abi = Abi.from([ { anonymous: true, inputs: [{ indexed: true, name: 'from', type: 'address' }], name: 'Ping', type: 'event', }, { anonymous: true, inputs: [ { indexed: true, name: 'from', type: 'address' }, { indexed: true, name: 'to', type: 'address' }, ], name: 'Pair', type: 'event', }, ]) const decoded = AbiEvent.decodeLog(abi, { data: '0x', topics: [ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(decoded.event.name).toMatchInlineSnapshot(`"Ping"`) expect(decoded.args).toMatchInlineSnapshot(` { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", } `) }) test('behavior: a selector match wins over an anonymous event', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, uint256 value)', ) const abi = [ { anonymous: true, inputs: [ { indexed: true, name: 'a', type: 'bytes32' }, { indexed: true, name: 'b', type: 'bytes32' }, ], name: 'Anon', type: 'event', }, transfer, ] const decoded = AbiEvent.decodeLog(abi, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }) expect(decoded.event.name).toMatchInlineSnapshot(`"Transfer"`) }) test('error: anonymous events are not matched in a mixed ABI', () => { const abi = [ AbiEvent.from('event Transfer(address indexed from, uint256 value)'), AbiEvent.from({ anonymous: true, inputs: [ { indexed: true, name: 'a', type: 'address' }, { indexed: true, name: 'b', type: 'address' }, ], name: 'Anon', type: 'event', }), ] expect(() => AbiEvent.decodeLog(abi, { data: '0x', topics: [ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }), ).toThrowErrorMatchingInlineSnapshot( `[AbiItem.NotFoundError: ABI item with name "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac" not found.]`, ) }) test('error: anonymous events with the same topic count', () => { const abi = Abi.from([ { anonymous: true, inputs: [ { indexed: true, name: 'from', type: 'address' }, { indexed: false, name: 'value', type: 'uint256' }, ], name: 'Count', type: 'event', }, { anonymous: true, inputs: [ { indexed: true, name: 'from', type: 'address' }, { indexed: false, name: 'note', type: 'string' }, ], name: 'Message', type: 'event', }, ]) expect(() => AbiEvent.decodeLog(abi, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }), ).toThrowErrorMatchingInlineSnapshot(` [AbiEvent.AnonymousEventAmbiguityError: Found ambiguous anonymous events for the log. - \`event Count(address indexed from, uint256 value)\` - \`event Message(address indexed from, string note)\` These events take the same number of indexed topics. Decode the log with \`AbiEvent.decode\` and the intended event.] `) }) test('error: selector topic not found', () => { const transfer = AbiEvent.from('event Transfer(address indexed from)') expect(() => AbiEvent.decodeLog([transfer], { data: '0x', topics: [] })) .toThrowErrorMatchingInlineSnapshot(` [AbiEvent.SelectorTopicNotFoundError: Selector topic not found.] `) }) }) describe('extractLogs', () => { test('behavior: anonymous events', () => { const abi = [ AbiEvent.from('event Transfer(address indexed from, uint256 value)'), AbiEvent.from({ anonymous: true, inputs: [ { indexed: true, name: 'from', type: 'address' }, { indexed: true, name: 'to', type: 'address' }, ], name: 'Ping', type: 'event', }), ] const transfer = { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(abi[0]!), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], } as const const ping = { data: '0x', topics: [ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', ], } as const // Without `eventName`, an anonymous event in a mixed ABI is not requested, // so a log with an unknown selector is not extracted as it. expect(AbiEvent.extractLogs(abi, [transfer, ping]).map((x) => x.eventName)) .toMatchInlineSnapshot(` [ "Transfer", ] `) expect( AbiEvent.extractLogs(abi, [transfer, ping], { eventName: ['Ping', 'Transfer'], }).map((x) => [x.eventName, x.args]), ).toMatchInlineSnapshot(` [ [ "Transfer", { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "value": 1n, }, ], [ "Ping", { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", }, ], ] `) expect( AbiEvent.extractLogs([abi[1]], [ping], { args: { to: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266' }, }).map((x) => x.eventName), ).toMatchInlineSnapshot(` [ "Ping", ] `) }) test('behavior: skips logs that fit several anonymous events', () => { const abi = Abi.from([ { anonymous: true, inputs: [ { indexed: true, name: 'from', type: 'address' }, { indexed: false, name: 'value', type: 'uint256' }, ], name: 'Count', type: 'event', }, { anonymous: true, inputs: [ { indexed: true, name: 'from', type: 'address' }, { indexed: false, name: 'note', type: 'string' }, ], name: 'Message', type: 'event', }, ]) const log = { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], } as const expect(AbiEvent.extractLogs(abi, [log])).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs(abi, [log], { eventName: 'Count' }).map( (x) => x.eventName, ), ).toMatchInlineSnapshot(` [ "Count", ] `) }) test('behavior: decodes matching logs', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const log = { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], } as const const decoded = AbiEvent.extractLogs([transfer], [log]) expect(decoded).toMatchInlineSnapshot(` [ { "args": { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "value": 1n, }, "data": "0x0000000000000000000000000000000000000000000000000000000000000001", "eventName": "Transfer", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac", ], }, ] `) }) test('behavior: skips non-matching logs in strict mode', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const approval = AbiEvent.from( 'event Approval(address indexed owner, address indexed spender, uint256 value)', ) const decoded = AbiEvent.extractLogs( [transfer], [ { data: '0x', topics: [AbiEvent.getSelector(approval)], }, { data: '0x', topics: [], }, ], ) expect(decoded).toMatchInlineSnapshot(`[]`) }) test('behavior: skips logs with non-canonical indexed topics', () => { const abi = Abi.from([ 'event Transfer(address indexed from, address indexed to, uint256 value)', 'event Paused(bool indexed paused)', 'event Count(uint8 indexed count)', ]) const [transfer, paused, count] = abi const logs = [ { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(transfer), '0xdeadbeef0000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }, { data: '0x', topics: [ AbiEvent.getSelector(paused), '0x0000000000000000000000000000000000000000000000000000000000000002', ], }, { data: '0x', topics: [ AbiEvent.getSelector(count), '0x0000000000000000000000000000000000000000000000000000000000000100', ], }, ] as const expect(AbiEvent.extractLogs(abi, logs)).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs(abi, logs, { args: { paused: false }, eventName: 'Paused', }), ).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs(abi, logs, { strict: false }).map( ({ args, eventName }) => ({ args, eventName }), ), ).toMatchInlineSnapshot(` [ { "args": {}, "eventName": "Transfer", }, { "args": {}, "eventName": "Paused", }, { "args": {}, "eventName": "Count", }, ] `) }) test('behavior: filters by event name', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const approval = AbiEvent.from( 'event Approval(address indexed owner, address indexed spender, uint256 value)', ) const decoded = AbiEvent.extractLogs( [transfer, approval], [ { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', ], }, { data: '0x0000000000000000000000000000000000000000000000000000000000000002', topics: [ AbiEvent.getSelector(approval), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', ], }, ], { eventName: 'Approval' }, ) expect(decoded).toMatchInlineSnapshot(` [ { "args": { "owner": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "spender": "0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad", "value": 2n, }, "data": "0x0000000000000000000000000000000000000000000000000000000000000002", "eventName": "Approval", "topics": [ "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad", ], }, ] `) }) test('behavior: filters by arguments', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const decoded = AbiEvent.extractLogs( [transfer], [ { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', ], }, { data: '0x0000000000000000000000000000000000000000000000000000000000000002', topics: [ AbiEvent.getSelector(transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }, ], { args: { from: '0xa5CC3c03994Db5b0D9a5eeDd10CaBaB0813678Ac', }, }, ) expect(decoded).toMatchInlineSnapshot(` [ { "args": { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad", "value": 1n, }, "data": "0x0000000000000000000000000000000000000000000000000000000000000001", "eventName": "Transfer", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad", ], }, ] `) }) test('behavior: tries ABI events with matching selectors', () => { const erc20Transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const erc721Transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)', ) const decoded = AbiEvent.extractLogs( [erc20Transfer, erc721Transfer], [ { data: '0x', topics: [ AbiEvent.getSelector(erc721Transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', '0x0000000000000000000000000000000000000000000000000000000000000001', ], }, ], ) expect(decoded).toMatchInlineSnapshot(` [ { "args": { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad", "tokenId": 1n, }, "data": "0x", "eventName": "Transfer", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad", "0x0000000000000000000000000000000000000000000000000000000000000001", ], }, ] `) }) test('behavior: non-strict mode decodes missing indexed topics from data', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const decoded = AbiEvent.extractLogs( [transfer], [ { data: '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], }, ], { strict: false }, ) expect(decoded).toMatchInlineSnapshot(` [ { "args": { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad", "value": 1n, }, "data": "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad0000000000000000000000000000000000000000000000000000000000000001", "eventName": "Transfer", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac", ], }, ] `) }) test('behavior: non-strict mode returns partial logs when decoding fails', () => { const transfer = AbiEvent.from('event Transfer(address indexed from)') const decoded = AbiEvent.extractLogs( [transfer], [ { data: '0x', topics: [AbiEvent.getSelector(transfer), '0xdead'], }, ], { strict: false }, ) expect(decoded).toMatchInlineSnapshot(` [ { "args": {}, "data": "0x", "eventName": "Transfer", "topics": [ "0x2d339b1e3334d3f43e7aa6d5b1fa3ca5e4228b2dd547d1710c726019d98e53fa", "0xdead", ], }, ] `) }) test('behavior: filters by non-indexed `string` arguments', () => { const message = AbiEvent.from( 'event Message(address indexed from, string text)', ) const log = { data: AbiParameters.encode(AbiParameters.from('string'), ['hello']), topics: [ AbiEvent.getSelector(message), '0x0000000000000000000000000000000000000000000000000000000000000001', ], } as const expect( AbiEvent.extractLogs([message], [log], { args: { text: 'goodbye' } }), ).toMatchInlineSnapshot(`[]`) expect(AbiEvent.extractLogs([message], [log], { args: { text: 'hello' } })) .toMatchInlineSnapshot(` [ { "args": { "from": "0x0000000000000000000000000000000000000001", "text": "hello", }, "data": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000", "eventName": "Message", "topics": [ "0x811f7cff0a3374ff67cccc3726035d34ba70410e0256818a891e4d6acc01d88e", "0x0000000000000000000000000000000000000000000000000000000000000001", ], }, ] `) }) test('behavior: filters by unnamed non-indexed `string` arguments', () => { const message = AbiEvent.from('event Message(address indexed, string)') const log = { data: AbiParameters.encode(AbiParameters.from('string'), ['hello']), topics: [ AbiEvent.getSelector(message), '0x0000000000000000000000000000000000000000000000000000000000000001', ], } as const expect( AbiEvent.extractLogs([message], [log], { args: [null, 'goodbye'] }), ).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs([message], [log], { args: ['0x0000000000000000000000000000000000000001', 'hello'], }), ).toMatchInlineSnapshot(` [ { "args": [ "0x0000000000000000000000000000000000000001", "hello", ], "data": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000", "eventName": "Message", "topics": [ "0x811f7cff0a3374ff67cccc3726035d34ba70410e0256818a891e4d6acc01d88e", "0x0000000000000000000000000000000000000000000000000000000000000001", ], }, ] `) }) test('behavior: filters by arguments of named + unnamed inputs', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed, uint256)', ) const log = { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ AbiEvent.getSelector(transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', ], } as const expect( AbiEvent.extractLogs([transfer], [log], { args: { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad' }, }), ).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs([transfer], [log], { args: { 1: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac' }, }), ).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs([transfer], [log], { args: { 1: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', 2: 1n, from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', }, }), ).toMatchInlineSnapshot(` [ { "args": { "1": "0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad", "2": 1n, "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", }, "data": "0x0000000000000000000000000000000000000000000000000000000000000001", "eventName": "Transfer", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad", ], }, ] `) }) test('behavior: non-strict mode keys named + unnamed inputs like `decode`', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed, uint256)', ) const selector = AbiEvent.getSelector(transfer) const from = '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' const logs = [ // The second indexed input is read from data. { data: '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad0000000000000000000000000000000000000000000000000000000000000001', topics: [selector, from], }, // The topic does not decode as an address. { data: '0x', topics: [selector, '0xdead'] }, ] as const expect( AbiEvent.extractLogs([transfer], logs, { strict: false }).map( (log) => log.args, ), ).toMatchInlineSnapshot(` [ { "1": "0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad", "2": 1n, "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", }, {}, ] `) expect( AbiEvent.extractLogs([transfer], logs, { args: { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad' }, strict: false, }), ).toMatchInlineSnapshot(`[]`) }) test('behavior: positional and keyed arguments do not match', () => { const named = AbiEvent.from('event Transfer(address indexed from)') const unnamed = AbiEvent.from('event Transfer(address indexed)') const topic = '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' expect( AbiEvent.extractLogs( [named] as readonly unknown[], [{ data: '0x', topics: [AbiEvent.getSelector(named), topic] }], { args: ['0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad'] }, ), ).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs( [unnamed] as readonly unknown[], [{ data: '0x', topics: [AbiEvent.getSelector(unnamed), topic] }], { args: { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad' } }, ), ).toMatchInlineSnapshot(`[]`) }) test('behavior: filters by non-indexed `bytes` and `bytes32` arguments in either case', () => { const payload = AbiEvent.from('event Payload(bytes data, bytes32 key)') const log = { data: AbiParameters.encode(AbiParameters.from('bytes, bytes32'), [ '0xabcd', `0x${'ab'.repeat(32)}`, ]), topics: [AbiEvent.getSelector(payload)], } as const expect( AbiEvent.extractLogs([payload], [log], { args: { data: '0xab' } }), ).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs([payload], [log], { args: { key: `0x${'cd'.repeat(32)}` }, }), ).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs([payload], [log], { args: { data: '0xABCD', key: `0x${'AB'.repeat(32)}` }, }), ).toMatchInlineSnapshot(` [ { "args": { "data": "0xabcd", "key": "0xabababababababababababababababababababababababababababababababab", }, "data": "0x0000000000000000000000000000000000000000000000000000000000000040abababababababababababababababababababababababababababababababab0000000000000000000000000000000000000000000000000000000000000002abcd000000000000000000000000000000000000000000000000000000000000", "eventName": "Payload", "topics": [ "0x1d7847897c7e3bd365111aaf33b5ea1cc653db465de055620fb4cde96c75147f", ], }, ] `) }) test('behavior: filters by indexed `bytes` and `bytes32` arguments', () => { const payload = AbiEvent.from( 'event Payload(bytes indexed data, bytes32 indexed key)', ) const log = { data: '0x', topics: [ AbiEvent.getSelector(payload), Hash.keccak256('0x1234'), `0x${'ab'.repeat(32)}`, ], } as const expect( AbiEvent.extractLogs([payload], [log], { args: { data: '0x5678' } }), ).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs([payload], [log], { args: { data: '0x1234', key: `0x${'AB'.repeat(32)}` }, }), ).toMatchInlineSnapshot(` [ { "args": { "data": "0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432", "key": "0xabababababababababababababababababababababababababababababababab", }, "data": "0x", "eventName": "Payload", "topics": [ "0x1d7847897c7e3bd365111aaf33b5ea1cc653db465de055620fb4cde96c75147f", "0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432", "0xabababababababababababababababababababababababababababababababab", ], }, ] `) }) test('behavior: non-strict mode filters indexed `string` arguments decoded from data', () => { const message = AbiEvent.from('event Message(string indexed text)') const log = { data: AbiParameters.encode(AbiParameters.from('string'), ['hello']), topics: [AbiEvent.getSelector(message)], } as const expect( AbiEvent.extractLogs([message], [log], { args: { text: 'goodbye' }, strict: false, }), ).toMatchInlineSnapshot(`[]`) expect( AbiEvent.extractLogs([message], [log], { args: { text: 'hello' }, strict: false, }), ).toMatchInlineSnapshot(` [ { "args": { "text": "hello", }, "data": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000", "eventName": "Message", "topics": [ "0x51a7f65c6325882f237d4aeb43228179cfad48b868511d508e24b4437a819137", ], }, ] `) }) test('behavior: formats RPC logs', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const decoded = AbiEvent.extractLogs( [transfer], [ { address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', blockHash: '0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4', blockNumber: '0x12d846c', data: '0x0000000000000000000000000000000000000000000000000000000000000001', logIndex: '0x10f', removed: false, topics: [ AbiEvent.getSelector(transfer), '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad', ], transactionHash: '0xcfa52db0bc2cb5bdcb2c5bd8816df7a2f018a0e3964ab1ef4d794cf327966e93', transactionIndex: '0x91', }, ], ) expect(decoded).toMatchInlineSnapshot(` [ { "address": "0xfba3912ca04dd458c843e2ee08967fc04f3579c2", "args": { "from": "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", "to": "0xa5Cc3C03994DB5B0d9a5EEdD10CaBab0813678ad", "value": 1n, }, "blockHash": "0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4", "blockNumber": 19760236n, "blockTimestamp": undefined, "data": "0x0000000000000000000000000000000000000000000000000000000000000001", "eventName": "Transfer", "logIndex": 271, "removed": false, "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac", "0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad", ], "transactionHash": "0xcfa52db0bc2cb5bdcb2c5bd8816df7a2f018a0e3964ab1ef4d794cf327966e93", "transactionIndex": 145, }, ] `) }) }) describe('encode', () => { test('default', () => { const transfer = AbiEvent.from('event Transfer()') expect(AbiEvent.encode(transfer)).toMatchInlineSnapshot(` { "topics": [ "0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0", ], } `) }) test('behavior: no hash', () => { const transfer = AbiEvent.from('event Transfer()') expect(AbiEvent.encode({ ...transfer, hash: undefined })) .toMatchInlineSnapshot(` { "topics": [ "0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0", ], } `) }) test('behavior: multiple inputs, no args', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed a, address indexed b, uint256 c)', ) expect(AbiEvent.encode(transfer)).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", ], } `) }) test('behavior: multiple inputs, with args, named', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed a, address indexed b, uint256 c)', ) expect( AbiEvent.encode(transfer, { a: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x0000000000000000000000000000000000000000000000000000000000000000", null, ], } `) expect( AbiEvent.encode(transfer, { b: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", null, "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) expect( AbiEvent.encode(transfer, { a: '0x0000000000000000000000000000000000000000', b: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x0000000000000000000000000000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) expect( AbiEvent.encode(transfer, { a: null, b: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", null, "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) expect( AbiEvent.encode(transfer, { a: ['0x0000000000000000000000000000000000000000', address.vitalik], b: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", [ "0x0000000000000000000000000000000000000000000000000000000000000000", "0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045", ], "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) }) test('behavior: multiple inputs, with args, unnamed', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed, address indexed, uint256 )', ) expect( AbiEvent.encode(transfer, ['0x0000000000000000000000000000000000000000']), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x0000000000000000000000000000000000000000000000000000000000000000", null, ], } `) expect( AbiEvent.encode(transfer, [ null, '0x0000000000000000000000000000000000000000', ]), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", null, "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) expect( AbiEvent.encode(transfer, [ '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', ]), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x0000000000000000000000000000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) expect( AbiEvent.encode(transfer, [ ['0x0000000000000000000000000000000000000000', address.vitalik], '0x0000000000000000000000000000000000000000', ]), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", [ "0x0000000000000000000000000000000000000000000000000000000000000000", "0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045", ], "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) }) test('behavior: multiple inputs, with args, partially named', () => { const transfer = AbiEvent.from( 'event Transfer(address indexed a, address indexed, uint256 c)', ) expect( AbiEvent.encode(transfer, { a: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x0000000000000000000000000000000000000000000000000000000000000000", null, ], } `) expect( AbiEvent.encode(transfer, { 1: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", null, "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) expect( AbiEvent.encode(transfer, { a: '0x0000000000000000000000000000000000000000', 1: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x0000000000000000000000000000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) expect( AbiEvent.encode(transfer, { a: null, 1: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", null, "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) expect( AbiEvent.encode(transfer, { a: ['0x0000000000000000000000000000000000000000', address.vitalik], 1: '0x0000000000000000000000000000000000000000', }), ).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", [ "0x0000000000000000000000000000000000000000000000000000000000000000", "0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045", ], "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) }) test('behavior: string inputs', () => { const transfer = AbiEvent.from('event Transfer(string indexed a, string b)') expect( AbiEvent.encode(transfer, { a: 'hello', }), ).toMatchInlineSnapshot(` { "topics": [ "0x89525573e05dbc5f7f7f72e98f8145459e8b183e54c819fa7029f09a04115d89", "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8", ], } `) }) test('behavior: leading non-indexed inputs', () => { const transfer = AbiEvent.from( 'event Transfer(uint256 a, string indexed b)', ) expect( AbiEvent.encode(transfer, { b: 'hello', }), ).toMatchInlineSnapshot(` { "topics": [ "0x9060fea662832699cca863409e283b48eb088414dc780c4d43afe69caefee4b5", "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8", ], } `) }) test('behavior: network', async () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 indexed value)', ) const { topics } = AbiEvent.encode(transfer) const logs = await anvilMainnet.request({ method: 'eth_getLogs', params: [ { address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', fromBlock: Hex.fromNumber(19760235n), toBlock: Hex.fromNumber(19760240n), topics, }, ], }) expect(AbiEvent.decode(transfer, logs[0]!)).toMatchInlineSnapshot(` { "from": "0x0000000000000000000000000000000000000000", "to": "0x0C04D9e9278EC5e4D424476D3Ebec70Cb5d648D1", "value": 603n, } `) }) test('error: tuple input', () => { const transfer = AbiEvent.from( 'event Transfer((string) indexed a, string b)', ) expect(() => AbiEvent.encode(transfer, { a: ['hello'], }), ).toThrowErrorMatchingInlineSnapshot( `[AbiEvent.FilterTypeNotSupportedError: Filter type "tuple" is not supported.]`, ) }) test('error: array input', () => { const transfer = AbiEvent.from( 'event Transfer(uint256[] indexed a, string b)', ) expect(() => AbiEvent.encode(transfer, { a: [1n], }), ).toThrowErrorMatchingInlineSnapshot( `[AbiEvent.FilterTypeNotSupportedError: Filter type "uint256[]" is not supported.]`, ) }) test('behavior: narrow indexed args at their type bounds', () => { const event = AbiEvent.from( 'event Foo(int8 indexed a, uint8 indexed b, bytes4 indexed c)', ) expect(AbiEvent.encode(event, { a: -128, b: 255, c: '0x01020304' })) .toMatchInlineSnapshot(` { "topics": [ "0xd6e31c53a9a68addf818ebcdb6a5b6f8a18be2d94c1c57fb0f1d4c8014bbbe8c", "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80", "0x00000000000000000000000000000000000000000000000000000000000000ff", "0x0102030400000000000000000000000000000000000000000000000000000000", ], } `) expect(AbiEvent.encode(event, { a: 127, b: 0 })).toMatchInlineSnapshot(` { "topics": [ "0xd6e31c53a9a68addf818ebcdb6a5b6f8a18be2d94c1c57fb0f1d4c8014bbbe8c", "0x000000000000000000000000000000000000000000000000000000000000007f", "0x0000000000000000000000000000000000000000000000000000000000000000", null, ], } `) }) test('error: odd-length indexed bytesN arg', () => { const event = AbiEvent.from( 'event Foo(bytes4 indexed a, bytes32 indexed b)', ) expect(() => AbiEvent.encode(event, { a: '0x1234567' })) .toThrowErrorMatchingInlineSnapshot(` [Hex.InvalidLengthError: Hex value \`"0x1234567"\` is an odd length (7 nibbles). It must be an even length.] `) expect(() => AbiEvent.encode(event, { b: `0x${'1'.repeat(63)}` })) .toThrowErrorMatchingInlineSnapshot(` [Hex.InvalidLengthError: Hex value \`"0x111111111111111111111111111111111111111111111111111111111111111"\` is an odd length (63 nibbles). It must be an even length.] `) }) test('error: indexed integer out of range', () => { const event = AbiEvent.from('event Foo(int8 indexed a, uint8 indexed b)') expect(() => AbiEvent.encode(event, { a: 128 }), ).toThrowErrorMatchingInlineSnapshot( `[Hex.IntegerOutOfRangeError: Number \`128\` is not in safe 8-bit signed integer range (\`-128\` to \`127\`)]`, ) expect(() => AbiEvent.encode(event, { a: -129 }), ).toThrowErrorMatchingInlineSnapshot( `[Hex.IntegerOutOfRangeError: Number \`-129\` is not in safe 8-bit signed integer range (\`-128\` to \`127\`)]`, ) expect(() => AbiEvent.encode(event, { b: 256 }), ).toThrowErrorMatchingInlineSnapshot( `[Hex.IntegerOutOfRangeError: Number \`256\` is not in safe 8-bit unsigned integer range (\`0\` to \`255\`)]`, ) }) test('error: indexed bytesN size mismatch', () => { const event = AbiEvent.from( 'event Foo(bytes4 indexed a, bytes32 indexed b)', ) expect(() => AbiEvent.encode(event, { a: '0x0102030405' }), ).toThrowErrorMatchingInlineSnapshot( `[AbiParameters.BytesSizeMismatchError: Size of bytes "0x0102030405" (bytes5) does not match expected size (bytes4).]`, ) expect(() => AbiEvent.encode(event, { a: '0x0102' }), ).toThrowErrorMatchingInlineSnapshot( `[AbiParameters.BytesSizeMismatchError: Size of bytes "0x0102" (bytes2) does not match expected size (bytes4).]`, ) expect(() => AbiEvent.encode(event, { b: `0x${'11'.repeat(31)}` }), ).toThrowErrorMatchingInlineSnapshot( `[AbiParameters.BytesSizeMismatchError: Size of bytes "0x11111111111111111111111111111111111111111111111111111111111111" (bytes31) does not match expected size (bytes32).]`, ) }) test('behavior: network', async () => { const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const { topics } = AbiEvent.encode(transfer) const logs = await anvilMainnet.request({ method: 'eth_getLogs', params: [{ topics }], }) expect(AbiEvent.decode(transfer, logs[0]!)).toMatchInlineSnapshot(` { "from": "0x634cf119d1964b88a426E1E782Bd09f37b51d949", "to": "0xB77c2290C5e5ACD8Ca4778876b3CAaE593741BAB", "value": 714357880932609n, } `) }) test('https://github.com/wevm/viem/issues/3278', () => { const event = AbiEvent.from('event test(uint32 indexed val)') expect( AbiEvent.encode(event, { val: 0, }), ).toMatchInlineSnapshot(` { "topics": [ "0xe3cff634ef3ac1857ee74821b8b4103c803384af6152771eef3a2a92bdda6db6", "0x0000000000000000000000000000000000000000000000000000000000000000", ], } `) }) test('behavior: overload with abi', () => { const abi = Abi.from([ 'event Transfer(address indexed from, address indexed to, uint256 value)', ]) // Test both overload patterns const abiEvent = AbiEvent.fromAbi(abi, 'Transfer') const result1 = AbiEvent.encode(abiEvent, { from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', }) const result2 = AbiEvent.encode(abi, 'Transfer', { from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', }) expect(result1).toEqual(result2) expect(result2).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266", "0x00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8", ], } `) }) test('behavior: overload with abi - no args', () => { const abi = Abi.from(['event Transfer()']) // Test both overload patterns const abiEvent = AbiEvent.fromAbi(abi, 'Transfer') const result1 = AbiEvent.encode(abiEvent) const result2 = AbiEvent.encode(abi, 'Transfer') expect(result1).toEqual(result2) expect(result2).toMatchInlineSnapshot(` { "topics": [ "0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0", ], } `) }) test('behavior: overload with array args', () => { const abi = Abi.from([ 'event Transfer(address indexed, address indexed, uint256)', ]) // Test both overload patterns with unnamed args const abiEvent = AbiEvent.fromAbi(abi, 'Transfer') const result1 = AbiEvent.encode(abiEvent, [ '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', ]) const result2 = AbiEvent.encode(abi, 'Transfer', [ '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', ]) expect(result1).toEqual(result2) expect(result2).toMatchInlineSnapshot(` { "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266", "0x00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8", ], } `) }) }) describe('format', () => { test('default', () => { const transfer = AbiEvent.from({ type: 'event', name: 'Transfer', inputs: [ { name: 'from', type: 'address', indexed: true }, { name: 'to', type: 'address', indexed: true }, { name: 'value', type: 'uint256' }, ], }) const formatted = AbiEvent.format(transfer) expect(formatted).toMatchInlineSnapshot( `"event Transfer(address indexed from, address indexed to, uint256 value)"`, ) }) }) describe('from', () => { test('default', () => { { const abiItem = AbiEvent.from({ type: 'event', name: 'Transfer', inputs: [ { name: 'from', type: 'address', indexed: true }, { name: 'to', type: 'address', indexed: true }, { name: 'value', type: 'uint256' }, ], }) expect(abiItem).toMatchInlineSnapshot(` { "hash": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "inputs": [ { "indexed": true, "name": "from", "type": "address", }, { "indexed": true, "name": "to", "type": "address", }, { "name": "value", "type": "uint256", }, ], "name": "Transfer", "type": "event", } `) } { const abiItem = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) expect(abiItem).toMatchInlineSnapshot(` { "hash": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "inputs": [ { "indexed": true, "name": "from", "type": "address", }, { "indexed": true, "name": "to", "type": "address", }, { "name": "value", "type": "uint256", }, ], "name": "Transfer", "type": "event", } `) } }) test('options: prepare', () => { const abiItem = AbiEvent.from( { type: 'event', name: 'Transfer', inputs: [ { name: 'from', type: 'address', indexed: true }, { name: 'to', type: 'address', indexed: true }, { name: 'value', type: 'uint256' }, ], }, { prepare: false }, ) expect(abiItem).toMatchInlineSnapshot(` { "inputs": [ { "indexed": true, "name": "from", "type": "address", }, { "indexed": true, "name": "to", "type": "address", }, { "name": "value", "type": "uint256", }, ], "name": "Transfer", "type": "event", } `) }) }) describe('fromAbi', () => { test('default', () => { expect(AbiEvent.fromAbi(wagmiContractConfig.abi, 'Approval')) .toMatchInlineSnapshot(` { "anonymous": false, "hash": "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925", "inputs": [ { "indexed": true, "name": "owner", "type": "address", }, { "indexed": true, "name": "approved", "type": "address", }, { "indexed": true, "name": "tokenId", "type": "uint256", }, ], "name": "Approval", "type": "event", } `) }) test('behavior: prepare = false', () => { expect( AbiEvent.fromAbi(wagmiContractConfig.abi, 'Approval', { prepare: false, }), ).toMatchInlineSnapshot(` { "anonymous": false, "inputs": [ { "indexed": true, "name": "owner", "type": "address", }, { "indexed": true, "name": "approved", "type": "address", }, { "indexed": true, "name": "tokenId", "type": "uint256", }, ], "name": "Approval", "type": "event", } `) }) test('behavior: name (hash)', () => { const item = AbiEvent.fromAbi(wagmiContractConfig.abi, 'Approval') expect( AbiEvent.fromAbi(wagmiContractConfig.abi, AbiEvent.getSelector(item)), ).toMatchInlineSnapshot(` { "anonymous": false, "hash": "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925", "inputs": [ { "indexed": true, "name": "owner", "type": "address", }, { "indexed": true, "name": "approved", "type": "address", }, { "indexed": true, "name": "tokenId", "type": "uint256", }, ], "name": "Approval", "type": "event", } `) }) test('error: no matching name', () => { expect(() => // @ts-expect-error AbiEvent.fromAbi(wagmiContractConfig.abi, 'approve'), ).toThrowErrorMatchingInlineSnapshot( `[AbiItem.NotFoundError: ABI event with name "approve" not found.]`, ) }) test('error: no matching name', () => { expect(() => AbiEvent.fromAbi([] as readonly unknown[], 'Approved'), ).toThrowErrorMatchingInlineSnapshot( `[AbiItem.NotFoundError: ABI item with name "Approved" not found.]`, ) }) test('error: no matching data', () => { expect(() => AbiEvent.fromAbi([], '0xdeadbeef'), ).toThrowErrorMatchingInlineSnapshot( `[AbiItem.NotFoundError: ABI item with name "0xdeadbeef" not found.]`, ) }) test('behavior: overloads', () => { const abi = Abi.from([ 'event Bar()', 'event Foo(address indexed)', 'event Foo(uint256 indexed)', ]) const item = AbiEvent.fromAbi(abi, 'Foo') expect(item).toMatchInlineSnapshot(` { "hash": "0xe773a60b784586770a963a70fa6ba2bdf31c462939b6ba36852ed45f5f722358", "inputs": [ { "indexed": true, "type": "address", }, ], "name": "Foo", "overloads": [ { "inputs": [ { "indexed": true, "type": "uint256", }, ], "name": "Foo", "type": "event", }, ], "type": "event", } `) }) test('behavior: overloads: no inputs', () => { const abi = Abi.from(['event Bar()', 'event Foo()', 'event Foo(uint256)']) const item = AbiEvent.fromAbi(abi, 'Foo') expect(item).toMatchInlineSnapshot(` { "hash": "0xbfb4ebcfff8f360b39de1de85df1edc256d63337b743120bf6e2e2144b973d38", "inputs": [], "name": "Foo", "overloads": [ { "inputs": [ { "type": "uint256", }, ], "name": "Foo", "type": "event", }, ], "type": "event", } `) }) test('overloads: no args', () => { const abi = Abi.from(['event Bar()', 'event Foo(uint256)', 'event Foo()']) const item = AbiEvent.fromAbi(abi, 'Foo') expect(item).toMatchInlineSnapshot(` { "hash": "0xbfb4ebcfff8f360b39de1de85df1edc256d63337b743120bf6e2e2144b973d38", "inputs": [], "name": "Foo", "overloads": [ { "inputs": [ { "type": "uint256", }, ], "name": "Foo", "type": "event", }, ], "type": "event", } `) }) test('behavior: overloads: different types', () => { const abi = Abi.from([ 'event Mint()', 'event Mint(uint256)', 'event Mint(string)', ]) const item = AbiEvent.fromAbi(abi, 'Mint') expect(item).toMatchInlineSnapshot(` { "hash": "0x34c73884fbbb790762253ae313e57da96c00670344647f0cb8d41ee92b9f1971", "inputs": [], "name": "Mint", "overloads": [ { "inputs": [ { "type": "uint256", }, ], "name": "Mint", "type": "event", }, { "inputs": [ { "type": "string", }, ], "name": "Mint", "type": "event", }, ], "type": "event", } `) const item_2 = AbiEvent.fromAbi(abi, 'Mint', { args: [420n], }) expect(item_2).toMatchInlineSnapshot(` { "hash": "0x07883703ed0e86588a40d76551c92f8a4b329e3bf19765e0e6749473c1a84665", "inputs": [ { "type": "uint256", }, ], "name": "Mint", "type": "event", } `) const item_3 = AbiEvent.fromAbi(abi, 'Mint', { args: ['foo'], }) expect(item_3).toMatchInlineSnapshot(` { "hash": "0xc5e1d731c47dbd6a8c38e6ee9137792904eae9d20174034d1dc9a5781a0f855b", "inputs": [ { "type": "string", }, ], "name": "Mint", "type": "event", } `) }) test('behavior: overloads: ambiguious types', () => { expect(() => AbiEvent.fromAbi( Abi.from(['event Foo(address)', 'event Foo(bytes20)']), 'Foo', { args: ['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'], }, ), ).toThrowErrorMatchingInlineSnapshot(` [AbiItem.AmbiguityError: Found ambiguous types in overloaded ABI Items. \`bytes20\` in \`Foo(bytes20)\`, and \`address\` in \`Foo(address)\` These types encode differently and cannot be distinguished at runtime. Remove one of the ambiguous items in the ABI.] `) expect(() => AbiEvent.fromAbi( Abi.from([ 'event Foo(string)', 'event Foo(uint)', 'event Foo(address)', ]), 'Foo', { args: ['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'], }, ), ).toThrowErrorMatchingInlineSnapshot(` [AbiItem.AmbiguityError: Found ambiguous types in overloaded ABI Items. \`address\` in \`Foo(address)\`, and \`string\` in \`Foo(string)\` These types encode differently and cannot be distinguished at runtime. Remove one of the ambiguous items in the ABI.] `) expect( AbiEvent.fromAbi( Abi.from([ 'event Foo(string)', 'event Foo(uint)', 'event Foo(address)', ]), 'Foo', { // 21 bytes (invalid address) args: ['0xA0Cf798816D4b9b9866b5330EEa46a18382f251eee'], }, ), ).toMatchInlineSnapshot(` { "hash": "0x9f0b7f1630bdb7d474466e2dfef0fb9dff65f7a50eec83935b68f77d0808f08a", "inputs": [ { "type": "string", }, ], "name": "Foo", "type": "event", } `) expect( AbiEvent.fromAbi( Abi.from([ 'event Foo(string)', 'event Foo(uint)', 'event Foo(address)', ]), 'Foo', { // non-hex (invalid address) args: ['0xA0Cf798816D4b9b9866b5330EEa46a18382f251z'], }, ), ).toMatchInlineSnapshot(` { "hash": "0x9f0b7f1630bdb7d474466e2dfef0fb9dff65f7a50eec83935b68f77d0808f08a", "inputs": [ { "type": "string", }, ], "name": "Foo", "type": "event", } `) expect(() => AbiEvent.fromAbi( Abi.from([ 'event Foo(address)', 'event Foo(uint)', 'event Foo(string)', ]), 'Foo', { args: ['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'], }, ), ).toThrowErrorMatchingInlineSnapshot(` [AbiItem.AmbiguityError: Found ambiguous types in overloaded ABI Items. \`string\` in \`Foo(string)\`, and \`address\` in \`Foo(address)\` These types encode differently and cannot be distinguished at runtime. Remove one of the ambiguous items in the ABI.] `) }) }) describe('getSelector', () => { test('default', () => { expect( AbiEvent.getSelector( 'event Transfer( address indexed from, address indexed to, uint256 value)', ), ).toEqual( '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', ) expect( AbiEvent.getSelector( 'event Transfer(address indexed from, address indexed to, uint256 value)', ), ).toEqual( '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', ) expect( AbiEvent.getSelector( 'Transfer(address indexed from, address indexed to, uint256 value)', ), ).toEqual( '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', ) }) test('behavior: from `AbiEvent`', () => { expect( AbiEvent.getSelector( AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ), ), ).toEqual( '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', ) }) }) test('exports', () => { expect(Object.keys(AbiEvent)).toMatchInlineSnapshot(` [ "assertArgs", "decode", "decodeLog", "extractLogs", "encode", "format", "from", "fromAbi", "getSelector", "ArgsMismatchError", "InputNotFoundError", "DataMismatchError", "TopicsMismatchError", "SelectorTopicMismatchError", "SelectorTopicNotFoundError", "FilterTypeNotSupportedError", "AnonymousEventAmbiguityError", ] `) })