walletconnect-monorepo icon indicating copy to clipboard operation
walletconnect-monorepo copied to clipboard

Can't call contract function with ethers.js

Open Flawe opened this issue 3 years ago • 2 comments

Hi,

I'm using ethers.js to call a contract function which works fine when using metamask in chrome but when using metamask on mobile linked with WalletConntect the function call never returns. The code looks something like this

provider = new WalletConnectProvider({
  infuraId: '...',
})
provider.on('chainChanged', onChainIdChanged)
await provider.enable()

// ...
// waiting for onChainIdChanged to get called
// ...

web3Provider = new ethers.providers.Web3Provider(provider)

contract = new ethers.Contract(
  contractAddress,
  contractAbi,
  web3Provider
)

const signer = web3Provider.getSigner()
const res = await contract
  .connect(signer)
  .myFunc(arg0, arg1, {
    value: ethers.utils.parseEther(amt.toString()),
  })

The myFunc call never returns, and I don't see anything in the metamask app.

Flawe avatar Sep 17 '21 22:09 Flawe

Hi, I'm also having the same problem, anyone can help ?

qieman avatar Jul 25 '22 09:07 qieman

were you able to figure this out? @Flawe @qieman

thegitparticle avatar Sep 03 '22 21:09 thegitparticle

@thegitparticle Using EthereumProvider instead of web3Provider solves this problem

qieman avatar Nov 11 '22 15:11 qieman

@qieman seems like there is no function EthereumProvider in ethers js?

thegitparticle avatar Nov 29 '22 15:11 thegitparticle

Hey @thegitparticle @qieman @Flawe is this still an issue?

finessevanes avatar Jan 10 '23 01:01 finessevanes

yea, it is. Even when using WalletConnect 2.0, it is not clear how to call custom ABI contract functions using WalletConnect on React Native. Let me know if any more info is needed

thegitparticle avatar Jan 11 '23 16:01 thegitparticle

This is good for now, I've let our dev team know and will follow up. Thanks @thegitparticle!

finessevanes avatar Jan 11 '23 18:01 finessevanes

@thegitparticle lmk if this RN example helps: https://github.com/WalletConnect/react-native-examples

finessevanes avatar Feb 16 '23 09:02 finessevanes

@finessevanes by a glance at the repo - seems like you got wallet apps but not dapps? The issue here to call a contract function from an ABI from a react native dapp and then send a transaction to sign to the connected wallet app.

correct me if I'm missing something

thegitparticle avatar Feb 17 '23 12:02 thegitparticle

@finessevanes

The same problem I`m using WalletConnect v2 How to call() contract with the new version when i using ethers^6?

It show me error on eth_call

image

As you can see, I put stubs for eth_chainId and eth_accounts, because ethers gives errors on them too

getProvider() {
    return {
      request: async (request: { method: string; params?: Array<any> | Record<string, any> }): Promise<any> => {

        if (request.method === "eth_chainId") {
          return this.wcChainId.split(":")[1];
        }

        if (request.method === "eth_accounts") {
          return [this.selectedAddress]
        }

        return this.client.request({
          topic: this.session.topic,
          chainId: this.wcChainId,
          request: { method: request.method, params: request.params },
        })
      }
    };
  }

Im using getProvider in this case

getContract() {
    const ethersProvider = new ethers.BrowserProvider(getProvider());
    const signer = await ethersProvider.getSigner();
    return new ethers.Contract(contractAddress, abi, signer);
}

And calling..

const contract = await getContract()
await contract.myMethod()

It works with window.ethereum but with WalletConnectV2 ethers show me the error

Ivan-dev-RED avatar Apr 20 '23 10:04 Ivan-dev-RED

Calling contract custom functions using WalletConnect on RN has been an issue since long. No official way of doing it.

And more importantly, ethers v6 is not supported by most libraries yet - WC, Wagmi and more. So, stick with ethers v5.7.[latest] and try it again.

thegitparticle avatar Apr 21 '23 06:04 thegitparticle

Seeing this same error with WC on a web dapp. A ERC20 allowance call fails, giving the 'Missing or invalid. request() method: eth_call'. It seems like other calls works but this specific one doesn't. Any updates around this or fixes?

rpedroni avatar May 22 '23 15:05 rpedroni

Seeing this error with WalletConnect V2 also. 'Missing or invalid. request() method: eth_call'

lm93547 avatar Jun 09 '23 08:06 lm93547

Has anybody solve this issue? I still have {"context": "client"} undefined error and idk why, session exist, topic is actual. But i still confuse with some things, e.g. provider.enable returns empty array of accounts. i'm following this example

lukachi avatar Jul 06 '23 15:07 lukachi