Transactor.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace qiniu\services\blockchain\bsc;
  3. use BI\BigInteger;
  4. use Exception;
  5. use qiniu\services\blockchain\bsc\src\Contract;
  6. use qiniu\services\blockchain\bsc\src\Utils;
  7. class Transactor
  8. {
  9. protected $web3;
  10. //context
  11. protected $gasPrice;
  12. protected $gasLimit;
  13. protected $value;
  14. protected $credential;
  15. public function __construct($web3, $credential)
  16. {
  17. $this->web3 = $web3;
  18. $this->credential = $credential;
  19. }
  20. public function setGasPrice($price = null)
  21. {
  22. $this->gasPrice = $price;
  23. return $this;
  24. }
  25. public function setGasLimit($limit = null)
  26. {
  27. $this->gasLimit = $limit;
  28. return $this;
  29. }
  30. public function setValue($value = null)
  31. {
  32. $this->value = $value;
  33. return $this;
  34. }
  35. public function setCredential($credential)
  36. {
  37. $this->credential = $credential;
  38. return $this;
  39. }
  40. protected function netVersion()
  41. {
  42. $cb = new Callback;
  43. $this->web3->net->version($cb);
  44. return $cb->result;
  45. }
  46. protected function getTransactionCount($address)
  47. {
  48. $cb = new Callback;
  49. $this->web3->eth->getTransactionCount($address, 'pending', $cb);
  50. return '0x' . $cb->result->toHex();
  51. }
  52. protected function estimateGasPrice()
  53. {
  54. $cb = new Callback;
  55. $this->web3->eth->gasPrice($cb);
  56. return '0x' . $cb->result->toHex();
  57. }
  58. protected function estimateGasUsage($tx)
  59. {
  60. //var_dump($tx);
  61. $cb = new Callback;
  62. $this->web3->eth->estimateGas($tx, $cb);
  63. return '0x' . $cb->result->toHex();
  64. }
  65. public function transact($tx): string
  66. {
  67. if (!isset($this->credential)) {
  68. throw new Exception('credential not set');
  69. }
  70. $from = $this->credential->getAddress();
  71. $tx['from'] = $from;
  72. if (!isset($tx['nonce'])) {
  73. $tx['nonce'] = $this->getTransactionCount($from);
  74. }
  75. if (!isset($tx['chainId'])) {
  76. $tx['chainId'] = $this->netVersion();
  77. }
  78. if (!isset($tx['value'])) {
  79. if (isset($this->value)) {
  80. $tx['value'] = $this->value;
  81. } else {
  82. $tx['value'] = '0x0';
  83. }
  84. }
  85. if (!isset($tx['gasPrice'])) {
  86. if (isset($this->gasPrice)) {
  87. $tx['gasPrice'] = $this->gasPrice;
  88. } else {
  89. $tx['gasPrice'] = $this->estimateGasPrice();
  90. }
  91. }
  92. if (!isset($tx['gasLimit'])) {
  93. if (isset($this->gasLimit)) {
  94. $tx['gasLimit'] = $this->gasLimit;
  95. } else {
  96. $originChainId = $tx['chainId'];
  97. $tx['chainId'] = '0x' . (new BigInteger($originChainId))->toHex();
  98. $tx['gasLimit'] = $this->estimateGasUsage($tx);
  99. $tx['chainId'] = $originChainId;
  100. }
  101. }
  102. $stx = $this->credential->signTransaction($tx);
  103. $cb = new Callback;
  104. $this->web3->eth->sendRawTransaction($stx, $cb);
  105. return $cb->result;
  106. }
  107. }