web3p / web3.php

A php interface for interacting with the Ethereum blockchain and ecosystem. Native ABI parsing and smart contract interactions.
MIT License
1.16k stars 543 forks source link

How to get the latest block of the contract? #262

Open Razorholt opened 2 years ago

Razorholt commented 2 years ago

I need to get the latest block because 'latest' as toBlock doesn't work. I tried the code below with $toBlock = 'latest'

$this->eth->getLogs([
  'fromBlock' => (is_int($fromBlock)) ? '0x' . dechex($fromBlock) : $fromBlock,
  'toBlock' => (is_int($toBlock)) ? '0x' . dechex($toBlock) : $toBlock,
  'topics' => [$this->ethabi->encodeEventSignature($this->events[$eventName])],
  'address' => $this->toAddress
],<callback here>);

but it says: fromBlock must be less than or equal to toBlock. It only returns a result when I set fromBlock to 0 and toBlock to 'latest' or when I use 2 specific blocknumbers.

That's why I need to get the latest block.

Thanks!

miguilimzero commented 2 years ago
$this->eth->blockNumber();
Razorholt commented 2 years ago
$this->eth->blockNumber();

Sorry, I meant the latest block of the contract.

sc0Vu commented 2 years ago

Uh, which evm chain did you use? The 'latest' should work because it's in spec.

Razorholt commented 2 years ago

Alright, I changed providers in testnet and it works now, Thanks!