From f61387ff54b0e09034bcd49f3bea9f1227b44bf6 Mon Sep 17 00:00:00 2001 From: Ravil Date: Tue, 24 Apr 2018 16:42:41 +0300 Subject: [PATCH 1/2] add traceTransaction Method --- src/Debug.php | 180 +++++++++++++++++++++++++ src/Methods/Debug/TraceTransaction.php | 64 +++++++++ 2 files changed, 244 insertions(+) create mode 100644 src/Debug.php create mode 100644 src/Methods/Debug/TraceTransaction.php diff --git a/src/Debug.php b/src/Debug.php new file mode 100644 index 00000000..d4e17c92 --- /dev/null +++ b/src/Debug.php @@ -0,0 +1,180 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3; + +use Web3\Providers\Provider; +use Web3\Providers\HttpProvider; +use Web3\RequestManagers\RequestManager; +use Web3\RequestManagers\HttpRequestManager; + +class Debug +{ + /** + * provider + * + * @var \Web3\Providers\Provider + */ + protected $provider; + + /** + * methods + * + * @var array + */ + private $methods = []; + + /** + * allowedMethods + * + * @var array + */ + private $allowedMethods = [ + 'debug_traceTransaction' + ]; + + /** + * construct + * + * @param string|\Web3\Providers\Provider $provider + * @return void + */ + public function __construct($provider) + { + if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) { + // check the uri schema + if (preg_match('/^https?:\/\//', $provider) === 1) { + $requestManager = new HttpRequestManager($provider, 5); + + $this->provider = new HttpProvider($requestManager); + } + } else if ($provider instanceof Provider) { + $this->provider = $provider; + } + } + + /** + * call + * + * @param string $name + * @param array $arguments + * @return void + */ + public function __call($name, $arguments) + { + if (empty($this->provider)) { + throw new \RuntimeException('Please set provider first.'); + } + + $class = explode('\\', get_class()); + + if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) { + $method = strtolower($class[1]) . '_' . $name; + + if (!in_array($method, $this->allowedMethods)) { + throw new \RuntimeException('Unallowed rpc method: ' . $method); + } + if ($this->provider->isBatch) { + $callback = null; + } else { + $callback = array_pop($arguments); + + if (is_callable($callback) !== true) { + throw new \InvalidArgumentException('The last param must be callback function.'); + } + } + if (!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]; + } + if ($methodObject->validate($arguments)) { + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + $this->provider->send($methodObject, $callback); + } + } + } + + /** + * 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], []); + } + return false; + } + + /** + * set + * + * @param string $name + * @param mixed $value + * @return mixed + */ + public function __set($name, $value) + { + $method = 'set' . ucfirst($name); + + if (method_exists($this, $method)) { + return call_user_func_array([$this, $method], [$value]); + } + return false; + } + + /** + * getProvider + * + * @return \Web3\Providers\Provider + */ + public function getProvider() + { + return $this->provider; + } + + /** + * setProvider + * + * @param \Web3\Providers\Provider $provider + * @return bool + */ + public function setProvider($provider) + { + if ($provider instanceof Provider) { + $this->provider = $provider; + return true; + } + return false; + } + + /** + * batch + * + * @param bool $status + * @return void + */ + public function batch($status) + { + $status = is_bool($status); + + $this->provider->batch($status); + } +} \ No newline at end of file diff --git a/src/Methods/Debug/TraceTransaction.php b/src/Methods/Debug/TraceTransaction.php new file mode 100644 index 00000000..b735a772 --- /dev/null +++ b/src/Methods/Debug/TraceTransaction.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Debug; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\BlockHashValidator; +use Web3\Formatters\HexFormatter; + +class TraceTransaction 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 From 218adfab561c5a464c5e22dc9175cd6b9bd5a9cb Mon Sep 17 00:00:00 2001 From: Ravil Date: Tue, 24 Apr 2018 17:02:57 +0300 Subject: [PATCH 2/2] update composer --- composer.json | 6 +++++- scripts/test.sh | 0 2 files changed, 5 insertions(+), 1 deletion(-) mode change 100755 => 100644 scripts/test.sh diff --git a/composer.json b/composer.json index e5030229..acce3859 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "sc0vu/web3.php", + "name": "ravilushqa/web3.php", "description": "Ethereum web3 interface.", "type": "library", "license": "MIT", @@ -7,6 +7,10 @@ { "name": "sc0Vu", "email": "alk03073135@gmail.com" + }, + { + "name": "ravilushqa", + "email": "galaktionov.r@gmail.com" } ], "require": { diff --git a/scripts/test.sh b/scripts/test.sh old mode 100755 new mode 100644