| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace blockchain;
- use GuzzleHttp\Client;
- use think\Config;
- use Tron\Address;
- use Tron\Api;
- use Tron\Exceptions\TronErrorException;
- use Tron\TRC20;
- use Tron\TRX;
- class TronService
- {
- const API_URI = 'https://api.trongrid.io';
- public static $trxWallet;
- public static $address;
- public static $decimal;
- public static $api;
- public function __construct(string $address, int $decimal)
- {
- if (!$address) {
- self::$trxWallet = null;
- }
- $config = Config::get('blockchain.trc');
- $uri = $config['node'] ?? self::API_URI;
- self::$api = new Api(new Client(['base_uri' => $uri]));
- $config = [
- 'contract_address' => $address,// USDT TRC20
- 'decimals' => $decimal,
- ];
- self::$trxWallet = new TRC20(self::$api, $config);
- self::$address = $address;
- self::$decimal = $decimal;
- }
- /**
- * @param string $money_type
- * @return TronService
- */
- public static function instance(string $money_type): TronService
- {
- $config = Config::get('blockchain.trc');
- $money_types = $config['tokens'];
- $address = $money_types[$money_type]['address'] ?? "";
- $decimal = $money_types[$money_type]['decimal'] ?? 0;
- return new self($address, $decimal);
- }
- //TODO main
- /**
- * 生成地址
- * @return array
- */
- public function createAddress(): array
- {
- if (!self::$trxWallet) {
- return [
- 'privateKey' => '',
- 'address' => '',
- 'hexAddress' => ''
- ];
- }
- try {
- $address = self::$trxWallet->generateAddress();
- return [
- 'privateKey' => $address->privateKey,
- 'address' => $address->address,
- 'hexAddress' => $address->hexAddress
- ];
- } catch (\Exception $e) {
- return [
- 'privateKey' => '',
- 'address' => '',
- 'hexAddress' => ''
- ];
- }
- }
- /**
- * 余额
- * @param Address $address
- * @return float
- */
- public function tokenBalance(Address $address): float
- {
- if (!self::$trxWallet) {
- return 0;
- }
- try {
- $balance = self::$trxWallet->balance($address);
- return (float)$balance;
- } catch (\Exception $e) {
- return 0;
- }
- }
- public function balance(string $address)
- {
- $address = new Address($address);
- $trx = new TRX(self::$api);
- return $trx->balance($address);
- }
- /**
- * 交易
- * @param Address $from
- * @param Address $to
- * @param float $amount
- * @return array
- */
- public function transfer(Address $from, Address $to, float $amount): array
- {
- try {
- $trx = new TRX(self::$api);
- $transfer = $trx->transfer($from, $to, $amount);
- return ['msg' => 'success', 'data' => $transfer, 'status' => true];
- } catch (\Exception $e) {
- return ['msg' => $e->getMessage(), 'status' => false, 'data' => $e->getTrace()];
- }
- }
- /**
- * 交易
- * @param Address $from
- * @param Address $to
- * @param float $amount
- * @return array
- */
- public function tokenTransfer(Address $from, Address $to, float $amount): array
- {
- if (!self::$trxWallet) {
- return ['msg' => '代币不支持转账', 'status' => false];
- }
- try {
- $transfer = self::$trxWallet->transfer($from, $to, $amount);
- return ['msg' => 'success', 'data' => $transfer, 'status' => true];
- } catch (\Exception $e) {
- return ['msg' => $e->getMessage(), 'status' => false, 'data' => $e->getTrace()];
- }
- }
- /**
- * 获取交易
- * @param $address
- * @param $start_time
- * @return array
- */
- public function getTransfer($address, $start_time): array
- {
- $contract_address = self::$address;
- try {
- $uri = "https://api.trongrid.io/v1/accounts/{$address}/transactions/trc20";
- return json_decode(do_request($uri, [
- 'min_timestamp' => $start_time,
- 'limit' => 200,
- 'contract_address' => $contract_address,
- 'order_by' => 'block_timestamp,asc'
- ], null, false), true);
- } catch (\Exception $e) {
- return ['msg' => $e->getMessage(), 'status' => false, 'data' => $e->getTrace()];
- }
- }
- }
|