This commit is contained in:
代码之美 2021-08-16 21:46:39 +07:00 committed by GitHub
commit fcbcf3970c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 273 additions and 104 deletions

View File

@ -18,13 +18,13 @@ Set minimum stability to dev
Then Then
``` ```
composer require sc0vu/web3.php dev-master composer require haohetao/web3.php dev-master
``` ```
Or you can add this line in composer.json 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 // 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 ### Contract

View File

@ -1,12 +1,12 @@
{ {
"name": "sc0vu/web3.php", "name": "haohetao/web3.php",
"description": "Ethereum web3 interface.", "description": "Ethereum web3 interface.",
"type": "library", "type": "library",
"license": "MIT", "license": "MIT",
"authors": [ "authors": [
{ {
"name": "sc0Vu", "name": "hetao",
"email": "alk03073135@gmail.com" "email": "haohetao@gmail.com"
} }
], ],
"require": { "require": {

View File

@ -2,9 +2,9 @@
/** /**
* This file is part of web3.php package. * This file is part of web3.php package.
* *
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com> * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
* *
* @author Peter Lai <alk03073135@gmail.com> * @author Peter Lai <alk03073135@gmail.com>
* @license MIT * @license MIT
*/ */
@ -44,56 +44,56 @@ class Contract
/** /**
* abi * abi
* *
* @var array * @var array
*/ */
protected $abi; protected $abi;
/** /**
* constructor * constructor
* *
* @var array * @var array
*/ */
protected $constructor = []; protected $constructor = [];
/** /**
* functions * functions
* *
* @var array * @var array
*/ */
protected $functions = []; protected $functions = [];
/** /**
* events * events
* *
* @var array * @var array
*/ */
protected $events = []; protected $events = [];
/** /**
* toAddress * toAddress
* *
* @var string * @var string
*/ */
protected $toAddress; protected $toAddress;
/** /**
* bytecode * bytecode
* *
* @var string * @var string
*/ */
protected $bytecode; protected $bytecode;
/** /**
* eth * eth
* *
* @var \Web3\Eth * @var \Web3\Eth
*/ */
protected $eth; protected $eth;
/** /**
* ethabi * ethabi
* *
* @var \Web3\Contracts\Ethabi * @var \Web3\Contracts\Ethabi
*/ */
protected $ethabi; protected $ethabi;
@ -167,7 +167,7 @@ class Contract
/** /**
* call * call
* *
* @param string $name * @param string $name
* @param array $arguments * @param array $arguments
* @return void * @return void
@ -184,7 +184,7 @@ class Contract
/** /**
* get * get
* *
* @param string $name * @param string $name
* @return mixed * @return mixed
*/ */
@ -200,7 +200,7 @@ class Contract
/** /**
* set * set
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @return mixed * @return mixed
@ -217,7 +217,7 @@ class Contract
/** /**
* getProvider * getProvider
* *
* @return \Web3\Providers\Provider * @return \Web3\Providers\Provider
*/ */
public function getProvider() public function getProvider()
@ -241,7 +241,7 @@ class Contract
/** /**
* getDefaultBlock * getDefaultBlock
* *
* @return string * @return string
*/ */
public function getDefaultBlock() public function getDefaultBlock()
@ -315,7 +315,7 @@ class Contract
/** /**
* setAbi * setAbi
* *
* @param string $abi * @param string $abi
* @return $this * @return $this
*/ */
@ -326,7 +326,7 @@ class Contract
/** /**
* getEthabi * getEthabi
* *
* @return array * @return array
*/ */
public function getEthabi() public function getEthabi()
@ -336,7 +336,7 @@ class Contract
/** /**
* getEth * getEth
* *
* @return \Web3\Eth * @return \Web3\Eth
*/ */
public function getEth() public function getEth()
@ -346,7 +346,7 @@ class Contract
/** /**
* setBytecode * setBytecode
* *
* @param string $bytecode * @param string $bytecode
* @return $this * @return $this
*/ */
@ -357,7 +357,7 @@ class Contract
/** /**
* setToAddress * setToAddress
* *
* @param string $bytecode * @param string $bytecode
* @return $this * @return $this
*/ */
@ -368,7 +368,7 @@ class Contract
/** /**
* at * at
* *
* @param string $address * @param string $address
* @return $this * @return $this
*/ */
@ -384,7 +384,7 @@ class Contract
/** /**
* bytecode * bytecode
* *
* @param string $bytecode * @param string $bytecode
* @return $this * @return $this
*/ */
@ -400,7 +400,7 @@ class Contract
/** /**
* abi * abi
* *
* @param string $abi * @param string $abi
* @return $this * @return $this
*/ */
@ -439,7 +439,7 @@ class Contract
/** /**
* new * new
* Deploy a contruct with params. * Deploy a contruct with params.
* *
* @param mixed * @param mixed
* @return void * @return void
*/ */
@ -481,7 +481,7 @@ class Contract
/** /**
* send * send
* Send function method. * Send function method.
* *
* @param mixed * @param mixed
* @return void * @return void
*/ */
@ -490,7 +490,6 @@ class Contract
if (isset($this->functions)) { if (isset($this->functions)) {
$arguments = func_get_args(); $arguments = func_get_args();
$method = array_splice($arguments, 0, 1)[0]; $method = array_splice($arguments, 0, 1)[0];
$callback = array_pop($arguments);
if (!is_string($method)) { if (!is_string($method)) {
throw new InvalidArgumentException('Please make sure the method is string.'); throw new InvalidArgumentException('Please make sure the method is string.');
@ -505,8 +504,17 @@ class Contract
if (count($functions) < 1) { if (count($functions) < 1) {
throw new InvalidArgumentException('Please make sure the method exists.'); 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.'); 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 // check the last one in arguments is transaction object
@ -564,12 +572,16 @@ class Contract
$transaction['to'] = $this->toAddress; $transaction['to'] = $this->toAddress;
$transaction['data'] = $functionSignature . Utils::stripZero($data); $transaction['data'] = $functionSignature . Utils::stripZero($data);
$this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){ if ($this->eth->provider->isBatch) {
if ($err !== null) { $this->eth->sendTransaction($transaction);
return call_user_func($callback, $err, null); } else {
} $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){
return call_user_func($callback, null, $transaction); 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)) { if (isset($this->functions)) {
$arguments = func_get_args(); $arguments = func_get_args();
$method = array_splice($arguments, 0, 1)[0]; $method = array_splice($arguments, 0, 1)[0];
$callback = array_pop($arguments);
if (!is_string($method)) { if (!is_string($method)) {
throw new InvalidArgumentException('Please make sure the method is string.'); throw new InvalidArgumentException('Please make sure the method is string.');
@ -600,8 +611,17 @@ class Contract
if (count($functions) < 1) { if (count($functions) < 1) {
throw new InvalidArgumentException('Please make sure the method exists.'); 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.'); 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 // check the arguments
@ -658,21 +678,25 @@ class Contract
$transaction['to'] = $this->toAddress; $transaction['to'] = $this->toAddress;
$transaction['data'] = $functionSignature . Utils::stripZero($data); $transaction['data'] = $functionSignature . Utils::stripZero($data);
$this->eth->call($transaction, $defaultBlock, function ($err, $transaction) use ($callback, $function){ if ($this->eth->provider->isBatch) {
if ($err !== null) { $this->eth->call($transaction, $defaultBlock);
return call_user_func($callback, $err, null); } else {
} $this->eth->call($transaction, $defaultBlock, function ($err, $transaction) use ($callback, $function){
$decodedTransaction = $this->ethabi->decodeParameters($function, $transaction); 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);
}); });
}
} }
} }
/** /**
* estimateGas * estimateGas
* Estimate function gas. * Estimate function gas.
* *
* @param mixed * @param mixed
* @return void * @return void
*/ */
@ -708,7 +732,7 @@ class Contract
if (!is_string($method)) { if (!is_string($method)) {
throw new InvalidArgumentException('Please make sure the method is string.'); throw new InvalidArgumentException('Please make sure the method is string.');
} }
$functions = []; $functions = [];
foreach ($this->functions as $function) { foreach ($this->functions as $function) {
if ($function["name"] === $method) { if ($function["name"] === $method) {
@ -721,7 +745,7 @@ class Contract
if (is_callable($callback) !== true) { if (is_callable($callback) !== true) {
throw new \InvalidArgumentException('The last param must be callback function.'); throw new \InvalidArgumentException('The last param must be callback function.');
} }
// check the last one in arguments is transaction object // check the last one in arguments is transaction object
$argsLen = count($arguments); $argsLen = count($arguments);
$transaction = []; $transaction = [];
@ -794,7 +818,7 @@ class Contract
* 1. Get the funtion data with params. * 1. Get the funtion data with params.
* 2. Sign the data with user private key. * 2. Sign the data with user private key.
* 3. Call sendRawTransaction. * 3. Call sendRawTransaction.
* *
* @param mixed * @param mixed
* @return void * @return void
*/ */
@ -822,7 +846,7 @@ class Contract
if (!is_string($method)) { if (!is_string($method)) {
throw new InvalidArgumentException('Please make sure the method is string.'); throw new InvalidArgumentException('Please make sure the method is string.');
} }
$functions = []; $functions = [];
foreach ($this->functions as $function) { foreach ($this->functions as $function) {
if ($function["name"] === $method) { if ($function["name"] === $method) {
@ -832,7 +856,7 @@ class Contract
if (count($functions) < 1) { if (count($functions) < 1) {
throw new InvalidArgumentException('Please make sure the method exists.'); throw new InvalidArgumentException('Please make sure the method exists.');
} }
$params = $arguments; $params = $arguments;
$data = ""; $data = "";
$functionName = ""; $functionName = "";
@ -857,4 +881,14 @@ class Contract
return $functionData; return $functionData;
} }
} }
/**
* batch
*
* @param bool $status
* @return void
*/
public function batch($status)
{
$this->eth->batch($status);
}
} }

View File

@ -173,8 +173,6 @@ class Eth
*/ */
public function batch($status) public function batch($status)
{ {
$status = is_bool($status);
$this->provider->batch($status); $this->provider->batch($status);
} }
} }

View 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;
}
}

View File

@ -2,9 +2,9 @@
/** /**
* This file is part of web3.php package. * This file is part of web3.php package.
* *
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com> * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
* *
* @author Peter Lai <alk03073135@gmail.com> * @author Peter Lai <alk03073135@gmail.com>
* @license MIT * @license MIT
*/ */
@ -20,35 +20,35 @@ class EthMethod extends JSONRPC implements IMethod
{ {
/** /**
* validators * validators
* *
* @var array * @var array
*/ */
protected $validators = []; protected $validators = [];
/** /**
* inputFormatters * inputFormatters
* *
* @var array * @var array
*/ */
protected $inputFormatters = []; protected $inputFormatters = [];
/** /**
* outputFormatters * outputFormatters
* *
* @var array * @var array
*/ */
protected $outputFormatters = []; protected $outputFormatters = [];
/** /**
* defaultValues * defaultValues
* *
* @var array * @var array
*/ */
protected $defaultValues = []; protected $defaultValues = [];
/** /**
* construct * construct
* *
* @param string $method * @param string $method
* @param array $arguments * @param array $arguments
* @return void * @return void
@ -60,7 +60,7 @@ class EthMethod extends JSONRPC implements IMethod
/** /**
* getInputFormatters * getInputFormatters
* *
* @return array * @return array
*/ */
public function getInputFormatters() public function getInputFormatters()
@ -70,7 +70,7 @@ class EthMethod extends JSONRPC implements IMethod
/** /**
* getOutputFormatters * getOutputFormatters
* *
* @return array * @return array
*/ */
public function getOutputFormatters() public function getOutputFormatters()
@ -80,7 +80,7 @@ class EthMethod extends JSONRPC implements IMethod
/** /**
* validate * validate
* *
* @param array $params * @param array $params
* @return bool * @return bool
*/ */
@ -134,7 +134,7 @@ class EthMethod extends JSONRPC implements IMethod
/** /**
* transform * transform
* *
* @param array $params * @param array $params
* @param array $rules * @param array $rules
* @return array * @return array
@ -155,4 +155,4 @@ class EthMethod extends JSONRPC implements IMethod
} }
return $params; return $params;
} }
} }

View 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);
// }
}

View File

@ -173,8 +173,6 @@ class Net
*/ */
public function batch($status) public function batch($status)
{ {
$status = is_bool($status);
$this->provider->batch($status); $this->provider->batch($status);
} }
} }

View File

@ -2,9 +2,9 @@
/** /**
* This file is part of web3.php package. * This file is part of web3.php package.
* *
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com> * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
* *
* @author Peter Lai <alk03073135@gmail.com> * @author Peter Lai <alk03073135@gmail.com>
* @license MIT * @license MIT
*/ */
@ -27,18 +27,18 @@ class Personal
/** /**
* methods * methods
* *
* @var array * @var array
*/ */
private $methods = []; private $methods = [];
/** /**
* allowedMethods * allowedMethods
* *
* @var array * @var array
*/ */
private $allowedMethods = [ private $allowedMethods = [
'personal_listAccounts', 'personal_newAccount', 'personal_unlockAccount', 'personal_lockAccount', 'personal_sendTransaction' 'personal_listAccounts', 'personal_newAccount', 'personal_unlockAccount', 'personal_lockAccount', 'personal_sendTransaction', 'personal_importRawKey'
]; ];
/** /**
@ -63,7 +63,7 @@ class Personal
/** /**
* call * call
* *
* @param string $name * @param string $name
* @param array $arguments * @param array $arguments
* @return void * @return void
@ -109,7 +109,7 @@ class Personal
/** /**
* get * get
* *
* @param string $name * @param string $name
* @return mixed * @return mixed
*/ */
@ -125,7 +125,7 @@ class Personal
/** /**
* set * set
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @return mixed * @return mixed
@ -142,7 +142,7 @@ class Personal
/** /**
* getProvider * getProvider
* *
* @return \Web3\Providers\Provider * @return \Web3\Providers\Provider
*/ */
public function getProvider() public function getProvider()
@ -152,7 +152,7 @@ class Personal
/** /**
* setProvider * setProvider
* *
* @param \Web3\Providers\Provider $provider * @param \Web3\Providers\Provider $provider
* @return bool * @return bool
*/ */
@ -167,14 +167,12 @@ class Personal
/** /**
* batch * batch
* *
* @param bool $status * @param bool $status
* @return void * @return void
*/ */
public function batch($status) public function batch($status)
{ {
$status = is_bool($status);
$this->provider->batch($status); $this->provider->batch($status);
} }
} }

View File

@ -74,8 +74,6 @@ class HttpProvider extends Provider implements IProvider
*/ */
public function batch($status) public function batch($status)
{ {
$status = is_bool($status);
$this->isBatch = $status; $this->isBatch = $status;
} }
@ -109,7 +107,7 @@ class HttpProvider extends Provider implements IProvider
return call_user_func($callback, null, $res); return call_user_func($callback, null, $res);
}; };
$this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy);
$this->methods[] = []; $this->methods = [];
$this->batch = []; $this->batch = [];
} }
} }

View File

@ -2,9 +2,9 @@
/** /**
* This file is part of web3.php package. * This file is part of web3.php package.
* *
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com> * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
* *
* @author Peter Lai <alk03073135@gmail.com> * @author Peter Lai <alk03073135@gmail.com>
* @license MIT * @license MIT
*/ */
@ -27,14 +27,14 @@ class Shh
/** /**
* methods * methods
* *
* @var array * @var array
*/ */
private $methods = []; private $methods = [];
/** /**
* allowedMethods * allowedMethods
* *
* @var array * @var array
*/ */
private $allowedMethods = [ private $allowedMethods = [
@ -64,7 +64,7 @@ class Shh
/** /**
* call * call
* *
* @param string $name * @param string $name
* @param array $arguments * @param array $arguments
* @return void * @return void
@ -110,7 +110,7 @@ class Shh
/** /**
* get * get
* *
* @param string $name * @param string $name
* @return mixed * @return mixed
*/ */
@ -126,7 +126,7 @@ class Shh
/** /**
* set * set
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @return mixed * @return mixed
@ -143,7 +143,7 @@ class Shh
/** /**
* getProvider * getProvider
* *
* @return \Web3\Providers\Provider * @return \Web3\Providers\Provider
*/ */
public function getProvider() public function getProvider()
@ -153,7 +153,7 @@ class Shh
/** /**
* setProvider * setProvider
* *
* @param \Web3\Providers\Provider $provider * @param \Web3\Providers\Provider $provider
* @return bool * @return bool
*/ */
@ -168,14 +168,12 @@ class Shh
/** /**
* batch * batch
* *
* @param bool $status * @param bool $status
* @return void * @return void
*/ */
public function batch($status) public function batch($status)
{ {
$status = is_bool($status);
$this->provider->batch($status); $this->provider->batch($status);
} }
} }

View 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);
}
}

View File

@ -283,8 +283,6 @@ class Web3
*/ */
public function batch($status) public function batch($status)
{ {
$status = is_bool($status);
$this->provider->batch($status); $this->provider->batch($status);
} }
} }