Skip to content

Commit

Permalink
Merge pull request #331 from web3p/feature/ws-provider
Browse files Browse the repository at this point in the history
feature: new websocket provider
  • Loading branch information
sc0Vu authored Jan 2, 2024
2 parents a8c543f + 9e6c369 commit 779a095
Show file tree
Hide file tree
Showing 31 changed files with 974 additions and 426 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ composer.lock
# nodejs
node_modules/
package-lock.json
package.json
package.json
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ $web3 = new Web3('http://localhost:8545');
```php
use Web3\Web3;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;

$web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));
$web3 = new Web3(new HttpProvider('http://localhost:8545'));

// timeout
$web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
$web3 = new Web3(new HttpProvider('http://localhost:8545', 0.1));
```

### You can use callback to each rpc call:
Expand All @@ -62,6 +61,43 @@ $web3->clientVersion(function ($err, $version) {
});
```

### Async
```php
use Web3\Web3;
use Web3\Providers\HttpAsyncProvider;

$web3 = new Web3(new HttpAsyncProvider('http://localhost:8545'));

// timeout
$web3 = new Web3(new HttpAsyncProvider('http://localhost:8545', 0.1));

// await
$promise = $web3->clientVersion(function ($err, $version) {
// do somthing
});
Async\await($promise);
```

### Websocket
```php
use Web3\Web3;
use Web3\Providers\WsProvider;

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

// timeout
$web3 = new Web3(new WsProvider('ws://localhost:8545', 0.1));

// await
$promise = $web3->clientVersion(function ($err, $version) {
// do somthing
});
Async\await($promise);

// close connection
$web3->provider->close();
```

### Eth
```php
use Web3\Web3;
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"react/async": "^4.0.0|^3.1.0",
"react/promise": "^2.9.0",
"react/event-loop": "^1.2",
"react/socket": "^1.13"
"react/socket": "^1.13",
"ratchet/pawl": "^0.4.1",
"react/promise-timer": "^1.10"
},
"require-dev": {
"phpunit/phpunit": "~8.0|~9.0"
Expand Down
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();
});
11 changes: 5 additions & 6 deletions src/Contract.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
use InvalidArgumentException;
use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Providers\WsProvider;
use Web3\Utils;
use Web3\Eth;
use Web3\Contracts\Ethabi;
Expand Down Expand Up @@ -118,9 +117,9 @@ public function __construct($provider, $abi, $defaultBlock = 'latest')
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider);

$this->provider = new HttpProvider($requestManager);
$this->provider = new HttpProvider($provider);
} else if (preg_match('/^wss?:\/\//', $provider) === 1) {
$this->provider = new WsProvider($provider);
}
} else if ($provider instanceof Provider) {
$this->provider = $provider;
Expand Down Expand Up @@ -177,7 +176,7 @@ public function __construct($provider, $abi, $defaultBlock = 'latest')
// if (empty($this->provider)) {
// throw new \RuntimeException('Please set provider first.');
// }
// $class = explode('\\', get_class());
// $class = explode('\\', get_class($this));
// if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
// }
// }
Expand Down
14 changes: 7 additions & 7 deletions src/Eth.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Providers\WsProvider;

class Eth
{
Expand Down Expand Up @@ -45,16 +44,17 @@ class Eth
* construct
*
* @param string|\Web3\Providers\Provider $provider
* @param float $timeout
* @return void
*/
public function __construct($provider)
public function __construct($provider, $timeout = 1)
{
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider);

$this->provider = new HttpProvider($requestManager);
$this->provider = new HttpProvider($provider, $timeout);
} else if (preg_match('/^wss?:\/\//', $provider) === 1) {
$this->provider = new WsProvider($provider, $timeout);
}
} else if ($provider instanceof Provider) {
$this->provider = $provider;
Expand All @@ -74,7 +74,7 @@ public function __call($name, $arguments)
throw new \RuntimeException('Please set provider first.');
}

$class = explode('\\', get_class());
$class = explode('\\', get_class($this));

if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
$method = strtolower($class[1]) . '_' . $name;
Expand Down
14 changes: 7 additions & 7 deletions src/Net.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Providers\WsProvider;

class Net
{
Expand Down Expand Up @@ -45,16 +44,17 @@ class Net
* construct
*
* @param string|\Web3\Providers\Provider $provider
* @param float $timeout
* @return void
*/
public function __construct($provider)
public function __construct($provider, $timeout = 1)
{
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider);

$this->provider = new HttpProvider($requestManager);
$this->provider = new HttpProvider($provider, $timeout);
} else if (preg_match('/^wss?:\/\//', $provider) === 1) {
$this->provider = new WsProvider($provider, $timeout);
}
} else if ($provider instanceof Provider) {
$this->provider = $provider;
Expand All @@ -74,7 +74,7 @@ public function __call($name, $arguments)
throw new \RuntimeException('Please set provider first.');
}

$class = explode('\\', get_class());
$class = explode('\\', get_class($this));

if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
$method = strtolower($class[1]) . '_' . $name;
Expand Down
14 changes: 7 additions & 7 deletions src/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Providers\WsProvider;

class Personal
{
Expand Down Expand Up @@ -45,16 +44,17 @@ class Personal
* construct
*
* @param string|\Web3\Providers\Provider $provider
* @param float $timeout
* @return void
*/
public function __construct($provider)
public function __construct($provider, $timeout = 1)
{
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider);

$this->provider = new HttpProvider($requestManager);
$this->provider = new HttpProvider($provider, $timeout);
} else if (preg_match('/^wss?:\/\//', $provider) === 1) {
$this->provider = new WsProvider($provider, $timeout);
}
} else if ($provider instanceof Provider) {
$this->provider = $provider;
Expand All @@ -74,7 +74,7 @@ public function __call($name, $arguments)
throw new \RuntimeException('Please set provider first.');
}

$class = explode('\\', get_class());
$class = explode('\\', get_class($this));

if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
$method = strtolower($class[1]) . '_' . $name;
Expand Down
Loading

0 comments on commit 779a095

Please sign in to comment.