This commit is contained in:
sc0Vu 2018-01-21 20:30:51 +08:00
parent c56642198a
commit bb43d04068
2 changed files with 113 additions and 14 deletions

View File

@ -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