sendTransaction.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. require('./exampleBase.php');
  3. $eth = $web3->eth;
  4. echo 'Eth Send Transaction' . PHP_EOL;
  5. $eth->accounts(function ($err, $accounts) use ($eth) {
  6. if ($err !== null) {
  7. echo 'Error: ' . $err->getMessage();
  8. return;
  9. }
  10. $fromAccount = $accounts[0];
  11. $toAccount = $accounts[1];
  12. // get balance
  13. $eth->getBalance($fromAccount, function ($err, $balance) use($fromAccount) {
  14. if ($err !== null) {
  15. echo 'Error: ' . $err->getMessage();
  16. return;
  17. }
  18. echo $fromAccount . ' Balance: ' . $balance . PHP_EOL;
  19. });
  20. $eth->getBalance($toAccount, function ($err, $balance) use($toAccount) {
  21. if ($err !== null) {
  22. echo 'Error: ' . $err->getMessage();
  23. return;
  24. }
  25. echo $toAccount . ' Balance: ' . $balance . PHP_EOL;
  26. });
  27. // send transaction
  28. $eth->sendTransaction([
  29. 'from' => $fromAccount,
  30. 'to' => $toAccount,
  31. 'value' => '0x11'
  32. ], function ($err, $transaction) use ($eth, $fromAccount, $toAccount) {
  33. if ($err !== null) {
  34. echo 'Error: ' . $err->getMessage();
  35. return;
  36. }
  37. echo 'Tx hash: ' . $transaction . PHP_EOL;
  38. // get balance
  39. $eth->getBalance($fromAccount, function ($err, $balance) use($fromAccount) {
  40. if ($err !== null) {
  41. echo 'Error: ' . $err->getMessage();
  42. return;
  43. }
  44. echo $fromAccount . ' Balance: ' . $balance . PHP_EOL;
  45. });
  46. $eth->getBalance($toAccount, function ($err, $balance) use($toAccount) {
  47. if ($err !== null) {
  48. echo 'Error: ' . $err->getMessage();
  49. return;
  50. }
  51. echo $toAccount . ' Balance: ' . $balance . PHP_EOL;
  52. });
  53. });
  54. });