ethers.js: Cannot read property '_hex' of undefined

I’m attempting to use a provider/signer obtained from web3Modal and WalletConnect, and then call a Contract method.

const providerOptions = {
	walletconnect: {
		package: window.WalletConnectProvider.default,
		options: {
			infuraId: INFURAID,
		}
	}
};
web3Modal = new window.Web3Modal.default({
	cacheProvider: false,
	providerOptions,
	disableInjectedProvider: false
});
web3Provider = await web3Modal.connect();
provider = new ethers.providers.Web3Provider(web3Provider).provider;
signer = new ethers.providers.Web3Provider(web3Provider).getSigner();

contract = new ethers.Contract(CONTRACTADDRESS, CONTRACTABI, signer);

The next line throws an error: tx = await contract.myMethod(PARAM1, ethers.utils.formatBytes32String(STRINGPARAM2)); Uncaught TypeError: Cannot read property '_hex' of undefined

This all works just fine when NOT using web3Modal/WalletConnect as a provider, and instead relying on injected MetaMask. I’m not sure what to do or even how to start debugging this. Any help would be wildly appreciated!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

great advice with the console.log. I will try to do that more as I can see how it is helpful along the way. I managed to get it to work by adding quotations in my .env file when defining the variable. I appreciate your help! NEXT_PUBLIC_METAMASK_KEY = YOURPRIVATEKEY NEXT_PUBLIC_METAMASK_KEY = “YOURPRIVATEKEY”

On Wed, Mar 2, 2022 at 11:01 PM Richard Moore @.***> wrote:

I’m not sure what ThirdwebSDK is; the error could be coming from that, perhaps?

Also, can you console.log the environment variable, to make sure the env file is being picked up. It’s a good idea to dump everything to the console to double check things are working.

— Reply to this email directly, view it on GitHub https://github.com/ethers-io/ethers.js/issues/1237#issuecomment-1057668425, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXRCQSPNUBWA4XWKGEWHTRDU6BBSRANCNFSM4WAEFZFA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: <ethers-io/ethers .@.***>

Note: the createToken function reverts if a token was already created for the same address and ticket number. This causes ethers to throw BigNumber conversion error in estimateGas for the InfuraProvider. Will create an issue for this later.

@EvilJordan I was able to create a token successfully using your contract on Rinkeby. Here’s the transaction hash

I also created a sample using ethers.js based on the original WalletConnect vanilla javascript example.

The code to call a contract using ethers.js is here:

https://github.com/yuetloo/web3modal-vanilla-js-example/blob/main/example.js#L176-L206

I used your contract on Rinkeby in the example. So, the Create Token section only shows up if you select to connect to the Rinkeby network.