From e6f7a58d3c0e59f5c878f8e0d1920b11b363c972 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 9 Jan 2018 17:36:03 +0800 Subject: [PATCH 01/36] HexFormatter --- src/Formatters/HexFormatter.php | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Formatters/HexFormatter.php diff --git a/src/Formatters/HexFormatter.php b/src/Formatters/HexFormatter.php new file mode 100644 index 0000000..37a7468 --- /dev/null +++ b/src/Formatters/HexFormatter.php @@ -0,0 +1,41 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; +use Web3\Formatters\Integer as IntegerFormatter; + +class HexFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + $value = Utils::toString($value); + $value = mb_strtolower($value); + + if (Utils::isZeroPrefixed($value)) { + return $value; + } elseif (Utils::isHex($value)) { + $value = '0x' . $value; + } else { + $value = Utils::toHex($value, true); + } + return $value; + } +} \ No newline at end of file From 48d00a26419c866bf0518f02745ab8579c47dbf0 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 9 Jan 2018 17:44:48 +0800 Subject: [PATCH 02/36] JSONRPC method and web3 api methods --- src/Methods/IMethod.php | 24 ++++ src/Methods/IRPC.php | 29 ++++ src/Methods/JSONRPC.php | 212 +++++++++++++++++++++++++++++ src/Methods/Web3/ClientVersion.php | 89 ++++++++++++ src/Methods/Web3/Sha3.php | 91 +++++++++++++ 5 files changed, 445 insertions(+) create mode 100644 src/Methods/IMethod.php create mode 100644 src/Methods/IRPC.php create mode 100644 src/Methods/JSONRPC.php create mode 100644 src/Methods/Web3/ClientVersion.php create mode 100644 src/Methods/Web3/Sha3.php diff --git a/src/Methods/IMethod.php b/src/Methods/IMethod.php new file mode 100644 index 0000000..5c4fd45 --- /dev/null +++ b/src/Methods/IMethod.php @@ -0,0 +1,24 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods; + +interface IMethod +{ + /** + * transform + * + * @param array &$data + * @param array $rules + * @return array + */ + public function transform($data, $rules); +} \ No newline at end of file diff --git a/src/Methods/IRPC.php b/src/Methods/IRPC.php new file mode 100644 index 0000000..e913685 --- /dev/null +++ b/src/Methods/IRPC.php @@ -0,0 +1,29 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods; + +interface IRPC +{ + /** + * __toString + * + * @return array + */ + public function __toString(); + + /** + * toPayload + * + * @return array + */ + public function toPayload(); +} \ No newline at end of file diff --git a/src/Methods/JSONRPC.php b/src/Methods/JSONRPC.php new file mode 100644 index 0000000..3822716 --- /dev/null +++ b/src/Methods/JSONRPC.php @@ -0,0 +1,212 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods; + +use InvalidArgumentException; +use Web3\Methods\IRPC; + +class JSONRPC implements IRPC +{ + /** + * id + * + * @var int + */ + protected $id = 0; + + /** + * rpcVersion + * + * @var string + */ + protected $rpcVersion = '2.0'; + + /** + * method + * + * @var string + */ + protected $method = ''; + + /** + * arguments + * + * @var array + */ + protected $arguments = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + public function __construct($method, $arguments) + { + $this->method = $method; + $this->arguments = $arguments; + } + + /** + * get + * + * @param string $name + * @return mixed + */ + public function __get($name) + { + $method = 'get' . ucfirst($name); + + if (method_exists($this, $method)) { + return call_user_func_array([$this, $method], []); + } + } + + /** + * set + * + * @param string $name + * @param mixed $value + * @return bool + */ + public function __set($name, $value) + { + $method = 'set' . ucfirst($name); + + if (method_exists($this, $method)) { + return call_user_func_array([$this, $method], [$value]); + } + return false; + } + + /** + * __toString + * + * @return array + */ + public function __toString() + { + $payload = $this->toPayload(); + + return json_encode($payload); + } + + /** + * setId + * + * @param int $id + * @return bool + */ + public function setId($id) + { + if (!is_int($id)) { + throw new InvalidArgumentException('Id must be integer.'); + } + $this->id = $id; + + return true; + } + + /** + * getId + * + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * getRpcVersion + * + * @return string + */ + public function getRpcVersion() + { + return $this->rpcVersion; + } + + /** + * getMethod + * + * @return string + */ + public function getMethod() + { + return $this->method; + } + + /** + * setArguments + * + * @param array $arguments + * @return array + */ + public function setArguments($arguments) + { + if (!is_array($arguments)) { + throw new InvalidArgumentException('Please use array when call setArguments.'); + } + $this->arguments = $arguments; + } + + /** + * getArguments + * + * @return array + */ + public function getArguments() + { + return $this->arguments; + } + + /** + * toPayload + * + * @return array + */ + public function toPayload() + { + if (empty($this->method) || !is_string($this->method)) { + throw new InvalidArgumentException('Please check the method set properly.'); + } + if (empty($this->id)) { + $id = rand(); + } else { + $id = $this->id; + } + $rpc = [ + 'id' => $id, + 'jsonrpc' => $this->rpcVersion, + 'method' => $this->method + ]; + + if (count($this->arguments) > 0) { + $rpc['params'] = $this->arguments; + } + return $rpc; + } + + /** + * toPayloadString + * + * @return string + */ + public function toPayloadString() + { + $payload = $this->toPayload(); + + return json_encode($payload); + } +} \ No newline at end of file diff --git a/src/Methods/Web3/ClientVersion.php b/src/Methods/Web3/ClientVersion.php new file mode 100644 index 0000000..d044265 --- /dev/null +++ b/src/Methods/Web3/ClientVersion.php @@ -0,0 +1,89 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Web3; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; + +class ClientVersion extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Web3/Sha3.php b/src/Methods/Web3/Sha3.php new file mode 100644 index 0000000..a1d2fa0 --- /dev/null +++ b/src/Methods/Web3/Sha3.php @@ -0,0 +1,91 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Web3; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; +use Web3\Formatters\HexFormatter; + +class Sha3 extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file From 917f08e74f2e3bb7fa91d3a8aabb62299dba25d9 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 9 Jan 2018 17:45:13 +0800 Subject: [PATCH 03/36] toString() --- src/Utils.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Utils.php b/src/Utils.php index 44e309c..c2a10e4 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -11,6 +11,7 @@ namespace Web3; +use RuntimeException; use InvalidArgumentException; use stdClass; use kornrunner\Keccak; @@ -214,6 +215,22 @@ class Utils return '0x' . $hash; } + /** + * toString + * + * @param mixed $value + * @return string + */ + public function toString($value) + { + try { + $value = (string) $value; + } catch (\Exception $e) { + throw new RuntimeException('Cannot transform value to string!'); + } + return $value; + } + /** * toWei * Change number from unit to wei. From 1a55f03f88ee3535cb0f10713565e7dd6bc9988e Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 9 Jan 2018 17:46:04 +0800 Subject: [PATCH 04/36] Web3 __call() and provider interface. --- src/Providers/HttpProvider.php | 13 ++++--- src/Providers/IProvider.php | 5 ++- src/Web3.php | 66 ++++++++++------------------------ 3 files changed, 26 insertions(+), 58 deletions(-) diff --git a/src/Providers/HttpProvider.php b/src/Providers/HttpProvider.php index 68f266b..5408774 100644 --- a/src/Providers/HttpProvider.php +++ b/src/Providers/HttpProvider.php @@ -31,19 +31,18 @@ class HttpProvider extends Provider implements IProvider /** * send * - * @param string $method - * @param array $arguments + * @param \Web3\Methods\Method $method * @param callable $callback * @return void */ - public function send($method, $arguments, $callback) + public function send($method, $callback) { - $rpc = $this->createRpc($method, $arguments); + $payload = $method->toPayloadString(); - if (!$this->isBatch) { - $this->requestManager->sendPayload(json_encode($rpc), $callback); + if (!$this->isBatch) { + $this->requestManager->sendPayload($payload, $callback); } else { - $this->batch[] = json_encode($rpc); + $this->batch[] = $payload; } } diff --git a/src/Providers/IProvider.php b/src/Providers/IProvider.php index c9751a9..bbc8a7e 100644 --- a/src/Providers/IProvider.php +++ b/src/Providers/IProvider.php @@ -16,12 +16,11 @@ interface IProvider /** * send * - * @param string $method - * @param array $arguments + * @param \Web3\Methods\Method $method * @param callable $callback * @return void */ - public function send($method, $arguments, $callback); + public function send($method, $callback); /** * batch diff --git a/src/Web3.php b/src/Web3.php index e212e00..48464ee 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -63,15 +63,15 @@ class Web3 * * @var array */ - private $methods = [ - 'web3_clientVersion' => [], - 'web3_sha3' => [ - 'params' => [ - [ - 'validators' => HexValidator::class - ] - ] - ] + private $methods = []; + + /** + * allowedMethods + * + * @var array + */ + private $allowedMethods = [ + 'web3_clientVersion', 'web3_sha3' ]; /** @@ -112,11 +112,9 @@ class Web3 if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) { $method = strtolower($class[1]) . '_' . $name; - if (!array_key_exists($method, $this->methods)) { + if (!in_array($method, $this->allowedMethods)) { throw new \RuntimeException('Unallowed rpc method: ' . $method); } - $allowedMethod = $this->methods[$method]; - if ($this->provider->isBatch) { $callback = null; } else { @@ -126,43 +124,15 @@ class Web3 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'])) { - $isError = true; - - foreach ($param['validators'] as $rule) { - if (isset($arguments[$key])) { - if (call_user_func([$rule, 'validate'], $arguments[$key]) === true) { - $isError = false; - break; - } - } else { - if (isset($param['default'])) { - $arguments[$key] = $param['default']; - $isError = false; - break; - } - } - } - if ($isError === true) { - 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 . '.'); - } - } - } - } - } + if (!array_key_exists($method, $this->methods)) { + // new the method + $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name)); + $methodObject = new $methodClass($method, $arguments); } - $this->provider->send($method, $arguments, $callback); + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + + $this->provider->send($methodObject, $callback); } } From 15506cc2b2d7ec80e24eb6f15fa77a989c05f037 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 9 Jan 2018 17:53:16 +0800 Subject: [PATCH 05/36] Net api methods. --- src/Methods/Net/Listening.php | 88 +++++++++++++++++++++++++++++++++++ src/Methods/Net/PeerCount.php | 88 +++++++++++++++++++++++++++++++++++ src/Methods/Net/Version.php | 88 +++++++++++++++++++++++++++++++++++ 3 files changed, 264 insertions(+) create mode 100644 src/Methods/Net/Listening.php create mode 100644 src/Methods/Net/PeerCount.php create mode 100644 src/Methods/Net/Version.php diff --git a/src/Methods/Net/Listening.php b/src/Methods/Net/Listening.php new file mode 100644 index 0000000..94139f3 --- /dev/null +++ b/src/Methods/Net/Listening.php @@ -0,0 +1,88 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Net; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; + +class Listening extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Net/PeerCount.php b/src/Methods/Net/PeerCount.php new file mode 100644 index 0000000..b021576 --- /dev/null +++ b/src/Methods/Net/PeerCount.php @@ -0,0 +1,88 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Net; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; + +class PeerCount extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Net/Version.php b/src/Methods/Net/Version.php new file mode 100644 index 0000000..9abc5ae --- /dev/null +++ b/src/Methods/Net/Version.php @@ -0,0 +1,88 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Net; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; + +class Version extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file From 8ed8d693624f52ecb06151bc8fad8dfc16a15940 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 9 Jan 2018 17:53:53 +0800 Subject: [PATCH 06/36] Net __call. --- src/Methods/Web3/ClientVersion.php | 3 +- src/Net.php | 61 +++++++++--------------------- 2 files changed, 19 insertions(+), 45 deletions(-) diff --git a/src/Methods/Web3/ClientVersion.php b/src/Methods/Web3/ClientVersion.php index d044265..5337223 100644 --- a/src/Methods/Web3/ClientVersion.php +++ b/src/Methods/Web3/ClientVersion.php @@ -22,8 +22,7 @@ class ClientVersion extends JSONRPC implements IMethod * * @var array */ - protected $inputFormatters = [ - ]; + protected $inputFormatters = []; /** * outputFormatters diff --git a/src/Net.php b/src/Net.php index 549e3e1..4ddf6a4 100644 --- a/src/Net.php +++ b/src/Net.php @@ -30,10 +30,15 @@ class Net * * @var array */ - private $methods = [ - 'net_version' => [], - 'net_peerCount' => [], - 'net_listening' => [], + private $methods = []; + + /** + * allowedMethods + * + * @var array + */ + private $allowedMethods = [ + 'net_version', 'net_peerCount', 'net_listening' ]; /** @@ -74,11 +79,9 @@ class Net if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) { $method = strtolower($class[1]) . '_' . $name; - if (!array_key_exists($method, $this->methods)) { + if (!in_array($method, $this->allowedMethods)) { throw new \RuntimeException('Unallowed rpc method: ' . $method); } - $allowedMethod = $this->methods[$method]; - if ($this->provider->isBatch) { $callback = null; } else { @@ -88,43 +91,15 @@ class Net 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'])) { - $isError = true; - - foreach ($param['validators'] as $rule) { - if (isset($arguments[$key])) { - if (call_user_func([$rule, 'validate'], $arguments[$key]) === true) { - $isError = false; - break; - } - } else { - if (isset($param['default'])) { - $arguments[$key] = $param['default']; - $isError = false; - break; - } - } - } - if ($isError === true) { - 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 . '.'); - } - } - } - } - } + if (!array_key_exists($method, $this->methods)) { + // new the method + $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name)); + $methodObject = new $methodClass($method, $arguments); } - $this->provider->send($method, $arguments, $callback); + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + + $this->provider->send($methodObject, $callback); } } From 6c4e422bfda4464d2704d01fa084a26f79165817 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 9 Jan 2018 18:21:18 +0800 Subject: [PATCH 07/36] Catch methods. --- src/Net.php | 3 +++ src/Web3.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/Net.php b/src/Net.php index 4ddf6a4..cdc9290 100644 --- a/src/Net.php +++ b/src/Net.php @@ -95,6 +95,9 @@ class Net // new the method $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name)); $methodObject = new $methodClass($method, $arguments); + $this->methods[$method] = $methodObject; + } else { + $methodObject = $this->methods[$method]; } $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); $methodObject->arguments = $inputs; diff --git a/src/Web3.php b/src/Web3.php index 48464ee..d2b46fb 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -128,6 +128,9 @@ class Web3 // new the method $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name)); $methodObject = new $methodClass($method, $arguments); + $this->methods[$method] = $methodObject; + } else { + $methodObject = $this->methods[$method]; } $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); $methodObject->arguments = $inputs; From 2340db233be7032b31b8fda008f1199e44da8685 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 13:01:06 +0800 Subject: [PATCH 08/36] BigNumberFormatter --- src/Formatters/BigNumberFormatter.php | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Formatters/BigNumberFormatter.php diff --git a/src/Formatters/BigNumberFormatter.php b/src/Formatters/BigNumberFormatter.php new file mode 100644 index 0000000..3c11437 --- /dev/null +++ b/src/Formatters/BigNumberFormatter.php @@ -0,0 +1,33 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +class BigNumberFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + $value = Utils::toString($value); + $bn = Utils::toBn($value); + + return $bn; + } +} \ No newline at end of file From ee8db87fdf8554e8b98b12bede980e91e8d690e0 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 13:03:39 +0800 Subject: [PATCH 09/36] Input formatters for each method. --- src/Methods/Net/PeerCount.php | 5 ++- src/Providers/HttpProvider.php | 68 +++++++++++++++++++++------------- test/unit/NetApiTest.php | 3 +- test/unit/NetBatchTest.php | 3 ++ test/unit/Web3ApiTest.php | 11 +++++- 5 files changed, 61 insertions(+), 29 deletions(-) diff --git a/src/Methods/Net/PeerCount.php b/src/Methods/Net/PeerCount.php index b021576..b638dd5 100644 --- a/src/Methods/Net/PeerCount.php +++ b/src/Methods/Net/PeerCount.php @@ -14,6 +14,7 @@ namespace Web3\Methods\Net; use InvalidArgumentException; use Web3\Methods\IMethod; use Web3\Methods\JSONRPC; +use Web3\Formatters\BigNumberFormatter; class PeerCount extends JSONRPC implements IMethod { @@ -29,7 +30,9 @@ class PeerCount extends JSONRPC implements IMethod * * @var array */ - protected $outputFormatters = []; + protected $outputFormatters = [ + BigNumberFormatter::class + ]; /** * construct diff --git a/src/Providers/HttpProvider.php b/src/Providers/HttpProvider.php index 5408774..b99de55 100644 --- a/src/Providers/HttpProvider.php +++ b/src/Providers/HttpProvider.php @@ -17,6 +17,13 @@ use Web3\RequestManagers\RequestManager; class HttpProvider extends Provider implements IProvider { + /** + * methods + * + * @var array + */ + protected $methods = []; + /** * construct * @@ -39,9 +46,22 @@ class HttpProvider extends Provider implements IProvider { $payload = $method->toPayloadString(); - if (!$this->isBatch) { - $this->requestManager->sendPayload($payload, $callback); + if (!$this->isBatch) { + $proxy = function ($err, $res) use ($method, $callback) { + if ($err !== null) { + return call_user_func($callback, $err, null); + } + if (!is_array($res)) { + $res = $method->transform([$res], $method->outputFormatters); + return call_user_func($callback, null, $res[0]); + } + $res = $method->transform($res, $method->outputFormatters); + + return call_user_func($callback, null, $res); + }; + $this->requestManager->sendPayload($payload, $proxy); } else { + $this->methods[] = $method; $this->batch[] = $payload; } } @@ -70,30 +90,26 @@ class HttpProvider extends Provider implements IProvider if (!$this->isBatch) { throw new \RuntimeException('Please batch json rpc first.'); } - $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $callback); + $methods = $this->methods; + $proxy = function ($err, $res) use ($methods, $callback) { + if ($err !== null) { + return call_user_func($callback, $err, null); + } + foreach ($methods as $key => $method) { + if (isset($res[$key])) { + if (!is_array($res[$key])) { + $transformed = $method->transform([$res[$key]], $method->outputFormatters); + $res[$key] = $transformed[0]; + } else { + $transformed = $method->transform($res[$key], $method->outputFormatters); + $res[$key] = $transformed; + } + } + } + return call_user_func($callback, null, $res); + }; + $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); + $this->methods[] = []; $this->batch = []; } - - /** - * createRpc - * - * @param string $rpc - * @param array $arguments - * @return array - */ - protected function createRpc($rpc, $arguments) - { - $this->id += 1; - - $rpc = [ - 'id' => $this->id, - 'jsonrpc' => $this->rpcVersion, - 'method' => $rpc - ]; - - if (count($arguments) > 0) { - $rpc['params'] = $arguments; - } - return $rpc; - } } \ No newline at end of file diff --git a/test/unit/NetApiTest.php b/test/unit/NetApiTest.php index a7c0e57..3766a3c 100644 --- a/test/unit/NetApiTest.php +++ b/test/unit/NetApiTest.php @@ -4,6 +4,7 @@ namespace Test\Unit; use RuntimeException; use Test\TestCase; +use phpseclib\Math\BigInteger as BigNumber; class NetApiTest extends TestCase { @@ -56,7 +57,7 @@ class NetApiTest extends TestCase if ($err !== null) { return $this->fail($err->getMessage()); } - $this->assertTrue(is_string($count)); + $this->assertTrue($count instanceof BigNumber); }); } diff --git a/test/unit/NetBatchTest.php b/test/unit/NetBatchTest.php index 1f81ab5..f3b58ec 100644 --- a/test/unit/NetBatchTest.php +++ b/test/unit/NetBatchTest.php @@ -4,6 +4,7 @@ namespace Test\Unit; use RuntimeException; use Test\TestCase; +use phpseclib\Math\BigInteger as BigNumber; class NetBatchTest extends TestCase { @@ -38,6 +39,7 @@ class NetBatchTest extends TestCase $net->batch(true); $net->version(); $net->listening(); + $net->peerCount(); $net->provider->execute(function ($err, $data) { if ($err !== null) { @@ -45,6 +47,7 @@ class NetBatchTest extends TestCase } $this->assertTrue(is_string($data[0])); $this->assertTrue(is_bool($data[1])); + $this->assertTrue($data[2] instanceof BigNumber); }); } } \ No newline at end of file diff --git a/test/unit/Web3ApiTest.php b/test/unit/Web3ApiTest.php index 137ec7d..a46f504 100644 --- a/test/unit/Web3ApiTest.php +++ b/test/unit/Web3ApiTest.php @@ -65,6 +65,13 @@ class Web3ApiTest extends TestCase } $this->assertEquals($hash, $this->testHash); }); + + $web3->sha3('hello world', function ($err, $hash) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertEquals($hash, $this->testHash); + }); } /** @@ -88,6 +95,8 @@ class Web3ApiTest extends TestCase /** * testWrongParam + * We transform data and throw invalid argument exception + * instead of runtime exception. * * @return void */ @@ -97,7 +106,7 @@ class Web3ApiTest extends TestCase $web3 = $this->web3; - $web3->sha3('hello world', function ($err, $hash) { + $web3->sha3($web3, function ($err, $hash) { if ($err !== null) { return $this->fail($err->getMessage()); } From a422d877b07168866973cc8aec1da469916d4237 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 15:59:41 +0800 Subject: [PATCH 10/36] Quantity formatter. --- src/Formatters/QuantityFormatter.php | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Formatters/QuantityFormatter.php diff --git a/src/Formatters/QuantityFormatter.php b/src/Formatters/QuantityFormatter.php new file mode 100644 index 0000000..f4c9153 --- /dev/null +++ b/src/Formatters/QuantityFormatter.php @@ -0,0 +1,33 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +class QuantityFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + $value = Utils::toString($value); + $bn = Utils::toBn($value); + + return '0x' . $bn->toHex(true); + } +} \ No newline at end of file From 5288b150f72b8f0675610b33aa28d802d34bcc31 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 15:59:59 +0800 Subject: [PATCH 11/36] String formatter. --- src/Formatters/StringFormatter.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/Formatters/StringFormatter.php diff --git a/src/Formatters/StringFormatter.php b/src/Formatters/StringFormatter.php new file mode 100644 index 0000000..ccd1bdd --- /dev/null +++ b/src/Formatters/StringFormatter.php @@ -0,0 +1,30 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +class StringFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + return Utils::toString($value); + } +} \ No newline at end of file From b8e47737875b7f8acce979291e307254a48df6b1 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 16:00:56 +0800 Subject: [PATCH 12/36] Hex formatter. --- src/Formatters/HexFormatter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Formatters/HexFormatter.php b/src/Formatters/HexFormatter.php index 37a7468..03ac30b 100644 --- a/src/Formatters/HexFormatter.php +++ b/src/Formatters/HexFormatter.php @@ -14,7 +14,6 @@ namespace Web3\Formatters; use InvalidArgumentException; use Web3\Utils; use Web3\Formatters\IFormatter; -use Web3\Formatters\Integer as IntegerFormatter; class HexFormatter implements IFormatter { From b48ac9f97f8f8e7e215bda79e715ddc9eb47b0a1 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 16:23:16 +0800 Subject: [PATCH 13/36] Transaction formatter. --- src/Formatters/TransactionFormatter.php | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/Formatters/TransactionFormatter.php diff --git a/src/Formatters/TransactionFormatter.php b/src/Formatters/TransactionFormatter.php new file mode 100644 index 0000000..49bc9f4 --- /dev/null +++ b/src/Formatters/TransactionFormatter.php @@ -0,0 +1,47 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; +use Web3\Formatters\HexFormatter; +use Web3\Formatters\QuantityFormatter; + +class TransactionFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + if (isset($value['gas'])) { + $value['gas'] = QuantityFormatter::format($value['gas']); + } + if (isset($value['gasPrice'])) { + $value['gasPrice'] = QuantityFormatter::format($value['gasPrice']); + } + if (isset($value['value'])) { + $value['value'] = QuantityFormatter::format($value['value']); + } + if (isset($value['data'])) { + $value['data'] = HexFormatter::format($value['gas']); + } + if (isset($value['nonce'])) { + $value['nonce'] = QuantityFormatter::format($value['nonce']); + } + return $value; + } +} \ No newline at end of file From e680f1adaf28026e014d613a9cc192be74bb64f5 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 16:30:54 +0800 Subject: [PATCH 14/36] Personal apis. --- src/Methods/Personal/ListAccounts.php | 88 +++++++++++++++++ src/Methods/Personal/NewAccount.php | 91 ++++++++++++++++++ src/Methods/Personal/SendTransaction.php | 92 ++++++++++++++++++ src/Methods/Personal/UnlockAccount.php | 116 +++++++++++++++++++++++ src/Personal.php | 90 ++++-------------- test/unit/PersonalApiTest.php | 2 +- 6 files changed, 409 insertions(+), 70 deletions(-) create mode 100644 src/Methods/Personal/ListAccounts.php create mode 100644 src/Methods/Personal/NewAccount.php create mode 100644 src/Methods/Personal/SendTransaction.php create mode 100644 src/Methods/Personal/UnlockAccount.php diff --git a/src/Methods/Personal/ListAccounts.php b/src/Methods/Personal/ListAccounts.php new file mode 100644 index 0000000..09ae40a --- /dev/null +++ b/src/Methods/Personal/ListAccounts.php @@ -0,0 +1,88 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; + +class ListAccounts extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Personal/NewAccount.php b/src/Methods/Personal/NewAccount.php new file mode 100644 index 0000000..3ac77a6 --- /dev/null +++ b/src/Methods/Personal/NewAccount.php @@ -0,0 +1,91 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; +use Web3\Formatters\StringFormatter; + +class NewAccount extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + StringFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Personal/SendTransaction.php b/src/Methods/Personal/SendTransaction.php new file mode 100644 index 0000000..a5a11b7 --- /dev/null +++ b/src/Methods/Personal/SendTransaction.php @@ -0,0 +1,92 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; +use Web3\Formatters\TransactionFormatter; +use Web3\Formatters\StringFormatter; + +class SendTransaction extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + TransactionFormatter::class, StringFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Personal/UnlockAccount.php b/src/Methods/Personal/UnlockAccount.php new file mode 100644 index 0000000..67ab8c7 --- /dev/null +++ b/src/Methods/Personal/UnlockAccount.php @@ -0,0 +1,116 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; +use Web3\Formatters\AddressFormatter; +use Web3\Formatters\StringFormatter; +use Web3\Formatters\QuantityFormatter; + +class UnlockAccount extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + AddressFormatter::class, StringFormatter::class, QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + private $defaultValues = [ + 'personal_unlockAccount' => [ + 2 => 300 + ] + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + if (count($params) < count($rules)) { + if (!isset($this->defaultValues[$this->method])) { + throw new \InvalidArgumentException('The params are less than inputFormatters.'); + } + $defaultValues = $this->defaultValues[$this->method]; + + foreach ($defaultValues as $key => $value) { + if (!isset($params[$key])) { + $params[$key] = $value; + } + } + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Personal.php b/src/Personal.php index a177b7f..5563581 100644 --- a/src/Personal.php +++ b/src/Personal.php @@ -34,36 +34,15 @@ class Personal * * @var array */ - private $methods = [ - 'personal_listAccounts' => [], - 'personal_newAccount' => [ - 'params' => [ - [ - 'validators' => StringValidator::class - ] - ] - ], - 'personal_unlockAccount' => [ - 'params' => [ - [ - 'validators' => AddressValidator::class - ], [ - 'validators' => StringValidator::class - ], [ - 'default' => 300, - 'validators' => QuantityValidator::class - ] - ] - ], - 'personal_sendTransaction' => [ - 'params' => [ - [ - 'validators' => TransactionValidator::class - ], [ - 'validators' => StringValidator::class - ] - ] - ] + private $methods = []; + + /** + * allowedMethods + * + * @var array + */ + private $allowedMethods = [ + 'personal_listAccounts', 'personal_newAccount', 'personal_unlockAccount', 'personal_sendTransaction' ]; /** @@ -104,11 +83,9 @@ class Personal if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) { $method = strtolower($class[1]) . '_' . $name; - if (!array_key_exists($method, $this->methods)) { + if (!in_array($method, $this->allowedMethods)) { throw new \RuntimeException('Unallowed rpc method: ' . $method); } - $allowedMethod = $this->methods[$method]; - if ($this->provider->isBatch) { $callback = null; } else { @@ -118,43 +95,18 @@ class Personal 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'])) { - $isError = true; - - foreach ($param['validators'] as $rule) { - if (isset($arguments[$key])) { - if (call_user_func([$rule, 'validate'], $arguments[$key]) === true) { - $isError = false; - break; - } - } else { - if (isset($param['default'])) { - $arguments[$key] = $param['default']; - $isError = false; - break; - } - } - } - if ($isError === true) { - 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 . '.'); - } - } - } - } - } + if (!array_key_exists($method, $this->methods)) { + // new the method + $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name)); + $methodObject = new $methodClass($method, $arguments); + $this->methods[$method] = $methodObject; + } else { + $methodObject = $this->methods[$method]; } - $this->provider->send($method, $arguments, $callback); + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + + $this->provider->send($methodObject, $callback); } } diff --git a/test/unit/PersonalApiTest.php b/test/unit/PersonalApiTest.php index 616094c..e9a449a 100644 --- a/test/unit/PersonalApiTest.php +++ b/test/unit/PersonalApiTest.php @@ -135,7 +135,7 @@ class PersonalApiTest extends TestCase $personal = $this->personal; - $personal->newAccount(123456, function ($err, $account) { + $personal->newAccount($personal, function ($err, $account) { if ($err !== null) { return $this->fail($err->getMessage()); } From 2baac410deada0544891cdaf523d48e5943e933d Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 16:34:24 +0800 Subject: [PATCH 15/36] Rename address formatter. --- src/Contract.php | 2 +- src/Formatters/{Address.php => AddressFormatter.php} | 2 +- test/unit/AddressFormatterTest.php | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/Formatters/{Address.php => AddressFormatter.php} (94%) diff --git a/src/Contract.php b/src/Contract.php index d3a5e8e..60c179b 100644 --- a/src/Contract.php +++ b/src/Contract.php @@ -27,7 +27,7 @@ use Web3\Contracts\Types\Str; use Web3\Contracts\Types\Uinteger; use Web3\Validators\AddressValidator; use Web3\Validators\HexValidator; -use Web3\Formatters\Address as AddressFormatter; +use Web3\Formatters\AddressFormatter; class Contract { diff --git a/src/Formatters/Address.php b/src/Formatters/AddressFormatter.php similarity index 94% rename from src/Formatters/Address.php rename to src/Formatters/AddressFormatter.php index 0c596b9..4c83107 100644 --- a/src/Formatters/Address.php +++ b/src/Formatters/AddressFormatter.php @@ -16,7 +16,7 @@ use Web3\Utils; use Web3\Formatters\IFormatter; use Web3\Formatters\Integer as IntegerFormatter; -class Address implements IFormatter +class AddressFormatter implements IFormatter { /** * format diff --git a/test/unit/AddressFormatterTest.php b/test/unit/AddressFormatterTest.php index 5b8429e..6bacb9f 100644 --- a/test/unit/AddressFormatterTest.php +++ b/test/unit/AddressFormatterTest.php @@ -3,14 +3,14 @@ namespace Test\Unit; use Test\TestCase; -use Web3\Formatters\Address; +use Web3\Formatters\AddressFormatter; class AddressFormatterTest extends TestCase { /** * formatter * - * @var \Web3\Formatters\Address + * @var \Web3\Formatters\AddressFormatter */ protected $formatter; @@ -22,7 +22,7 @@ class AddressFormatterTest extends TestCase public function setUp() { parent::setUp(); - $this->formatter = new Address; + $this->formatter = new AddressFormatter; } /** From 26b6844f443d3643ee5124155ca2381a25904a29 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 16:46:08 +0800 Subject: [PATCH 16/36] Eth protocol version. --- src/Methods/Eth/ProtocolVersion.php | 91 +++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/Methods/Eth/ProtocolVersion.php diff --git a/src/Methods/Eth/ProtocolVersion.php b/src/Methods/Eth/ProtocolVersion.php new file mode 100644 index 0000000..4d937f0 --- /dev/null +++ b/src/Methods/Eth/ProtocolVersion.php @@ -0,0 +1,91 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; +use Web3\Formatters\BigNumberFormatter; + +class ProtocolVersion extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = [ + BigNumberFormatter::class + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file From df143d7aeefa90e0e7319f59ac015b9f06fb286c Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 16:49:42 +0800 Subject: [PATCH 17/36] Eth syncing. --- src/Methods/Eth/Syncing.php | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/Methods/Eth/Syncing.php diff --git a/src/Methods/Eth/Syncing.php b/src/Methods/Eth/Syncing.php new file mode 100644 index 0000000..6192c4d --- /dev/null +++ b/src/Methods/Eth/Syncing.php @@ -0,0 +1,88 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; + +class Syncing extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file From b9a8bccfb98f0e0a09ff17cebc84d9a5a96e6c5d Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 16:50:05 +0800 Subject: [PATCH 18/36] Eth batch test. --- test/unit/EthBatchTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/EthBatchTest.php b/test/unit/EthBatchTest.php index d6094de..2ab28eb 100644 --- a/test/unit/EthBatchTest.php +++ b/test/unit/EthBatchTest.php @@ -4,6 +4,7 @@ namespace Test\Unit; use RuntimeException; use Test\TestCase; +use phpseclib\Math\BigInteger as BigNumber; class EthBatchTest extends TestCase { @@ -43,7 +44,7 @@ class EthBatchTest extends TestCase if ($err !== null) { return $this->fail('Got error!'); } - $this->assertTrue(is_string($data[0])); + $this->assertTrue($data[0] instanceof BigNumber); $this->assertTrue($data[1] !== null); }); } From 46189b795a26e392d769113e17b705c28123ff0c Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 17:24:42 +0800 Subject: [PATCH 19/36] Optional quantity formatter. --- src/Formatters/OptionalQuantityFormatter.php | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/Formatters/OptionalQuantityFormatter.php diff --git a/src/Formatters/OptionalQuantityFormatter.php b/src/Formatters/OptionalQuantityFormatter.php new file mode 100644 index 0000000..c191755 --- /dev/null +++ b/src/Formatters/OptionalQuantityFormatter.php @@ -0,0 +1,37 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; +use Web3\Validators\TagValidator; + +class OptionalQuantityFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + if (TagValidator::validate($value)) { + return $value; + } + $value = Utils::toString($value); + $bn = Utils::toBn($value); + + return '0x' . $bn->toHex(true); + } +} \ No newline at end of file From fa63012c060089a81b8393b49c7a04ac055be78f Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 17:57:08 +0800 Subject: [PATCH 20/36] EthMethod --- src/Methods/EthMethod.php | 108 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/Methods/EthMethod.php diff --git a/src/Methods/EthMethod.php b/src/Methods/EthMethod.php new file mode 100644 index 0000000..e73e589 --- /dev/null +++ b/src/Methods/EthMethod.php @@ -0,0 +1,108 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; + +class EthMethod extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + if (count($params) < count($rules)) { + if (!isset($this->defaultValues) || empty($this->defaultValues)) { + throw new \InvalidArgumentException('The params are less than inputFormatters.'); + } + $defaultValues = $this->defaultValues; + + foreach ($defaultValues as $key => $value) { + if (!isset($params[$key])) { + $params[$key] = $value; + } + + } + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file From 05a1a638cf402a92e1557007cb0248f9a4a60035 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 17:57:53 +0800 Subject: [PATCH 21/36] Net use EthMethod --- src/Methods/Net/Listening.php | 55 ++++++----------------------------- src/Methods/Net/PeerCount.php | 55 ++++++----------------------------- src/Methods/Net/Version.php | 55 ++++++----------------------------- 3 files changed, 27 insertions(+), 138 deletions(-) diff --git a/src/Methods/Net/Listening.php b/src/Methods/Net/Listening.php index 94139f3..2e9945c 100644 --- a/src/Methods/Net/Listening.php +++ b/src/Methods/Net/Listening.php @@ -12,10 +12,9 @@ namespace Web3\Methods\Net; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; -class Listening extends JSONRPC implements IMethod +class Listening extends EthMethod { /** * inputFormatters @@ -31,6 +30,13 @@ class Listening extends JSONRPC implements IMethod */ protected $outputFormatters = []; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -42,47 +48,4 @@ class Listening extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file diff --git a/src/Methods/Net/PeerCount.php b/src/Methods/Net/PeerCount.php index b638dd5..1983dfe 100644 --- a/src/Methods/Net/PeerCount.php +++ b/src/Methods/Net/PeerCount.php @@ -12,11 +12,10 @@ namespace Web3\Methods\Net; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; use Web3\Formatters\BigNumberFormatter; -class PeerCount extends JSONRPC implements IMethod +class PeerCount extends EthMethod { /** * inputFormatters @@ -34,6 +33,13 @@ class PeerCount extends JSONRPC implements IMethod BigNumberFormatter::class ]; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -45,47 +51,4 @@ class PeerCount extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file diff --git a/src/Methods/Net/Version.php b/src/Methods/Net/Version.php index 9abc5ae..cf34149 100644 --- a/src/Methods/Net/Version.php +++ b/src/Methods/Net/Version.php @@ -12,10 +12,9 @@ namespace Web3\Methods\Net; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; -class Version extends JSONRPC implements IMethod +class Version extends EthMethod { /** * inputFormatters @@ -31,6 +30,13 @@ class Version extends JSONRPC implements IMethod */ protected $outputFormatters = []; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -42,47 +48,4 @@ class Version extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file From ce217225a9554a16865e712b8c9600e56c89f004 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 17:58:05 +0800 Subject: [PATCH 22/36] Web3 use EthMethod --- src/Methods/Web3/ClientVersion.php | 55 +++++------------------------- src/Methods/Web3/Sha3.php | 55 +++++------------------------- 2 files changed, 18 insertions(+), 92 deletions(-) diff --git a/src/Methods/Web3/ClientVersion.php b/src/Methods/Web3/ClientVersion.php index 5337223..90f12fe 100644 --- a/src/Methods/Web3/ClientVersion.php +++ b/src/Methods/Web3/ClientVersion.php @@ -12,10 +12,9 @@ namespace Web3\Methods\Web3; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; -class ClientVersion extends JSONRPC implements IMethod +class ClientVersion extends EthMethod { /** * inputFormatters @@ -31,6 +30,13 @@ class ClientVersion extends JSONRPC implements IMethod */ protected $outputFormatters = []; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -42,47 +48,4 @@ class ClientVersion extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file diff --git a/src/Methods/Web3/Sha3.php b/src/Methods/Web3/Sha3.php index a1d2fa0..c899f89 100644 --- a/src/Methods/Web3/Sha3.php +++ b/src/Methods/Web3/Sha3.php @@ -12,11 +12,10 @@ namespace Web3\Methods\Web3; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; use Web3\Formatters\HexFormatter; -class Sha3 extends JSONRPC implements IMethod +class Sha3 extends EthMethod { /** * inputFormatters @@ -34,6 +33,13 @@ class Sha3 extends JSONRPC implements IMethod */ protected $outputFormatters = []; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -45,47 +51,4 @@ class Sha3 extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file From ba6d50b25bec1514d4c4a9acf43e824d2d6685a6 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 17:58:20 +0800 Subject: [PATCH 23/36] Personal use EthMethod --- src/Methods/Personal/ListAccounts.php | 55 ++++---------------- src/Methods/Personal/NewAccount.php | 55 ++++---------------- src/Methods/Personal/SendTransaction.php | 55 ++++---------------- src/Methods/Personal/UnlockAccount.php | 66 ++---------------------- 4 files changed, 31 insertions(+), 200 deletions(-) diff --git a/src/Methods/Personal/ListAccounts.php b/src/Methods/Personal/ListAccounts.php index 09ae40a..9bc4e15 100644 --- a/src/Methods/Personal/ListAccounts.php +++ b/src/Methods/Personal/ListAccounts.php @@ -12,10 +12,9 @@ namespace Web3\Methods\Personal; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; -class ListAccounts extends JSONRPC implements IMethod +class ListAccounts extends EthMethod { /** * inputFormatters @@ -31,6 +30,13 @@ class ListAccounts extends JSONRPC implements IMethod */ protected $outputFormatters = []; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -42,47 +48,4 @@ class ListAccounts extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file diff --git a/src/Methods/Personal/NewAccount.php b/src/Methods/Personal/NewAccount.php index 3ac77a6..3043276 100644 --- a/src/Methods/Personal/NewAccount.php +++ b/src/Methods/Personal/NewAccount.php @@ -12,11 +12,10 @@ namespace Web3\Methods\Personal; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; use Web3\Formatters\StringFormatter; -class NewAccount extends JSONRPC implements IMethod +class NewAccount extends EthMethod { /** * inputFormatters @@ -34,6 +33,13 @@ class NewAccount extends JSONRPC implements IMethod */ protected $outputFormatters = []; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -45,47 +51,4 @@ class NewAccount extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file diff --git a/src/Methods/Personal/SendTransaction.php b/src/Methods/Personal/SendTransaction.php index a5a11b7..f49c5c8 100644 --- a/src/Methods/Personal/SendTransaction.php +++ b/src/Methods/Personal/SendTransaction.php @@ -12,12 +12,11 @@ namespace Web3\Methods\Personal; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; use Web3\Formatters\TransactionFormatter; use Web3\Formatters\StringFormatter; -class SendTransaction extends JSONRPC implements IMethod +class SendTransaction extends EthMethod { /** * inputFormatters @@ -35,6 +34,13 @@ class SendTransaction extends JSONRPC implements IMethod */ protected $outputFormatters = []; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -46,47 +52,4 @@ class SendTransaction extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file diff --git a/src/Methods/Personal/UnlockAccount.php b/src/Methods/Personal/UnlockAccount.php index 67ab8c7..5a0568c 100644 --- a/src/Methods/Personal/UnlockAccount.php +++ b/src/Methods/Personal/UnlockAccount.php @@ -12,13 +12,12 @@ namespace Web3\Methods\Personal; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; use Web3\Formatters\AddressFormatter; use Web3\Formatters\StringFormatter; use Web3\Formatters\QuantityFormatter; -class UnlockAccount extends JSONRPC implements IMethod +class UnlockAccount extends EthMethod { /** * inputFormatters @@ -41,10 +40,8 @@ class UnlockAccount extends JSONRPC implements IMethod * * @var array */ - private $defaultValues = [ - 'personal_unlockAccount' => [ - 2 => 300 - ] + protected $defaultValues = [ + 2 => 300 ]; /** @@ -58,59 +55,4 @@ class UnlockAccount extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - if (count($params) < count($rules)) { - if (!isset($this->defaultValues[$this->method])) { - throw new \InvalidArgumentException('The params are less than inputFormatters.'); - } - $defaultValues = $this->defaultValues[$this->method]; - - foreach ($defaultValues as $key => $value) { - if (!isset($params[$key])) { - $params[$key] = $value; - } - } - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file From 32b494ea1b59f5c8a37de914e755dc6bf9a6ee20 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 18:21:02 +0800 Subject: [PATCH 24/36] Boolean formatter --- src/Formatters/BooleanFormatter.php | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Formatters/BooleanFormatter.php diff --git a/src/Formatters/BooleanFormatter.php b/src/Formatters/BooleanFormatter.php new file mode 100644 index 0000000..f6cd328 --- /dev/null +++ b/src/Formatters/BooleanFormatter.php @@ -0,0 +1,33 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +class BooleanFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return int + */ + public static function format($value) + { + if (!is_bool($value)) { + throw new InvalidArgumentException('The value to inputFormat function must be boolean.'); + } + return (int) $value; + } +} \ No newline at end of file From 69eb1e87723b59d568587f274dfde0537e2c20cb Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 18:21:22 +0800 Subject: [PATCH 25/36] Eth methods --- src/Methods/Eth/Accounts.php | 51 ++++++++++++++++ src/Methods/Eth/BlockNumber.php | 51 ++++++++++++++++ src/Methods/Eth/Call.php | 57 ++++++++++++++++++ src/Methods/Eth/Coinbase.php | 51 ++++++++++++++++ src/Methods/Eth/EstimateGas.php | 54 +++++++++++++++++ src/Methods/Eth/GasPrice.php | 51 ++++++++++++++++ src/Methods/Eth/GetBalance.php | 57 ++++++++++++++++++ src/Methods/Eth/GetBlockByHash.php | 55 ++++++++++++++++++ src/Methods/Eth/GetBlockByNumber.php | 55 ++++++++++++++++++ .../Eth/GetBlockTransactionCountByHash.php | 54 +++++++++++++++++ .../Eth/GetBlockTransactionCountByNumber.php | 56 ++++++++++++++++++ src/Methods/Eth/GetCode.php | 57 ++++++++++++++++++ src/Methods/Eth/GetStorageAt.php | 58 +++++++++++++++++++ .../Eth/GetTransactionByBlockHashAndIndex.php | 55 ++++++++++++++++++ .../GetTransactionByBlockNumberAndIndex.php | 55 ++++++++++++++++++ src/Methods/Eth/GetTransactionByHash.php | 54 +++++++++++++++++ src/Methods/Eth/GetTransactionCount.php | 57 ++++++++++++++++++ src/Methods/Eth/GetUncleCountByBlockHash.php | 54 +++++++++++++++++ .../Eth/GetUncleCountByBlockNumber.php | 56 ++++++++++++++++++ src/Methods/Eth/Hashrate.php | 51 ++++++++++++++++ src/Methods/Eth/Mining.php | 51 ++++++++++++++++ src/Methods/Eth/ProtocolVersion.php | 55 +++--------------- src/Methods/Eth/SendRawTransaction.php | 54 +++++++++++++++++ src/Methods/Eth/SendTransaction.php | 54 +++++++++++++++++ src/Methods/Eth/Sign.php | 55 ++++++++++++++++++ src/Methods/Eth/Syncing.php | 55 +++--------------- 26 files changed, 1321 insertions(+), 92 deletions(-) create mode 100644 src/Methods/Eth/Accounts.php create mode 100644 src/Methods/Eth/BlockNumber.php create mode 100644 src/Methods/Eth/Call.php create mode 100644 src/Methods/Eth/Coinbase.php create mode 100644 src/Methods/Eth/EstimateGas.php create mode 100644 src/Methods/Eth/GasPrice.php create mode 100644 src/Methods/Eth/GetBalance.php create mode 100644 src/Methods/Eth/GetBlockByHash.php create mode 100644 src/Methods/Eth/GetBlockByNumber.php create mode 100644 src/Methods/Eth/GetBlockTransactionCountByHash.php create mode 100644 src/Methods/Eth/GetBlockTransactionCountByNumber.php create mode 100644 src/Methods/Eth/GetCode.php create mode 100644 src/Methods/Eth/GetStorageAt.php create mode 100644 src/Methods/Eth/GetTransactionByBlockHashAndIndex.php create mode 100644 src/Methods/Eth/GetTransactionByBlockNumberAndIndex.php create mode 100644 src/Methods/Eth/GetTransactionByHash.php create mode 100644 src/Methods/Eth/GetTransactionCount.php create mode 100644 src/Methods/Eth/GetUncleCountByBlockHash.php create mode 100644 src/Methods/Eth/GetUncleCountByBlockNumber.php create mode 100644 src/Methods/Eth/Hashrate.php create mode 100644 src/Methods/Eth/Mining.php create mode 100644 src/Methods/Eth/SendRawTransaction.php create mode 100644 src/Methods/Eth/SendTransaction.php create mode 100644 src/Methods/Eth/Sign.php diff --git a/src/Methods/Eth/Accounts.php b/src/Methods/Eth/Accounts.php new file mode 100644 index 0000000..62b349b --- /dev/null +++ b/src/Methods/Eth/Accounts.php @@ -0,0 +1,51 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Accounts extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/BlockNumber.php b/src/Methods/Eth/BlockNumber.php new file mode 100644 index 0000000..da0db24 --- /dev/null +++ b/src/Methods/Eth/BlockNumber.php @@ -0,0 +1,51 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class BlockNumber extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/Call.php b/src/Methods/Eth/Call.php new file mode 100644 index 0000000..3ec10b1 --- /dev/null +++ b/src/Methods/Eth/Call.php @@ -0,0 +1,57 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\TransactionFormatter; +use Web3\Formatters\OptionalQuantityFormatter; + +class Call extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + TransactionFormatter::class, OptionalQuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = [ + 1 => 'latest' + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/Coinbase.php b/src/Methods/Eth/Coinbase.php new file mode 100644 index 0000000..4ae5512 --- /dev/null +++ b/src/Methods/Eth/Coinbase.php @@ -0,0 +1,51 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Coinbase extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/EstimateGas.php b/src/Methods/Eth/EstimateGas.php new file mode 100644 index 0000000..a11f80e --- /dev/null +++ b/src/Methods/Eth/EstimateGas.php @@ -0,0 +1,54 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\TransactionFormatter; + +class EstimateGas extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + TransactionFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GasPrice.php b/src/Methods/Eth/GasPrice.php new file mode 100644 index 0000000..430877e --- /dev/null +++ b/src/Methods/Eth/GasPrice.php @@ -0,0 +1,51 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class GasPrice extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetBalance.php b/src/Methods/Eth/GetBalance.php new file mode 100644 index 0000000..ba4053a --- /dev/null +++ b/src/Methods/Eth/GetBalance.php @@ -0,0 +1,57 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\AddressFormatter; +use Web3\Formatters\OptionalQuantityFormatter; + +class GetBalance extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + AddressFormatter::class, OptionalQuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = [ + 1 => 'latest' + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetBlockByHash.php b/src/Methods/Eth/GetBlockByHash.php new file mode 100644 index 0000000..1eb9708 --- /dev/null +++ b/src/Methods/Eth/GetBlockByHash.php @@ -0,0 +1,55 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\HexFormatter; +use Web3\Formatters\BooleanFormatter; + +class GetBlockByHash extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class, BooleanFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetBlockByNumber.php b/src/Methods/Eth/GetBlockByNumber.php new file mode 100644 index 0000000..51cdc2d --- /dev/null +++ b/src/Methods/Eth/GetBlockByNumber.php @@ -0,0 +1,55 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\OptionalQuantityFormatter; +use Web3\Formatters\BooleanFormatter; + +class GetBlockByNumber extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + OptionalQuantityFormatter::class, BooleanFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetBlockTransactionCountByHash.php b/src/Methods/Eth/GetBlockTransactionCountByHash.php new file mode 100644 index 0000000..b504bbb --- /dev/null +++ b/src/Methods/Eth/GetBlockTransactionCountByHash.php @@ -0,0 +1,54 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\HexFormatter; + +class GetBlockTransactionCountByHash extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetBlockTransactionCountByNumber.php b/src/Methods/Eth/GetBlockTransactionCountByNumber.php new file mode 100644 index 0000000..b09312e --- /dev/null +++ b/src/Methods/Eth/GetBlockTransactionCountByNumber.php @@ -0,0 +1,56 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\OptionalQuantityFormatter; + +class GetBlockTransactionCountByNumber extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + OptionalQuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = [ + 'latest' + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetCode.php b/src/Methods/Eth/GetCode.php new file mode 100644 index 0000000..425163c --- /dev/null +++ b/src/Methods/Eth/GetCode.php @@ -0,0 +1,57 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\AddressFormatter; +use Web3\Formatters\OptionalQuantityFormatter; + +class GetCode extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + AddressFormatter::class, OptionalQuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = [ + 1 => 'latest' + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetStorageAt.php b/src/Methods/Eth/GetStorageAt.php new file mode 100644 index 0000000..752f7bb --- /dev/null +++ b/src/Methods/Eth/GetStorageAt.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\AddressFormatter; +use Web3\Formatters\QuantityFormatter; +use Web3\Formatters\OptionalQuantityFormatter; + +class GetStorageAt extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + AddressFormatter::class, QuantityFormatter::class, OptionalQuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = [ + 2 => 'latest' + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetTransactionByBlockHashAndIndex.php b/src/Methods/Eth/GetTransactionByBlockHashAndIndex.php new file mode 100644 index 0000000..5d67117 --- /dev/null +++ b/src/Methods/Eth/GetTransactionByBlockHashAndIndex.php @@ -0,0 +1,55 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\HexFormatter; +use Web3\Formatters\QuantityFormatter; + +class GetTransactionByBlockHashAndIndex extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class, QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetTransactionByBlockNumberAndIndex.php b/src/Methods/Eth/GetTransactionByBlockNumberAndIndex.php new file mode 100644 index 0000000..f348948 --- /dev/null +++ b/src/Methods/Eth/GetTransactionByBlockNumberAndIndex.php @@ -0,0 +1,55 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\OptionalQuantityFormatter; +use Web3\Formatters\QuantityFormatter; + +class GetTransactionByBlockNumberAndIndex extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + OptionalQuantityFormatter::class, QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetTransactionByHash.php b/src/Methods/Eth/GetTransactionByHash.php new file mode 100644 index 0000000..7413d31 --- /dev/null +++ b/src/Methods/Eth/GetTransactionByHash.php @@ -0,0 +1,54 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\HexFormatter; + +class GetTransactionByHash extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetTransactionCount.php b/src/Methods/Eth/GetTransactionCount.php new file mode 100644 index 0000000..233d9e0 --- /dev/null +++ b/src/Methods/Eth/GetTransactionCount.php @@ -0,0 +1,57 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\AddressFormatter; +use Web3\Formatters\OptionalQuantityFormatter; + +class GetTransactionCount extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + AddressFormatter::class, OptionalQuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = [ + 1 => 'latest' + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetUncleCountByBlockHash.php b/src/Methods/Eth/GetUncleCountByBlockHash.php new file mode 100644 index 0000000..6cbde85 --- /dev/null +++ b/src/Methods/Eth/GetUncleCountByBlockHash.php @@ -0,0 +1,54 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\HexFormatter; + +class GetUncleCountByBlockHash extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetUncleCountByBlockNumber.php b/src/Methods/Eth/GetUncleCountByBlockNumber.php new file mode 100644 index 0000000..177610f --- /dev/null +++ b/src/Methods/Eth/GetUncleCountByBlockNumber.php @@ -0,0 +1,56 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\OptionalQuantityFormatter; + +class GetUncleCountByBlockNumber extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + OptionalQuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = [ + 'latest' + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/Hashrate.php b/src/Methods/Eth/Hashrate.php new file mode 100644 index 0000000..3d49685 --- /dev/null +++ b/src/Methods/Eth/Hashrate.php @@ -0,0 +1,51 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Hashrate extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/Mining.php b/src/Methods/Eth/Mining.php new file mode 100644 index 0000000..c7688e5 --- /dev/null +++ b/src/Methods/Eth/Mining.php @@ -0,0 +1,51 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Mining extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/ProtocolVersion.php b/src/Methods/Eth/ProtocolVersion.php index 4d937f0..d95c624 100644 --- a/src/Methods/Eth/ProtocolVersion.php +++ b/src/Methods/Eth/ProtocolVersion.php @@ -12,11 +12,10 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; use Web3\Formatters\BigNumberFormatter; -class ProtocolVersion extends JSONRPC implements IMethod +class ProtocolVersion extends EthMethod { /** * inputFormatters @@ -34,6 +33,13 @@ class ProtocolVersion extends JSONRPC implements IMethod BigNumberFormatter::class ]; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -45,47 +51,4 @@ class ProtocolVersion extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file diff --git a/src/Methods/Eth/SendRawTransaction.php b/src/Methods/Eth/SendRawTransaction.php new file mode 100644 index 0000000..9fcb742 --- /dev/null +++ b/src/Methods/Eth/SendRawTransaction.php @@ -0,0 +1,54 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\HexFormatter; + +class SendRawTransaction extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/SendTransaction.php b/src/Methods/Eth/SendTransaction.php new file mode 100644 index 0000000..4bbb576 --- /dev/null +++ b/src/Methods/Eth/SendTransaction.php @@ -0,0 +1,54 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\TransactionFormatter; + +class SendTransaction extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + TransactionFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/Sign.php b/src/Methods/Eth/Sign.php new file mode 100644 index 0000000..dcc3c07 --- /dev/null +++ b/src/Methods/Eth/Sign.php @@ -0,0 +1,55 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\AddressFormatter; +use Web3\Formatters\HexFormatter; + +class Sign extends EthMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + AddressFormatter::class, HexFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/Syncing.php b/src/Methods/Eth/Syncing.php index 6192c4d..0628394 100644 --- a/src/Methods/Eth/Syncing.php +++ b/src/Methods/Eth/Syncing.php @@ -12,10 +12,9 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; -use Web3\Methods\IMethod; -use Web3\Methods\JSONRPC; +use Web3\Methods\EthMethod; -class Syncing extends JSONRPC implements IMethod +class Syncing extends EthMethod { /** * inputFormatters @@ -31,6 +30,13 @@ class Syncing extends JSONRPC implements IMethod */ protected $outputFormatters = []; + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + /** * construct * @@ -42,47 +48,4 @@ class Syncing extends JSONRPC implements IMethod // { // parent::__construct($method, $arguments); // } - - /** - * getInputFormatters - * - * @return array - */ - public function getInputFormatters() - { - return $this->inputFormatters; - } - - /** - * getOutputFormatters - * - * @return array - */ - public function getOutputFormatters() - { - return $this->outputFormatters; - } - - /** - * transform - * - * @param array $params - * @param array $rules - * @return array - */ - public function transform($params, $rules) - { - if (!is_array($params)) { - throw new InvalidArgumentException('Please use array params when call transform.'); - } - if (!is_array($rules)) { - throw new InvalidArgumentException('Please use array rules when call transform.'); - } - foreach ($params as $key => $param) { - if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); - } - } - return $params; - } } \ No newline at end of file From 32bc7949fd31c7e66188a3f5bcd88311c333571e Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 18:21:40 +0800 Subject: [PATCH 26/36] Eth --- src/Eth.php | 476 ++++++++++++++++------------------------------------ 1 file changed, 142 insertions(+), 334 deletions(-) diff --git a/src/Eth.php b/src/Eth.php index e97fad7..1a12b0e 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -41,303 +41,138 @@ class Eth * * @var array */ - private $methods = [ - 'eth_protocolVersion' => [], - 'eth_syncing' => [], - 'eth_coinbase' => [], - 'eth_mining' => [], - 'eth_hashrate' => [], - 'eth_gasPrice' => [], - 'eth_accounts' => [], - 'eth_blockNumber' => [], - 'eth_getBalance' => [ - 'params'=> [ - [ - 'validators' => AddressValidator::class, - ], [ - 'default' => 'latest', - 'validators' => [ - TagValidator::class, QuantityValidator::class, - ] - ] - ] - ], - 'eth_getStorageAt' => [ - 'params'=> [ - [ - 'validators' => AddressValidator::class, - ], [ - 'validators' => QuantityValidator::class, - ], [ - 'default' => 'latest', - 'validators' => [ - TagValidator::class, QuantityValidator::class, - ] - ], - ] - ], - 'eth_getTransactionCount' => [ - 'params'=> [ - [ - 'validators' => AddressValidator::class, - ], [ - 'default' => 'latest', - 'validators' => [ - TagValidator::class, QuantityValidator::class, - ] - ], - ] - ], - 'eth_getBlockTransactionCountByHash' => [ - 'params' => [ - [ - 'validators' => BlockHashValidator::class - ] - ] - ], - 'eth_getBlockTransactionCountByNumber' => [ - 'params' => [ - [ - 'default' => 'latest', - 'validators' => [ - TagValidator::class, QuantityValidator::class, - ] - ] - ] - ], - 'eth_getUncleCountByBlockHash' => [ - 'params' => [ - [ - 'validators' => BlockHashValidator::class - ] - ] - ], - 'eth_getUncleCountByBlockNumber' => [ - 'params' => [ - [ - 'default' => 'latest', - 'validators' => [ - TagValidator::class, QuantityValidator::class, - ] - ] - ] - ], - 'eth_getCode' => [ - 'params'=> [ - [ - 'validators' => AddressValidator::class, - ], [ - 'default' => 'latest', - 'validators' => [ - TagValidator::class, QuantityValidator::class, - ] - ], - ] - ], - 'eth_sign' => [ - 'params'=> [ - [ - 'validators' => AddressValidator::class, - ], [ - 'validators' => HexValidator::class - ], - ] - ], - 'eth_sendTransaction' => [ - 'params' => [ - [ - 'validators' => TransactionValidator::class - ] - ] - ], - 'eth_sendRawTransaction' => [ - 'params' => [ - [ - 'validators' => HexValidator::class - ] - ] - ], - 'eth_call' => [ - 'params' => [ - [ - 'validators' => CallValidator::class - ], [ - 'default' => 'latest', - 'validators' => [ - QuantityValidator::class, TagValidator::class - ] - ] - ] - ], - 'eth_estimateGas' => [ - 'params' => [ - [ - 'validators' => TransactionValidator::class - ] - ] - ], - 'eth_getBlockByHash' => [ - 'params' => [ - [ - 'validators' => BlockHashValidator::class - ], [ - 'validators' => BooleanValidator::class - ] - ] - ], - 'eth_getBlockByNumber' => [ - 'params' => [ - [ - 'validators' => [ - QuantityValidator::class, TagValidator::class - ] - ], [ - 'validators' => BooleanValidator::class - ] - ] - ], - 'eth_getTransactionByHash' => [ - 'params' => [ - [ - 'validators' => BlockHashValidator::class - ] - ] - ], - 'eth_getTransactionByBlockHashAndIndex' => [ - 'params' => [ - [ - 'validators' => BlockHashValidator::class - ], [ - 'validators' => QuantityValidator::class - ] - ] - ], - 'eth_getTransactionByBlockNumberAndIndex' => [ - 'params' => [ - [ - 'validators' => [ - QuantityValidator::class, TagValidator::class - ] - ], [ - 'validators' => QuantityValidator::class - ] - ] - ], - 'eth_getTransactionReceipt' => [ - 'params' => [ - [ - 'validators' => BlockHashValidator::class - ] - ] - ], - 'eth_getUncleByBlockHashAndIndex' => [ - 'params' => [ - [ - 'validators' => BlockHashValidator::class - ], [ - 'validators' => QuantityValidator::class - ] - ] - ], - 'eth_getUncleByBlockNumberAndIndex' => [ - 'params' => [ - [ - 'validators' => [ - QuantityValidator::class, TagValidator::class - ] - ], [ - 'validators' => QuantityValidator::class - ] - ] - ], - 'eth_getCompilers' => [], - 'eth_compileSolidity' => [ - 'params' => [ - [ - 'validators' => StringValidator::class - ] - ] - ], - 'eth_compileLLL' => [ - 'params' => [ - [ - 'validators' => StringValidator::class - ] - ] - ], - 'eth_compileSerpent' => [ - 'params' => [ - [ - 'validators' => StringValidator::class - ] - ] - ], - 'eth_newFilter' => [ - 'params' => [ - [ - 'validators' => FilterValidator::class - ] - ] - ], - 'eth_newBlockFilter' => [ - 'params' => [ - [ - 'validators' => QuantityValidator::class - ] - ] - ], - 'eth_newPendingTransactionFilter' => [], - 'eth_uninstallFilter' => [ - 'params' => [ - [ - 'validators' => QuantityValidator::class - ] - ] - ], - 'eth_getFilterChanges' => [ - 'params' => [ - [ - 'validators' => QuantityValidator::class - ] - ] - ], - 'eth_getFilterLogs' => [ - 'params' => [ - [ - 'validators' => QuantityValidator::class - ] - ] - ], - 'eth_getLogs' => [ - 'params' => [ - [ - 'validators' => FilterValidator::class - ] - ] - ], - 'eth_getWork' => [], - 'eth_submitWork' => [ - 'params' => [ - [ - 'validators' => NonceValidator::class - ], [ - 'validators' => BlockHashValidator::class - ], [ - 'validators' => BlockHashValidator::class - ] - ] - ], - 'eth_submitHashrate' => [ - 'params' => [ - [ - 'validators' => BlockHashValidator::class - ], [ - 'validators' => BlockHashValidator::class - ] - ] - ], + private $methods = []; + + /** + * allowedMethods + * + * @var array + */ + private $allowedMethods = [ + 'eth_protocolVersion', 'eth_syncing', 'eth_coinbase', 'eth_mining', 'eth_hashrate', 'eth_gasPrice', 'eth_accounts', 'eth_blockNumber', 'eth_getBalance', 'eth_getStorageAt', 'eth_getTransactionCount', 'eth_getBlockTransactionCountByHash', 'eth_getBlockTransactionCountByNumber', 'eth_getUncleCountByBlockHash', 'eth_getUncleCountByBlockNumber', 'eth_getCode', 'eth_sign', 'eth_sendTransaction', 'eth_sendRawTransaction', 'eth_call', 'eth_estimateGas', 'eth_getBlockByHash', 'eth_getBlockByNumber', 'eth_getTransactionByHash', 'eth_getTransactionByBlockHashAndIndex', 'eth_getTransactionByBlockNumberAndIndex' ]; + /** + * methods + * + * @var array + */ + // private $methods = [ + // 'eth_getTransactionReceipt' => [ + // 'params' => [ + // [ + // 'validators' => BlockHashValidator::class + // ] + // ] + // ], + // 'eth_getUncleByBlockHashAndIndex' => [ + // 'params' => [ + // [ + // 'validators' => BlockHashValidator::class + // ], [ + // 'validators' => QuantityValidator::class + // ] + // ] + // ], + // 'eth_getUncleByBlockNumberAndIndex' => [ + // 'params' => [ + // [ + // 'validators' => [ + // QuantityValidator::class, TagValidator::class + // ] + // ], [ + // 'validators' => QuantityValidator::class + // ] + // ] + // ], + // 'eth_getCompilers' => [], + // 'eth_compileSolidity' => [ + // 'params' => [ + // [ + // 'validators' => StringValidator::class + // ] + // ] + // ], + // 'eth_compileLLL' => [ + // 'params' => [ + // [ + // 'validators' => StringValidator::class + // ] + // ] + // ], + // 'eth_compileSerpent' => [ + // 'params' => [ + // [ + // 'validators' => StringValidator::class + // ] + // ] + // ], + // 'eth_newFilter' => [ + // 'params' => [ + // [ + // 'validators' => FilterValidator::class + // ] + // ] + // ], + // 'eth_newBlockFilter' => [ + // 'params' => [ + // [ + // 'validators' => QuantityValidator::class + // ] + // ] + // ], + // 'eth_newPendingTransactionFilter' => [], + // 'eth_uninstallFilter' => [ + // 'params' => [ + // [ + // 'validators' => QuantityValidator::class + // ] + // ] + // ], + // 'eth_getFilterChanges' => [ + // 'params' => [ + // [ + // 'validators' => QuantityValidator::class + // ] + // ] + // ], + // 'eth_getFilterLogs' => [ + // 'params' => [ + // [ + // 'validators' => QuantityValidator::class + // ] + // ] + // ], + // 'eth_getLogs' => [ + // 'params' => [ + // [ + // 'validators' => FilterValidator::class + // ] + // ] + // ], + // 'eth_getWork' => [], + // 'eth_submitWork' => [ + // 'params' => [ + // [ + // 'validators' => NonceValidator::class + // ], [ + // 'validators' => BlockHashValidator::class + // ], [ + // 'validators' => BlockHashValidator::class + // ] + // ] + // ], + // 'eth_submitHashrate' => [ + // 'params' => [ + // [ + // 'validators' => BlockHashValidator::class + // ], [ + // 'validators' => BlockHashValidator::class + // ] + // ] + // ], + // ]; + /** * construct * @@ -376,11 +211,9 @@ class Eth if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) { $method = strtolower($class[1]) . '_' . $name; - if (!array_key_exists($method, $this->methods)) { + if (!in_array($method, $this->allowedMethods)) { throw new \RuntimeException('Unallowed rpc method: ' . $method); } - $allowedMethod = $this->methods[$method]; - if ($this->provider->isBatch) { $callback = null; } else { @@ -390,43 +223,18 @@ class Eth 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'])) { - $isError = true; - - foreach ($param['validators'] as $rule) { - if (isset($arguments[$key])) { - if (call_user_func([$rule, 'validate'], $arguments[$key]) === true) { - $isError = false; - break; - } - } else { - if (isset($param['default'])) { - $arguments[$key] = $param['default']; - $isError = false; - break; - } - } - } - if ($isError === true) { - 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 . '.'); - } - } - } - } - } + if (!array_key_exists($method, $this->methods)) { + // new the method + $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name)); + $methodObject = new $methodClass($method, $arguments); + $this->methods[$method] = $methodObject; + } else { + $methodObject = $this->methods[$method]; } - $this->provider->send($method, $arguments, $callback); + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + + $this->provider->send($methodObject, $callback); } } From 2eefb14b6460775419566b0196926deb0d910b96 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 10:49:52 +0800 Subject: [PATCH 27/36] Validators. --- src/Methods/EthMethod.php | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/Methods/EthMethod.php b/src/Methods/EthMethod.php index e73e589..82ad760 100644 --- a/src/Methods/EthMethod.php +++ b/src/Methods/EthMethod.php @@ -12,11 +12,19 @@ namespace Web3\Methods; use InvalidArgumentException; +use RuntimeException; use Web3\Methods\IMethod; use Web3\Methods\JSONRPC; class EthMethod extends JSONRPC implements IMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * @@ -70,6 +78,46 @@ class EthMethod extends JSONRPC implements IMethod return $this->outputFormatters; } + /** + * validate + * + * @param array $params + * @return bool + */ + public function validate($params) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call validate.'); + } + $rules = $this->validators; + + if (count($params) < count($rules)) { + if (!isset($this->defaultValues) || empty($this->defaultValues)) { + throw new InvalidArgumentException('The params are less than validators.'); + } + $defaultValues = $this->defaultValues; + + foreach ($defaultValues as $key => $value) { + if (!isset($params[$key])) { + $params[$key] = $value; + } + + } + } elseif (count($params) > count($rules)) { + throw new InvalidArgumentException('The params are more than validators.'); + } + foreach ($rules as $key => $rule) { + if (isset($params[$key])) { + if (call_user_func([$rule, 'validate'], $params[$key]) === false) { + throw new RuntimeException('Wrong type of ' . $this->method . ' method argument ' . $key . '.'); + } + } else { + throw new RuntimeException($this->method . ' method argument ' . $key . ' doesn\'t have default value.'); + } + } + return true; + } + /** * transform * From e6adbdd303281bfb5d6925464106c2a50ee9813e Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 10:51:25 +0800 Subject: [PATCH 28/36] Web3 apis and test. --- src/Methods/Web3/ClientVersion.php | 7 +++++++ src/Methods/Web3/Sha3.php | 10 ++++++++++ src/Web3.php | 17 +++++++++++++---- test/unit/Web3ApiTest.php | 4 ++-- test/unit/Web3BatchTest.php | 24 ++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/Methods/Web3/ClientVersion.php b/src/Methods/Web3/ClientVersion.php index 90f12fe..c119df1 100644 --- a/src/Methods/Web3/ClientVersion.php +++ b/src/Methods/Web3/ClientVersion.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class ClientVersion extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Web3/Sha3.php b/src/Methods/Web3/Sha3.php index c899f89..911aa26 100644 --- a/src/Methods/Web3/Sha3.php +++ b/src/Methods/Web3/Sha3.php @@ -14,9 +14,19 @@ namespace Web3\Methods\Web3; use InvalidArgumentException; use Web3\Methods\EthMethod; use Web3\Formatters\HexFormatter; +use Web3\Validators\StringValidator; class Sha3 extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + StringValidator::class + ]; + /** * inputFormatters * diff --git a/src/Web3.php b/src/Web3.php index d2b46fb..dcc792c 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -132,10 +132,19 @@ class Web3 } else { $methodObject = $this->methods[$method]; } - $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); - $methodObject->arguments = $inputs; - - $this->provider->send($methodObject, $callback); + try { + if ($methodObject->validate($arguments)) { + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + $this->provider->send($methodObject, $callback); + } + } catch (\Exception $e) { + if (is_callable($callback) === true) { + call_user_func($callback, $e, null); + } else { + throw $e; + } + } } } diff --git a/test/unit/Web3ApiTest.php b/test/unit/Web3ApiTest.php index a46f504..c7c42fb 100644 --- a/test/unit/Web3ApiTest.php +++ b/test/unit/Web3ApiTest.php @@ -102,13 +102,13 @@ class Web3ApiTest extends TestCase */ public function testWrongParam() { - $this->expectException(RuntimeException::class); + // $this->expectException(RuntimeException::class); $web3 = $this->web3; $web3->sha3($web3, function ($err, $hash) { if ($err !== null) { - return $this->fail($err->getMessage()); + return $this->assertTrue($err instanceof RuntimeException); } $this->assertTrue(true); }); diff --git a/test/unit/Web3BatchTest.php b/test/unit/Web3BatchTest.php index c96e93b..5edaf54 100644 --- a/test/unit/Web3BatchTest.php +++ b/test/unit/Web3BatchTest.php @@ -54,4 +54,28 @@ class Web3BatchTest extends TestCase $this->assertEquals($data[1], $this->testHash); }); } + + /** + * testWrongParam + * + * @return void + */ + public function testWrongParam() + { + $this->expectException(RuntimeException::class); + + $web3 = $this->web3; + + $web3->batch(true); + $web3->clientVersion(); + $web3->sha3($web3); + + $web3->provider->execute(function ($err, $data) { + if ($err !== null) { + return $this->fail('Got error!'); + } + $this->assertTrue(is_string($data[0])); + $this->assertEquals($data[1], $this->testHash); + }); + } } \ No newline at end of file From 819608d5101aa3bc222656a5c8b3c9e67f4543ba Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 11:05:25 +0800 Subject: [PATCH 29/36] Net apis. --- src/Methods/Net/Listening.php | 7 +++++++ src/Methods/Net/PeerCount.php | 7 +++++++ src/Methods/Net/Version.php | 7 +++++++ src/Net.php | 9 +++++---- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Methods/Net/Listening.php b/src/Methods/Net/Listening.php index 2e9945c..8354358 100644 --- a/src/Methods/Net/Listening.php +++ b/src/Methods/Net/Listening.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class Listening extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Net/PeerCount.php b/src/Methods/Net/PeerCount.php index 1983dfe..85f6582 100644 --- a/src/Methods/Net/PeerCount.php +++ b/src/Methods/Net/PeerCount.php @@ -17,6 +17,13 @@ use Web3\Formatters\BigNumberFormatter; class PeerCount extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Net/Version.php b/src/Methods/Net/Version.php index cf34149..f33f64c 100644 --- a/src/Methods/Net/Version.php +++ b/src/Methods/Net/Version.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class Version extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Net.php b/src/Net.php index cdc9290..6b52b2b 100644 --- a/src/Net.php +++ b/src/Net.php @@ -99,10 +99,11 @@ class Net } else { $methodObject = $this->methods[$method]; } - $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); - $methodObject->arguments = $inputs; - - $this->provider->send($methodObject, $callback); + if ($methodObject->validate($arguments)) { + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + $this->provider->send($methodObject, $callback); + } } } From 34647e7fdfd410536c113173520b4bf04ccc5467 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 11:07:36 +0800 Subject: [PATCH 30/36] Web3 apis. --- src/Web3.php | 16 ++++------------ test/unit/Web3ApiTest.php | 4 ++-- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/Web3.php b/src/Web3.php index dcc792c..f659957 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -132,18 +132,10 @@ class Web3 } else { $methodObject = $this->methods[$method]; } - try { - if ($methodObject->validate($arguments)) { - $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); - $methodObject->arguments = $inputs; - $this->provider->send($methodObject, $callback); - } - } catch (\Exception $e) { - if (is_callable($callback) === true) { - call_user_func($callback, $e, null); - } else { - throw $e; - } + if ($methodObject->validate($arguments)) { + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + $this->provider->send($methodObject, $callback); } } } diff --git a/test/unit/Web3ApiTest.php b/test/unit/Web3ApiTest.php index c7c42fb..a46f504 100644 --- a/test/unit/Web3ApiTest.php +++ b/test/unit/Web3ApiTest.php @@ -102,13 +102,13 @@ class Web3ApiTest extends TestCase */ public function testWrongParam() { - // $this->expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $web3 = $this->web3; $web3->sha3($web3, function ($err, $hash) { if ($err !== null) { - return $this->assertTrue($err instanceof RuntimeException); + return $this->fail($err->getMessage()); } $this->assertTrue(true); }); From cf5288774cac1060cc2da7660c8db28b609abda0 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 11:11:09 +0800 Subject: [PATCH 31/36] Personal apis. --- src/Methods/Personal/ListAccounts.php | 7 +++++++ src/Methods/Personal/NewAccount.php | 10 ++++++++++ src/Methods/Personal/SendTransaction.php | 11 +++++++++++ src/Methods/Personal/UnlockAccount.php | 12 ++++++++++++ src/Personal.php | 9 +++++---- test/unit/PersonalBatchTest.php | 24 ++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/Methods/Personal/ListAccounts.php b/src/Methods/Personal/ListAccounts.php index 9bc4e15..a742767 100644 --- a/src/Methods/Personal/ListAccounts.php +++ b/src/Methods/Personal/ListAccounts.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class ListAccounts extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Personal/NewAccount.php b/src/Methods/Personal/NewAccount.php index 3043276..7af5fa6 100644 --- a/src/Methods/Personal/NewAccount.php +++ b/src/Methods/Personal/NewAccount.php @@ -13,10 +13,20 @@ namespace Web3\Methods\Personal; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\StringValidator; use Web3\Formatters\StringFormatter; class NewAccount extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + StringValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Personal/SendTransaction.php b/src/Methods/Personal/SendTransaction.php index f49c5c8..733f1fb 100644 --- a/src/Methods/Personal/SendTransaction.php +++ b/src/Methods/Personal/SendTransaction.php @@ -13,11 +13,22 @@ namespace Web3\Methods\Personal; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TransactionValidator; +use Web3\Validators\StringValidator; use Web3\Formatters\TransactionFormatter; use Web3\Formatters\StringFormatter; class SendTransaction extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + TransactionValidator::class, StringValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Personal/UnlockAccount.php b/src/Methods/Personal/UnlockAccount.php index 5a0568c..0e2155d 100644 --- a/src/Methods/Personal/UnlockAccount.php +++ b/src/Methods/Personal/UnlockAccount.php @@ -13,12 +13,24 @@ namespace Web3\Methods\Personal; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\AddressValidator; +use Web3\Validators\StringValidator; +use Web3\Validators\QuantityValidator; use Web3\Formatters\AddressFormatter; use Web3\Formatters\StringFormatter; use Web3\Formatters\QuantityFormatter; class UnlockAccount extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + AddressValidator::class, StringValidator::class, QuantityValidator::class + ]; + /** * inputFormatters * diff --git a/src/Personal.php b/src/Personal.php index 5563581..44f09b5 100644 --- a/src/Personal.php +++ b/src/Personal.php @@ -103,10 +103,11 @@ class Personal } else { $methodObject = $this->methods[$method]; } - $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); - $methodObject->arguments = $inputs; - - $this->provider->send($methodObject, $callback); + if ($methodObject->validate($arguments)) { + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + $this->provider->send($methodObject, $callback); + } } } diff --git a/test/unit/PersonalBatchTest.php b/test/unit/PersonalBatchTest.php index d15dba8..0c21cec 100644 --- a/test/unit/PersonalBatchTest.php +++ b/test/unit/PersonalBatchTest.php @@ -47,4 +47,28 @@ class PersonalBatchTest extends TestCase $this->assertTrue(is_string($data[1])); }); } + + /** + * testWrongParam + * + * @return void + */ + public function testWrongParam() + { + $this->expectException(RuntimeException::class); + + $personal = $this->personal; + + $personal->batch(true); + $personal->listAccounts(); + $personal->newAccount($personal); + + $personal->provider->execute(function ($err, $data) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertTrue(is_string($data[0])); + $this->assertEquals($data[1], $this->testHash); + }); + } } \ No newline at end of file From 1513023aab79eb74122338b467dd77637d82f12d Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 12:11:33 +0800 Subject: [PATCH 32/36] Eth apis. --- src/Eth.php | 123 +----------------- src/Methods/Eth/Accounts.php | 7 + src/Methods/Eth/BlockNumber.php | 7 + src/Methods/Eth/Call.php | 14 ++ src/Methods/Eth/Coinbase.php | 7 + src/Methods/Eth/CompileLLL.php | 64 +++++++++ src/Methods/Eth/CompileSerpent.php | 64 +++++++++ src/Methods/Eth/CompileSolidity.php | 64 +++++++++ src/Methods/Eth/EstimateGas.php | 10 ++ src/Methods/Eth/GasPrice.php | 7 + src/Methods/Eth/GetBalance.php | 14 ++ src/Methods/Eth/GetBlockByHash.php | 11 ++ src/Methods/Eth/GetBlockByNumber.php | 11 ++ .../Eth/GetBlockTransactionCountByHash.php | 10 ++ .../Eth/GetBlockTransactionCountByNumber.php | 10 ++ src/Methods/Eth/GetCode.php | 14 ++ src/Methods/Eth/GetCompilers.php | 58 +++++++++ src/Methods/Eth/GetFilterChanges.php | 64 +++++++++ src/Methods/Eth/GetFilterLogs.php | 64 +++++++++ src/Methods/Eth/GetLogs.php | 61 +++++++++ src/Methods/Eth/GetStorageAt.php | 14 ++ .../Eth/GetTransactionByBlockHashAndIndex.php | 11 ++ .../GetTransactionByBlockNumberAndIndex.php | 13 ++ src/Methods/Eth/GetTransactionByHash.php | 10 ++ src/Methods/Eth/GetTransactionCount.php | 14 ++ src/Methods/Eth/GetTransactionReceipt.php | 64 +++++++++ .../Eth/GetUncleByBlockHashAndIndex.php | 66 ++++++++++ .../Eth/GetUncleByBlockNumberAndIndex.php | 68 ++++++++++ src/Methods/Eth/GetUncleCountByBlockHash.php | 10 ++ .../Eth/GetUncleCountByBlockNumber.php | 13 ++ src/Methods/Eth/GetWork.php | 58 +++++++++ src/Methods/Eth/Hashrate.php | 7 + src/Methods/Eth/Mining.php | 7 + src/Methods/Eth/NewBlockFilter.php | 64 +++++++++ src/Methods/Eth/NewFilter.php | 61 +++++++++ .../Eth/NewPendingTransactionFilter.php | 58 +++++++++ src/Methods/Eth/ProtocolVersion.php | 7 + src/Methods/Eth/SendRawTransaction.php | 10 ++ src/Methods/Eth/SendTransaction.php | 10 ++ src/Methods/Eth/Sign.php | 11 ++ src/Methods/Eth/SubmitHashrate.php | 61 +++++++++ src/Methods/Eth/SubmitWork.php | 65 +++++++++ src/Methods/Eth/Syncing.php | 7 + src/Methods/Eth/UninstallFilter.php | 64 +++++++++ test/unit/EthApiTest.php | 3 +- 45 files changed, 1337 insertions(+), 123 deletions(-) create mode 100644 src/Methods/Eth/CompileLLL.php create mode 100644 src/Methods/Eth/CompileSerpent.php create mode 100644 src/Methods/Eth/CompileSolidity.php create mode 100644 src/Methods/Eth/GetCompilers.php create mode 100644 src/Methods/Eth/GetFilterChanges.php create mode 100644 src/Methods/Eth/GetFilterLogs.php create mode 100644 src/Methods/Eth/GetLogs.php create mode 100644 src/Methods/Eth/GetTransactionReceipt.php create mode 100644 src/Methods/Eth/GetUncleByBlockHashAndIndex.php create mode 100644 src/Methods/Eth/GetUncleByBlockNumberAndIndex.php create mode 100644 src/Methods/Eth/GetWork.php create mode 100644 src/Methods/Eth/NewBlockFilter.php create mode 100644 src/Methods/Eth/NewFilter.php create mode 100644 src/Methods/Eth/NewPendingTransactionFilter.php create mode 100644 src/Methods/Eth/SubmitHashrate.php create mode 100644 src/Methods/Eth/SubmitWork.php create mode 100644 src/Methods/Eth/UninstallFilter.php diff --git a/src/Eth.php b/src/Eth.php index 1a12b0e..ac6f39c 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -49,130 +49,9 @@ class Eth * @var array */ private $allowedMethods = [ - 'eth_protocolVersion', 'eth_syncing', 'eth_coinbase', 'eth_mining', 'eth_hashrate', 'eth_gasPrice', 'eth_accounts', 'eth_blockNumber', 'eth_getBalance', 'eth_getStorageAt', 'eth_getTransactionCount', 'eth_getBlockTransactionCountByHash', 'eth_getBlockTransactionCountByNumber', 'eth_getUncleCountByBlockHash', 'eth_getUncleCountByBlockNumber', 'eth_getCode', 'eth_sign', 'eth_sendTransaction', 'eth_sendRawTransaction', 'eth_call', 'eth_estimateGas', 'eth_getBlockByHash', 'eth_getBlockByNumber', 'eth_getTransactionByHash', 'eth_getTransactionByBlockHashAndIndex', 'eth_getTransactionByBlockNumberAndIndex' + 'eth_protocolVersion', 'eth_syncing', 'eth_coinbase', 'eth_mining', 'eth_hashrate', 'eth_gasPrice', 'eth_accounts', 'eth_blockNumber', 'eth_getBalance', 'eth_getStorageAt', 'eth_getTransactionCount', 'eth_getBlockTransactionCountByHash', 'eth_getBlockTransactionCountByNumber', 'eth_getUncleCountByBlockHash', 'eth_getUncleCountByBlockNumber', 'eth_getUncleByBlockHashAndIndex', 'eth_getUncleByBlockNumberAndIndex', 'eth_getCode', 'eth_sign', 'eth_sendTransaction', 'eth_sendRawTransaction', 'eth_call', 'eth_estimateGas', 'eth_getBlockByHash', 'eth_getBlockByNumber', 'eth_getTransactionByHash', 'eth_getTransactionByBlockHashAndIndex', 'eth_getTransactionByBlockNumberAndIndex', 'eth_getTransactionReceipt', 'eth_getCompilers', 'eth_compileSolidity', 'eth_compileLLL', 'eth_compileSerpent', 'eth_getWork', 'eth_newFilter', 'eth_newBlockFilter', 'eth_newPendingTransactionFilter', 'eth_uninstallFilter', 'eth_getFilterChanges', 'eth_getFilterLogs', 'eth_getLogs', 'eth_submitWork', 'eth_submitHashrate' ]; - /** - * methods - * - * @var array - */ - // private $methods = [ - // 'eth_getTransactionReceipt' => [ - // 'params' => [ - // [ - // 'validators' => BlockHashValidator::class - // ] - // ] - // ], - // 'eth_getUncleByBlockHashAndIndex' => [ - // 'params' => [ - // [ - // 'validators' => BlockHashValidator::class - // ], [ - // 'validators' => QuantityValidator::class - // ] - // ] - // ], - // 'eth_getUncleByBlockNumberAndIndex' => [ - // 'params' => [ - // [ - // 'validators' => [ - // QuantityValidator::class, TagValidator::class - // ] - // ], [ - // 'validators' => QuantityValidator::class - // ] - // ] - // ], - // 'eth_getCompilers' => [], - // 'eth_compileSolidity' => [ - // 'params' => [ - // [ - // 'validators' => StringValidator::class - // ] - // ] - // ], - // 'eth_compileLLL' => [ - // 'params' => [ - // [ - // 'validators' => StringValidator::class - // ] - // ] - // ], - // 'eth_compileSerpent' => [ - // 'params' => [ - // [ - // 'validators' => StringValidator::class - // ] - // ] - // ], - // 'eth_newFilter' => [ - // 'params' => [ - // [ - // 'validators' => FilterValidator::class - // ] - // ] - // ], - // 'eth_newBlockFilter' => [ - // 'params' => [ - // [ - // 'validators' => QuantityValidator::class - // ] - // ] - // ], - // 'eth_newPendingTransactionFilter' => [], - // 'eth_uninstallFilter' => [ - // 'params' => [ - // [ - // 'validators' => QuantityValidator::class - // ] - // ] - // ], - // 'eth_getFilterChanges' => [ - // 'params' => [ - // [ - // 'validators' => QuantityValidator::class - // ] - // ] - // ], - // 'eth_getFilterLogs' => [ - // 'params' => [ - // [ - // 'validators' => QuantityValidator::class - // ] - // ] - // ], - // 'eth_getLogs' => [ - // 'params' => [ - // [ - // 'validators' => FilterValidator::class - // ] - // ] - // ], - // 'eth_getWork' => [], - // 'eth_submitWork' => [ - // 'params' => [ - // [ - // 'validators' => NonceValidator::class - // ], [ - // 'validators' => BlockHashValidator::class - // ], [ - // 'validators' => BlockHashValidator::class - // ] - // ] - // ], - // 'eth_submitHashrate' => [ - // 'params' => [ - // [ - // 'validators' => BlockHashValidator::class - // ], [ - // 'validators' => BlockHashValidator::class - // ] - // ] - // ], - // ]; - /** * construct * diff --git a/src/Methods/Eth/Accounts.php b/src/Methods/Eth/Accounts.php index 62b349b..4b75220 100644 --- a/src/Methods/Eth/Accounts.php +++ b/src/Methods/Eth/Accounts.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class Accounts extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Eth/BlockNumber.php b/src/Methods/Eth/BlockNumber.php index da0db24..f402239 100644 --- a/src/Methods/Eth/BlockNumber.php +++ b/src/Methods/Eth/BlockNumber.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class BlockNumber extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Eth/Call.php b/src/Methods/Eth/Call.php index 3ec10b1..6a605e5 100644 --- a/src/Methods/Eth/Call.php +++ b/src/Methods/Eth/Call.php @@ -13,11 +13,25 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TagValidator; +use Web3\Validators\QuantityValidator; +use Web3\Validators\CallValidator; use Web3\Formatters\TransactionFormatter; use Web3\Formatters\OptionalQuantityFormatter; class Call extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + CallValidator::class, [ + TagValidator::class, QuantityValidator::class + ] + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/Coinbase.php b/src/Methods/Eth/Coinbase.php index 4ae5512..47027b1 100644 --- a/src/Methods/Eth/Coinbase.php +++ b/src/Methods/Eth/Coinbase.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class Coinbase extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Eth/CompileLLL.php b/src/Methods/Eth/CompileLLL.php new file mode 100644 index 0000000..69d33f6 --- /dev/null +++ b/src/Methods/Eth/CompileLLL.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\StringValidator; +use Web3\Formatters\StringFormatter; + +class CompileLLL extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + StringValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + StringFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/CompileSerpent.php b/src/Methods/Eth/CompileSerpent.php new file mode 100644 index 0000000..39b105d --- /dev/null +++ b/src/Methods/Eth/CompileSerpent.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\StringValidator; +use Web3\Formatters\StringFormatter; + +class CompileSerpent extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + StringValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + StringFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/CompileSolidity.php b/src/Methods/Eth/CompileSolidity.php new file mode 100644 index 0000000..05de3c0 --- /dev/null +++ b/src/Methods/Eth/CompileSolidity.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\StringValidator; +use Web3\Formatters\StringFormatter; + +class CompileSolidity extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + StringValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + StringFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/EstimateGas.php b/src/Methods/Eth/EstimateGas.php index a11f80e..63b10c7 100644 --- a/src/Methods/Eth/EstimateGas.php +++ b/src/Methods/Eth/EstimateGas.php @@ -13,10 +13,20 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TransactionValidator; use Web3\Formatters\TransactionFormatter; class EstimateGas extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + TransactionValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GasPrice.php b/src/Methods/Eth/GasPrice.php index 430877e..8bef5d4 100644 --- a/src/Methods/Eth/GasPrice.php +++ b/src/Methods/Eth/GasPrice.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class GasPrice extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetBalance.php b/src/Methods/Eth/GetBalance.php index ba4053a..793dad4 100644 --- a/src/Methods/Eth/GetBalance.php +++ b/src/Methods/Eth/GetBalance.php @@ -13,11 +13,25 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TagValidator; +use Web3\Validators\QuantityValidator; +use Web3\Validators\AddressValidator; use Web3\Formatters\AddressFormatter; use Web3\Formatters\OptionalQuantityFormatter; class GetBalance extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + AddressValidator::class, [ + TagValidator::class, QuantityValidator::class + ] + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetBlockByHash.php b/src/Methods/Eth/GetBlockByHash.php index 1eb9708..f81eafd 100644 --- a/src/Methods/Eth/GetBlockByHash.php +++ b/src/Methods/Eth/GetBlockByHash.php @@ -13,11 +13,22 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\BlockHashValidator; +use Web3\Validators\BooleanValidator; use Web3\Formatters\HexFormatter; use Web3\Formatters\BooleanFormatter; class GetBlockByHash extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + BlockHashValidator::class, BooleanValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetBlockByNumber.php b/src/Methods/Eth/GetBlockByNumber.php index 51cdc2d..fecb5aa 100644 --- a/src/Methods/Eth/GetBlockByNumber.php +++ b/src/Methods/Eth/GetBlockByNumber.php @@ -13,11 +13,22 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\QuantityValidator; +use Web3\Validators\BooleanValidator; use Web3\Formatters\OptionalQuantityFormatter; use Web3\Formatters\BooleanFormatter; class GetBlockByNumber extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + QuantityValidator::class, BooleanValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetBlockTransactionCountByHash.php b/src/Methods/Eth/GetBlockTransactionCountByHash.php index b504bbb..259e160 100644 --- a/src/Methods/Eth/GetBlockTransactionCountByHash.php +++ b/src/Methods/Eth/GetBlockTransactionCountByHash.php @@ -13,10 +13,20 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\BlockHashValidator; use Web3\Formatters\HexFormatter; class GetBlockTransactionCountByHash extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + BlockHashValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetBlockTransactionCountByNumber.php b/src/Methods/Eth/GetBlockTransactionCountByNumber.php index b09312e..064ff08 100644 --- a/src/Methods/Eth/GetBlockTransactionCountByNumber.php +++ b/src/Methods/Eth/GetBlockTransactionCountByNumber.php @@ -13,10 +13,20 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\QuantityValidator; use Web3\Formatters\OptionalQuantityFormatter; class GetBlockTransactionCountByNumber extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + QuantityValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetCode.php b/src/Methods/Eth/GetCode.php index 425163c..35db4aa 100644 --- a/src/Methods/Eth/GetCode.php +++ b/src/Methods/Eth/GetCode.php @@ -13,11 +13,25 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TagValidator; +use Web3\Validators\QuantityValidator; +use Web3\Validators\AddressValidator; use Web3\Formatters\AddressFormatter; use Web3\Formatters\OptionalQuantityFormatter; class GetCode extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + AddressValidator::class, [ + TagValidator::class, QuantityValidator::class + ] + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetCompilers.php b/src/Methods/Eth/GetCompilers.php new file mode 100644 index 0000000..0f6cfb4 --- /dev/null +++ b/src/Methods/Eth/GetCompilers.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class GetCompilers extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = []; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetFilterChanges.php b/src/Methods/Eth/GetFilterChanges.php new file mode 100644 index 0000000..c3534bb --- /dev/null +++ b/src/Methods/Eth/GetFilterChanges.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\QuantityValidator; +use Web3\Formatters\QuantityFormatter; + +class GetFilterChanges extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + QuantityValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetFilterLogs.php b/src/Methods/Eth/GetFilterLogs.php new file mode 100644 index 0000000..71d9de8 --- /dev/null +++ b/src/Methods/Eth/GetFilterLogs.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\QuantityValidator; +use Web3\Formatters\QuantityFormatter; + +class GetFilterLogs extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + QuantityValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetLogs.php b/src/Methods/Eth/GetLogs.php new file mode 100644 index 0000000..a068ebd --- /dev/null +++ b/src/Methods/Eth/GetLogs.php @@ -0,0 +1,61 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\FilterValidator; + +class GetLogs extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + FilterValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetStorageAt.php b/src/Methods/Eth/GetStorageAt.php index 752f7bb..2d6b8da 100644 --- a/src/Methods/Eth/GetStorageAt.php +++ b/src/Methods/Eth/GetStorageAt.php @@ -13,12 +13,26 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TagValidator; +use Web3\Validators\QuantityValidator; +use Web3\Validators\AddressValidator; use Web3\Formatters\AddressFormatter; use Web3\Formatters\QuantityFormatter; use Web3\Formatters\OptionalQuantityFormatter; class GetStorageAt extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + AddressValidator::class, QuantityValidator::class, [ + TagValidator::class, QuantityValidator::class + ] + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetTransactionByBlockHashAndIndex.php b/src/Methods/Eth/GetTransactionByBlockHashAndIndex.php index 5d67117..4aecba9 100644 --- a/src/Methods/Eth/GetTransactionByBlockHashAndIndex.php +++ b/src/Methods/Eth/GetTransactionByBlockHashAndIndex.php @@ -13,11 +13,22 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\BlockHashValidator; +use Web3\Validators\QuantityValidator; use Web3\Formatters\HexFormatter; use Web3\Formatters\QuantityFormatter; class GetTransactionByBlockHashAndIndex extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + BlockHashValidator::class, QuantityValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetTransactionByBlockNumberAndIndex.php b/src/Methods/Eth/GetTransactionByBlockNumberAndIndex.php index f348948..5ec7e81 100644 --- a/src/Methods/Eth/GetTransactionByBlockNumberAndIndex.php +++ b/src/Methods/Eth/GetTransactionByBlockNumberAndIndex.php @@ -13,11 +13,24 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TagValidator; +use Web3\Validators\QuantityValidator; use Web3\Formatters\OptionalQuantityFormatter; use Web3\Formatters\QuantityFormatter; class GetTransactionByBlockNumberAndIndex extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + [ + TagValidator::class, QuantityValidator::class + ], QuantityValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetTransactionByHash.php b/src/Methods/Eth/GetTransactionByHash.php index 7413d31..8d5c60a 100644 --- a/src/Methods/Eth/GetTransactionByHash.php +++ b/src/Methods/Eth/GetTransactionByHash.php @@ -13,10 +13,20 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\BlockHashValidator; use Web3\Formatters\HexFormatter; class GetTransactionByHash extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + BlockHashValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetTransactionCount.php b/src/Methods/Eth/GetTransactionCount.php index 233d9e0..6c16d7c 100644 --- a/src/Methods/Eth/GetTransactionCount.php +++ b/src/Methods/Eth/GetTransactionCount.php @@ -13,11 +13,25 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TagValidator; +use Web3\Validators\QuantityValidator; +use Web3\Validators\AddressValidator; use Web3\Formatters\AddressFormatter; use Web3\Formatters\OptionalQuantityFormatter; class GetTransactionCount extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + AddressValidator::class, [ + TagValidator::class, QuantityValidator::class + ] + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetTransactionReceipt.php b/src/Methods/Eth/GetTransactionReceipt.php new file mode 100644 index 0000000..6cd1fdf --- /dev/null +++ b/src/Methods/Eth/GetTransactionReceipt.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\BlockHashValidator; +use Web3\Formatters\HexFormatter; + +class GetTransactionReceipt extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + BlockHashValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetUncleByBlockHashAndIndex.php b/src/Methods/Eth/GetUncleByBlockHashAndIndex.php new file mode 100644 index 0000000..28c5d6d --- /dev/null +++ b/src/Methods/Eth/GetUncleByBlockHashAndIndex.php @@ -0,0 +1,66 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\BlockHashValidator; +use Web3\Validators\QuantityValidator; +use Web3\Formatters\HexFormatter; +use Web3\Formatters\QuantityFormatter; + +class GetUncleByBlockHashAndIndex extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + BlockHashValidator::class, QuantityValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class, QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetUncleByBlockNumberAndIndex.php b/src/Methods/Eth/GetUncleByBlockNumberAndIndex.php new file mode 100644 index 0000000..1b4e21b --- /dev/null +++ b/src/Methods/Eth/GetUncleByBlockNumberAndIndex.php @@ -0,0 +1,68 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\TagValidator; +use Web3\Validators\QuantityValidator; +use Web3\Formatters\OptionalQuantityFormatter; +use Web3\Formatters\QuantityFormatter; + +class GetUncleByBlockNumberAndIndex extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + [ + TagValidator::class, QuantityValidator::class + ], QuantityValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + OptionalQuantityFormatter::class, QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/GetUncleCountByBlockHash.php b/src/Methods/Eth/GetUncleCountByBlockHash.php index 6cbde85..33da325 100644 --- a/src/Methods/Eth/GetUncleCountByBlockHash.php +++ b/src/Methods/Eth/GetUncleCountByBlockHash.php @@ -13,10 +13,20 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\BlockHashValidator; use Web3\Formatters\HexFormatter; class GetUncleCountByBlockHash extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + BlockHashValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetUncleCountByBlockNumber.php b/src/Methods/Eth/GetUncleCountByBlockNumber.php index 177610f..5cb5569 100644 --- a/src/Methods/Eth/GetUncleCountByBlockNumber.php +++ b/src/Methods/Eth/GetUncleCountByBlockNumber.php @@ -13,10 +13,23 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TagValidator; +use Web3\Validators\QuantityValidator; use Web3\Formatters\OptionalQuantityFormatter; class GetUncleCountByBlockNumber extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + [ + TagValidator::class, QuantityValidator::class + ] + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/GetWork.php b/src/Methods/Eth/GetWork.php new file mode 100644 index 0000000..092edba --- /dev/null +++ b/src/Methods/Eth/GetWork.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class GetWork extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = []; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/Hashrate.php b/src/Methods/Eth/Hashrate.php index 3d49685..a08d6ab 100644 --- a/src/Methods/Eth/Hashrate.php +++ b/src/Methods/Eth/Hashrate.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class Hashrate extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Eth/Mining.php b/src/Methods/Eth/Mining.php index c7688e5..71c22f3 100644 --- a/src/Methods/Eth/Mining.php +++ b/src/Methods/Eth/Mining.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class Mining extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Eth/NewBlockFilter.php b/src/Methods/Eth/NewBlockFilter.php new file mode 100644 index 0000000..863ce2c --- /dev/null +++ b/src/Methods/Eth/NewBlockFilter.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\QuantityValidator; +use Web3\Formatters\QuantityFormatter; + +class NewBlockFilter extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + QuantityValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/NewFilter.php b/src/Methods/Eth/NewFilter.php new file mode 100644 index 0000000..17e3a09 --- /dev/null +++ b/src/Methods/Eth/NewFilter.php @@ -0,0 +1,61 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\FilterValidator; + +class NewFilter extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + FilterValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/NewPendingTransactionFilter.php b/src/Methods/Eth/NewPendingTransactionFilter.php new file mode 100644 index 0000000..29657fe --- /dev/null +++ b/src/Methods/Eth/NewPendingTransactionFilter.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class NewPendingTransactionFilter extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = []; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/ProtocolVersion.php b/src/Methods/Eth/ProtocolVersion.php index d95c624..032739e 100644 --- a/src/Methods/Eth/ProtocolVersion.php +++ b/src/Methods/Eth/ProtocolVersion.php @@ -17,6 +17,13 @@ use Web3\Formatters\BigNumberFormatter; class ProtocolVersion extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Eth/SendRawTransaction.php b/src/Methods/Eth/SendRawTransaction.php index 9fcb742..8508200 100644 --- a/src/Methods/Eth/SendRawTransaction.php +++ b/src/Methods/Eth/SendRawTransaction.php @@ -13,10 +13,20 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\HexValidator; use Web3\Formatters\HexFormatter; class SendRawTransaction extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + HexValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/SendTransaction.php b/src/Methods/Eth/SendTransaction.php index 4bbb576..990810b 100644 --- a/src/Methods/Eth/SendTransaction.php +++ b/src/Methods/Eth/SendTransaction.php @@ -13,10 +13,20 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\TransactionValidator; use Web3\Formatters\TransactionFormatter; class SendTransaction extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + TransactionValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/Sign.php b/src/Methods/Eth/Sign.php index dcc3c07..0509566 100644 --- a/src/Methods/Eth/Sign.php +++ b/src/Methods/Eth/Sign.php @@ -13,11 +13,22 @@ namespace Web3\Methods\Eth; use InvalidArgumentException; use Web3\Methods\EthMethod; +use Web3\Validators\AddressValidator; +use Web3\Validators\HexValidator; use Web3\Formatters\AddressFormatter; use Web3\Formatters\HexFormatter; class Sign extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + AddressValidator::class, HexValidator::class + ]; + /** * inputFormatters * diff --git a/src/Methods/Eth/SubmitHashrate.php b/src/Methods/Eth/SubmitHashrate.php new file mode 100644 index 0000000..b365a19 --- /dev/null +++ b/src/Methods/Eth/SubmitHashrate.php @@ -0,0 +1,61 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\BlockHashValidator; + +class SubmitHashrate extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + BlockHashValidator::class, BlockHashValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/SubmitWork.php b/src/Methods/Eth/SubmitWork.php new file mode 100644 index 0000000..44200cf --- /dev/null +++ b/src/Methods/Eth/SubmitWork.php @@ -0,0 +1,65 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\NonceValidator; +use Web3\Validators\BlockHashValidator; +use Web3\Formatters\QuantityFormatter; + +class SubmitWork extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + NonceValidator::class, BlockHashValidator::class, BlockHashValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Methods/Eth/Syncing.php b/src/Methods/Eth/Syncing.php index 0628394..3713320 100644 --- a/src/Methods/Eth/Syncing.php +++ b/src/Methods/Eth/Syncing.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class Syncing extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Eth/UninstallFilter.php b/src/Methods/Eth/UninstallFilter.php new file mode 100644 index 0000000..a9863ef --- /dev/null +++ b/src/Methods/Eth/UninstallFilter.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\QuantityValidator; +use Web3\Formatters\QuantityFormatter; + +class UninstallFilter extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + QuantityValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/test/unit/EthApiTest.php b/test/unit/EthApiTest.php index 8d4092f..8c1ee63 100644 --- a/test/unit/EthApiTest.php +++ b/test/unit/EthApiTest.php @@ -4,6 +4,7 @@ namespace Test\Unit; use RuntimeException; use Test\TestCase; +use phpseclib\Math\BigInteger as BigNumber; class EthApiTest extends TestCase { @@ -39,7 +40,7 @@ class EthApiTest extends TestCase if ($err !== null) { return $this->fail($err->getMessage()); } - $this->assertTrue(is_string($version)); + $this->assertTrue($version instanceof BigNumber); }); } From 6afc41f65aac50e1b251ac3fcbf86bfef11afd8c Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 12:11:53 +0800 Subject: [PATCH 33/36] Validate array of rules. --- src/Methods/EthMethod.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Methods/EthMethod.php b/src/Methods/EthMethod.php index 82ad760..ca11ce7 100644 --- a/src/Methods/EthMethod.php +++ b/src/Methods/EthMethod.php @@ -108,8 +108,22 @@ class EthMethod extends JSONRPC implements IMethod } foreach ($rules as $key => $rule) { if (isset($params[$key])) { - if (call_user_func([$rule, 'validate'], $params[$key]) === false) { - throw new RuntimeException('Wrong type of ' . $this->method . ' method argument ' . $key . '.'); + if (is_array($rule)) { + $isError = true; + + foreach ($rule as $r) { + if (call_user_func([$rule, 'validate'], $params[$key]) === true) { + $isError = false; + break; + } + } + if ($isError) { + throw new RuntimeException('Wrong type of ' . $this->method . ' method argument ' . $key . '.'); + } + } else { + if (call_user_func([$rule, 'validate'], $params[$key]) === false) { + throw new RuntimeException('Wrong type of ' . $this->method . ' method argument ' . $key . '.'); + } } } else { throw new RuntimeException($this->method . ' method argument ' . $key . ' doesn\'t have default value.'); From 2cbb1fe8603ccc64027afc08899eb8de3361d48a Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 12:16:17 +0800 Subject: [PATCH 34/36] gas to data --- src/Formatters/TransactionFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Formatters/TransactionFormatter.php b/src/Formatters/TransactionFormatter.php index 49bc9f4..f59a7fa 100644 --- a/src/Formatters/TransactionFormatter.php +++ b/src/Formatters/TransactionFormatter.php @@ -37,7 +37,7 @@ class TransactionFormatter implements IFormatter $value['value'] = QuantityFormatter::format($value['value']); } if (isset($value['data'])) { - $value['data'] = HexFormatter::format($value['gas']); + $value['data'] = HexFormatter::format($value['data']); } if (isset($value['nonce'])) { $value['nonce'] = QuantityFormatter::format($value['nonce']); From 4d3ed88b7b92405237abcc4e8db05a68109c7b82 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 12:22:32 +0800 Subject: [PATCH 35/36] Remove boolean formatter. --- src/Formatters/Boolean.php | 33 ------------------------------ test/unit/BooleanFormatterTest.php | 4 ++-- 2 files changed, 2 insertions(+), 35 deletions(-) delete mode 100644 src/Formatters/Boolean.php diff --git a/src/Formatters/Boolean.php b/src/Formatters/Boolean.php deleted file mode 100644 index 652190e..0000000 --- a/src/Formatters/Boolean.php +++ /dev/null @@ -1,33 +0,0 @@ - - * - * @author Peter Lai - * @license MIT - */ - -namespace Web3\Formatters; - -use InvalidArgumentException; -use Web3\Utils; -use Web3\Formatters\IFormatter; - -class Boolean implements IFormatter -{ - /** - * format - * - * @param mixed $value - * @return int - */ - public static function format($value) - { - if (!is_bool($value)) { - throw new InvalidArgumentException('The value to inputFormat function must be boolean.'); - } - return (int) $value; - } -} \ No newline at end of file diff --git a/test/unit/BooleanFormatterTest.php b/test/unit/BooleanFormatterTest.php index f5dff3b..2522092 100644 --- a/test/unit/BooleanFormatterTest.php +++ b/test/unit/BooleanFormatterTest.php @@ -3,7 +3,7 @@ namespace Test\Unit; use Test\TestCase; -use Web3\Formatters\Boolean; +use Web3\Formatters\BooleanFormatter; class BooleanFormatterTest extends TestCase { @@ -22,7 +22,7 @@ class BooleanFormatterTest extends TestCase public function setUp() { parent::setUp(); - $this->formatter = new Boolean; + $this->formatter = new BooleanFormatter; } /** From 4a60041ddec85d819277379db9df8cbc0ecbcca6 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 12:28:44 +0800 Subject: [PATCH 36/36] Fix call function statically error Non-static method Web3\Utils::toString() should not be called statically. --- src/Utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils.php b/src/Utils.php index c2a10e4..96af4fd 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -221,7 +221,7 @@ class Utils * @param mixed $value * @return string */ - public function toString($value) + public static function toString($value) { try { $value = (string) $value;