TronKit.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace TronTool;
  3. class TronKit{
  4. // const HAPY_TOKEN = 'TS2Hzo6KpAc8Ym2nGb3idpMtUpM2GiK2gL';
  5. public $api;
  6. public $credential;
  7. function __construct($tronApi,$credential = null){
  8. $this->api = $tronApi;
  9. $this->credential = $credential;
  10. }
  11. function setCredential($credential){
  12. $this->credential = $credential;
  13. }
  14. function getCredential(){
  15. if(is_null($this->credential)){
  16. throw new \Exception('Credential not set.');
  17. }
  18. return $this->credential;
  19. }
  20. function sendTrx($to,$amount){
  21. $credential = $this->getCredential();
  22. $from = $credential->address()->base58();
  23. $tx = $this->api->createTransaction($to,$amount,$from);
  24. $signedTx = $credential->signTx($tx);
  25. $ret = $this->api->broadcastTransaction($signedTx);
  26. return (object)[
  27. 'txid' => $signedTx->txID,
  28. 'result' => $ret->result
  29. ];
  30. }
  31. function broadcast($tx){
  32. return $this->api->broadcastTransaction($tx);
  33. }
  34. function getTrxBalance($address){
  35. return $this->api->getBalance($address);
  36. }
  37. function contract($abi){
  38. $credential = $this->getCredential();
  39. return new Contract($this->api,$abi,$credential);
  40. return $inst;
  41. }
  42. function trc20($address){
  43. $credential = $this->getCredential();
  44. $inst = new Trc20($this->api,$credential);
  45. return $inst->at($address);
  46. }
  47. }