How to use eth_subscribe with web3.php?
I'm considering to work with eth_subscribe function to be notified about transactions on specific addresses. But when I use it in web3.php project, I receive the error below:
PHP Fatal error: Uncaught RuntimeException: Unallowed rpc method: eth_subscribe...
Is it possible to help me solve this issue please?
I would like to know as well :smiley:
This library has no way to seamlessly extend it. It hardcodes whitelisting and doesn't provide a way to add additional methods without replacing the Web3 and Eth classes. The simplest workaround is bypassing the dynamic features and making the request directly:
$web3 = new Web3('http://example.com');
/** @var RequestManager $rm */
$rm = $web3->getProvider()->getRequestManager();
$rpc = new JSONRPC('eth_chainId', []);
$rm->sendPayload($rpc->toPayloadString(), function ($err, $value) {
// $value has the chain id now
});
You can get closer to the "right" way by creating a class that extends EthMethod
<?php
namespace Your\Project\Methods\Eth;
use Web3\Methods\EthMethod;
class ChainId extends EthMethod { }
$web3 = new Web3('http://example.com');
$web3->getProvider()->send(new ChainId('eth_chainId', []), function ($err, $value) {
// $value has the chain id now
});
It's not possible to remove the need to pass eth_chainId from the constructor with the current implementation of this library.