I’m working on a project to interact with an ERC721 contract through a Node.js backend using ethers.js. The contract allows minting collectible cards (NFTs). Here’s an overview of my setup:
Main.sol file:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract Main is ERC721, Ownable {
uint256 public nextTokenId;
mapping(uint256 => string) private _tokenURIs;
constructor() ERC721("PokemonNFT", "PKMN") {
nextTokenId = 1;
}
function mintCard(uint256 collectionId, address to, string memory tokenURI) public onlyOwner {
uint256 tokenId = nextTokenId;
_safeMint(to, tokenId);
_setTokenURI(tokenId, tokenURI);
nextTokenId += 1;
}
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
return _tokenURIs[tokenId];
}
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal {
require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token");
_tokenURIs[tokenId] = _tokenURI;
}
}
Node.js Script (index.js):
require('dotenv').config();
const express = require('express');
const { ethers } = require('ethers');
const app = express();
const port = 3000;
// Contract information
const CONTRACT_ADDRESS = 'contract address';
const ABI = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "collectionId",
"type": "uint256"
},
{
"internalType": "address",
"name": "walletAddress",
"type": "address"
},
{
"internalType": "string",
"name": "tokenURI",
"type": "string"
}
],
"name": "mintCard",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "tokenURI",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
];
// Set up provider and signer
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545');
const walletPrivateKey = 'walletkey';
const signer = new ethers.Wallet(walletPrivateKey, provider); // Create signer
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, signer); // Connect contract to signer
// Minting API endpoint
app.get('/mint/:collectionId/:walletAddress/:tokenURI', async (req, res) => {
const { collectionId, walletAddress, tokenURI } = req.params;
try {
const tx = await contract.mintCard(collectionId, walletAddress, tokenURI);
await tx.wait(); // Wait for the transaction to be confirmed
res.status(200).json({ success: true, transactionHash: tx.hash });
} catch (error) {
console.error(error);
res.status(500).json({ success: false, message: error.message });
}
});
// API endpoint to get token information
app.get('/nft/:tokenId', async (req, res) => {
const tokenId = req.params.tokenId; // Get tokenId from the request parameters
try {
const tokenURI = await contract.tokenURI(tokenId); // Call tokenURI function
res.json({ tokenId, tokenURI }); // Respond with tokenId and tokenURI
} catch (error) {
res.status(400).json({ error: 'Token not found' }); // Handle error if token is not found
}
});
// Start the server
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
Problem:
When I try to call the /mint endpoint with this URL:
http://localhost:3000/mint/1/0xYourWalletAddressHere/https://metadata-url
I receive an error saying :
node index.js
Server is running at http://localhost:3000
Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (error={"reason":"Error: Transaction reverted: function selector was not recognized and there's no fallback function","code":"UNPREDICTABLE_GAS_LIMIT","method":"estimateGas","transaction":{"from":"Mywalletaddress","maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"maxFeePerGas":{"type":"BigNumber","hex":"0xc1b71080"},"to":"contractaddress","data":"0xf2afa53c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000007b8d4736382c43a4ea117af8f57ed95823d42c6c0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000003b68747470733a2f2f74636772657075626c69632e636f6d2f6d656469612f62696e6172792f3030302f3530302f3233382f3530303233382e6a70670000000000","type":2,"accessList":null},"error":{"reason":"processing response error","code":"SERVER_ERROR","body":"{\"jsonrpc\":\"2.0\",\"id\":49,\"error\":{\"code\":-32603,\"message\":\"Error: Transaction reverted: function selector was not recognized and there's no fallback function\",\"data\":{\"message\":\"Error: Transaction reverted: function selector was not recognized and there's no fallback function\",\"data\":\"0x\"}}}","error":{"code":-32603,"data":{"message":"Error: Transaction reverted: function selector was not recognized and there's no fallback function","data":"0x"}},"requestBody":"{\"method\":\"eth_estimateGas\",\"params\":[{\"type\":\"0x2\",\"maxFeePerGas\":\"0xc1b71080\",\"maxPriorityFeePerGas\":\"0x59682f00\",\"from\":\"walletaddress\",\"to\":\"contreactaddress\",\"data\":\"0xf2afa53c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000007b8d4736382c43a4ea117af8f57ed95823d42c6c0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000003b68747470733a2f2f74636772657075626c69632e636f6d2f6d656469612f62696e6172792f3030302f3530302f3233382f3530303233382e6a70670000000000\"}],\"id\":49,\"jsonrpc\":\"2.0\"}","requestMethod":"POST","url":"http://127.0.0.1:8545"}}, tx={"data":"0xf2afa53c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000007b8d4736382c43a4ea117af8f57ed95823d42c6c0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000003b68747470733a2f2f74636772657075626c69632e636f6d2f6d656469612f62696e6172792f3030302f3530302f3233382f3530303233382e6a70670000000000","to":{},"from":"0x7b8D4736382c43A4eA117AF8F57eD95823d42c6C","type":2,"maxFeePerGas":{"type":"BigNumber","hex":"0xc1b71080"},"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"nonce":{},"gasLimit":{},"chainId":{}}, code=UNPREDICTABLE_GAS_LIMIT, version=abstract-signer/5.7.0)
at Logger.makeError (/home/kali/collectible-card-game-daar/node_modules/@ethersproject/logger/lib/index.js:238:21)
at Logger.throwError (/home/kali/collectible-card-game-daar/node_modules/@ethersproject/logger/lib/index.js:247:20)
at /home/kali/collectible-card-game-daar/node_modules/@ethersproject/abstract-signer/lib/index.js:365:47
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Promise.all (index 7) {
reason: 'cannot estimate gas; transaction may fail or may require manual gas limit',
code: 'UNPREDICTABLE_GAS_LIMIT',
error: Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="Error: Transaction reverted: function selector was not recognized and there's no fallback function", method="estimateGas", transaction={"from":"walletadress","maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"maxFeePerGas":{"type":"BigNumber","hex":"0xc1b71080"},"to":"contractaddress","data":"0xf2afa53c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000007b8
I also have a deployement script that contain this code”└─$
cat 000001-deploy-game.ts
import 'dotenv/config'
import { DeployFunction } from 'hardhat-deploy/types'
const deployer: DeployFunction = async hre => {
if (hre.network.config.chainId !== 31337) return
const { deployer } = await hre.getNamedAccounts()
await hre.deployments.deploy('Main', { from: deployer, log: true })
}
export default deployer
and an artifcat? json script with”
──(kali㉿kali)-[~/collectible-card-game-daar/contracts/deployments/localhost]
└─$ cat Main.json
{
"address": "contractaddress",
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "string",
"name": "name",
"type": "string"
},
{
"internalType": "int256",
"name": "cardCount",
"type": "int256"
}
],
"name": "createCollection",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
"transactionHash": "0xcd0a1ec8b116b46b2f3c16f8148fc567ab98e37d9a438b700574d80136d95c79",
"receipt": {
"to": null,
"from": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"contractAddress": "contractaddress",
"transactionIndex": 0,
"gasUsed": "706710",
”
and another main.json with: ┌──(kali㉿kali)-[~/…/contracts/artifacts/src/Main.sol]
└─$ cat Main.json
{
"_format": "hh-sol-artifact-1",
"contractName": "Main",
"sourceName": "src/Main.sol",
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "string",
"name": "name",
"type": "string"
},
{
"internalType": "int256",
"name": "cardCount",
"type": "int256"
}
],
"name": "createCollection",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
I’m not sure what i’m doing wrong i’m totally new to this and I need help, thanks.