Issue interacting with contract while using cloud functions

I am trying to fetch the metadata of a erc721 token by calling the tokenURI function. This is being done using cloud functions:

const web3 = Moralis.web3ByChain(“0x13881”); // Polygon mumbai
const abi = Moralis.abis.erc721;

   // create contract instance
   const contract = new web3.eth.Contract(abi, contractAddress);
   
   // get contract name
   //const name = await contract.methods.name().call()
   
   let uri = await contract.methods.tokenURI(tokenId).call();

When looking at the logs, I receive this error:

Failed running cloud function HandleTransfer for user undefined with:
Input: {“contractAddress”:“0x06e65c70b752b996e3dc0c66c8424e14b52b8266”,“from”:“0x0000000000000000000000000000000000000000”,“tokenId”:“36”}
Error: {“message”:“Cannot read properties of undefined (reading ‘erc721’)”,“code”:141}

As you can see the contract address and tokenId seem to be correct. I was previously able to call this the other day with no issue. Im wondering if it is a abi issue.

Would appreciate some help.

I think that this is the problem, in cloud code that abi may not be accessible, you may need to copy the ABI in cloud code

1 Like

That could be it, think I could just pass in the abi to call the tokenURI function then? Like this:

{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “tokenId”,
“type”: “uint256”
}
],
“name”: “tokenURI”,
“outputs”: [
{
“internalType”: “string”,
“name”: “”,
“type”: “string”
}
],
“stateMutability”: “view”,
“type”: “function”
}

Yes, you can do it like that

1 Like