after user 1 created NFT, user 2 bought it.
after user 2 bought the NFT, user 3 canât buy him (metamask shows error while trying to buy âTransaction Error. Exception thrown in contract code.â ) and user 2 canât resell it. here is the error in console log:
index.js:223 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length')
at h.formatParam (index.js:223)
at index.js:99
at Array.map (<anonymous>)
at h.encodeParameters (index.js:93)
at index.js:438
at Array.map (<anonymous>)
at Object.b._encodeMethodABI (index.js:437)
at Object.b._processExecuteArguments (index.js:700)
at Object.b._executeMethod (index.js:719)
at ensureMarketplaceIsApproved (main.js:493)
Here is âat ensureMarketplaceIsApproved (main.js:493)â
const approvedAddress = await contract.methods.getApproved(tokenId).call({from: userAddress});
here is the full function:
/**************** Ensure markeplace approved token ******************/
ensureMarketplaceIsApproved = async (tokenId, tokenAddress) => {
// taking the metamask address of the user
user = await Moralis.User.current();
const userAddress = user.get('ethAddress');
const web3 = await Moralis.enable();
const contract = new web3.eth.Contract(tokenContractAbi, tokenAddress);
const approvedAddress = await contract.methods.getApproved(tokenId).call({from: userAddress});
if(approvedAddress != MARKETPLACE_CONTRACT_ADDRESS) {
await contract.methods.approve(MARKETPLACE_CONTRACT_ADDRESS, tokenId).send({from: userAddress});
}
}