TRXTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * This file is part of the tron-php package
  4. *
  5. * @category tron-php
  6. * @package tron-php
  7. * @author Fenguoz <243944672@qq.com>
  8. * @license https://github.com/Fenguoz/tron-php/blob/master/LICENSE MIT
  9. * @link https://github.com/Fenguoz/tron-php
  10. */
  11. namespace Tests;
  12. use GuzzleHttp\Client;
  13. use PHPUnit\Framework\TestCase;
  14. use Tron\Address;
  15. use Tron\Api;
  16. use Tron\TRX;
  17. class TRXTest extends TestCase
  18. {
  19. const URI = 'https://api.shasta.trongrid.io'; // shasta testnet
  20. const ADDRESS = 'TGytofNKuSReFmFxsgnNx19em3BAVBTpVB';
  21. const PRIVATE_KEY = '0xf1b4b7d86a3eff98f1bace9cb2665d0cad3a3f949bc74a7ffb2aaa968c07f521';
  22. const BLOCK_ID = 13402554;
  23. const TX_HASH = '539e6c2429f19a8626fadc1211985728e310f5bd5d2749c88db2e3f22a8fdf69';
  24. private function getTRX()
  25. {
  26. $api = new Api(new Client(['base_uri' => self::URI]));
  27. $trxWallet = new TRX($api);
  28. return $trxWallet;
  29. }
  30. public function testGenerateAddress()
  31. {
  32. $addressData = $this->getTRX()->generateAddress();
  33. var_dump($addressData);
  34. $this->assertTrue(true);
  35. }
  36. public function testPrivateKeyToAddress()
  37. {
  38. $privateKey = self::PRIVATE_KEY;
  39. $addressData = $this->getTRX()->privateKeyToAddress($privateKey);
  40. var_dump($addressData);
  41. $this->assertTrue(true);
  42. }
  43. public function testBalance()
  44. {
  45. $address = new Address(
  46. self::ADDRESS,
  47. '',
  48. $this->getTRX()->tron->address2HexString(self::ADDRESS)
  49. );
  50. $balanceData = $this->getTRX()->balance($address);
  51. var_dump($balanceData);
  52. $this->assertTrue(true);
  53. }
  54. public function testTransfer()
  55. {
  56. $privateKey = self::PRIVATE_KEY;
  57. $address = self::ADDRESS;
  58. $amount = 1;
  59. $from = $this->getTRX()->privateKeyToAddress($privateKey);
  60. $to = new Address(
  61. $address,
  62. '',
  63. $this->getTRX()->tron->address2HexString($address)
  64. );
  65. $transferData = $this->getTRX()->transfer($from, $to, $amount);
  66. var_dump($transferData);
  67. $this->assertTrue(true);
  68. }
  69. public function testBlockNumber()
  70. {
  71. $blockData = $this->getTRX()->blockNumber();
  72. var_dump($blockData);
  73. $this->assertTrue(true);
  74. }
  75. public function testBlockByNumber()
  76. {
  77. $blockID = self::BLOCK_ID;
  78. $blockData = $this->getTRX()->blockByNumber($blockID);
  79. var_dump($blockData);
  80. $this->assertTrue(true);
  81. }
  82. public function testTransactionReceipt()
  83. {
  84. $txHash = self::TX_HASH;
  85. $txData = $this->getTRX()->transactionReceipt($txHash);
  86. var_dump($txData);
  87. $this->assertTrue(true);
  88. }
  89. }