i am getting the issue
Request_Endpoint: POST /api/contract/gameSession
user balance - → 0.000468434
Request_Endpoint: POST /api/contract/gameSession
user balance - → 0.000468434
Request_Endpoint: POST /api/contract/gameSession
Request_Endpoint: POST /api/contract/gameSession
Request_Endpoint: POST /api/contract/gameSession
user balance - → 0.000466562
Request_Endpoint: POST /api/contract/gameSession
user balance - → 0.000466562
user balance - → 0.000466562
Error while creating game session: _FuelError: The account(s) sending the transaction don’t have enough funds to cover the transaction.
try {
let { privateKey, network, networkNumber } = req.body;
if (!privateKey) {
return res.status(400).json({
message: "private key is required.",
status: 400,
success: false,
});
}
const nodeUrl = getNodeUrl(networkNumber as NetworkType);
const provider = await Provider.create(nodeUrl);
const userPrivateKey = privateKey;
const userWallet = Wallet.fromPrivateKey(userPrivateKey, provider);
const balance = await userWallet.getBalance();
// eslint-disable-next-line camelcase
const contract = new GameSession(
GAME_SESSION_CONTRACT as string,
userWallet
);
const id: IdentityInput = {
Address: {
bits: userWallet.address.toB256().toString(),
},
};
const sessionStarted = await contract.functions
.get_game_session_started(id)
.get();
const calls = [
contract.functions.get_game_session_started(id),
contract.functions.update_game_session_increase_score(id, 1),
contract.functions.get_game_session_score(id),
];
if (!sessionStarted.value) {
calls.push(contract.functions.start_new_game_session(id));
}
const { maxFee: totalMaxFee, gasUsed: totalGasUsed } = await contract
.multiCall(calls)
.getTransactionCost();
const formattedBalance = (parseFloat(balance.toString()) / 10 ** 9).toFixed(
9
);
console.log("user balance - -->", formattedBalance.toString());
if (balance.lt(totalMaxFee)) {
console.log(
"User wallet has insufficient funds, funding from sponsor..."
);
const sponserWalletPvtKey = getSponserWalletPvtKey(network);
const sponserWallet = Wallet.fromPrivateKey(
sponserWalletPvtKey,
provider
);
await sponserWallet.transfer(
userWallet.address,
totalMaxFee.add(new BN(0.0001 * 10 ** 9))
);
console.log("User wallet funded successfully.");
}
const updateTransaction = await contract.functions
.update_game_session_increase_score(id, 1)
.call();
const session = await contract.functions.get_game_session_score(id).get();
return res.status(201).json({
status: 201,
success: true,
message: "Score Updated Successfully",
data: {
transactionHash: updateTransaction.transactionId,
sessionScore: session.value.toNumber(),
},
});
} catch (error: any) {
console.log("Error while creating game session: ", error);
return res
.status(500)
.json({ success: false, error: error?.message ? error.message : error });
}
};
this is my code.
This error occurs when
I am hitting multiple requests at the same time.
when I send a single request, then there is no problem related to funds