simolus3 / web3dart

Ethereum library, written in Dart.
MIT License
442 stars 270 forks source link

fromBlock and toBlock FilterOptions parameters non-functional for past events #168

Open tidley opened 3 years ago

tidley commented 3 years ago

Past events are not returned from an auto-generated .g.dart file function.

A generated .g.dart file for a smart contract includes the following transferEvents function, which works for live monitoring and prints the requested information when a new event occurs. The auto-generated function:

/// Returns a live stream of all Transfer events emitted by this contract.
  Stream<Transfer> transferEvents(
      {_i1.BlockNum? fromBlock, _i1.BlockNum? toBlock}) {
    final event = self.event('Transfer');
    final filter = _i1.FilterOptions.events(
        contract: self, event: event, fromBlock: fromBlock, toBlock: toBlock);
    return client.events(filter).map((_i1.FilterEvent result) {
      final decoded = event.decodeResults(result.topics!, result.data!);
      return Transfer(decoded);
    });
  }

Above function called using:

var txs = await erc20
      .transferEvents()
      .map((event) => 'data ${event.from}')
      .listen(print);

The issue is when using the fromBlock and toBlock parameters as they don't return data from the requested blocks. This has been tested using:

  final startBlock = new BlockNum.genesis();
  final endBlock = new BlockNum.current();

and by replacing .transferEvents() with: .transferEvents(fromBlock: startBlock, toBlock: endBlock).

The block numbers have also been set to only look at a single block known to contain a transfer transaction, instead of the entire blockchain, yet no data are returned.

simolus3 commented 3 years ago

Thanks for the report! This should be fixed in 213b0d4deba9623fed462e65ffd482bf156a65b8, can you try that out?

tidley commented 3 years ago

Hi, thanks, unfortunately no data are returned when looking at past blocks containing a transaction.

Tested various start and end block values, also .current for endBlock. Still performing as before.

Hanggi commented 2 years ago

I also found that the .events has no filter or from/to.