From 809ffc43877b588ed265b9af293eeb4c24161073 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Wed, 20 Dec 2017 16:13:35 +0800 Subject: [PATCH] sendTransaction example --- examples/accounts.php | 5 +-- examples/exampleBase.php | 7 +++++ examples/sendTransaction.php | 60 ++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 examples/exampleBase.php create mode 100644 examples/sendTransaction.php diff --git a/examples/accounts.php b/examples/accounts.php index 3125d34..6f3dc31 100644 --- a/examples/accounts.php +++ b/examples/accounts.php @@ -1,10 +1,7 @@ eth; echo 'Eth Get Account and Balance' . PHP_EOL; diff --git a/examples/exampleBase.php b/examples/exampleBase.php new file mode 100644 index 0000000..9f335f1 --- /dev/null +++ b/examples/exampleBase.php @@ -0,0 +1,7 @@ +eth; + +echo 'Eth Send Transaction' . PHP_EOL; +$eth->accounts(function ($err, $accounts) use ($eth) { + if ($err !== null) { + echo 'Error: ' . $err->getMessage(); + return; + } + $fromAccount = $accounts->result[0]; + $toAccount = $accounts->result[1]; + + // get balance + $eth->getBalance($fromAccount, function ($err, $balance) use($fromAccount) { + if ($err !== null) { + echo 'Error: ' . $err->getMessage(); + return; + } + echo $fromAccount . ' Balance: ' . $balance->result . PHP_EOL; + }); + $eth->getBalance($toAccount, function ($err, $balance) use($toAccount) { + if ($err !== null) { + echo 'Error: ' . $err->getMessage(); + return; + } + echo $toAccount . ' Balance: ' . $balance->result . PHP_EOL; + }); + + // send transaction + $eth->sendTransaction([ + 'from' => $fromAccount, + 'to' => $toAccount, + 'value' => '0x11' + ], function ($err, $transaction) use ($eth, $fromAccount, $toAccount) { + if ($err !== null) { + echo 'Error: ' . $err->getMessage(); + return; + } + echo 'Tx hash: ' . $transaction->result . PHP_EOL; + + // get balance + $eth->getBalance($fromAccount, function ($err, $balance) use($fromAccount) { + if ($err !== null) { + echo 'Error: ' . $err->getMessage(); + return; + } + echo $fromAccount . ' Balance: ' . $balance->result . PHP_EOL; + }); + $eth->getBalance($toAccount, function ($err, $balance) use($toAccount) { + if ($err !== null) { + echo 'Error: ' . $err->getMessage(); + return; + } + echo $toAccount . ' Balance: ' . $balance->result . PHP_EOL; + }); + }); +}); \ No newline at end of file