TRC20.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. $format = Formatter::toAddressFormat($address->hexAddress);
  25. $body = $this->_api->post('/wallet/triggersmartcontract', [
  26. 'contract_address' => $this->contractAddress->hexAddress,
  27. 'function_selector' => 'balanceOf(address)',
  28. 'parameter' => $format,
  29. 'owner_address' => $address->hexAddress,
  30. ]);
  31. if (isset($body->result->code)) {
  32. throw new TronErrorException(hex2bin($body->result->message));
  33. }
  34. try {
  35. $balance = Utils::toDisplayAmount(hexdec($body->constant_result[0]), $this->decimals);
  36. } catch (InvalidArgumentException $e) {
  37. throw new TronErrorException($e->getMessage());
  38. }
  39. return $balance;
  40. }
  41. public function transfer(Address $from, Address $to, float $amount): Transaction
  42. {
  43. $this->tron->setAddress($from->address);
  44. $this->tron->setPrivateKey($from->privateKey);
  45. $toFormat = Formatter::toAddressFormat($to->hexAddress);
  46. try {
  47. $amount = Utils::toMinUnitByDecimals($amount, $this->decimals);
  48. } catch (InvalidArgumentException $e) {
  49. throw new TronErrorException($e->getMessage());
  50. }
  51. $numberFormat = Formatter::toIntegerFormat($amount);
  52. $body = $this->_api->post('/wallet/triggersmartcontract', [
  53. 'contract_address' => $this->contractAddress->hexAddress,
  54. 'function_selector' => 'transfer(address,uint256)',
  55. 'parameter' => "{$toFormat}{$numberFormat}",
  56. 'fee_limit' => 100000000,
  57. 'call_value' => 0,
  58. 'owner_address' => $from->hexAddress,
  59. ], true);
  60. var_dump($body);die;
  61. if (isset($body['result']['code'])) {
  62. throw new TransactionException(hex2bin($body['result']['message']));
  63. }
  64. try {
  65. $tradeobj = $this->tron->signTransaction($body['transaction']);
  66. $response = $this->tron->sendRawTransaction($tradeobj);
  67. } catch (TronException $e) {
  68. throw new TransactionException($e->getMessage(), $e->getCode());
  69. }
  70. if (isset($response['result']) && $response['result'] == true) {
  71. return new Transaction(
  72. $body['transaction']['txID'],
  73. $body['transaction']['raw_data'],
  74. 'PACKING'
  75. );
  76. } else {
  77. throw new TransactionException('Transfer Fail');
  78. }
  79. }
  80. }