Skip to content

Instantly share code, notes, and snippets.

@NotoriousPyro
Created April 17, 2024 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NotoriousPyro/7c3545bbd6f9046f4becd278470fb7e3 to your computer and use it in GitHub Desktop.
Save NotoriousPyro/7c3545bbd6f9046f4becd278470fb7e3 to your computer and use it in GitHub Desktop.
Error handling with Anchor
try {
const signature = await connection.sendTransaction(tx, {
minContextSlot: blockAndContextInfoManager.minContextSlot,
maxRetries: 3,
skipPreflight: false
});
await confirmTransaction(signature).then(
async => {
if (!knownSwapInstructions.has(routeHash)) {
knownSwapInstructions.set(routeHash, swapInfo);
}
swapInstructionSuccessStats.set(routeHash, {
succeeded: swapInstructionSuccessInfo?.succeeded + 1 || 1, failed: swapInstructionSuccessInfo?.failed || 0
})
});
logger.log("Sig:", signature)
return resolve(signature);
} catch (e) {
if (
e.message
&& typeof e.message === "string"
&& e.message.search(/Blockhash not found/) !== -1
) {
blockAndContextInfoManager.setBlockhashInvalid();
return;
}
if (e.logs) {
try {
const anchorError = AnchorError.parse(e.logs);
const programName = programIdsToString.get(anchorError?.program?.toString()) || "unknown";
if (programName) {
switch (programName) {
case "Jupiter":
switch (anchorError.error.errorCode.code) {
case JupiterErrors.SlippageToleranceExceeded:
return;
default:
break;
}
}
}
swapInstructionSuccessStats.set(routeHash, { succeeded: swapInstructionSuccessInfo?.succeeded || 0, failed: swapInstructionSuccessInfo?.failed + 1 || 1 });
if (debug) {
logger.error(e.message, JSON.stringify({ swapInfo }, null, 2), JSON.stringify(e.logs, null, 2));
return;
}
} catch (e) {
if (debug) {
logger.error("Failed to parse anchor error", JSON.stringify({ swapInfo }, null, 2), e);
return;
}
}
if (debug) {
logger.error("Failed", JSON.stringify({ swapInfo }, null, 2), e);
}
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment