Hi everybody,
I’m trying to call a funcion by node, a mint function on my NFT smart contract
I want it mints a new NFT and assign it to the user address that makes the request
This is my contract function
function assign(string calldata tokenURI, bytes calldata bytesId) public {
uint256 _tokenId = abi.decode(bytesId, (uint256));
_mint(msg.sender, _tokenId);
_setTokenURI(_tokenId, tokenURI);
emit Assigned(_tokenId, msg.sender, bytesId);
}
This is my node function
const web3 = new Web3(new Web3.providers.HttpProvider(
"https://rpc.ankr.com/polygon_mumbai"
));
Contract.setProvider("https://rpc.ankr.com/polygon_mumbai");
const contract = new Contract(CONTRACT_NFT_ABI, CONTRACT_NFT_ADDRESS, {
from: userAddress
});
const encoded = contract.methods.assign(tokenURI, plotID).encodeABI();
const block = await web3.eth.getBlock("latest");
const gasLimit = Math.round(block.gasLimit / block.transactions.length);
const tx = {
from: userAddress,
gas: gasLimit,
to: CONTRACT_NFT_ADDRESS,
data: encoded
}
await web3.eth.accounts.signTransaction(tx, PRIVATE_KEY)
.then(signed => {
web3.eth.sendSignedTransaction(signed.rawTransaction, function(error, transactionHash){
if (transactionHash){
res.status(200).send({
res: "OK",
msg: transactionHash
});
}
if (error) {
res.status(400).send({
res: "KO",
msg: error
});
}
})}).catch((err) => {
res.status(400).send({
res: "KO",
msg: err
});
});```
If I call this function I get a transaction and the NFT is sent to the PRIVATE_KEY’s address and not the userAddress, so not what I want
If I use contract.methods.assign(tokenURI, plotID).send({from: userAddress}) I get “unknown account” error
How can I do?
Thanks