From a47544b991bd2745dc8343921487ea2fb6e563f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=B9=8B=E7=BE=8E?= Date: Sun, 16 Aug 2020 20:45:41 +0800 Subject: [PATCH] contract batch send transaction --- src/Contract.php | 90 +++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/src/Contract.php b/src/Contract.php index 87ad1fc..7bace7f 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 @@ class Contract /** * call - * + * * @param string $name * @param array $arguments * @return void @@ -184,7 +184,7 @@ class Contract /** * get - * + * * @param string $name * @return mixed */ @@ -200,7 +200,7 @@ class Contract /** * set - * + * * @param string $name * @param mixed $value * @return mixed @@ -217,7 +217,7 @@ class Contract /** * getProvider - * + * * @return \Web3\Providers\Provider */ public function getProvider() @@ -241,7 +241,7 @@ class Contract /** * getDefaultBlock - * + * * @return string */ public function getDefaultBlock() @@ -315,7 +315,7 @@ class Contract /** * setAbi - * + * * @param string $abi * @return $this */ @@ -326,7 +326,7 @@ class Contract /** * getEthabi - * + * * @return array */ public function getEthabi() @@ -336,7 +336,7 @@ class Contract /** * getEth - * + * * @return \Web3\Eth */ public function getEth() @@ -346,7 +346,7 @@ class Contract /** * setBytecode - * + * * @param string $bytecode * @return $this */ @@ -357,7 +357,7 @@ class Contract /** * setToAddress - * + * * @param string $bytecode * @return $this */ @@ -368,7 +368,7 @@ class Contract /** * at - * + * * @param string $address * @return $this */ @@ -384,7 +384,7 @@ class Contract /** * bytecode - * + * * @param string $bytecode * @return $this */ @@ -400,7 +400,7 @@ class Contract /** * abi - * + * * @param string $abi * @return $this */ @@ -439,7 +439,7 @@ class Contract /** * new * Deploy a contruct with params. - * + * * @param mixed * @return void */ @@ -481,7 +481,7 @@ class Contract /** * send * Send function method. - * + * * @param mixed * @return void */ @@ -490,7 +490,6 @@ class Contract 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 @@ class Contract 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 @@ class Contract $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); + }); + } } } @@ -672,7 +684,7 @@ class Contract /** * estimateGas * Estimate function gas. - * + * * @param mixed * @return void */ @@ -708,7 +720,7 @@ class Contract 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 +733,7 @@ class Contract 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 +806,7 @@ class Contract * 1. Get the funtion data with params. * 2. Sign the data with user private key. * 3. Call sendRawTransaction. - * + * * @param mixed * @return void */ @@ -822,7 +834,7 @@ class Contract 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 +844,7 @@ class Contract if (count($functions) < 1) { throw new InvalidArgumentException('Please make sure the method exists.'); } - + $params = $arguments; $data = ""; $functionName = "";