web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.16k stars 4.91k forks source link

"Wagmi Web3js Adapter" example doesn't work #7020

Closed sequencerr closed 4 months ago

sequencerr commented 4 months ago

the docs page with example: https://docs.web3js.org/guides/wagmi_usage/wagmi/#reference-implementation

code

const client = getClient(wagmiConfig, { chainId: mainnet.id });
const accounts = await new Web3(client.transport).eth.getAccounts();

error:

RpcRequestError: RPC Request failed.

URL: https://cloudflare-eth.com/
Request body: {"method":"eth_accounts","params":[]}

Details: Method not supported
Version: viem@2.9.29
SantiagoDevRel commented 4 months ago

Hi @sequencerr thanks for submitting this! right, the method that its sending should be eth_requestAccounts no eth_accounts, the devs will look into this, thank u!

avkos commented 4 months ago

Looks like the network https://cloudflare-eth.com/ does not support eth_accounts method. Check their documentation for available RPC methods. @sequencerr

avkos commented 4 months ago

if you need to send RPC that doesn't exists inside web3js. You can send it using this code

web3.requestManager.send({
  method: 'eth_requestAccounts', // method name
  params: [....], // method params
});
sequencerr commented 4 months ago

https://developers.cloudflare.com/web3/ethereum-gateway/reference/supported-api-methods

Can I leave all in wagmi client.transport but change to some other Ethereum JSON-RPC API node Gateway url?

avkos commented 4 months ago
export function clientToWeb3js(client?: Client<Transport, Chain>) {
    if (!client) {
        return new Web3()
    }

    const {transport} = client

    if (transport.type === 'fallback') {
        return new Web3(transport.transports[0].value.url) // <== HERE
    }
    return new Web3(transport) // <== HERE
}

actually, yes you can set any URL you want But, I think using Wagmi it is better to set the correct chain in Wagmi config https://wagmi.sh/core/api/chains

sequencerr commented 4 months ago
import { mainnet } from '@wagmi/core/chains';
(mainnet.rpcUrls.default.http as unknown as string[]) = ['https://some-eth-rpc.com']; // or maybe better with defineChain()

Thank you!