Kit.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace qiniu\services\blockchain\bsc;
  3. use Exception;
  4. class Kit
  5. {
  6. protected $client;
  7. protected $credential;
  8. protected $transactor;
  9. function __construct($client, $credential)
  10. {
  11. $this->client = $client;
  12. $this->credential = $credential;
  13. $this->transactor = new Transactor($client, $credential);
  14. }
  15. function getSender()
  16. {
  17. return $this->credential->getAddress();
  18. }
  19. function balanceOf($addr)
  20. {
  21. return $this->client->getBalance($addr);
  22. }
  23. function transfer($to, $value)
  24. {
  25. $tx = [
  26. 'to' => $to,
  27. 'value' => $value
  28. ];
  29. return $this->transactor->transact($tx);
  30. }
  31. //abi, bytecode, args...
  32. function deployContract()
  33. {
  34. $args = func_get_args();
  35. if (count($args) < 2) {
  36. throw new Exception('no enough deploy parameters');
  37. }
  38. $abi = array_shift($args);
  39. $bytecode = array_shift($args);
  40. $contract = new SmartContract($this->client, $this->credential, $abi);
  41. $contract->bytecode($bytecode);
  42. return $contract->instantiate(...$args);
  43. }
  44. function waitForConfirmation($txid, $timeout = 300)
  45. {
  46. return $this->client->waitForConfirmation($txid, $timeout);
  47. }
  48. function bep20($addr)
  49. {
  50. return new Bep20($this->client, $this->credential, $addr);
  51. }
  52. function bep721($addr)
  53. {
  54. return new Bep721($this->client, $this->credential, $addr);
  55. }
  56. }