TRC20Test.php 3.0 KB

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