diff --git a/src/Methods/Net/PeerCount.php b/src/Methods/Net/PeerCount.php index b021576..b638dd5 100644 --- a/src/Methods/Net/PeerCount.php +++ b/src/Methods/Net/PeerCount.php @@ -14,6 +14,7 @@ namespace Web3\Methods\Net; use InvalidArgumentException; use Web3\Methods\IMethod; use Web3\Methods\JSONRPC; +use Web3\Formatters\BigNumberFormatter; class PeerCount extends JSONRPC implements IMethod { @@ -29,7 +30,9 @@ class PeerCount extends JSONRPC implements IMethod * * @var array */ - protected $outputFormatters = []; + protected $outputFormatters = [ + BigNumberFormatter::class + ]; /** * construct diff --git a/src/Providers/HttpProvider.php b/src/Providers/HttpProvider.php index 5408774..b99de55 100644 --- a/src/Providers/HttpProvider.php +++ b/src/Providers/HttpProvider.php @@ -17,6 +17,13 @@ use Web3\RequestManagers\RequestManager; class HttpProvider extends Provider implements IProvider { + /** + * methods + * + * @var array + */ + protected $methods = []; + /** * construct * @@ -39,9 +46,22 @@ class HttpProvider extends Provider implements IProvider { $payload = $method->toPayloadString(); - if (!$this->isBatch) { - $this->requestManager->sendPayload($payload, $callback); + if (!$this->isBatch) { + $proxy = function ($err, $res) use ($method, $callback) { + if ($err !== null) { + return call_user_func($callback, $err, null); + } + if (!is_array($res)) { + $res = $method->transform([$res], $method->outputFormatters); + return call_user_func($callback, null, $res[0]); + } + $res = $method->transform($res, $method->outputFormatters); + + return call_user_func($callback, null, $res); + }; + $this->requestManager->sendPayload($payload, $proxy); } else { + $this->methods[] = $method; $this->batch[] = $payload; } } @@ -70,30 +90,26 @@ class HttpProvider extends Provider implements IProvider if (!$this->isBatch) { throw new \RuntimeException('Please batch json rpc first.'); } - $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $callback); + $methods = $this->methods; + $proxy = function ($err, $res) use ($methods, $callback) { + if ($err !== null) { + return call_user_func($callback, $err, null); + } + foreach ($methods as $key => $method) { + if (isset($res[$key])) { + if (!is_array($res[$key])) { + $transformed = $method->transform([$res[$key]], $method->outputFormatters); + $res[$key] = $transformed[0]; + } else { + $transformed = $method->transform($res[$key], $method->outputFormatters); + $res[$key] = $transformed; + } + } + } + return call_user_func($callback, null, $res); + }; + $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); + $this->methods[] = []; $this->batch = []; } - - /** - * createRpc - * - * @param string $rpc - * @param array $arguments - * @return array - */ - protected function createRpc($rpc, $arguments) - { - $this->id += 1; - - $rpc = [ - 'id' => $this->id, - 'jsonrpc' => $this->rpcVersion, - 'method' => $rpc - ]; - - if (count($arguments) > 0) { - $rpc['params'] = $arguments; - } - return $rpc; - } } \ No newline at end of file diff --git a/test/unit/NetApiTest.php b/test/unit/NetApiTest.php index a7c0e57..3766a3c 100644 --- a/test/unit/NetApiTest.php +++ b/test/unit/NetApiTest.php @@ -4,6 +4,7 @@ namespace Test\Unit; use RuntimeException; use Test\TestCase; +use phpseclib\Math\BigInteger as BigNumber; class NetApiTest extends TestCase { @@ -56,7 +57,7 @@ class NetApiTest extends TestCase if ($err !== null) { return $this->fail($err->getMessage()); } - $this->assertTrue(is_string($count)); + $this->assertTrue($count instanceof BigNumber); }); } diff --git a/test/unit/NetBatchTest.php b/test/unit/NetBatchTest.php index 1f81ab5..f3b58ec 100644 --- a/test/unit/NetBatchTest.php +++ b/test/unit/NetBatchTest.php @@ -4,6 +4,7 @@ namespace Test\Unit; use RuntimeException; use Test\TestCase; +use phpseclib\Math\BigInteger as BigNumber; class NetBatchTest extends TestCase { @@ -38,6 +39,7 @@ class NetBatchTest extends TestCase $net->batch(true); $net->version(); $net->listening(); + $net->peerCount(); $net->provider->execute(function ($err, $data) { if ($err !== null) { @@ -45,6 +47,7 @@ class NetBatchTest extends TestCase } $this->assertTrue(is_string($data[0])); $this->assertTrue(is_bool($data[1])); + $this->assertTrue($data[2] instanceof BigNumber); }); } } \ No newline at end of file diff --git a/test/unit/Web3ApiTest.php b/test/unit/Web3ApiTest.php index 137ec7d..a46f504 100644 --- a/test/unit/Web3ApiTest.php +++ b/test/unit/Web3ApiTest.php @@ -65,6 +65,13 @@ class Web3ApiTest extends TestCase } $this->assertEquals($hash, $this->testHash); }); + + $web3->sha3('hello world', function ($err, $hash) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertEquals($hash, $this->testHash); + }); } /** @@ -88,6 +95,8 @@ class Web3ApiTest extends TestCase /** * testWrongParam + * We transform data and throw invalid argument exception + * instead of runtime exception. * * @return void */ @@ -97,7 +106,7 @@ class Web3ApiTest extends TestCase $web3 = $this->web3; - $web3->sha3('hello world', function ($err, $hash) { + $web3->sha3($web3, function ($err, $hash) { if ($err !== null) { return $this->fail($err->getMessage()); }