Skip to content

Commit

Permalink
Merge pull request #537 from soroswap/fix/SDEXSwapErrorHandling
Browse files Browse the repository at this point in the history
🥅Add custom error messages for Stellar path payments
  • Loading branch information
esteblock authored Sep 3, 2024
2 parents 6353cf0 + b816a59 commit 433e9a9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
35 changes: 29 additions & 6 deletions src/helpers/horizon/createHorizonTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,36 @@ export const createStellarPathPayment = async (trade: InterfaceTrade, allowedSli
try {
const transactionResult = await serverHorizon?.submitTransaction(transactionToSubmit);
return transactionResult;
} catch (e) {
console.log("Error", e);
// @ts-ignore
if (e.response.data.extras.result_codes.operations.includes("op_under_dest_min")) {
throw new Error("Try increasing slippage");
// @ts-ignore
} catch (e: any) {
const errorResponseArray = e.response.data.extras.result_codes?.operations;
if (errorResponseArray) {
console.log(errorResponseArray)
const operations = e.response.data.extras.result_codes?.operations
for (let operation of operations) {
switch (operation) {
case "op_under_dest_min":
throw new Error("Try increasing slippage");
case "op_underfunded":
throw new Error("Underfunded");
case "op_no_destination":
throw new Error("No destination account");
case "op_no_trust":
throw new Error("No trustline");
case "op_too_few_offers":
throw new Error("Too few offers");
case "op_not_Authorized":
throw new Error("Not authorized");
case "op_over_source_max":
throw new Error("Over source max");
case "AxiosError: Request failed with status code 504":
throw new Error("Connection timeout");
default:
throw new Error(operation)
}
}
}


throw e
}
}
4 changes: 2 additions & 2 deletions src/hooks/useSwapCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RouterMethod, useRouterCallback } from './useRouterCallback';
import { createStellarPathPayment } from 'helpers/horizon/createHorizonTransaction';
import { extractContractError } from 'functions/extractContractError';


// Returns a function that will execute a swap, if the parameters are all valid
// and the user has approved the slippage adjusted input amount for the trade

Expand Down Expand Up @@ -244,8 +245,7 @@ export function useSwapCallback(
if (!result) throw new Error('Cannot create path payment')
return result;
} catch (error: any) {
console.error(error);
throw new Error('Cannot create path payment');
throw new Error(error);
}
default:
throw new Error('Unsupported platform');
Expand Down

0 comments on commit 433e9a9

Please sign in to comment.