Contract.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace TronTool;
  3. use InvalidArgumentException;
  4. use Web3\Utils;
  5. use Web3\Contracts\Ethabi;
  6. use Web3\Contracts\Types\Address as EthAddress;
  7. use Web3\Contracts\Types\Boolean;
  8. use Web3\Contracts\Types\Bytes;
  9. use Web3\Contracts\Types\DynamicBytes;
  10. use Web3\Contracts\Types\Integer;
  11. use Web3\Contracts\Types\Str;
  12. use Web3\Contracts\Types\Uinteger;
  13. use Web3\Validators\AddressValidator;
  14. use Web3\Validators\HexValidator;
  15. use Web3\Formatters\AddressFormatter;
  16. use Web3\Validators\StringValidator;
  17. class TronAddress extends EthAddress{
  18. function inputFormat($value, $name){
  19. $hex = Address::decode($value);
  20. return parent::inputFormat($hex,$name);
  21. }
  22. public function outputFormat($value, $name){
  23. $hex = parent::outputFormat($value,$name);
  24. return Address::encode($hex);
  25. }
  26. }
  27. class Contract{
  28. protected $api;
  29. protected $abi;
  30. protected $ethabi;
  31. protected $constructor = [];
  32. protected $functions = [];
  33. protected $events = [];
  34. protected $toAddress;
  35. protected $bytecode;
  36. protected $credential;
  37. function __construct($tronApi,$abi,$credential=null){
  38. $abi = Utils::jsonToArray($abi, 5);
  39. foreach ($abi as $item) {
  40. if (isset($item['type'])) {
  41. if ($item['type'] === 'function') {
  42. $this->functions[$item['name']] = $item;
  43. } elseif ($item['type'] === 'constructor') {
  44. $this->constructor = $item;
  45. } elseif ($item['type'] === 'event') {
  46. $this->events[$item['name']] = $item;
  47. }
  48. }
  49. }
  50. $this->abi = $abi;
  51. $this->api = $tronApi;
  52. $this->credential = $credential;
  53. $this->ethabi = new Ethabi([
  54. 'address' => new TronAddress,
  55. 'bool' => new Boolean,
  56. 'bytes' => new Bytes,
  57. 'dynamicBytes' => new DynamicBytes,
  58. 'int' => new Integer,
  59. 'string' => new Str,
  60. 'uint' => new Uinteger,
  61. ]);
  62. }
  63. function at($address) {
  64. //$this->toAddress = Address::fromBase58($address);
  65. $this->toAddress = $address;
  66. return $this;
  67. }
  68. function bytecode($bytecode){
  69. $this->bytecode = Utils::stripZero($bytecode);
  70. return $this;
  71. }
  72. function credential($credential){
  73. $this->credential = $credential;
  74. return $this;
  75. }
  76. public function deploy()
  77. {
  78. if(is_null($this->credential)){
  79. throw new \Exception('Sender credential not set.');
  80. }
  81. if (isset($this->constructor)) {
  82. $constructor = $this->constructor;
  83. $arguments = func_get_args();
  84. if (count($arguments) < count($constructor['inputs'])) {
  85. throw new InvalidArgumentException('Please make sure you have put all constructor params and callback.');
  86. }
  87. if (!isset($this->bytecode)) {
  88. throw new \InvalidArgumentException('Please call bytecode($bytecode) before new().');
  89. }
  90. $params = array_splice($arguments, 0, count($constructor['inputs']));
  91. $data = $this->ethabi->encodeParameters($constructor, $params);
  92. $data = substr($data,2);
  93. $tx = $this->api->deployContract(
  94. $this->abi,
  95. $this->bytecode,
  96. $data,
  97. 'EzToken',
  98. 0,
  99. $this->credential->address()->base58()
  100. );
  101. $signedTx = $this->credential->signTx($tx);
  102. //var_dump($signedTx);
  103. $ret = $this->api->broadcastTransaction($signedTx);
  104. return (object)[
  105. 'tx' => $signedTx,
  106. 'result' => $ret->result
  107. ];
  108. /*
  109. $transaction = [];
  110. if (count($arguments) > 0) {
  111. $transaction = $arguments[0];
  112. }
  113. $transaction['data'] = '0x' . $this->bytecode . Utils::stripZero($data);
  114. $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){
  115. if ($err !== null) {
  116. return call_user_func($callback, $err, null);
  117. }
  118. return call_user_func($callback, null, $transaction);
  119. });
  120. */
  121. }
  122. }
  123. function send()
  124. {
  125. if(is_null($this->credential)){
  126. throw new \Exception('Sender credential not set.');
  127. }
  128. if (isset($this->functions)) {
  129. $arguments = func_get_args();
  130. $method = array_splice($arguments, 0, 1)[0];
  131. if (!is_string($method) || !isset($this->functions[$method])) {
  132. throw new InvalidArgumentException('Please make sure the method exists.');
  133. }
  134. $function = $this->functions[$method];
  135. if (count($arguments) < count($function['inputs'])) {
  136. throw new InvalidArgumentException('Please make sure you have put all function params and callback.');
  137. }
  138. $params = array_splice($arguments, 0, count($function['inputs']));
  139. $data = $this->ethabi->encodeParameters($function, $params);
  140. $data = substr($data,2);
  141. $functionName = Utils::jsonMethodToString($function);
  142. //var_dump($data,$functionName);
  143. $feeLimit = 1000000000;
  144. $callValue = 0;
  145. $bandwidthLimit = 0;
  146. /*
  147. $payload = [
  148. 'contract_address' => $this->toAddress->hex(),
  149. 'function_selector' => $functionName,
  150. 'parameter' => $data,
  151. 'owner_address' => $this->credential->address()->hex(),
  152. 'fee_limit' => $feeLimit,
  153. 'call_value' => $callValue,
  154. 'consume_user_resource_percent' => $bandwidthLimit,
  155. ];
  156. //var_dump($payload);
  157. $ret = $this->api->post('/wallet/triggersmartcontract', $payload);
  158. */
  159. $ret = $this->api->triggerSmartContract(
  160. $this->toAddress,
  161. $functionName,
  162. $data,
  163. 0,
  164. $this->credential->address()->base58()
  165. );
  166. //var_dump($ret);
  167. if($ret->result->result == false){
  168. throw new Exception('Error build contract transaction.');
  169. }
  170. $signedTx = $this->credential->signTx($ret->transaction);
  171. //var_dump($signedTx);
  172. $ret = $this->api->broadcastTransaction($signedTx);
  173. return (object)[
  174. 'tx' => $signedTx,
  175. 'result' => $ret->result
  176. ];
  177. }
  178. }
  179. function call()
  180. {
  181. if(is_null($this->credential)){
  182. throw new \Exception('Sender credential not set.');
  183. }
  184. if (isset($this->functions)) {
  185. $arguments = func_get_args();
  186. $method = array_splice($arguments, 0, 1)[0];
  187. if (!is_string($method) || !isset($this->functions[$method])) {
  188. throw new InvalidArgumentException('Please make sure the method exists.');
  189. }
  190. $function = $this->functions[$method];
  191. if (count($arguments) < count($function['inputs'])) {
  192. throw new InvalidArgumentException('Please make sure you have put all function params and callback.');
  193. }
  194. $params = array_splice($arguments, 0, count($function['inputs']));
  195. $data = $this->ethabi->encodeParameters($function, $params);
  196. $data = substr($data,2);
  197. $functionName = Utils::jsonMethodToString($function);
  198. //var_dump($data,$functionName);
  199. $feeLimit = 1000000000;
  200. $callValue = 0;
  201. $bandwidthLimit = 0;
  202. /*
  203. $payload = [
  204. 'contract_address' => $this->toAddress->hex(),
  205. 'function_selector' => $functionName,
  206. 'parameter' => $data,
  207. 'owner_address' => $this->credential->address()->hex(),
  208. 'fee_limit' => $feeLimit,
  209. 'call_value' => $callValue,
  210. 'consume_user_resource_percent' => $bandwidthLimit,
  211. ];
  212. //var_dump($payload);
  213. $ret = $this->client->post('/wallet/triggersmartcontract', $payload);
  214. */
  215. $ret = $this->api->triggerSmartContract(
  216. $this->toAddress,
  217. $functionName,
  218. $data,
  219. 0,
  220. $this->credential->address()->base58()
  221. );
  222. //var_dump($ret);
  223. if($ret->result->result == false){
  224. throw new \Exception('Error build contract transaction.');
  225. }
  226. $decoded = $this->ethabi->decodeParameters($function,$ret->constant_result[0]);
  227. return array_values($decoded);
  228. }
  229. }
  230. function events($since = 0){
  231. /*
  232. $api = '/event/contract/' . $this->toAddress->base58();
  233. $payload = [ 'since' => $since, 'sort' => 'block_timestamp' ];
  234. $ret = $this->api->get($api,$payload);
  235. */
  236. $ret = $this->api->getContractEvents($this->toAddress,$since);
  237. return $ret;
  238. }
  239. }