web3.php icon indicating copy to clipboard operation
web3.php copied to clipboard

How to use eth_subscribe with web3.php?

Open mbsaberi opened this issue 6 years ago • 2 comments

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?

mbsaberi avatar Mar 24 '20 09:03 mbsaberi

I would like to know as well :smiley:

99hops avatar Sep 14 '21 16:09 99hops

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.

swichers avatar Feb 18 '22 04:02 swichers