Merge 4fcb583f1f
into fb94ea92c5
This commit is contained in:
commit
fcbcf3970c
18
README.md
18
README.md
@ -18,13 +18,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"
|
||||
```
|
||||
|
||||
|
||||
@ -158,6 +158,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
|
||||
|
||||
|
@ -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": {
|
||||
|
@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -585,7 +597,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.');
|
||||
@ -600,8 +611,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 arguments
|
||||
@ -658,14 +678,18 @@ class Contract
|
||||
$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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -857,4 +881,14 @@ class Contract
|
||||
return $functionData;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* batch
|
||||
*
|
||||
* @param bool $status
|
||||
* @return void
|
||||
*/
|
||||
public function batch($status)
|
||||
{
|
||||
$this->eth->batch($status);
|
||||
}
|
||||
}
|
||||
|
@ -173,8 +173,6 @@ class Eth
|
||||
*/
|
||||
public function batch($status)
|
||||
{
|
||||
$status = is_bool($status);
|
||||
|
||||
$this->provider->batch($status);
|
||||
}
|
||||
}
|
36
src/Formatters/PrivateFormatter.php
Normal file
36
src/Formatters/PrivateFormatter.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of web3.php package.
|
||||
*
|
||||
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
|
||||
*
|
||||
* @author Peter Lai <alk03073135@gmail.com>
|
||||
* @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;
|
||||
}
|
||||
}
|
66
src/Methods/Personal/ImportRawKey.php
Normal file
66
src/Methods/Personal/ImportRawKey.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of web3.php package.
|
||||
*
|
||||
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
|
||||
*
|
||||
* @author Peter Lai <alk03073135@gmail.com>
|
||||
* @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);
|
||||
// }
|
||||
}
|
@ -173,8 +173,6 @@ class Net
|
||||
*/
|
||||
public function batch($status)
|
||||
{
|
||||
$status = is_bool($status);
|
||||
|
||||
$this->provider->batch($status);
|
||||
}
|
||||
}
|
@ -38,7 +38,7 @@ class Personal
|
||||
* @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'
|
||||
];
|
||||
|
||||
/**
|
||||
@ -173,8 +173,6 @@ class Personal
|
||||
*/
|
||||
public function batch($status)
|
||||
{
|
||||
$status = is_bool($status);
|
||||
|
||||
$this->provider->batch($status);
|
||||
}
|
||||
}
|
||||
|
@ -74,8 +74,6 @@ class HttpProvider extends Provider implements IProvider
|
||||
*/
|
||||
public function batch($status)
|
||||
{
|
||||
$status = is_bool($status);
|
||||
|
||||
$this->isBatch = $status;
|
||||
}
|
||||
|
||||
@ -109,7 +107,7 @@ class HttpProvider extends Provider implements IProvider
|
||||
return call_user_func($callback, null, $res);
|
||||
};
|
||||
$this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy);
|
||||
$this->methods[] = [];
|
||||
$this->methods = [];
|
||||
$this->batch = [];
|
||||
}
|
||||
}
|
@ -174,8 +174,6 @@ class Shh
|
||||
*/
|
||||
public function batch($status)
|
||||
{
|
||||
$status = is_bool($status);
|
||||
|
||||
$this->provider->batch($status);
|
||||
}
|
||||
}
|
31
src/Validators/PrivateValidator.php
Normal file
31
src/Validators/PrivateValidator.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of web3.php package.
|
||||
*
|
||||
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
|
||||
*
|
||||
* @author Peter Lai <alk03073135@gmail.com>
|
||||
* @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);
|
||||
}
|
||||
}
|
@ -283,8 +283,6 @@ class Web3
|
||||
*/
|
||||
public function batch($status)
|
||||
{
|
||||
$status = is_bool($status);
|
||||
|
||||
$this->provider->batch($status);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user