Merge pull request #37 from sc0Vu/contract-getData

Contract get data
This commit is contained in:
Kuan-Cheng,Lai 2018-01-21 20:35:58 +08:00 committed by GitHub
commit 49dd1e9043
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 16 deletions

View File

@ -447,7 +447,7 @@ class Contract
*/ */
public function estimateGas() public function estimateGas()
{ {
if (isset($this->functions) || isset($this->callback)) { if (isset($this->functions) || isset($this->constructor)) {
$arguments = func_get_args(); $arguments = func_get_args();
$callback = array_pop($arguments); $callback = array_pop($arguments);
@ -461,7 +461,7 @@ class Contract
throw new \InvalidArgumentException('The last param must be callback function.'); throw new \InvalidArgumentException('The last param must be callback function.');
} }
if (!isset($this->bytecode)) { if (!isset($this->bytecode)) {
throw new \InvalidArgumentException('Please call bytecode($bytecode) before new().'); throw new \InvalidArgumentException('Please call bytecode($bytecode) before estimateGas().');
} }
$params = array_splice($arguments, 0, count($constructor['inputs'])); $params = array_splice($arguments, 0, count($constructor['inputs']));
$data = $this->ethabi->encodeParameters($constructor, $params); $data = $this->ethabi->encodeParameters($constructor, $params);
@ -507,4 +507,54 @@ class Contract
}); });
} }
} }
/**
* getData
* Get the function method call data.
* With this function, you can send signed contract function transaction.
* 1. Get the funtion data with params.
* 2. Sign the data with user private key.
* 3. Call sendRawTransaction.
*
* @param mixed
* @return void
*/
public function getData()
{
if (isset($this->functions) || isset($this->constructor)) {
$arguments = func_get_args();
$functionData = '';
if (empty($this->toAddress) && !empty($this->bytecode)) {
$constructor = $this->constructor;
if (count($arguments) < count($constructor['inputs'])) {
throw new InvalidArgumentException('Please make sure you have put all constructor params and callback.');
}
if (!isset($this->bytecode)) {
throw new \InvalidArgumentException('Please call bytecode($bytecode) before getData().');
}
$params = array_splice($arguments, 0, count($constructor['inputs']));
$data = $this->ethabi->encodeParameters($constructor, $params);
$functionData = $this->bytecode . Utils::stripZero($data);
} else {
$method = array_splice($arguments, 0, 1)[0];
if (!is_string($method) && !isset($this->functions[$method])) {
throw new InvalidArgumentException('Please make sure the method is existed.');
}
$function = $this->functions[$method];
if (count($arguments) < count($function['inputs'])) {
throw new InvalidArgumentException('Please make sure you have put all function params and callback.');
}
$params = array_splice($arguments, 0, count($function['inputs']));
$data = $this->ethabi->encodeParameters($function, $params);
$functionName = Utils::jsonMethodToString($function);
$functionSignature = $this->ethabi->encodeFunctionSignature($functionName);
$functionData = Utils::stripZero($functionSignature) . Utils::stripZero($data);
}
return $functionData;
}
}
} }

File diff suppressed because one or more lines are too long