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)) { $this->accounts = $accounts; return; } } }); } /** * testDeploy * * @return void */ public function testDeploy() { $contract = $this->contract; if (!isset($this->accounts[0])) { $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1'; } else { $account = $this->accounts[0]; } $contract->bytecode($this->testBytecode)->new(1000000, 'Game Token', 1, 'GT', [ 'from' => $account, 'gas' => '0x200b20' ], function ($err, $result) use ($contract) { if ($err !== null) { // infura api gg return $this->assertTrue($err !== null); } if ($result) { echo "\nTransaction has made:) id: " . $result . "\n"; } $transactionId = $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) { $this->contractAddress = $transaction->contractAddress; echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n"; } }); }); } /** * testSend * * @return void */ public function testSend() { $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(1000000, '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) { echo "\nTransaction has made:) id: " . $result . "\n"; } $transactionId = $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) { $this->contractAddress = $transaction->contractAddress; echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n"; } }); }); if (!isset($this->contractAddress)) { $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2'; } $contract->at($this->contractAddress)->send('transfer', $toAccount, 16, [ 'from' => $fromAccount, 'gas' => '0x200b20' ], function ($err, $result) use ($contract, $fromAccount, $toAccount) { if ($err !== null) { // infura api gg return $this->assertTrue($err !== null); } if ($result) { echo "\nTransaction has made:) id: " . $result . "\n"; } $transactionId = $result; $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1)); $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount) { if ($err !== null) { return $this->fail($err); } if ($transaction) { $topics = $transaction->logs[0]->topics; echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n"; // validate topics $this->assertEquals(Ethabi::encodeEventSignature($this->contract->events['Transfer']), $topics[0]); $this->assertEquals('0x' . IntegerFormatter::format($fromAccount), $topics[1]); $this->assertEquals('0x' . IntegerFormatter::format($toAccount), $topics[2]); } }); }); } /** * 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) { echo "\nTransaction has made:) id: " . $result . "\n"; } $transactionId = $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) { $this->contractAddress = $transaction->contractAddress; echo "\nTransaction has mind:) block number: " . $transaction->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)) { // $bn = Utils::toBn($result); // $this->assertEquals($bn->toString(), '10000', 'Balance should be 10000.'); $this->assertTrue($result !== null); } }); } /** * testEstimateGas * * @return void */ public function testEstimateGas() { $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) { echo "\nTransaction has made:) id: " . $result . "\n"; } $transactionId = $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) { $this->contractAddress = $transaction->contractAddress; echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n"; } }); }); $contract->bytecode($this->testBytecode)->estimateGas(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 (isset($result)) { echo "\nEstimate gas: " . $result->toString() . "\n"; $this->assertTrue($result !== null); } }); if (!isset($this->contractAddress)) { $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2'; } $contract->at($this->contractAddress)->estimateGas('balanceOf', $fromAccount, [ 'from' => $fromAccount ], function ($err, $result) use ($contract) { if ($err !== null) { // infura api gg return $this->assertTrue($err !== null); } if (isset($result)) { echo "\nEstimate gas: " . $result->toString() . "\n"; $this->assertTrue($result !== null); } }); } }