walletconnect-monorepo
walletconnect-monorepo copied to clipboard
Can't call contract function with ethers.js
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.
Hi, I'm also having the same problem, anyone can help ?
were you able to figure this out? @Flawe @qieman
@thegitparticle Using EthereumProvider instead of web3Provider solves this problem
@qieman seems like there is no function EthereumProvider in ethers js?
Hey @thegitparticle @qieman @Flawe is this still an issue?
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
This is good for now, I've let our dev team know and will follow up. Thanks @thegitparticle!
@thegitparticle lmk if this RN example helps: https://github.com/WalletConnect/react-native-examples
@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
@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
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
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.
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?
Seeing this error with WalletConnect V2 also. 'Missing or invalid. request() method: eth_call'
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