This is my function:
async function mintNFT(tokenURI) {
const nonce = await web3.eth.getTransactionCount(PUBLIC_KEY, "latest"); //get latest nonce
//the transaction
const tx = {
from: PUBLIC_KEY,
to: contractAddress,
nonce: nonce,
gas: 500000,
data: nftContract.methods.mintNFT(PUBLIC_KEY, tokenURI).encodeABI(),
};
const signPromise = web3.eth.accounts.signTransaction(tx, PRIVATE_KEY);
signPromise
.then((signedTx) => {
web3.eth.sendSignedTransaction(
signedTx.rawTransaction,
function (err, hash) {
console.log(
"The hash of your transaction is: ",
hash,
"\nCheck Alchemy's Mempool to view the status of your transaction!"
);
if (!err) {
} else {
console.log(
"Something went wrong when submitting your transaction:",
err
);
}
}
);
})
.catch((err) => {
console.log("Promise failed:", err);
});
}
I have tried run it in a loop:
for (let i = 0; i < 10000; i++) {
mintNFT("https://ipfs.io/ipfs/cid/" + i);
}
But it has error every iteration:
Something went wrong when submitting your transaction: Error: Returned error: replacement transaction underpriced
at Object.ErrorResponse (/home/user/Projects/contract/node_modules/web3-core-helpers/lib/errors.js:28:19)
at /home/user/Projects/contract/node_modules/web3-core-requestmanager/lib/index.js:302:36
at /home/user/Projects/contract/node_modules/@alch/alchemy-web3/dist/cjs/util/promises.js:28:9
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
data: null
}
But if i run function one time outside a loop, it has no errors.
How to run that function in a loop or how to wrap function code with loop?