123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843 |
- <?php
- namespace Test\Unit;
- use RuntimeException;
- use InvalidArgumentException;
- use Test\TestCase;
- use phpseclib\Math\BigInteger as BigNumber;
- class EthApiTest extends TestCase
- {
- /**
- * eth
- *
- * @var \Web3\Eth
- */
- protected $eth;
- /**
- * setUp
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $this->eth = $this->web3->eth;
- }
- /**
- * testProtocolVersion
- *
- * @return void
- */
- public function testProtocolVersion()
- {
- $eth = $this->eth;
- $eth->protocolVersion(function ($err, $version) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertTrue($version instanceof BigNumber);
- });
- }
- /**
- * testSyncing
- *
- * @return void
- */
- public function testSyncing()
- {
- $eth = $this->eth;
- $eth->syncing(function ($err, $syncing) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- // due to the result might be object or bool, only test is null
- $this->assertTrue($syncing !== null);
- });
- }
- /**
- * testCoinbase
- *
- * @return void
- */
- public function testCoinbase()
- {
- $eth = $this->eth;
- $eth->coinbase(function ($err, $coinbase) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertEquals($coinbase, $this->coinbase);
- });
- }
- /**
- * testMining
- *
- * @return void
- */
- public function testMining()
- {
- $eth = $this->eth;
- $eth->mining(function ($err, $mining) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertTrue($mining);
- });
- }
- /**
- * testHashrate
- *
- * @return void
- */
- public function testHashrate()
- {
- $eth = $this->eth;
- $eth->hashrate(function ($err, $hashrate) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertEquals($hashrate->toString(), '0');
- });
- }
- /**
- * testGasPrice
- *
- * @return void
- */
- public function testGasPrice()
- {
- $eth = $this->eth;
- $eth->gasPrice(function ($err, $gasPrice) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertTrue(is_numeric($gasPrice->toString()));
- });
- }
- /**
- * testAccounts
- *
- * @return void
- */
- public function testAccounts()
- {
- $eth = $this->eth;
- $eth->accounts(function ($err, $accounts) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertTrue(is_array($accounts));
- });
- }
- /**
- * testBlockNumber
- *
- * @return void
- */
- public function testBlockNumber()
- {
- $eth = $this->eth;
- $eth->blockNumber(function ($err, $blockNumber) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertTrue(is_numeric($blockNumber->toString()));
- });
- }
- /**
- * testGetBalance
- *
- * @return void
- */
- public function testGetBalance()
- {
- $eth = $this->eth;
- $eth->getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', function ($err, $balance) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertTrue(is_numeric($balance->toString()));
- });
- }
- /**
- * testGetStorageAt
- *
- * @return void
- */
- public function testGetStorageAt()
- {
- $eth = $this->eth;
- $eth->getStorageAt('0x561a2aa10f9a8589c93665554c871106342f70af', '0x0', function ($err, $storage) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertTrue(is_string($storage));
- });
- }
- /**
- * testGetTransactionCount
- *
- * @return void
- */
- public function testGetTransactionCount()
- {
- $eth = $this->eth;
- $eth->getTransactionCount('0x561a2aa10f9a8589c93665554c871106342f70af', function ($err, $transactionCount) {
- if ($err !== null) {
- return $this->fail($err->getMessage());
- }
- $this->assertTrue(is_numeric($transactionCount->toString()));
- });
- }
- /**
- * testGetBlockTransactionCountByHash
- *
- * @return void
- */
- public function testGetBlockTransactionCountByHash()
- {
- $eth = $this->eth;
- $eth->getBlockTransactionCountByHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', function ($err, $transactionCount) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_numeric($transactionCount->toString()));
- });
- }
- /**
- * testGetBlockTransactionCountByNumber
- *
- * @return void
- */
- public function testGetBlockTransactionCountByNumber()
- {
- $eth = $this->eth;
- $eth->getBlockTransactionCountByNumber('0x0', function ($err, $transactionCount) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_numeric($transactionCount->toString()));
- });
- }
- /**
- * testGetUncleCountByBlockHash
- *
- * @return void
- */
- public function testGetUncleCountByBlockHash()
- {
- $eth = $this->eth;
- $eth->getUncleCountByBlockHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', function ($err, $uncleCount) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_numeric($uncleCount->toString()));
- });
- }
- /**
- * testGetUncleCountByBlockNumber
- *
- * @return void
- */
- public function testGetUncleCountByBlockNumber()
- {
- $eth = $this->eth;
- $eth->getUncleCountByBlockNumber('0x0', function ($err, $uncleCount) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_numeric($uncleCount->toString()));
- });
- }
- /**
- * testGetCode
- *
- * @return void
- */
- public function testGetCode()
- {
- $eth = $this->eth;
- $eth->getCode('0x407d73d8a49eeb85d32cf465507dd71d507100c1', function ($err, $code) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($code));
- });
- }
- /**
- * testSign
- *
- * @return void
- */
- public function testSign()
- {
- $eth = $this->eth;
- $eth->sign('0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0xdeadbeaf', function ($err, $sign) {
- if ($err !== null) {
- // infura banned us to sign message
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($sign));
- });
- }
- /**
- * testSendTransaction
- *
- * @return void
- */
- public function testSendTransaction()
- {
- $eth = $this->eth;
- $eth->sendTransaction([
- 'from' => "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
- 'to' => "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
- 'gas' => "0x76c0",
- 'gasPrice' => "0x9184e72a000",
- 'value' => "0x9184e72a",
- 'data' => "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
- ], function ($err, $transaction) {
- if ($err !== null) {
- // infura banned us to send transaction
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($transaction));
- });
- }
- /**
- * testSendRawTransaction
- *
- * @return void
- */
- public function testSendRawTransaction()
- {
- $eth = $this->eth;
- $eth->sendRawTransaction('0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675', function ($err, $transaction) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($transaction));
- });
- }
- /**
- * testCall
- *
- * @return void
- */
- public function testCall()
- {
- $eth = $this->eth;
- $eth->call([
- // 'from' => "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
- 'to' => "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
- 'gas' => "0x76c0",
- 'gasPrice' => "0x9184e72a000",
- 'value' => "0x9184e72a",
- 'data' => "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
- ], function ($err, $transaction) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($transaction));
- });
- }
- /**
- * testEstimateGas
- *
- * @return void
- */
- public function testEstimateGas()
- {
- $eth = $this->eth;
- $eth->estimateGas([
- 'from' => "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
- 'to' => "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
- 'gas' => "0x76c0",
- 'gasPrice' => "0x9184e72a000",
- 'value' => "0x9184e72a",
- 'data' => "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
- ], function ($err, $gas) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_numeric($gas->toString()));
- });
- }
- /**
- * testGetBlockByHash
- *
- * @return void
- */
- public function testGetBlockByHash()
- {
- $eth = $this->eth;
- $eth->getBlockByHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', false, function ($err, $block) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue($block !== null);
- });
- }
- /**
- * testGetBlockByNumber
- *
- * @return void
- */
- public function testGetBlockByNumber()
- {
- $eth = $this->eth;
- $eth->getBlockByNumber('latest', false, function ($err, $block) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- // weired behavior, see https://github.com/sc0Vu/web3.php/issues/16
- $this->assertTrue($block !== null);
- });
- }
- /**
- * testGetTransactionByHash
- *
- * @return void
- */
- public function testGetTransactionByHash()
- {
- $eth = $this->eth;
- $eth->getTransactionByHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', function ($err, $transaction) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue($transaction !== null);
- });
- }
- /**
- * testGetTransactionByBlockHashAndIndex
- *
- * @return void
- */
- public function testGetTransactionByBlockHashAndIndex()
- {
- $eth = $this->eth;
- $eth->getTransactionByBlockHashAndIndex('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', '0x0', function ($err, $transaction) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue($transaction !== null);
- });
- }
- /**
- * testGetTransactionByBlockNumberAndIndex
- *
- * @return void
- */
- public function testGetTransactionByBlockNumberAndIndex()
- {
- $eth = $this->eth;
- $eth->getTransactionByBlockNumberAndIndex('0xe8', '0x0', function ($err, $transaction) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue($transaction !== null);
- });
- }
- /**
- * testGetTransactionReceipt
- *
- * @return void
- */
- public function testGetTransactionReceipt()
- {
- $eth = $this->eth;
- $eth->getTransactionReceipt('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', function ($err, $transaction) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue($transaction !== null);
- });
- }
- /**
- * testGetUncleByBlockHashAndIndex
- *
- * @return void
- */
- public function testGetUncleByBlockHashAndIndex()
- {
- $eth = $this->eth;
- $eth->getUncleByBlockHashAndIndex('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', '0x0', function ($err, $uncle) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue($uncle !== null);
- });
- }
- /**
- * testGetUncleByBlockNumberAndIndex
- *
- * @return void
- */
- public function testGetUncleByBlockNumberAndIndex()
- {
- $eth = $this->eth;
- $eth->getUncleByBlockNumberAndIndex('0xe8', '0x0', function ($err, $uncle) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue($uncle !== null);
- });
- }
- /**
- * testGetCompilers
- *
- * @return void
- */
- public function testGetCompilers()
- {
- $eth = $this->eth;
- $eth->getCompilers(function ($err, $compilers) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_array($compilers));
- $this->assertEquals($compilers[0], 'solidity');
- });
- }
- /**
- * testCompileSolidity
- *
- * @return void
- */
- public function testCompileSolidity()
- {
- $eth = $this->eth;
- $eth->compileSolidity('contract test { function multiply(uint a) returns(uint d) { return a * 7; } }', function ($err, $compiled) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($compiled));
- });
- }
- /**
- * testCompileLLL
- *
- * @return void
- */
- public function testCompileLLL()
- {
- $eth = $this->eth;
- $eth->compileLLL('(returnlll (suicide (caller)))', function ($err, $compiled) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($compiled));
- });
- }
- /**
- * testCompileSerpent
- *
- * @return void
- */
- public function testCompileSerpent()
- {
- $eth = $this->eth;
- $eth->compileSerpent('\/* some serpent *\/', function ($err, $compiled) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($compiled));
- });
- }
- /**
- * testNewFilter
- *
- * @return void
- */
- public function testNewFilter()
- {
- $eth = $this->eth;
- $eth->newFilter([
- 'fromBlock' => '0x1',
- 'toBlock' => '0x2',
- 'address' => '0x8888f1f195afa192cfee860698584c030f4c9db1',
- 'topics' => ['0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b', null, ['0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b', '0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc']]
- ], function ($err, $filter) {
- if ($err !== null) {
- // infura banned us to new filter
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($filter));
- });
- }
- /**
- * testNewBlockFilter
- *
- * @return void
- */
- public function testNewBlockFilter()
- {
- $eth = $this->eth;
- $eth->newBlockFilter('0x01', function ($err, $filter) {
- if ($err !== null) {
- // infura banned us to new block filter
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($filter));
- });
- }
- /**
- * testNewPendingTransactionFilter
- *
- * @return void
- */
- public function testNewPendingTransactionFilter()
- {
- $eth = $this->eth;
- $eth->newPendingTransactionFilter(function ($err, $filter) {
- if ($err !== null) {
- // infura banned us to new pending transaction filter
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_string($filter));
- });
- }
- /**
- * testUninstallFilter
- *
- * @return void
- */
- public function testUninstallFilter()
- {
- $eth = $this->eth;
- $eth->uninstallFilter('0x01', function ($err, $filter) {
- if ($err !== null) {
- // infura banned us to uninstall filter
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_bool($filter));
- });
- }
- /**
- * testGetFilterChanges
- *
- * @return void
- */
- public function testGetFilterChanges()
- {
- $eth = $this->eth;
- $eth->getFilterChanges('0x01', function ($err, $changes) {
- if ($err !== null) {
- // infura banned us to get filter changes
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_array($changes));
- });
- }
- /**
- * testGetFilterLogs
- *
- * @return void
- */
- public function testGetFilterLogs()
- {
- $eth = $this->eth;
- $eth->getFilterLogs('0x01', function ($err, $logs) {
- if ($err !== null) {
- // infura banned us to get filter logs
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_array($logs));
- });
- }
- /**
- * testGetLogs
- *
- * @return void
- */
- public function testGetLogs()
- {
- $eth = $this->eth;
- $eth->getLogs([
- 'fromBlock' => '0x1',
- 'toBlock' => '0x2',
- 'address' => '0x8888f1f195afa192cfee860698584c030f4c9db1',
- 'topics' => ['0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b', null, ['0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b', '0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc']]
- ], function ($err, $logs) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_array($logs));
- });
- }
- /**
- * testGetWork
- *
- * @return void
- */
- public function testGetWork()
- {
- $eth = $this->eth;
- $eth->getWork(function ($err, $work) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_array($work));
- });
- }
- /**
- * testSubmitWork
- *
- * @return void
- */
- public function testSubmitWork()
- {
- $eth = $this->eth;
- $eth->submitWork(
- '0x0000000000000001',
- '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
- '0xD1FE5700000000000000000000000000D1FE5700000000000000000000000000'
- , function ($err, $work) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_bool($work));
- });
- }
- /**
- * testSubmitHashrate
- *
- * @return void
- */
- public function testSubmitHashrate()
- {
- $eth = $this->eth;
- $eth->submitHashrate(
- '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
- '0xD1FE5700000000000000000000000000D1FE5700000000000000000000000000'
- , function ($err, $work) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(is_bool($work));
- });
- }
- /**
- * testUnallowedMethod
- *
- * @return void
- */
- public function testUnallowedMethod()
- {
- $this->expectException(RuntimeException::class);
- $eth = $this->eth;
- $eth->hello(function ($err, $hello) {
- if ($err !== null) {
- return $this->assertTrue($err !== null);
- }
- $this->assertTrue(true);
- });
- }
- /**
- * testWrongCallback
- *
- * @return void
- */
- public function testWrongCallback()
- {
- $this->expectException(InvalidArgumentException::class);
- $eth = $this->eth;
- $eth->protocolVersion();
- }
- }
|