so i deployed my contract to the mumbai testnet via hardhat, now i want to interact with the smart Contract via web3.js, but when i call
contract.methods.createNFT(WALLET_ADDRESS, URI).send({ from: account.address })
i get the unknown account error.
This is my setup:
NFT.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract MyNFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("MyNFT", "MNFT") {}
function createNFT(address recipient, string memory uri)
public
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, uri);
return newItemId;
}
}
test.js (node script that should trigger the createNFT function):
const Web3 = require('web3');
async function main() {
const { abi } = require('./artifacts/contracts/NFT.sol/MyNFT.json');
const rpcURL = 'https://rpc-mumbai.maticvigil.com'
const web3 = new Web3(new Web3.providers.HttpProvider(rpcURL));
const address = '0x...'
const WALLET_ADDRESS = "0x..."
const URI = "ipfs://QmUwUaiynu25kAGpuhqNgHehUhEzMGv5Y9SJRCK5aEM6F3"
const account = web3.eth.accounts.privateKeyToAccount("0x...");
const contract = new web3.eth.Contract(abi, address);
try {
r = await contract.methods.createNFT(WALLET_ADDRESS, URI).send({ from: account.address });
console.log(r);
} catch (err) {
console.log(err);
}
}
main().then((r) => {
console.log(r);
}).catch(err => {
console.log(err);
});