[SOLVED] from_block, to_block, from_date, to_date are not working in getContractEvents?

I’m currently working on getting events using getContractEvents. However, I tried to use the filter for real-time monitoring, but I am having a hard time understanding the meaning of the parameters. Can someone give me the exact meaning and examples of the parameters pointed out above?

When you are using from_block, to_block or from_date, to_date parameters, the response from the API response gets narrowed down to the events that are emitted between from_block, to_block or from_date, to_date.

Yes… You are right. That’s what the documentation says too.
But when I tried, these parameters had no effect. Do I have to use pair parameters?

For example, to get all events from block 100001 I set from_block to 100001 and did not use to_block. But in getContractEvents(), it was getting from 100000. Why? what am I doing wrong…

no, it is not compulsory to use both from and to.

Can you share the code or the other params which you are passing, so I can test it from my side.

1 Like

Thanks.

I share with you my way.

The code:

const ABI = {
      anonymous: false,
      inputs: [
        { indexed: false, internalType: "uint256", name: "data", type: "uint256" }
      ],
      name: "blinkEvent",
      type: "event",
};
    
const options = {
      chain: "0x4",
      address: "0xE1Ca572A6eF730a689DD7410ac506db590b226e3",
      topic: "0x1cb6056beb959587a774ae7cd856a3569b7ca72caf1cff8cecba45b801a56e69",
      from_block: 11486221,
      abi: ABI,
};
const events = await Moralis.EvmApi.events.getContractEvents(options);

The Result: (Only what we need…)

[
  {
    transaction_hash: '0x1adf69a73899dd0766551553465986f7ed9297819442753b6c8e1fa9c511c3ad',
    address: '0xe1ca572a6ef730a689dd7410ac506db590b226e3',
    block_timestamp: '2022-10-03T06:48:27.000Z',
    block_number: '11486221',
    block_hash: '0x79b2778f5b1f9906a0f8967548c4424356dc8484c83834dbc665f3a9e83633de',
    data: { data: '10' }
  },
  {
    transaction_hash: '0x4cb11341fbc194053dc6239f9538783395e6ff3b43784dd4733119e9d8ab22d2',
    address: '0xe1ca572a6ef730a689dd7410ac506db590b226e3',
    block_timestamp: '2022-10-03T06:29:24.000Z',
    block_number: '11486146',
    block_hash: '0xed9007bd72fe86dac7a9a0895b318a91c95402dba7ebb0700968353c5bba2baf',
    data: { data: '1' }
  }
]

Can you update param name to camelCase and try again. All the param names in sdk follows camelCase format.

fromBlock: 11486221,

It should return the expected results after updating.

1 Like

Great!

It was resolved. That is, the camelCase format was the reason.
Thank you a lot

1 Like