diff --git a/src/Eth.php b/src/Eth.php index 071b202..386a36a 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -35,15 +35,28 @@ class Eth 'eth_blockNumber' => [], 'eth_getBalance' => [ 'params'=> [ - AddressValidator::class, - TagValidator::class + [ + 'validators' => AddressValidator::class, + ], [ + 'default' => 'latest', + 'validators' => [ + TagValidator::class, QuantityValidator::class, + ] + ] ] ], 'eth_getStorageAt' => [ 'params'=> [ - AddressValidator::class, - QuantityValidator::class, - TagValidator::class + [ + 'validators' => AddressValidator::class, + ], [ + 'validators' => QuantityValidator::class, + ], [ + 'default' => 'latest', + 'validators' => [ + TagValidator::class, QuantityValidator::class, + ] + ], ] ], ]; @@ -91,24 +104,43 @@ class Eth } $allowedMethod = $this->methods[$method]; - if (isset($allowedMethod['params'])) { - // validate params - foreach ($allowedMethod['params'] as $key => $rule) { - if (call_user_func([$rule, 'validate'], $arguments[$key]) === false) { - throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); - } - } - } if ($this->provider->isBatch) { - $this->provider->send($method, $arguments, null); + $callback = null; } else { $callback = array_pop($arguments); if (is_callable($callback) !== true) { throw new \InvalidArgumentException('The last param must be callback function.'); } - $this->provider->send($method, $arguments, $callback); } + if (isset($allowedMethod['params']) && is_array($allowedMethod['params'])) { + // validate params + foreach ($allowedMethod['params'] as $key => $param) { + if (isset($param['validators'])) { + if (is_array($param['validators'])) { + foreach ($param['validators'] as $rule) { + if (!isset($arguments[$key]) || call_user_func([$rule, 'validate'], $arguments[$key]) === false) { + if (isset($param['default']) && !isset($arguments[$key])) { + $arguments[$key] = $param['default']; + break; + } else { + throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); + } + } + } + } else { + if (!isset($arguments[$key]) || call_user_func([$param['validators'], 'validate'], $arguments[$key]) === false) { + if (isset($param['default']) && !isset($arguments[$key])) { + $arguments[$key] = $param['default']; + } else { + throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); + } + } + } + } + } + } + $this->provider->send($method, $arguments, $callback); } } diff --git a/src/Validators/TagValidator.php b/src/Validators/TagValidator.php index 17fcf1a..ab436d5 100644 --- a/src/Validators/TagValidator.php +++ b/src/Validators/TagValidator.php @@ -18,7 +18,6 @@ class TagValidator 'latest', 'earliest', 'pending' ]; - // maybe change in_int and preg_match future - return (is_int($value) || preg_match('/^0x[a-fA-f0-9]+/', $value) >= 1 || in_array($value, $tags)); + return in_array($value, $tags); } } \ No newline at end of file diff --git a/src/Web3.php b/src/Web3.php index f458835..383d603 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -84,23 +84,33 @@ class Web3 } $allowedMethod = $this->methods[$method]; + if ($this->provider->isBatch) { + $callback = null; + } else { + $callback = array_pop($arguments); + + if (is_callable($callback) !== true) { + throw new \InvalidArgumentException('The last param must be callback function.'); + } + } if (isset($allowedMethod['params']) && is_array($allowedMethod['params'])) { // validate params foreach ($allowedMethod['params'] as $key => $param) { if (isset($param['validators'])) { if (is_array($param['validators'])) { foreach ($param['validators'] as $rule) { - if (call_user_func([$rule, 'validate'], $arguments[$key]) === false) { + if (!isset($arguments[$key]) || call_user_func([$rule, 'validate'], $arguments[$key]) === false) { if (isset($param['default']) && !isset($arguments[$key])) { $arguments[$key] = $param['default']; + break; } else { throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); } } } } else { - if (call_user_func([$param['validators'], 'validate'], $arguments[$key]) === false) { - if (!isset($param['default']) && !isset($arguments[$key])) { + if (!isset($arguments[$key]) || call_user_func([$param['validators'], 'validate'], $arguments[$key]) === false) { + if (isset($param['default']) && !isset($arguments[$key])) { $arguments[$key] = $param['default']; } else { throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); @@ -110,16 +120,7 @@ class Web3 } } } - if ($this->provider->isBatch) { - $this->provider->send($method, $arguments, null); - } else { - $callback = array_pop($arguments); - - if (is_callable($callback) !== true) { - throw new \InvalidArgumentException('The last param must be callback function.'); - } - $this->provider->send($method, $arguments, $callback); - } + $this->provider->send($method, $arguments, $callback); } } diff --git a/test/unit/EthTest.php b/test/unit/EthTest.php index c2354f7..da45d8a 100644 --- a/test/unit/EthTest.php +++ b/test/unit/EthTest.php @@ -208,7 +208,7 @@ class EthTest extends TestCase { $eth = $this->web3->eth; - $eth->getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', 'latest', function ($err, $balance) { + $eth->getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', function ($err, $balance) { if ($err !== null) { return $this->fail($err->getMessage()); } @@ -229,7 +229,7 @@ class EthTest extends TestCase { $eth = $this->web3->eth; - $eth->getStorageAt('0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x0', 'latest', function ($err, $storage) { + $eth->getStorageAt('0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x0', function ($err, $storage) { if ($err !== null) { return $this->fail($err->getMessage()); }