Input formatters for each method.
This commit is contained in:
parent
2340db233b
commit
ee8db87fdf
@ -14,6 +14,7 @@ namespace Web3\Methods\Net;
|
|||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Web3\Methods\IMethod;
|
use Web3\Methods\IMethod;
|
||||||
use Web3\Methods\JSONRPC;
|
use Web3\Methods\JSONRPC;
|
||||||
|
use Web3\Formatters\BigNumberFormatter;
|
||||||
|
|
||||||
class PeerCount extends JSONRPC implements IMethod
|
class PeerCount extends JSONRPC implements IMethod
|
||||||
{
|
{
|
||||||
@ -29,7 +30,9 @@ class PeerCount extends JSONRPC implements IMethod
|
|||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $outputFormatters = [];
|
protected $outputFormatters = [
|
||||||
|
BigNumberFormatter::class
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct
|
* construct
|
||||||
|
@ -17,6 +17,13 @@ use Web3\RequestManagers\RequestManager;
|
|||||||
|
|
||||||
class HttpProvider extends Provider implements IProvider
|
class HttpProvider extends Provider implements IProvider
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* methods
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $methods = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct
|
* construct
|
||||||
*
|
*
|
||||||
@ -40,8 +47,21 @@ class HttpProvider extends Provider implements IProvider
|
|||||||
$payload = $method->toPayloadString();
|
$payload = $method->toPayloadString();
|
||||||
|
|
||||||
if (!$this->isBatch) {
|
if (!$this->isBatch) {
|
||||||
$this->requestManager->sendPayload($payload, $callback);
|
$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 {
|
} else {
|
||||||
|
$this->methods[] = $method;
|
||||||
$this->batch[] = $payload;
|
$this->batch[] = $payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,30 +90,26 @@ class HttpProvider extends Provider implements IProvider
|
|||||||
if (!$this->isBatch) {
|
if (!$this->isBatch) {
|
||||||
throw new \RuntimeException('Please batch json rpc first.');
|
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 = [];
|
$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;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -4,6 +4,7 @@ namespace Test\Unit;
|
|||||||
|
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use Test\TestCase;
|
use Test\TestCase;
|
||||||
|
use phpseclib\Math\BigInteger as BigNumber;
|
||||||
|
|
||||||
class NetApiTest extends TestCase
|
class NetApiTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -56,7 +57,7 @@ class NetApiTest extends TestCase
|
|||||||
if ($err !== null) {
|
if ($err !== null) {
|
||||||
return $this->fail($err->getMessage());
|
return $this->fail($err->getMessage());
|
||||||
}
|
}
|
||||||
$this->assertTrue(is_string($count));
|
$this->assertTrue($count instanceof BigNumber);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ namespace Test\Unit;
|
|||||||
|
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use Test\TestCase;
|
use Test\TestCase;
|
||||||
|
use phpseclib\Math\BigInteger as BigNumber;
|
||||||
|
|
||||||
class NetBatchTest extends TestCase
|
class NetBatchTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -38,6 +39,7 @@ class NetBatchTest extends TestCase
|
|||||||
$net->batch(true);
|
$net->batch(true);
|
||||||
$net->version();
|
$net->version();
|
||||||
$net->listening();
|
$net->listening();
|
||||||
|
$net->peerCount();
|
||||||
|
|
||||||
$net->provider->execute(function ($err, $data) {
|
$net->provider->execute(function ($err, $data) {
|
||||||
if ($err !== null) {
|
if ($err !== null) {
|
||||||
@ -45,6 +47,7 @@ class NetBatchTest extends TestCase
|
|||||||
}
|
}
|
||||||
$this->assertTrue(is_string($data[0]));
|
$this->assertTrue(is_string($data[0]));
|
||||||
$this->assertTrue(is_bool($data[1]));
|
$this->assertTrue(is_bool($data[1]));
|
||||||
|
$this->assertTrue($data[2] instanceof BigNumber);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -65,6 +65,13 @@ class Web3ApiTest extends TestCase
|
|||||||
}
|
}
|
||||||
$this->assertEquals($hash, $this->testHash);
|
$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
|
* testWrongParam
|
||||||
|
* We transform data and throw invalid argument exception
|
||||||
|
* instead of runtime exception.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@ -97,7 +106,7 @@ class Web3ApiTest extends TestCase
|
|||||||
|
|
||||||
$web3 = $this->web3;
|
$web3 = $this->web3;
|
||||||
|
|
||||||
$web3->sha3('hello world', function ($err, $hash) {
|
$web3->sha3($web3, function ($err, $hash) {
|
||||||
if ($err !== null) {
|
if ($err !== null) {
|
||||||
return $this->fail($err->getMessage());
|
return $this->fail($err->getMessage());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user