diff --git a/README.md b/README.md index aa1d5f97..93415199 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,13 @@ Set minimum stability to dev Then ``` -composer require sc0vu/web3.php dev-master +composer require haohetao/web3.php dev-master ``` Or you can add this line in composer.json ``` -"sc0vu/web3.php": "dev-master" +"haohetao/web3.php": "dev-master" ``` @@ -157,6 +157,20 @@ $personal->provider->execute(function ($err, $data) { // do something }); ``` +contract +```php +$contract->batch(true); +foreach ($accounts as $account) { + $contract->call('balanceOf', $account); +} + +$contract->eth->provider->execute(function ($err, $data) use ($accounts) { + if ($err !== null) { + throw new $err; + } + print_r($data); +}); +``` ### Contract diff --git a/composer.json b/composer.json index 69a34631..2ee76ed6 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,12 @@ { - "name": "sc0vu/web3.php", + "name": "haohetao/web3.php", "description": "Ethereum web3 interface.", "type": "library", "license": "MIT", "authors": [ { - "name": "sc0Vu", - "email": "alk03073135@gmail.com" + "name": "hetao", + "email": "haohetao@gmail.com" } ], "require": { diff --git a/src/Contract.php b/src/Contract.php index 87ad1fc4..dd9efabb 100644 --- a/src/Contract.php +++ b/src/Contract.php @@ -2,9 +2,9 @@ /** * This file is part of web3.php package. - * + * * (c) Kuan-Cheng,Lai - * + * * @author Peter Lai * @license MIT */ @@ -44,56 +44,56 @@ class Contract /** * abi - * + * * @var array */ protected $abi; /** * constructor - * + * * @var array */ protected $constructor = []; /** * functions - * + * * @var array */ protected $functions = []; /** * events - * + * * @var array */ protected $events = []; /** * toAddress - * + * * @var string */ protected $toAddress; /** * bytecode - * + * * @var string */ protected $bytecode; /** * eth - * + * * @var \Web3\Eth */ protected $eth; /** * ethabi - * + * * @var \Web3\Contracts\Ethabi */ protected $ethabi; @@ -167,7 +167,7 @@ public function __construct($provider, $abi, $defaultBlock = 'latest') /** * call - * + * * @param string $name * @param array $arguments * @return void @@ -184,7 +184,7 @@ public function __construct($provider, $abi, $defaultBlock = 'latest') /** * get - * + * * @param string $name * @return mixed */ @@ -200,7 +200,7 @@ public function __get($name) /** * set - * + * * @param string $name * @param mixed $value * @return mixed @@ -217,7 +217,7 @@ public function __set($name, $value) /** * getProvider - * + * * @return \Web3\Providers\Provider */ public function getProvider() @@ -241,7 +241,7 @@ public function setProvider($provider) /** * getDefaultBlock - * + * * @return string */ public function getDefaultBlock() @@ -315,7 +315,7 @@ public function getAbi() /** * setAbi - * + * * @param string $abi * @return $this */ @@ -326,7 +326,7 @@ public function setAbi($abi) /** * getEthabi - * + * * @return array */ public function getEthabi() @@ -336,7 +336,7 @@ public function getEthabi() /** * getEth - * + * * @return \Web3\Eth */ public function getEth() @@ -346,7 +346,7 @@ public function getEth() /** * setBytecode - * + * * @param string $bytecode * @return $this */ @@ -357,7 +357,7 @@ public function setBytecode($bytecode) /** * setToAddress - * + * * @param string $bytecode * @return $this */ @@ -368,7 +368,7 @@ public function setToAddress($address) /** * at - * + * * @param string $address * @return $this */ @@ -384,7 +384,7 @@ public function at($address) /** * bytecode - * + * * @param string $bytecode * @return $this */ @@ -400,7 +400,7 @@ public function bytecode($bytecode) /** * abi - * + * * @param string $abi * @return $this */ @@ -439,7 +439,7 @@ public function abi($abi) /** * new * Deploy a contruct with params. - * + * * @param mixed * @return void */ @@ -481,7 +481,7 @@ public function new() /** * send * Send function method. - * + * * @param mixed * @return void */ @@ -490,7 +490,6 @@ public function send() if (isset($this->functions)) { $arguments = func_get_args(); $method = array_splice($arguments, 0, 1)[0]; - $callback = array_pop($arguments); if (!is_string($method)) { throw new InvalidArgumentException('Please make sure the method is string.'); @@ -505,8 +504,17 @@ public function send() if (count($functions) < 1) { throw new InvalidArgumentException('Please make sure the method exists.'); } - if (is_callable($callback) !== true) { +/* if (is_callable($callback) !== true) { throw new \InvalidArgumentException('The last param must be callback function.'); + }*/ + if ($this->eth->provider->isBatch) { + $callback = null; + } else { + $callback = array_pop($arguments); + + if (is_callable($callback) !== true) { + throw new \InvalidArgumentException('The last param must be callback function.'); + } } // check the last one in arguments is transaction object @@ -564,12 +572,16 @@ public function send() $transaction['to'] = $this->toAddress; $transaction['data'] = $functionSignature . Utils::stripZero($data); - $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){ - if ($err !== null) { - return call_user_func($callback, $err, null); - } - return call_user_func($callback, null, $transaction); - }); + if ($this->eth->provider->isBatch) { + $this->eth->sendTransaction($transaction); + } else { + $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){ + if ($err !== null) { + return call_user_func($callback, $err, null); + } + return call_user_func($callback, null, $transaction); + }); + } } } @@ -585,7 +597,6 @@ public function call() if (isset($this->functions)) { $arguments = func_get_args(); $method = array_splice($arguments, 0, 1)[0]; - $callback = array_pop($arguments); if (!is_string($method)) { throw new InvalidArgumentException('Please make sure the method is string.'); @@ -600,8 +611,17 @@ public function call() if (count($functions) < 1) { throw new InvalidArgumentException('Please make sure the method exists.'); } - if (is_callable($callback) !== true) { +/* if (is_callable($callback) !== true) { throw new \InvalidArgumentException('The last param must be callback function.'); + }*/ + if ($this->eth->provider->isBatch) { + $callback = null; + } else { + $callback = array_pop($arguments); + + if (is_callable($callback) !== true) { + throw new \InvalidArgumentException('The last param must be callback function.'); + } } // check the arguments @@ -658,21 +678,25 @@ public function call() $transaction['to'] = $this->toAddress; $transaction['data'] = $functionSignature . Utils::stripZero($data); - $this->eth->call($transaction, $defaultBlock, function ($err, $transaction) use ($callback, $function){ - if ($err !== null) { - return call_user_func($callback, $err, null); - } - $decodedTransaction = $this->ethabi->decodeParameters($function, $transaction); + if ($this->eth->provider->isBatch) { + $this->eth->call($transaction, $defaultBlock); + } else { + $this->eth->call($transaction, $defaultBlock, function ($err, $transaction) use ($callback, $function){ + if ($err !== null) { + return call_user_func($callback, $err, null); + } + $decodedTransaction = $this->ethabi->decodeParameters($function, $transaction); - return call_user_func($callback, null, $decodedTransaction); - }); + return call_user_func($callback, null, $decodedTransaction); + }); + } } } /** * estimateGas * Estimate function gas. - * + * * @param mixed * @return void */ @@ -708,7 +732,7 @@ public function estimateGas() if (!is_string($method)) { throw new InvalidArgumentException('Please make sure the method is string.'); } - + $functions = []; foreach ($this->functions as $function) { if ($function["name"] === $method) { @@ -721,7 +745,7 @@ public function estimateGas() if (is_callable($callback) !== true) { throw new \InvalidArgumentException('The last param must be callback function.'); } - + // check the last one in arguments is transaction object $argsLen = count($arguments); $transaction = []; @@ -794,7 +818,7 @@ public function estimateGas() * 1. Get the funtion data with params. * 2. Sign the data with user private key. * 3. Call sendRawTransaction. - * + * * @param mixed * @return void */ @@ -822,7 +846,7 @@ public function getData() if (!is_string($method)) { throw new InvalidArgumentException('Please make sure the method is string.'); } - + $functions = []; foreach ($this->functions as $function) { if ($function["name"] === $method) { @@ -832,7 +856,7 @@ public function getData() if (count($functions) < 1) { throw new InvalidArgumentException('Please make sure the method exists.'); } - + $params = $arguments; $data = ""; $functionName = ""; @@ -857,4 +881,14 @@ public function getData() return $functionData; } } + /** + * batch + * + * @param bool $status + * @return void + */ + public function batch($status) + { + $this->eth->batch($status); + } } diff --git a/src/Eth.php b/src/Eth.php index 78d97989..32d3eb87 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -173,8 +173,6 @@ public function setProvider($provider) */ public function batch($status) { - $status = is_bool($status); - $this->provider->batch($status); } -} \ No newline at end of file +} diff --git a/src/Formatters/PrivateFormatter.php b/src/Formatters/PrivateFormatter.php new file mode 100644 index 00000000..4420f58e --- /dev/null +++ b/src/Formatters/PrivateFormatter.php @@ -0,0 +1,36 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +class PrivateFormatter 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)) { + $value = Utils::stripZero($value); + } + return $value; + } +} diff --git a/src/Methods/EthMethod.php b/src/Methods/EthMethod.php index 5240eb75..ea588be4 100644 --- a/src/Methods/EthMethod.php +++ b/src/Methods/EthMethod.php @@ -2,9 +2,9 @@ /** * This file is part of web3.php package. - * + * * (c) Kuan-Cheng,Lai - * + * * @author Peter Lai * @license MIT */ @@ -20,35 +20,35 @@ 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 @@ -60,7 +60,7 @@ class EthMethod extends JSONRPC implements IMethod /** * getInputFormatters - * + * * @return array */ public function getInputFormatters() @@ -70,7 +70,7 @@ public function getInputFormatters() /** * getOutputFormatters - * + * * @return array */ public function getOutputFormatters() @@ -80,7 +80,7 @@ public function getOutputFormatters() /** * validate - * + * * @param array $params * @return bool */ @@ -134,7 +134,7 @@ public function validate(&$params) /** * transform - * + * * @param array $params * @param array $rules * @return array @@ -155,4 +155,4 @@ public function transform($params, $rules) } return $params; } -} \ No newline at end of file +} diff --git a/src/Methods/Personal/ImportRawKey.php b/src/Methods/Personal/ImportRawKey.php new file mode 100644 index 00000000..63fb71ed --- /dev/null +++ b/src/Methods/Personal/ImportRawKey.php @@ -0,0 +1,66 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\PrivateValidator; +use Web3\Validators\StringValidator; +use Web3\Formatters\StringFormatter; +use Web3\Formatters\PrivateFormatter; + +class ImportRawKey extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + PrivateValidator::class, StringValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + PrivateFormatter::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); + // } +} diff --git a/src/Net.php b/src/Net.php index bb06615d..31957208 100644 --- a/src/Net.php +++ b/src/Net.php @@ -173,8 +173,6 @@ public function setProvider($provider) */ public function batch($status) { - $status = is_bool($status); - $this->provider->batch($status); } -} \ No newline at end of file +} diff --git a/src/Personal.php b/src/Personal.php index 75aa200c..770004d8 100644 --- a/src/Personal.php +++ b/src/Personal.php @@ -2,9 +2,9 @@ /** * This file is part of web3.php package. - * + * * (c) Kuan-Cheng,Lai - * + * * @author Peter Lai * @license MIT */ @@ -27,18 +27,18 @@ class Personal /** * methods - * + * * @var array */ private $methods = []; /** * allowedMethods - * + * * @var array */ private $allowedMethods = [ - 'personal_listAccounts', 'personal_newAccount', 'personal_unlockAccount', 'personal_lockAccount', 'personal_sendTransaction' + 'personal_listAccounts', 'personal_newAccount', 'personal_unlockAccount', 'personal_lockAccount', 'personal_sendTransaction', 'personal_importRawKey' ]; /** @@ -63,7 +63,7 @@ public function __construct($provider) /** * call - * + * * @param string $name * @param array $arguments * @return void @@ -109,7 +109,7 @@ public function __call($name, $arguments) /** * get - * + * * @param string $name * @return mixed */ @@ -125,7 +125,7 @@ public function __get($name) /** * set - * + * * @param string $name * @param mixed $value * @return mixed @@ -142,7 +142,7 @@ public function __set($name, $value) /** * getProvider - * + * * @return \Web3\Providers\Provider */ public function getProvider() @@ -152,7 +152,7 @@ public function getProvider() /** * setProvider - * + * * @param \Web3\Providers\Provider $provider * @return bool */ @@ -167,14 +167,12 @@ public function setProvider($provider) /** * batch - * + * * @param bool $status * @return void */ public function batch($status) { - $status = is_bool($status); - $this->provider->batch($status); } } diff --git a/src/Providers/HttpProvider.php b/src/Providers/HttpProvider.php index b99de556..979490c9 100644 --- a/src/Providers/HttpProvider.php +++ b/src/Providers/HttpProvider.php @@ -74,8 +74,6 @@ public function send($method, $callback) */ public function batch($status) { - $status = is_bool($status); - $this->isBatch = $status; } @@ -109,7 +107,7 @@ public function execute($callback) return call_user_func($callback, null, $res); }; $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); - $this->methods[] = []; + $this->methods = []; $this->batch = []; } -} \ No newline at end of file +} diff --git a/src/Shh.php b/src/Shh.php index bc438e4b..14c9d031 100644 --- a/src/Shh.php +++ b/src/Shh.php @@ -2,9 +2,9 @@ /** * This file is part of web3.php package. - * + * * (c) Kuan-Cheng,Lai - * + * * @author Peter Lai * @license MIT */ @@ -27,14 +27,14 @@ class Shh /** * methods - * + * * @var array */ private $methods = []; /** * allowedMethods - * + * * @var array */ private $allowedMethods = [ @@ -64,7 +64,7 @@ public function __construct($provider) /** * call - * + * * @param string $name * @param array $arguments * @return void @@ -110,7 +110,7 @@ public function __call($name, $arguments) /** * get - * + * * @param string $name * @return mixed */ @@ -126,7 +126,7 @@ public function __get($name) /** * set - * + * * @param string $name * @param mixed $value * @return mixed @@ -143,7 +143,7 @@ public function __set($name, $value) /** * getProvider - * + * * @return \Web3\Providers\Provider */ public function getProvider() @@ -153,7 +153,7 @@ public function getProvider() /** * setProvider - * + * * @param \Web3\Providers\Provider $provider * @return bool */ @@ -168,14 +168,12 @@ public function setProvider($provider) /** * batch - * + * * @param bool $status * @return void */ public function batch($status) { - $status = is_bool($status); - $this->provider->batch($status); } -} \ No newline at end of file +} diff --git a/src/Validators/PrivateValidator.php b/src/Validators/PrivateValidator.php new file mode 100644 index 00000000..5656c228 --- /dev/null +++ b/src/Validators/PrivateValidator.php @@ -0,0 +1,31 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Validators; + +use Web3\Validators\IValidator; + +class PrivateValidator +{ + /** + * validate + * + * @param string $value + * @return bool + */ + public static function validate($value) + { + if (!is_string($value)) { + return false; + } + return (preg_match('/^(0x)?[a-fA-F0-9]{64}$/', $value) >= 1); + } +} diff --git a/src/Web3.php b/src/Web3.php index 35a2cded..47c01863 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -283,8 +283,6 @@ public function getUtils() */ public function batch($status) { - $status = is_bool($status); - $this->provider->batch($status); } -} \ No newline at end of file +}