Eth
This commit is contained in:
parent
69eb1e8772
commit
32bc7949fd
476
src/Eth.php
476
src/Eth.php
@ -41,303 +41,138 @@ 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_getCode', 'eth_sign', 'eth_sendTransaction', 'eth_sendRawTransaction', 'eth_call', 'eth_estimateGas', 'eth_getBlockByHash', 'eth_getBlockByNumber', 'eth_getTransactionByHash', 'eth_getTransactionByBlockHashAndIndex', 'eth_getTransactionByBlockNumberAndIndex'
|
||||
];
|
||||
|
||||
/**
|
||||
* methods
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
// private $methods = [
|
||||
// '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
|
||||
// ]
|
||||
// ]
|
||||
// ],
|
||||
// ];
|
||||
|
||||
/**
|
||||
* construct
|
||||
*
|
||||
@ -376,11 +211,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 +223,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user