add: defaultBlock in Contract.call() method

This commit is contained in:
toknsiT 2019-08-15 11:23:06 +08:00
parent 8a34582d23
commit 7cdb9a31a2

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
*/ */
@ -12,12 +12,6 @@
namespace Web3; namespace Web3;
use InvalidArgumentException; use InvalidArgumentException;
use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Utils;
use Web3\Eth;
use Web3\Contracts\Ethabi; use Web3\Contracts\Ethabi;
use Web3\Contracts\Types\Address; use Web3\Contracts\Types\Address;
use Web3\Contracts\Types\Boolean; use Web3\Contracts\Types\Boolean;
@ -26,9 +20,14 @@ use Web3\Contracts\Types\DynamicBytes;
use Web3\Contracts\Types\Integer; use Web3\Contracts\Types\Integer;
use Web3\Contracts\Types\Str; use Web3\Contracts\Types\Str;
use Web3\Contracts\Types\Uinteger; use Web3\Contracts\Types\Uinteger;
use Web3\Eth;
use Web3\Formatters\AddressFormatter;
use Web3\Providers\HttpProvider;
use Web3\Providers\Provider;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Utils;
use Web3\Validators\AddressValidator; use Web3\Validators\AddressValidator;
use Web3\Validators\HexValidator; use Web3\Validators\HexValidator;
use Web3\Formatters\AddressFormatter;
use Web3\Validators\StringValidator; use Web3\Validators\StringValidator;
class Contract class Contract
@ -42,56 +41,63 @@ 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;
/**
* defaultBlock
*
* @var string|int
*/
protected $defaultBlock;
/** /**
* 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;
@ -103,7 +109,7 @@ class Contract
* @param string|\stdClass|array $abi * @param string|\stdClass|array $abi
* @return void * @return void
*/ */
public function __construct($provider, $abi) public function __construct($provider, $abi, $defaultBlock = 'pending')
{ {
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) { if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema // check the uri schema
@ -148,11 +154,12 @@ class Contract
'string' => new Str, 'string' => new Str,
'uint' => new Uinteger, 'uint' => new Uinteger,
]); ]);
$this->defaultBlock = $defaultBlock;
} }
/** /**
* call * call
* *
* @param string $name * @param string $name
* @param array $arguments * @param array $arguments
* @return void * @return void
@ -169,7 +176,7 @@ class Contract
/** /**
* get * get
* *
* @param string $name * @param string $name
* @return mixed * @return mixed
*/ */
@ -185,7 +192,7 @@ class Contract
/** /**
* set * set
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @return mixed * @return mixed
@ -202,7 +209,7 @@ class Contract
/** /**
* getProvider * getProvider
* *
* @return \Web3\Providers\Provider * @return \Web3\Providers\Provider
*/ */
public function getProvider() public function getProvider()
@ -212,7 +219,7 @@ class Contract
/** /**
* setProvider * setProvider
* *
* @param \Web3\Providers\Provider $provider * @param \Web3\Providers\Provider $provider
* @return $this * @return $this
*/ */
@ -226,7 +233,7 @@ class Contract
/** /**
* getFunctions * getFunctions
* *
* @return array * @return array
*/ */
public function getFunctions() public function getFunctions()
@ -236,7 +243,7 @@ class Contract
/** /**
* getEvents * getEvents
* *
* @return array * @return array
*/ */
public function getEvents() public function getEvents()
@ -254,7 +261,7 @@ class Contract
/** /**
* getConstructor * getConstructor
* *
* @return array * @return array
*/ */
public function getConstructor() public function getConstructor()
@ -264,7 +271,7 @@ class Contract
/** /**
* getAbi * getAbi
* *
* @return array * @return array
*/ */
public function getAbi() public function getAbi()
@ -274,7 +281,7 @@ class Contract
/** /**
* setAbi * setAbi
* *
* @param string $abi * @param string $abi
* @return $this * @return $this
*/ */
@ -285,7 +292,7 @@ class Contract
/** /**
* getEthabi * getEthabi
* *
* @return array * @return array
*/ */
public function getEthabi() public function getEthabi()
@ -295,7 +302,7 @@ class Contract
/** /**
* getEth * getEth
* *
* @return \Web3\Eth * @return \Web3\Eth
*/ */
public function getEth() public function getEth()
@ -305,7 +312,7 @@ class Contract
/** /**
* setBytecode * setBytecode
* *
* @param string $bytecode * @param string $bytecode
* @return $this * @return $this
*/ */
@ -316,7 +323,7 @@ class Contract
/** /**
* setToAddress * setToAddress
* *
* @param string $bytecode * @param string $bytecode
* @return $this * @return $this
*/ */
@ -327,7 +334,7 @@ class Contract
/** /**
* at * at
* *
* @param string $address * @param string $address
* @return $this * @return $this
*/ */
@ -343,7 +350,7 @@ class Contract
/** /**
* bytecode * bytecode
* *
* @param string $bytecode * @param string $bytecode
* @return $this * @return $this
*/ */
@ -359,7 +366,7 @@ class Contract
/** /**
* abi * abi
* *
* @param string $abi * @param string $abi
* @return $this * @return $this
*/ */
@ -398,12 +405,11 @@ class Contract
/** /**
* new * new
* Deploy a contruct with params. * Deploy a contruct with params.
* *
* @param mixed * @param mixed
* @return void * @return void
*/ */
public function new() function new () {
{
if (isset($this->constructor)) { if (isset($this->constructor)) {
$constructor = $this->constructor; $constructor = $this->constructor;
$arguments = func_get_args(); $arguments = func_get_args();
@ -428,7 +434,7 @@ class Contract
} }
$transaction['data'] = '0x' . $this->bytecode . Utils::stripZero($data); $transaction['data'] = '0x' . $this->bytecode . Utils::stripZero($data);
$this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){ $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback) {
if ($err !== null) { if ($err !== null) {
return call_user_func($callback, $err, null); return call_user_func($callback, $err, null);
} }
@ -440,7 +446,7 @@ class Contract
/** /**
* send * send
* Send function method. * Send function method.
* *
* @param mixed * @param mixed
* @return void * @return void
*/ */
@ -523,7 +529,7 @@ 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){ $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback) {
if ($err !== null) { if ($err !== null) {
return call_user_func($callback, $err, null); return call_user_func($callback, $err, null);
} }
@ -535,7 +541,7 @@ class Contract
/** /**
* call * call
* Call function method. * Call function method.
* *
* @param mixed * @param mixed
* @return void * @return void
*/ */
@ -618,7 +624,7 @@ 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, function ($err, $transaction) use ($callback, $function){ $this->eth->call($transaction, $this->defaultBlock, function ($err, $transaction) use ($callback, $function) {
if ($err !== null) { if ($err !== null) {
return call_user_func($callback, $err, null); return call_user_func($callback, $err, null);
} }
@ -632,7 +638,7 @@ class Contract
/** /**
* estimateGas * estimateGas
* Estimate function gas. * Estimate function gas.
* *
* @param mixed * @param mixed
* @return void * @return void
*/ */
@ -668,7 +674,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) {
@ -681,7 +687,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 = [];
@ -738,7 +744,7 @@ class Contract
$transaction['data'] = $functionSignature . Utils::stripZero($data); $transaction['data'] = $functionSignature . Utils::stripZero($data);
} }
$this->eth->estimateGas($transaction, function ($err, $gas) use ($callback){ $this->eth->estimateGas($transaction, function ($err, $gas) use ($callback) {
if ($err !== null) { if ($err !== null) {
return call_user_func($callback, $err, null); return call_user_func($callback, $err, null);
} }
@ -754,7 +760,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
*/ */
@ -782,7 +788,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) {
@ -792,7 +798,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 = "";