Hi,
I’m building an API that calls a function when a user calls the API. It calls a so called claim function. But when I try to send 2 API requests at the same time the program fails because it still has a pending tx and it gives the following error: Error: Returned error: replacement transaction underpriced.
But I do not want to replace the prior transaction. I simply want to just send another transaction to the mempool without waiting for the prior transaction to complete. Does anyone know how to do this? I’ve read about nonce but giving a nonce will not work and still ask for a replacement.
app.post("/claim", cors(corsOptions), (req, res) => {
console.log(req.query.address);
executeClaim(req.query.address);
res.json({
test: req.body,
});
});
async function executeClaim(claimerAddress) {
console.log("initiating new claim");
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;
contract.defaultChain = "rinkeby";
contract.defaultHardfork = "london";
const gasLimit = await contract.methods.claim(claimerAddress).estimateGas({
from: account.address,
});
console.log(gasLimit);
const nonce = await web3.eth.getTransactionCount(web3.eth.defaultAccount,);
await contract.methods
.claim(claimerAddress)
.send({ from: account.address, gasLimit: gasLimit, nonce: nonce })
.then(function (receipt) {
console.log("Succesfully Claimed: ", receipt);
})
.catch((err) => {
console.log("Error [claim]: ", err);
});
}
So what happens is:
Person 1 and Person 2 call the API at the same time. The API will send Person 1’s tx (claim function) then tries to send Person 2’s tx but in the catch I get the error: Error: Returned error: replacement transaction underpriced