digitaldonkey/ethereum-php

Add event listening

digitaldonkey opened this issue · 1 comments

We actually have that now technically in the Library.
You can pass any FilterChange with your contract to get the Event data.

SmartContract->processLog(FilterChange $filterChange)

But Even better, related to above I wrote some event listener which allows you to

  • Process blocks from Block 0 to Blocks in the future
  • Use a PHP script to react on any new TX happening onChain

Please check out https://github.com/digitaldonkey/ethereum-php-eventlistener
It has plenty of examples

/**
* @param \Ethereum\DataType\FilterChange $filterChange
* @throws \Exception
*
* @return \Ethereum\EmittedEvent with emitted Data.
*/
public function processLog(FilterChange $filterChange) {
if ($filterChange->address->hexVal() !== $this->contractAddress) {
return null;
}
if (is_array($filterChange->topics)) {
$topic = $filterChange->topics[0]->hexVal();
if (isset($this->events[$topic])) {
$transaction = $this->eth->eth_getTransactionByHash($filterChange->transactionHash);
// We have a relevant event.
$event = new EmittedEvent($this->events[$topic], $filterChange, $transaction);
// Process onEventName handler.
if (method_exists($this, $event->getHandler())) {
call_user_func([$this, $event->getHandler()], $event);
}
return $event;
}
}
return null;
}