TronService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. return [
  54. 'privateKey' => '',
  55. 'address' => '',
  56. 'hexAddress' => ''
  57. ];
  58. }
  59. try {
  60. $address = self::$trxWallet->generateAddress();
  61. return [
  62. 'privateKey' => $address->privateKey,
  63. 'address' => $address->address,
  64. 'hexAddress' => $address->hexAddress
  65. ];
  66. } catch (\Exception $e) {
  67. return [
  68. 'privateKey' => '',
  69. 'address' => '',
  70. 'hexAddress' => ''
  71. ];
  72. }
  73. }
  74. /**
  75. * 余额
  76. * @param Address $address
  77. * @return float
  78. */
  79. public function tokenBalance(Address $address): float
  80. {
  81. if (!self::$trxWallet) {
  82. return 0;
  83. }
  84. try {
  85. $balance = self::$trxWallet->balance($address);
  86. return (float)$balance;
  87. } catch (\Exception $e) {
  88. return 0;
  89. }
  90. }
  91. public function balance(string $address)
  92. {
  93. $address = new Address($address);
  94. $trx = new TRX(self::$api);
  95. return $trx->balance($address);
  96. }
  97. /**
  98. * 交易
  99. * @param Address $from
  100. * @param Address $to
  101. * @param float $amount
  102. * @return array
  103. */
  104. public function transfer(Address $from, Address $to, float $amount): array
  105. {
  106. try {
  107. $trx = new TRX(self::$api);
  108. $transfer = $trx->transfer($from, $to, $amount);
  109. return ['msg' => 'success', 'data' => $transfer, 'status' => true];
  110. } catch (\Exception $e) {
  111. return ['msg' => $e->getMessage(), 'status' => false, 'data' => $e->getTrace()];
  112. }
  113. }
  114. /**
  115. * 交易
  116. * @param Address $from
  117. * @param Address $to
  118. * @param float $amount
  119. * @return array
  120. */
  121. public function tokenTransfer(Address $from, Address $to, float $amount): array
  122. {
  123. if (!self::$trxWallet) {
  124. return ['msg' => '代币不支持转账', 'status' => false];
  125. }
  126. try {
  127. $transfer = self::$trxWallet->transfer($from, $to, $amount);
  128. return ['msg' => 'success', 'data' => $transfer, 'status' => true];
  129. } catch (\Exception $e) {
  130. return ['msg' => $e->getMessage(), 'status' => false, 'data' => $e->getTrace()];
  131. }
  132. }
  133. /**
  134. * 获取交易
  135. * @param $address
  136. * @param $start_time
  137. * @return array
  138. */
  139. public function getTransfer($address, $start_time): array
  140. {
  141. $contract_address = self::$address;
  142. try {
  143. $uri = "https://api.trongrid.io/v1/accounts/{$address}/transactions/trc20";
  144. return json_decode(do_request($uri, [
  145. 'min_timestamp' => $start_time,
  146. 'limit' => 200,
  147. 'contract_address' => $contract_address,
  148. 'order_by' => 'block_timestamp,asc'
  149. ], null, false), true);
  150. } catch (\Exception $e) {
  151. return ['msg' => $e->getMessage(), 'status' => false, 'data' => $e->getTrace()];
  152. }
  153. }
  154. }