Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Slice starting at offset "32" is out-of-bounds (size: 32). #323

Closed
1 task done
pyk opened this issue Apr 6, 2023 · 7 comments · Fixed by #326
Closed
1 task done

bug: Slice starting at offset "32" is out-of-bounds (size: 32). #323

pyk opened this issue Apr 6, 2023 · 7 comments · Fixed by #326

Comments

@pyk
Copy link
Contributor

pyk commented Apr 6, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Package Version

0.2.9

Current Behavior

Given the following events:

[
  {
    address: '0xa6efae0c9293b4ee340de31022900ba747eaa92d',
    topics: [
      '0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f',
      '0x0000000000000000000000003e48298a5fe88e4d62985dff65dee39a25914975'
    ],
    data: '0x0000000000000000000000000000000000000000000000001c07b57c93e04da9000000000000000000000000000000000000000000000000000000000072b4a7',
    blockNumber: '0x4a1d17a',
    transactionHash: '0xff5a6fa427e1fdc7bec5a453f266cdd7c1bddc66492199548419e0172d07bcc5',
    transactionIndex: '0x1',
    blockHash: '0x4319ea349a8c82015e8e4b62202d0a7c3349f83a95dfcc3924acf930ca4d0d7d',
    logIndex: '0x12',
    removed: false
  },
  {
    address: '0xa6efae0c9293b4ee340de31022900ba747eaa92d',
    topics: [
      '0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f',
      '0x0000000000000000000000003e48298a5fe88e4d62985dff65dee39a25914975'
    ],
    data: '0x0000000000000000000000000000000000000000000000000040ba631dd532ce00000000000000000000000000000000000000000000000000000000000108e1',
    blockNumber: '0x4a1d17a',
    transactionHash: '0xff5a6fa427e1fdc7bec5a453f266cdd7c1bddc66492199548419e0172d07bcc5',
    transactionIndex: '0x1',
    blockHash: '0x4319ea349a8c82015e8e4b62202d0a7c3349f83a95dfcc3924acf930ca4d0d7d',
    logIndex: '0x2c',
    removed: false
  },
  {
    address: '0xd5e31fc5f4a009a49312c20742df187b35975528',
    topics: [
      '0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f',
      '0x000000000000000000000000e8b67ebf4825fec2ab6c010a01064f5fa54672a5',
      '0x0000000000000000000000000000000000000000000000000000000000000195'
    ],
    data: '0x0000000000000000000000000000000000000000000000003a3a7bb04ddfa000',
    blockNumber: '0x4a1d188',
    transactionHash: '0x60ac417de5d876ddf121d37476e62e46675cae81d724a34f3f76d64d9bb62fc6',
    transactionIndex: '0x5',
    blockHash: '0xbbf0b40bc00c8e0979a2348ba5e37623db192f03731eb831d166cf757a1a95ed',
    logIndex: '0x14',
    removed: false
  }
]

The following getLogs will raise an exception:

// Arbitrum Chain
const mintLogs = await rpc.getLogs({
        event: {
            type: "event",
            name: "Mint",
            inputs: [
                { type: "address", indexed: true, name: "sender" },
                { type: "uint256", indexed: false, name: "amount0" },
                { type: "uint256", indexed: false, name: "amount1" },
            ],
        },
        fromBlock: 77713750n,
        toBlock: 77713811n,
    });

Expected Behavior

viem should not raise an exception and returns the first two logs

Steps To Reproduce

To get the content of the logs:

    const fromBlock = 77713750n;
    const toBlock = 77713811n;

    const selector = getEventSelector("Mint(address,uint256,uint256)");
    console.log("DEBUG: selector", selector);
    const result = await fetch(
        "ARBITRUM_RPC",
        {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                method: "eth_getLogs",
                params: [
                    {
                        fromBlock: numberToHex(fromBlock),
                        toBlock: numberToHex(toBlock),
                        topics: [selector],
                    },
                ],
                id: 1,
                jsonrpc: "2.0",
            }),
        }
    );
    const data = await result.json();

to get the exception:

import { createPublicClient, http } from 'viem';
import { arbitrum } from 'viem/chains';

const client = createPublicClient({
  chain: arbitrum,
  transport: http(),
});

const mintLogs = await client.getLogs({
  event: {
    type: 'event',
    name: 'Mint',
    inputs: [
      { type: 'address', indexed: true, name: 'sender' },
      { type: 'uint256', indexed: false, name: 'amount0' },
      { type: 'uint256', indexed: false, name: 'amount1' },
    ],
  },
  fromBlock: 77713750n,
  toBlock: 77713811n,
});
console.log('DEBUG: mintLogs', mintLogs);

export default `Block number: ${blockNumber}`;

Link to Minimal Reproducible Example (StackBlitz, CodeSandbox, GitHub repo etc.)

https://stackblitz.com/edit/viem-getting-started-fpo973?file=index.ts

Anything else?

No response

@pyk
Copy link
Contributor Author

pyk commented Apr 6, 2023

Related issue and fixes #168 #169

@jxom
Copy link
Member

jxom commented Apr 6, 2023

Looks like the third log in that list does not honor the event ABI passed (it consists of 2 indexed parameters instead of 1, and also consists of 1 non-indexed parameter instead of 2) – so when we come to decode the topics, an error is thrown.

// Correct Log
topics: [
  '0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f',
  '0x0000000000000000000000003e48298a5fe88e4d62985dff65dee39a25914975' // ✅ { type: 'address', indexed: true, name: 'sender' },
],
data: '0x
  0000000000000000000000000000000000000000000000000040ba631dd532ce // ✅ { type: 'uint256', indexed: false, name: 'amount0' }
  00000000000000000000000000000000000000000000000000000000000108e1 // ✅ { type: 'uint256', indexed: false, name: 'amount1' }
',

// Incorrect Log
topics: [
  '0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f',
  '0x000000000000000000000000e8b67ebf4825fec2ab6c010a01064f5fa54672a5', // ✅ { type: 'address', indexed: true, name: 'sender' },
  '0x0000000000000000000000000000000000000000000000000000000000000195' // ❌ { type: 'uint256', indexed: false, name: 'amount0' }
],
data: '0x
  0000000000000000000000000000000000000000000000003a3a7bb04ddfa000 // ✅ { type: 'uint256', indexed: false, name: 'amount1' }
',

We could probably do some smarts internally to predict what the indexed/non-indexed args are based off the topics & data. Will take a look into it.

@pyk
Copy link
Contributor Author

pyk commented Apr 6, 2023

@jxom how about we prevent decodeParams consuming unavailable data?

https://github.com/wagmi-dev/viem/blob/4b3c23315a1e15a9a8c98497cecfae463fb7595a/src/utils/abi/decodeAbiParameters.ts#L43-L59

something like if position >= data.length; break then just skip the log if all defined non-indexed params are not available.

@jxom
Copy link
Member

jxom commented Apr 6, 2023

That is also a good idea!

@pyk
Copy link
Contributor Author

pyk commented Apr 6, 2023

@jxom Great! I have submitted #325 to fix this issue.

Sorry I don't write the test coz I can't setup the viem repo on my local or github codespaces due to error
Screenshot 2023-04-07 at 05 11 52

@pyk
Copy link
Contributor Author

pyk commented Apr 7, 2023

tysm @jxom! <3

Copy link
Contributor

github-actions bot commented Jun 3, 2024

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Viem version. If you have any questions or comments you can create a new discussion thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants