Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for event log and event handling #55

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Abi.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static function decode(array $params, string $msgData)
}
elseif ($lengthType === 'dynamic') {
// Dynamic length type.
$offsetInChars = 2 * Rlp::getByteValueAtOffsetPos($msgData, $pos);
$offsetInChars = 2 * Rlp::getByteValueAt($msgData, $pos);
$rlpDecoded = Rlp::decode(substr($msgData, $offsetInChars));

if (count($rlpDecoded) === 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/DataType/EthBytes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class EthBytes extends EthD
*/
public function validate($val, array $params)
{
if (!ctype_xdigit($this->removeHexPrefix($val))) {
if (!ctype_xdigit($this->removeHexPrefix($val)) && $this->removeHexPrefix($val) != '') {
throw new \InvalidArgumentException(
'Value of dynamic ABI type is not a valid hex string.'
);
Expand Down
7 changes: 4 additions & 3 deletions src/Ethereum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Exception;
use Ethereum\DataType\EthD;
use Ethereum\DataType\EthD32;
use Ethereum\DataType\EthB;
use Ethereum\EcRecover;
use Ethereum\DataType\FilterChange;

Expand Down Expand Up @@ -286,7 +287,7 @@ private function createReturnValue($value, string $return_type_class, string $me
elseif (!$is_primitive) {

if ($array_val) {
if ($method === 'eth_getFilterChanges') {
if ($method === 'eth_getFilterChanges' || $method === 'eth_getLogs') {
// Only be [FilterChange| D32]
$return = $this->handleFilterChangeValues($value);
}
Expand Down Expand Up @@ -428,7 +429,7 @@ protected static function handleFilterChangeValues(array $values) {

if (substr($type, 0, 1) === '[') {
// param is an array. E.g topics.
$className = '\Ethereum\Datatype\\' . str_replace(['[', ']'], '', $type);
$className = '\Ethereum\DataType\\' . str_replace(['[', ']'], '', $type);
$sub = [];
foreach ($val[$key] as $subVal) {
$sub[] = new $className($subVal);
Expand All @@ -438,7 +439,7 @@ protected static function handleFilterChangeValues(array $values) {
// @todo We'll need to decode the ABI of the values too!
}
else {
$className = '\Ethereum\Datatype\\' . $type;
$className = '\Ethereum\DataType\\' . $type;
$processed[] = isset($val[$key]) ? new $className($val[$key]) : null;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public function decode(FilterChange $filterChange) {

// Removing topic[0]. Topic[1-n] are indexed values.
$indexedValues = array_slice($filterChange->topics, 1);
$offset = 0;

foreach ($this->inputs as $i => $param) {
if ($param->indexed) {
$values[$param->name] = $indexedValues[$i]->convertByAbi($param->type);
$values[$param->name] = $indexedValues[$i - $offset]->convertByAbi($param->type);
}
else {
$abiDecode[] = $param;
$offset++;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/RLP/Rlp.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private static function charLength(string $hex)
return 2 * hexdec($hex);
}

protected static function getByteValueAt(string $msgData, int $pos)
public static function getByteValueAt(string $msgData, int $pos)
{
return self::byteLength(substr($msgData, $pos, 64));
}
Expand Down
11 changes: 8 additions & 3 deletions src/SmartContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ public function __call(string $method, array $args)

/**
* @param \Ethereum\DataType\FilterChange $filterChange
*
* @param boolean $eventHandle
*
* @throws \Exception
*
* @return \Ethereum\EmittedEvent with emitted Data.
*/
public function processLog(FilterChange $filterChange) {
public function processLog(FilterChange $filterChange, $eventHandle = true) {

if ($filterChange->address->hexVal() !== $this->contractAddress) {
return null;
Expand All @@ -114,8 +117,10 @@ public function processLog(FilterChange $filterChange) {
// 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);
if ($eventHandle) {
if (method_exists($this, $event->getHandler())) {
call_user_func([$this, $event->getHandler()], $event);
}
}
return $event;
}
Expand Down