TronService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace blockchain;
  3. use GuzzleHttp\Client;
  4. use think\Config;
  5. use Tron\Address;
  6. use Tron\Api;
  7. use Tron\Exceptions\TronErrorException;
  8. use Tron\TRC20;
  9. use Tron\TRX;
  10. class TronService
  11. {
  12. const API_URI = 'https://api.trongrid.io';
  13. public static $trxWallet;
  14. public static $address;
  15. public static $decimal;
  16. public static $api;
  17. public function __construct(string $address, int $decimal)
  18. {
  19. if (!$address) {
  20. self::$trxWallet = null;
  21. }
  22. $config = Config::get('blockchain.trc');
  23. $uri = $config['node'] ?? self::API_URI;
  24. self::$api = new Api(new Client(['base_uri' => $uri]));
  25. $config = [
  26. 'contract_address' => $address,// USDT TRC20
  27. 'decimals' => $decimal,
  28. ];
  29. self::$trxWallet = new TRC20(self::$api, $config);
  30. self::$address = $address;
  31. self::$decimal = $decimal;
  32. }
  33. /**
  34. * @param string $money_type
  35. * @return TronService
  36. */
  37. public static function instance(string $money_type): TronService
  38. {
  39. $config = Config::get('blockchain.trc');
  40. $money_types = $config['tokens'];
  41. $address = $money_types[$money_type]['address'] ?? "";
  42. $decimal = $money_types[$money_type]['decimal'] ?? 0;
  43. return new self($address, $decimal);
  44. }
  45. //TODO main
  46. /**
  47. * 生成地址
  48. * @return array
  49. */
  50. public function createAddress(): array
  51. {
  52. if (!self::$trxWallet) {
  53. var_dump('代币不支持创建地址');die();
  54. return [
  55. 'privateKey' => '',
  56. 'address' => '',
  57. 'hexAddress' => ''
  58. ];
  59. }
  60. try {
  61. $address = self::$trxWallet->generateAddress();
  62. var_dump($address);die();
  63. return [
  64. 'privateKey' => $address->privateKey,
  65. 'address' => $address->address,
  66. 'hexAddress' => $address->hexAddress
  67. ];
  68. } catch (\Exception $e) {
  69. return [
  70. 'privateKey' => '',
  71. 'address' => '',
  72. 'hexAddress' => ''
  73. ];
  74. }
  75. }
  76. /**
  77. * 余额
  78. * @param string $address
  79. * @return float
  80. */
  81. public function tokenBalance(string $address): float
  82. {
  83. if (!self::$trxWallet) {
  84. return 0;
  85. }
  86. try {
  87. $balance = self::$trxWallet->balance(new Address($address));
  88. return (float)$balance;
  89. } catch (\Exception $e) {
  90. return 0;
  91. }
  92. }
  93. public function balance(string $address)
  94. {
  95. $address = new Address($address);
  96. $trx = new TRX(self::$api);
  97. return $trx->balance($address);
  98. }
  99. /**
  100. * 交易
  101. * @param Address $from
  102. * @param Address $to
  103. * @param float $amount
  104. * @return array
  105. */
  106. public function transfer(Address $from, Address $to, float $amount): array
  107. {
  108. try {
  109. $trx = new TRX(self::$api);
  110. $transfer = $trx->transfer($from, $to, $amount);
  111. return ['msg' => 'success', 'data' => $transfer, 'status' => true];
  112. } catch (\Exception $e) {
  113. return ['msg' => $e->getMessage(), 'status' => false, 'data' => $e->getTrace()];
  114. }
  115. }
  116. /**
  117. * 交易
  118. * @param Address $from
  119. * @param Address $to
  120. * @param float $amount
  121. * @return array
  122. */
  123. public function tokenTransfer(Address $from, Address $to, float $amount): array
  124. {
  125. if (!self::$trxWallet) {
  126. return ['msg' => '代币不支持转账', 'status' => false];
  127. }
  128. try {
  129. $transfer = self::$trxWallet->transfer($from, $to, $amount);
  130. return ['msg' => 'success', 'data' => $transfer, 'status' => true];
  131. } catch (\Exception $e) {
  132. return ['msg' => $e->getMessage(), 'status' => false, 'data' => $e->getTrace()];
  133. }
  134. }
  135. /**
  136. * 获取交易
  137. * @param $address
  138. * @param $start_time
  139. * @return array
  140. */
  141. public function getTransfer($address, $start_time): array
  142. {
  143. $contract_address = self::$address;
  144. try {
  145. $uri = "https://api.trongrid.io/v1/accounts/{$address}/transactions/trc20";
  146. return json_decode(do_request($uri, [
  147. 'min_timestamp' => $start_time,
  148. 'limit' => 200,
  149. 'contract_address' => $contract_address,
  150. 'order_by' => 'block_timestamp,asc'
  151. ], null, false), true);
  152. } catch (\Exception $e) {
  153. return ['msg' => $e->getMessage(), 'status' => false, 'data' => $e->getTrace()];
  154. }
  155. }
  156. }