TronService.php 4.6 KB

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