[SOLVED] moralis.executeFunction with web3Auth provider

I worked through the Moralis tutorial and want to extend it with Web3Auth authentication
Moralis tutorial: https://moralis.io/how-to-create-your-own-metaverse/
Web3Auth authentication integration: https://docs.moralis.io/moralis-dapp/users/web3-login/web3auth
Strangely
const transaction = await Moralis.executeFunction(contractOptions);
throws an error
but isPlotAssigned works
return await Moralis.executeFunction(contractOptions);

Error: logic.js:596 Error: Internal JSON-RPC error. at d ([email protected]:2:1068648) at Object.internal ([email protected]:2:1069255) at t.sendAsync ([email protected]:2:396381) at async t.request ([email protected]:2:396651)

What does your authenticate and contractOptions code look like? Did you set the right chainId for the contract executeFunction will be run on? E.g.

authenticate({
  chainId: '0x13881',
  provider: 'web3Auth',
  clientId: '',
});

Yes. Looks exactly as you have it - with the web3Auth and the same chainId

const contractOptions = {
        contractAddress: contractAddress,
        abi: contractABI,
        functionName: "assign",
        params: {
            tokenURI:_tokenURI,
            bytesId:document.getElementById("plotID").value
        }
    }

Can you give specific details - contract address, ABI, static parameter values, etc. Try hardcoded parameters if you haven’t already. Are you using an external wallet or the wallet address generated by Web3Auth? Make sure you have enough native balance for gas.

What is isPlotAssigned?

The tutorial is published on moralis https://moralis.io/how-to-create-your-own-metaverse/
isPlotAssigned is a function in the code of the tutorial https://github.com/DanielMoralisSamples/33_MetaverseLand
So steps to reproduce are - make the above code work with Polygon Mumbai (Matic)/Meta mask wallet as described.
Then follow the steps to change the authentication provider to Web3Auth https://docs.moralis.io/moralis-dapp/users/web3-login/web3auth
In index.html I have

 <script src="https://unpkg.com/@web3auth/[email protected]"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script>
        <script src="https://unpkg.com/[email protected]/dist/moralis.js"></script>
        <script src="static/js/logic.js"></script>
   Change authentication to web3Auth provider as noted in your response;

If you still have issues you can post specific details of your contract call code.

const transaction = await Moralis.executeFunction(contractOptions);
contractOptions is

{"contractAddress":"0x1Ee9280E238Ab594a46A901b8649E8f153334721","abi":[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"approved","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":false,"internalType":"bool","name":"approved","type":"bool"}],"name":"ApprovalForAll","type":"event"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"approve","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"tokenURI","type":"string"},{"internalType":"bytes","name":"bytesId","type":"bytes"}],"name":"assign","outputs":[],"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":true,"internalType":"address","name":"assignee","type":"address"},{"indexed":false,"internalType":"bytes","name":"bytesId","type":"bytes"}],"name":"Assigned","type":"event"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"bool","name":"approved","type":"bool"}],"name":"setApprovalForAll","outputs":[],"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"transferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"bytesId","type":"bytes"}],"name":"exist","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"getApproved","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"operator","type":"address"}],"name":"isApprovedForAll","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"tokenURI","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}],"functionName":"assign","params":{"tokenURI":"https://ipfs.moralis.io:2053/ipfs/QmewwPTZ5ihBNm2iNn6zJTMVntLGWSFBBfq9Ep5W8eFBLw","bytesId":"0xc5ee6a8ad434c649a21153047d690f1c4fe088f12791676cf9f91f076e441b26"}}

Error is

Error: Internal JSON-RPC error.
    at d ([email protected]:2:1068648)
    at Object.internal ([email protected]:2:1069255)
    at t.sendAsync ([email protected]:2:396381)
    at async t.request ([email protected]:2:396651)
ο»Ώ

Any updates on this? Facing the same issue here, but for me if I use Metamask when connecting with web3auth my executeFunction works, but when using other authentication method (gmail, for example) I get

Error: Internal JSON-RPC error.
at getEthJsonRpcError (errors.ts:188:1)
at Object.internal (errors.ts:52:1)
at provider.sendAsync (INetworkController.ts:125:1)
at async provider.request (INetworkController.ts:149:1)

This is for when you try any type of function (read or write) or only for write functions?

The default authentication in Moralis is the MetaMask wallet authentication . So, I modified my code that uses web3Auth Authentication as follows and it will additionally authenticate to metamask wallet. Please confirm it works for you @React

   try {    
    user = await Moralis.authenticate({
        provider: "web3Auth",
        clientId: "BBkCAL8e7ExOJkGz70dbxmQMmLeZpcBZxymzjGk-SWr8gKAQ9gW33NQyQquQbm-nvQbsNSJdiHW0No_26v0TYl8",
        chainId: "0x13881"
    });
        console.log(user);
        console.log(user.get('ethAddress'));
      //  const chainIdHex = await Moralis.switchNetwork("0x13881");
   await  Moralis.enableWeb3();
} catch(error) {
    console.log(error);
}
1 Like

In this case, when I call Moralis.enableWeb3() it authenticates with metamask and then looses whatever wallet I was authenticated with via email or any other provider.

Only for write functions, read functions work fine

There may be a way to send parameters to enableWeb3

I agree, I changed my auth code to:

const user = await authenticate({
                      provider: "web3Auth",
                      clientId: process.env.CLIENTID,
                      chainId: "0x13881",
                    });
                    await Moralis.enableWeb3({ provider: "web3Auth", clientId: process.env.CLIENTID, chainId:  "0x13881" });

enableWeb3 worked, but now on calling some write function on my contract I get this error:

Error: [object Object]
    at provider.sendAsync (INetworkController.ts:117:1)
    at async provider.request (INetworkController.ts:141:1)

Do you have enough currency in your generated wallet address to pay for the transaction (for social logins)?

Error: [object Object]

Try console.log on that error object to find out what it is.

Thanks a lot! That was the issue, I added enough currency and all working fine now.

1 Like

@React @glad
i still faced this error, the console log show:

Error: Internal JSON-RPC error.
    at getEthJsonRpcError (errors.ts:188:1)
    at Object.internal (errors.ts:52:1)
    at provider.sendAsync (INetworkController.ts:125:1)
    at async provider.request (INetworkController.ts:149:1)

i followed all these above steps and my account has enough ETH, please help me

Hope you’re connected to the proper network ?

I did it, read function still works as usual so i think network is correct.
Only write function is not work