You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've got a pretty straightforward nonce incrementer and retry function running. But noticing that for some reason, I'm no longer receiving "INSUFFICIENT FUNDS" as I used to when there are no funds in my wallet.
I'm noticing that transactions are being sent, but never being mined with await transactionResult.wait() never resolving. Occasionally I'll get "TRANSACTION REPLACED" after 5 retries incrementing maxFeePerGas up by 20% each time.
Very curious to know if anyone else has faced this issue
import { ethers, Signer } from "ethers";
import { createNonceManager } from "./nonceManager";
import { validatePayload } from "./validatePayload";
import { parseArgs } from "./parseArgs";
import { serializeBigInt } from "./serializeResult";
export async function signWithCapsuleEthers(signer: Signer, payload: any) {
// console.log("payload", payload);
// Validate payload and ensure type safety
validatePayload(payload);
const args = parseArgs(payload.function_arguments as string);
const provider = new ethers.JsonRpcProvider(payload.primary_rpc_url);
const contract = new ethers.Contract(
payload.contract_address,
payload.contract_abi,
signer
);
const nonceManager = createNonceManager(signer);
const network = await provider.getNetwork();
const gasIncrementPercentage = 20;
// Estimate gas
const [initialGasEstimate, feeData] = await Promise.all([
contract[payload.function].estimateGas(...args, {
from: await signer.getAddress(),
}),
provider.getFeeData(),
]);
let gasToUse =
initialGasEstimate - initialGasEstimate / BigInt(gasIncrementPercentage);
const maxRetries = 5;
let attempt = 0;
while (attempt < maxRetries) {
try {
console.log(`[ATTEMPT ${attempt + 1}] Gas to use: ${gasToUse.toString()}`);
const tx = {
type: 2,
to: payload.contract_address,
data: contract.interface.encodeFunctionData(payload.function, args),
chainId: network.chainId,
nonce: await nonceManager.getNonce(),
maxFeePerGas: feeData.maxFeePerGas ?? ethers.parseUnits("20", "gwei"),
maxPriorityFeePerGas:
feeData.maxPriorityFeePerGas ?? ethers.parseUnits("2", "gwei"),
gasLimit: gasToUse, // Using our incrementing gas value
};
const transactionResult = await signer.sendTransaction(tx);
console.log("[TRANSACTION RESULT]", transactionResult);
console.log("[WAITING FOR TRANSACTION]");
await transactionResult.wait();
console.log("[TRANSACTION COMPLETED]");
nonceManager.incrementNonce();
const serializedResult = serializeBigInt(transactionResult);
return {
success: true,
data: serializedResult,
isReadFunction: false,
contractName: payload.contract_name,
functionName: payload.function,
functionArguments: payload.function_arguments,
};
} catch (error: any) {
console.error(
`[ATTEMPT ${attempt + 1} FAILED]:`,
{
message: error.message,
gasUsed: gasToUse.toString(),
},
`[INITIAL GAS ESTIMATE]: ${initialGasEstimate.toString()}`,
`[MAX FEE PER GAS]: ${feeData.maxFeePerGas?.toString()}`,
`[MAX PRIORITY FEE PER GAS]: ${feeData.maxPriorityFeePerGas?.toString()}`
);
// Increment gas for next attempt
gasToUse = gasToUse + initialGasEstimate / BigInt(gasIncrementPercentage);
attempt++;
// If we've tried all attempts, throw the last error
if (attempt === maxRetries) {
return {
success: false,
error: {
message: error.message,
code: error.code,
details: error.transaction,
finalGasAttempted: gasToUse.toString(),
initialGasEstimate: initialGasEstimate.toString(),
attempts: attempt,
},
};
}
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hey All!
I've got a pretty straightforward nonce incrementer and retry function running. But noticing that for some reason, I'm no longer receiving
"INSUFFICIENT FUNDS"
as I used to when there are no funds in my wallet.I'm noticing that transactions are being sent, but never being mined with
await transactionResult.wait()
never resolving. Occasionally I'll get "TRANSACTION REPLACED" after 5 retries incrementing maxFeePerGas up by 20% each time.Very curious to know if anyone else has faced this issue
Beta Was this translation helpful? Give feedback.
All reactions