Contract call method
This commit is contained in:
parent
b7e73c47e4
commit
6c1a0e35fe
@ -386,4 +386,50 @@ 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);
|
||||
$functionName = Utils::jsonMethodToString($function);
|
||||
$functionSignature = $this->ethabi->encodeFunctionSignature($functionName);
|
||||
$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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -304,8 +304,8 @@ class ContractTest extends TestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->contract = new Contract('http://localhost:8545', $this->testAbi);
|
||||
// $this->contract = new Contract($this->web3->provider, $this->testAbi);
|
||||
// $this->contract = new Contract('http://localhost:8545', $this->testAbi);
|
||||
$this->contract = new Contract($this->web3->provider, $this->testAbi);
|
||||
$this->contract->eth->accounts(function ($err, $accounts) {
|
||||
if ($err === null) {
|
||||
if (isset($accounts->result)) {
|
||||
@ -399,6 +399,10 @@ class ContractTest extends TestCase
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (!isset($this->contractAddress)) {
|
||||
$this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
|
||||
}
|
||||
$contract->at($this->contractAddress)->send('transfer', $toAccount, 16, [
|
||||
'from' => $fromAccount,
|
||||
'gas' => '0x200b20'
|
||||
@ -429,4 +433,66 @@ class ContractTest extends TestCase
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* testCall
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCall()
|
||||
{
|
||||
$contract = $this->contract;
|
||||
|
||||
if (!isset($this->accounts[0])) {
|
||||
$fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
|
||||
} else {
|
||||
$fromAccount = $this->accounts[0];
|
||||
}
|
||||
if (!isset($this->accounts[1])) {
|
||||
$toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
|
||||
} else {
|
||||
$toAccount = $this->accounts[1];
|
||||
}
|
||||
$contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
|
||||
'from' => $fromAccount,
|
||||
'gas' => '0x200b20'
|
||||
], function ($err, $result) use ($contract) {
|
||||
if ($err !== null) {
|
||||
// infura api gg
|
||||
return $this->assertTrue($err !== null);
|
||||
}
|
||||
if ($result->result) {
|
||||
echo "\nTransaction has made:) id: " . $result->result . "\n";
|
||||
}
|
||||
$transactionId = $result->result;
|
||||
$this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
|
||||
|
||||
$contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
|
||||
if ($err !== null) {
|
||||
return $this->fail($err);
|
||||
}
|
||||
if ($transaction->result) {
|
||||
$this->contractAddress = $transaction->result->contractAddress;
|
||||
echo "\nTransaction has mind:) block number: " . $transaction->result->blockNumber . "\n";
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (!isset($this->contractAddress)) {
|
||||
$this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
|
||||
}
|
||||
$contract->at($this->contractAddress)->call('balanceOf', $fromAccount, [
|
||||
'from' => $fromAccount
|
||||
], function ($err, $result) use ($contract) {
|
||||
if ($err !== null) {
|
||||
// infura api gg
|
||||
return $this->assertTrue($err !== null);
|
||||
}
|
||||
if (isset($result->result)) {
|
||||
// $bn = Utils::toBn($result->result);
|
||||
// $this->assertEquals($bn->toString(), '10000', 'Balance should be 10000.');
|
||||
$this->assertTrue($result->result !== null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user