Skip to content

Commit

Permalink
Add ws example accounts2.php
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Dec 28, 2023
1 parent 6e833e0 commit eba1833
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/accounts2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

require('./exampleBase.php');
use React\Async;
use React\Promise;
use Web3\Web3;

$web3 = new Web3('ws://127.0.0.1:8545');

$eth = $web3->eth;

echo 'Eth Get Account and Balance' . PHP_EOL;
$promises = [];
$promises[] = $eth->accounts(function ($err, $accounts) use ($eth) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}

foreach ($accounts as $account) {
echo 'Account: ' . $account . PHP_EOL;

$promises[] = $eth->getBalance($account, function ($err, $balance) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
echo 'Balance: ' . $balance . PHP_EOL;
});
}

// wait all promises
Async\await(Promise\all($promises));
echo 'close connection...' . PHP_EOL;
$eth->provider->close();
});

0 comments on commit eba1833

Please sign in to comment.