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/Eth.php b/src/Eth.php index e97fad7..ac6f39c 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -41,301 +41,15 @@ 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_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' ]; /** @@ -376,11 +90,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 +102,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); } } 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/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 diff --git a/src/Formatters/Boolean.php b/src/Formatters/BooleanFormatter.php similarity index 92% rename from src/Formatters/Boolean.php rename to src/Formatters/BooleanFormatter.php index 652190e..f6cd328 100644 --- a/src/Formatters/Boolean.php +++ b/src/Formatters/BooleanFormatter.php @@ -15,7 +15,7 @@ use InvalidArgumentException; use Web3\Utils; use Web3\Formatters\IFormatter; -class Boolean implements IFormatter +class BooleanFormatter implements IFormatter { /** * format diff --git a/src/Formatters/HexFormatter.php b/src/Formatters/HexFormatter.php new file mode 100644 index 0000000..03ac30b --- /dev/null +++ b/src/Formatters/HexFormatter.php @@ -0,0 +1,40 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +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 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 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 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 diff --git a/src/Formatters/TransactionFormatter.php b/src/Formatters/TransactionFormatter.php new file mode 100644 index 0000000..f59a7fa --- /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['data']); + } + if (isset($value['nonce'])) { + $value['nonce'] = QuantityFormatter::format($value['nonce']); + } + return $value; + } +} \ No newline at end of file diff --git a/src/Methods/Eth/Accounts.php b/src/Methods/Eth/Accounts.php new file mode 100644 index 0000000..4b75220 --- /dev/null +++ b/src/Methods/Eth/Accounts.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Accounts 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/BlockNumber.php b/src/Methods/Eth/BlockNumber.php new file mode 100644 index 0000000..f402239 --- /dev/null +++ b/src/Methods/Eth/BlockNumber.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class BlockNumber 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/Call.php b/src/Methods/Eth/Call.php new file mode 100644 index 0000000..6a605e5 --- /dev/null +++ b/src/Methods/Eth/Call.php @@ -0,0 +1,71 @@ + + * + * @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\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 + * + * @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..47027b1 --- /dev/null +++ b/src/Methods/Eth/Coinbase.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Coinbase 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/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 new file mode 100644 index 0000000..63b10c7 --- /dev/null +++ b/src/Methods/Eth/EstimateGas.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @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..8bef5d4 --- /dev/null +++ b/src/Methods/Eth/GasPrice.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class GasPrice 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/GetBalance.php b/src/Methods/Eth/GetBalance.php new file mode 100644 index 0000000..793dad4 --- /dev/null +++ b/src/Methods/Eth/GetBalance.php @@ -0,0 +1,71 @@ + + * + * @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\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 + * + * @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..f81eafd --- /dev/null +++ b/src/Methods/Eth/GetBlockByHash.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\BooleanValidator; +use Web3\Formatters\HexFormatter; +use Web3\Formatters\BooleanFormatter; + +class GetBlockByHash extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + BlockHashValidator::class, BooleanValidator::class + ]; + + /** + * 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..fecb5aa --- /dev/null +++ b/src/Methods/Eth/GetBlockByNumber.php @@ -0,0 +1,66 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @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..259e160 --- /dev/null +++ b/src/Methods/Eth/GetBlockTransactionCountByHash.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 GetBlockTransactionCountByHash 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/GetBlockTransactionCountByNumber.php b/src/Methods/Eth/GetBlockTransactionCountByNumber.php new file mode 100644 index 0000000..064ff08 --- /dev/null +++ b/src/Methods/Eth/GetBlockTransactionCountByNumber.php @@ -0,0 +1,66 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @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..35db4aa --- /dev/null +++ b/src/Methods/Eth/GetCode.php @@ -0,0 +1,71 @@ + + * + * @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\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 + * + * @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/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 new file mode 100644 index 0000000..2d6b8da --- /dev/null +++ b/src/Methods/Eth/GetStorageAt.php @@ -0,0 +1,72 @@ + + * + * @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\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 + * + * @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..4aecba9 --- /dev/null +++ b/src/Methods/Eth/GetTransactionByBlockHashAndIndex.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 GetTransactionByBlockHashAndIndex 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/GetTransactionByBlockNumberAndIndex.php b/src/Methods/Eth/GetTransactionByBlockNumberAndIndex.php new file mode 100644 index 0000000..5ec7e81 --- /dev/null +++ b/src/Methods/Eth/GetTransactionByBlockNumberAndIndex.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 GetTransactionByBlockNumberAndIndex 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/GetTransactionByHash.php b/src/Methods/Eth/GetTransactionByHash.php new file mode 100644 index 0000000..8d5c60a --- /dev/null +++ b/src/Methods/Eth/GetTransactionByHash.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 GetTransactionByHash 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/GetTransactionCount.php b/src/Methods/Eth/GetTransactionCount.php new file mode 100644 index 0000000..6c16d7c --- /dev/null +++ b/src/Methods/Eth/GetTransactionCount.php @@ -0,0 +1,71 @@ + + * + * @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\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 + * + * @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/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 new file mode 100644 index 0000000..33da325 --- /dev/null +++ b/src/Methods/Eth/GetUncleCountByBlockHash.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 GetUncleCountByBlockHash 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/GetUncleCountByBlockNumber.php b/src/Methods/Eth/GetUncleCountByBlockNumber.php new file mode 100644 index 0000000..5cb5569 --- /dev/null +++ b/src/Methods/Eth/GetUncleCountByBlockNumber.php @@ -0,0 +1,69 @@ + + * + * @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; + +class GetUncleCountByBlockNumber extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + [ + TagValidator::class, QuantityValidator::class + ] + ]; + + /** + * 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/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 new file mode 100644 index 0000000..a08d6ab --- /dev/null +++ b/src/Methods/Eth/Hashrate.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Hashrate 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/Mining.php b/src/Methods/Eth/Mining.php new file mode 100644 index 0000000..71c22f3 --- /dev/null +++ b/src/Methods/Eth/Mining.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Mining 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/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 new file mode 100644 index 0000000..032739e --- /dev/null +++ b/src/Methods/Eth/ProtocolVersion.php @@ -0,0 +1,61 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\BigNumberFormatter; + +class ProtocolVersion extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = []; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = [ + BigNumberFormatter::class + ]; + + /** + * 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/SendRawTransaction.php b/src/Methods/Eth/SendRawTransaction.php new file mode 100644 index 0000000..8508200 --- /dev/null +++ b/src/Methods/Eth/SendRawTransaction.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @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..990810b --- /dev/null +++ b/src/Methods/Eth/SendTransaction.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @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..0509566 --- /dev/null +++ b/src/Methods/Eth/Sign.php @@ -0,0 +1,66 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @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/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 new file mode 100644 index 0000000..3713320 --- /dev/null +++ b/src/Methods/Eth/Syncing.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Eth; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Syncing 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/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/src/Methods/EthMethod.php b/src/Methods/EthMethod.php new file mode 100644 index 0000000..ca11ce7 --- /dev/null +++ b/src/Methods/EthMethod.php @@ -0,0 +1,170 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @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; + } + + /** + * 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 (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.'); + } + } + return true; + } + + /** + * 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 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/Net/Listening.php b/src/Methods/Net/Listening.php new file mode 100644 index 0000000..8354358 --- /dev/null +++ b/src/Methods/Net/Listening.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Net; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Listening 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/Net/PeerCount.php b/src/Methods/Net/PeerCount.php new file mode 100644 index 0000000..85f6582 --- /dev/null +++ b/src/Methods/Net/PeerCount.php @@ -0,0 +1,61 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Net; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Formatters\BigNumberFormatter; + +class PeerCount extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = []; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = [ + BigNumberFormatter::class + ]; + + /** + * 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/Net/Version.php b/src/Methods/Net/Version.php new file mode 100644 index 0000000..f33f64c --- /dev/null +++ b/src/Methods/Net/Version.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Net; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class Version 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/Personal/ListAccounts.php b/src/Methods/Personal/ListAccounts.php new file mode 100644 index 0000000..a742767 --- /dev/null +++ b/src/Methods/Personal/ListAccounts.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class ListAccounts 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/Personal/NewAccount.php b/src/Methods/Personal/NewAccount.php new file mode 100644 index 0000000..7af5fa6 --- /dev/null +++ b/src/Methods/Personal/NewAccount.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @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/Personal/SendTransaction.php b/src/Methods/Personal/SendTransaction.php new file mode 100644 index 0000000..733f1fb --- /dev/null +++ b/src/Methods/Personal/SendTransaction.php @@ -0,0 +1,66 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @var array + */ + protected $inputFormatters = [ + TransactionFormatter::class, 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/Personal/UnlockAccount.php b/src/Methods/Personal/UnlockAccount.php new file mode 100644 index 0000000..0e2155d --- /dev/null +++ b/src/Methods/Personal/UnlockAccount.php @@ -0,0 +1,70 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @var array + */ + protected $inputFormatters = [ + AddressFormatter::class, StringFormatter::class, QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = [ + 2 => 300 + ]; + + /** + * 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/Web3/ClientVersion.php b/src/Methods/Web3/ClientVersion.php new file mode 100644 index 0000000..c119df1 --- /dev/null +++ b/src/Methods/Web3/ClientVersion.php @@ -0,0 +1,58 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Web3; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; + +class ClientVersion 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/Web3/Sha3.php b/src/Methods/Web3/Sha3.php new file mode 100644 index 0000000..911aa26 --- /dev/null +++ b/src/Methods/Web3/Sha3.php @@ -0,0 +1,64 @@ + + * + * @author Peter Lai + * @license MIT + */ + +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 + * + * @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/Net.php b/src/Net.php index 549e3e1..6b52b2b 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,19 @@ 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->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); } - $this->provider->send($method, $arguments, $callback); } } diff --git a/src/Personal.php b/src/Personal.php index a177b7f..44f09b5 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,19 @@ 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]; + } + if ($methodObject->validate($arguments)) { + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + $this->provider->send($methodObject, $callback); } - $this->provider->send($method, $arguments, $callback); } } diff --git a/src/Providers/HttpProvider.php b/src/Providers/HttpProvider.php index 68f266b..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 * @@ -31,19 +38,31 @@ 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) { + $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->batch[] = json_encode($rpc); + $this->methods[] = $method; + $this->batch[] = $payload; } } @@ -71,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/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/Utils.php b/src/Utils.php index 44e309c..96af4fd 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 static 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. diff --git a/src/Web3.php b/src/Web3.php index e212e00..f659957 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,19 @@ 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->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); } - $this->provider->send($method, $arguments, $callback); } } 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; } /** 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; } /** 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); }); } 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); }); } 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/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()); } 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 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()); } 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