wevm / viem

TypeScript Interface for Ethereum
https://viem.sh
Other
2.51k stars 797 forks source link

bug: getContractEvent filtering args are ignored for Zora RPC #2522

Closed veganbeef closed 2 months ago

veganbeef commented 2 months ago

Check existing issues

Viem Version

2.15.1

Current Behavior

When I call the PublicClient.getContractEvents method using Conduit's Zora RPC (at rpc.zora.energy), the from and to properties of the args parameter are ignored, and events are returned for txs from any address to any address. (The behavior is the same whether or not the strict parameter is passed to the function.)

However, a raw cURL command to the same Zora RPC results in properly filtered results.

Expected Behavior

The PublicClient.getContractEvents should properly filter out events where the from and to args do not match.

Steps To Reproduce

To reproduce this issue, install viem and then execute the following script using Node.js:

const { createPublicClient, http } = require('viem');
const { zora } = require('viem/chains');

const MintAbi = [
  {
    anonymous: false,
    inputs: [
      {
        indexed: true,
        internalType: 'address',
        name: 'from',
        type: 'address',
      },
      {
        indexed: true,
        internalType: 'address',
        name: 'to',
        type: 'address',
      },
      {
        indexed: true,
        internalType: 'uint256',
        name: 'tokenId',
        type: 'uint256',
      },
    ],
    name: 'Transfer',
    type: 'event',
  },
  {
    anonymous: false,
    inputs: [
      {
        indexed: true,
        internalType: 'address',
        name: 'operator',
        type: 'address',
      },
      {
        indexed: true,
        internalType: 'address',
        name: 'from',
        type: 'address',
      },
      {
        indexed: true,
        internalType: 'address',
        name: 'to',
        type: 'address',
      },
      {
        indexed: false,
        internalType: 'uint256',
        name: 'id',
        type: 'uint256',
      },
      {
        indexed: false,
        internalType: 'uint256',
        name: 'value',
        type: 'uint256',
      },
    ],
    name: 'TransferSingle',
    type: 'event',
  },
  {
    anonymous: false,
    inputs: [
      {
        indexed: true,
        internalType: 'address',
        name: 'operator',
        type: 'address',
      },
      {
        indexed: true,
        internalType: 'address',
        name: 'from',
        type: 'address',
      },
      {
        indexed: true,
        internalType: 'address',
        name: 'to',
        type: 'address',
      },
      {
        indexed: false,
        internalType: 'uint256[]',
        name: 'ids',
        type: 'uint256[]',
      },
      {
        indexed: false,
        internalType: 'uint256[]',
        name: 'values',
        type: 'uint256[]',
      },
    ],
    name: 'TransferBatch',
    type: 'event',
  },
];

async function fetchContractEvents() {
  // Create a public client
  const client = createPublicClient({
    chain: zora,
    transport: http('https://rpc.zora.energy')
  });

  // Define parameters
  const fromBlock = 17486764n;
  const toBlock = 17496764n;
  const contractAddress = '0xff6936cee101b9d08ad498db5969a3cb8ff4a77a';
  const fromAddress = '0x0000000000000000000000000000000000000000';
  const toAddress = '0x0A4066534E21dF54331EDcb65A2F41151eD20912';

  try {
    const events = await client.getContractEvents({
      address: contractAddress,
      abi: MintAbi,
      fromBlock,
      toBlock,
      args: {
        from: fromAddress,
        to: toAddress
      },
      strict: true
    });

    console.log('Events:', events);
  } catch (error) {
    console.error('Error fetching events:', error);
  }
}

fetchContractEvents();

To see proper execution (where an empty array of event logs is returned), execute this seemingly equivalent cURL command:

curl https://rpc.zora.energy/ \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "eth_getLogs",
    "params": [{
      "fromBlock": "0x'$(printf '%x' 17486764)'",
      "toBlock": "0x'$(printf '%x' 17496764)'",
      "address": "0xff6936cee101b9d08ad498db5969a3cb8ff4a77a",
      "topics": [
        null,
        "0x0000000000000000000000000000000000000000000000000000000000000000",
        "0x0000000000000000000000000A4066534E21dF54331EDcb65A2F41151eD20912"
      ]
    }]
  }'

Link to Minimal Reproducible Example

No response

Anything else?

jxom commented 2 months ago

good catch! will look into this tomorrow.

veganbeef commented 2 months ago

The above viem code produces the following RPC call, which includes a bunch of extra topic values:

{
  "jsonrpc": "2.0",
  "id": 0,
  "method": "eth_getLogs",
  "params": [
    {
      "address": "0xff6936cee101b9d08ad498db5969a3cb8ff4a77a",
      "topics": [
        [
          "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
          "0x0000000000000000000000000000000000000000000000000000000000000000",
          "0x0000000000000000000000000a4066534e21df54331edcb65a2f41151ed20912",
          null,
          "0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62",
          null,
          "0x0000000000000000000000000000000000000000000000000000000000000000",
          "0x0000000000000000000000000a4066534e21df54331edcb65a2f41151ed20912",
          "0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb",
          null,
          "0x0000000000000000000000000000000000000000000000000000000000000000",
          "0x0000000000000000000000000a4066534e21df54331edcb65a2f41151ed20912"
        ]
      ],
      "fromBlock": "0x10ad3ac",
      "toBlock": "0x10afabc"
    }
  ]
}
jxom commented 2 months ago

Noted! Unfortunately it is not possible with topic sets to filter based on a set of event selectors with their arguments, so the approach we are taking in #2533 is to filter logs via the ABI's event selectors (topic set of event selectors), and then filter logs w/ the matching arguments on the client side.

github-actions[bot] commented 2 months ago

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.