Add bytecode.

This commit is contained in:
sc0Vu 2018-01-02 14:08:01 +08:00
parent c67ec3ccaa
commit 591e88d4cf
3 changed files with 92 additions and 12 deletions

View File

@ -26,6 +26,7 @@ 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\Validators\AddressValidator; use Web3\Validators\AddressValidator;
use Web3\Validators\HexValidator;
use Web3\Formatters\Address as AddressFormatter; use Web3\Formatters\Address as AddressFormatter;
class Contract class Contract
@ -72,6 +73,13 @@ class Contract
*/ */
protected $toAddress; protected $toAddress;
/**
* bytecode
*
* @var string
*/
protected $bytecode;
/** /**
* eth * eth
* *
@ -268,7 +276,6 @@ class Contract
public function at($address) public function at($address)
{ {
if (AddressValidator::validate($address) === false) { if (AddressValidator::validate($address) === false) {
var_dump($address);
throw new InvalidArgumentException('Please make sure address is valid.'); throw new InvalidArgumentException('Please make sure address is valid.');
} }
$this->toAddress = AddressFormatter::format($address); $this->toAddress = AddressFormatter::format($address);
@ -276,6 +283,22 @@ class Contract
return $this; return $this;
} }
/**
* bytecode
*
* @param string $bytecode
* @return $this
*/
public function bytecode($bytecode)
{
if (HexValidator::validate($bytecode) === false) {
throw new InvalidArgumentException('Please make sure bytecode is valid.');
}
$this->bytecode = Utils::stripZero($bytecode);
return $this;
}
/** /**
* new * new
* Deploy a contruct with params. * Deploy a contruct with params.
@ -296,6 +319,9 @@ 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.');
} }
if (!isset($this->bytecode)) {
throw new \InvalidArgumentException('Please call bytecode($bytecode) before new().');
}
$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);
$transaction = []; $transaction = [];
@ -304,7 +330,7 @@ class Contract
$transaction = $arguments[0]; $transaction = $arguments[0];
} }
$transaction['to'] = ''; $transaction['to'] = '';
$transaction['data'] = $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) {
@ -359,4 +385,49 @@ class Contract
}); });
} }
} }
/**
* call
* Call function method.
*
* @param mixed
* @return void
*/
public function call()
{
if (isset($this->functions)) {
$arguments = func_get_args();
$method = array_splice($arguments, 0, 1)[0];
$callback = array_pop($arguments);
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.');
}
if (is_callable($callback) !== true) {
throw new \InvalidArgumentException('The last param must be callback function.');
}
$params = array_splice($arguments, 0, count($function['inputs']));
$data = $this->ethabi->encodeParameters($function, $params);
$functionSignature = $this->ethabi->encodeFunctionSignature($function['name']);
$transaction = [];
if (count($arguments) > 0) {
$transaction = $arguments[0];
}
$transaction['to'] = $this->toAddress;
$transaction['data'] = $functionSignature . Utils::stripZero($data);
$this->eth->call($transaction, function ($err, $transaction) use ($callback){
if ($err !== null) {
return call_user_func($callback, $err, null);
}
return call_user_func($callback, null, $transaction);
});
}
}
} }

View File

@ -33,25 +33,25 @@ class TransactionValidator
return false; return false;
} }
if (AddressValidator::validate($value['from']) === false) { if (AddressValidator::validate($value['from']) === false) {
return false; return false;
} }
if (isset($value['to']) && AddressValidator::validate($value['to']) === false && $value['to'] !== '') { if (isset($value['to']) && AddressValidator::validate($value['to']) === false && $value['to'] !== '') {
return false; return false;
} }
if (isset($value['gas']) && QuantityValidator::validate($value['gas']) === false) { if (isset($value['gas']) && QuantityValidator::validate($value['gas']) === false) {
return false; return false;
} }
if (isset($value['gasPrice']) && QuantityValidator::validate($value['gasPrice']) === false) { if (isset($value['gasPrice']) && QuantityValidator::validate($value['gasPrice']) === false) {
return false; return false;
} }
if (isset($value['value']) && QuantityValidator::validate($value['value']) === false) { if (isset($value['value']) && QuantityValidator::validate($value['value']) === false) {
return false; return false;
} }
if (isset($value['data']) && HexValidator::validate($value['data']) === false) { if (isset($value['data']) && HexValidator::validate($value['data']) === false) {
return false; return false;
} }
if (isset($value['nonce']) && QuantityValidator::validate($value['nonce']) === false) { if (isset($value['nonce']) && QuantityValidator::validate($value['nonce']) === false) {
return false; return false;
} }
return true; return true;
} }

File diff suppressed because one or more lines are too long