TRC20.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Tron;
  3. use IEXBase\TronAPI\Exception\TronException;
  4. use Tron\Exceptions\TransactionException;
  5. use Tron\Exceptions\TronErrorException;
  6. use Tron\Support\Formatter;
  7. use Tron\Support\Utils;
  8. use InvalidArgumentException;
  9. class TRC20 extends TRX
  10. {
  11. protected $contractAddress;
  12. public function __construct(Api $_api, array $config)
  13. {
  14. parent::__construct($_api, $config);
  15. $this->contractAddress = new Address(
  16. $config['contract_address'],
  17. '',
  18. $this->tron->address2HexString($config['contract_address'])
  19. );
  20. $this->decimals = $config['decimals'];
  21. }
  22. public function balance(Address $address)
  23. {
  24. $address2 = '41a614f803b6fd780986a42c78ec9c7f77e6ded13c';
  25. $hexAddress = substr($address2, 2);
  26. var_dump($hexAddress);
  27. $format = Formatter::toAddressFormat($hexAddress);
  28. // var_dump($this->contractAddress->hexAddress);
  29. var_dump($format);
  30. var_dump($address->hexAddress);
  31. var_dump($address2);
  32. $body = $this->_api->post('/wallet/triggersmartcontract', [
  33. 'contract_address' => $address2,
  34. 'function_selector' => 'balanceOf(address)',
  35. 'parameter' => $format,
  36. 'owner_address' =>$address2,
  37. ]);
  38. var_dump($body);die();
  39. if (isset($body->result->code)) {
  40. throw new TronErrorException(hex2bin($body->result->message));
  41. }
  42. try {
  43. $balance = Utils::toDisplayAmount(hexdec($body->constant_result[0]), $this->decimals);
  44. } catch (InvalidArgumentException $e) {
  45. throw new TronErrorException($e->getMessage());
  46. }
  47. return $balance;
  48. }
  49. public function transfer(Address $from, Address $to, float $amount): Transaction
  50. {
  51. $this->tron->setAddress($from->address);
  52. $this->tron->setPrivateKey($from->privateKey);
  53. $toFormat = Formatter::toAddressFormat($to->hexAddress);
  54. try {
  55. $amount = Utils::toMinUnitByDecimals($amount, $this->decimals);
  56. } catch (InvalidArgumentException $e) {
  57. throw new TronErrorException($e->getMessage());
  58. }
  59. $numberFormat = Formatter::toIntegerFormat($amount);
  60. $body = $this->_api->post('/wallet/triggersmartcontract', [
  61. 'contract_address' => $this->contractAddress->hexAddress,
  62. 'function_selector' => 'transfer(address,uint256)',
  63. 'parameter' => "{$toFormat}{$numberFormat}",
  64. 'fee_limit' => 100000000,
  65. 'call_value' => 0,
  66. 'owner_address' => $from->hexAddress,
  67. ], true);
  68. if (isset($body['result']['code'])) {
  69. throw new TransactionException(hex2bin($body['result']['message']));
  70. }
  71. try {
  72. $tradeobj = $this->tron->signTransaction($body['transaction']);
  73. $response = $this->tron->sendRawTransaction($tradeobj);
  74. } catch (TronException $e) {
  75. throw new TransactionException($e->getMessage(), $e->getCode());
  76. }
  77. if (isset($response['result']) && $response['result'] == true) {
  78. return new Transaction(
  79. $body['transaction']['txID'],
  80. $body['transaction']['raw_data'],
  81. 'PACKING'
  82. );
  83. } else {
  84. throw new TransactionException('Transfer Fail');
  85. }
  86. }
  87. }