TRC20Test.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\TRC20;
  17. class TRC20Test 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. const CONTRACT = [
  25. 'contract_address' => 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', // USDT TRC20
  26. 'decimals' => 6,
  27. ];
  28. private function getTRC20()
  29. {
  30. $api = new Api(new Client(['base_uri' => self::URI]));
  31. $config = self::CONTRACT;
  32. $trxWallet = new TRC20($api, $config);
  33. return $trxWallet;
  34. }
  35. public function testGenerateAddress()
  36. {
  37. $addressData = $this->getTRC20()->generateAddress();
  38. var_dump($addressData);
  39. $this->assertTrue(true);
  40. }
  41. public function testPrivateKeyToAddress()
  42. {
  43. $privateKey = self::PRIVATE_KEY;
  44. $addressData = $this->getTRC20()->privateKeyToAddress($privateKey);
  45. var_dump($addressData);
  46. $this->assertTrue(true);
  47. }
  48. public function testBalance()
  49. {
  50. $address = new Address(
  51. self::ADDRESS,
  52. '',
  53. $this->getTRC20()->tron->address2HexString(self::ADDRESS)
  54. );
  55. $balanceData = $this->getTRC20()->balance($address);
  56. var_dump($balanceData);
  57. $this->assertTrue(true);
  58. }
  59. public function testTransfer()
  60. {
  61. $privateKey = self::PRIVATE_KEY;
  62. $address = self::ADDRESS;
  63. $amount = 1;
  64. $from = $this->getTRC20()->privateKeyToAddress($privateKey);
  65. $to = new Address(
  66. $address,
  67. '',
  68. $this->getTRC20()->tron->address2HexString($address)
  69. );
  70. $transferData = $this->getTRC20()->transfer($from, $to, $amount);
  71. var_dump($transferData);
  72. $this->assertTrue(true);
  73. }
  74. public function testBlockNumber()
  75. {
  76. $blockData = $this->getTRC20()->blockNumber();
  77. var_dump($blockData);
  78. $this->assertTrue(true);
  79. }
  80. public function testBlockByNumber()
  81. {
  82. $blockID = self::BLOCK_ID;
  83. $blockData = $this->getTRC20()->blockByNumber($blockID);
  84. var_dump($blockData);
  85. $this->assertTrue(true);
  86. }
  87. public function testTransactionReceipt()
  88. {
  89. $txHash = self::TX_HASH;
  90. $txData = $this->getTRC20()->transactionReceipt($txHash);
  91. var_dump($txData);
  92. $this->assertTrue(true);
  93. }
  94. }