From e197037bb14b444350aecf735f755332694ea7ba Mon Sep 17 00:00:00 2001 From: Matias Poblete <86752543+MattPoblete@users.noreply.github.com> Date: Mon, 2 Sep 2024 18:30:17 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85Add=20custom=20error=20messages=20f?= =?UTF-8?q?or=20Stellar=20path=20payments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../horizon/createHorizonTransaction.ts | 35 +++++++++++++++---- src/hooks/useSwapCallback.tsx | 4 +-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/helpers/horizon/createHorizonTransaction.ts b/src/helpers/horizon/createHorizonTransaction.ts index f06cd981..38bc0d13 100644 --- a/src/helpers/horizon/createHorizonTransaction.ts +++ b/src/helpers/horizon/createHorizonTransaction.ts @@ -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 } } diff --git a/src/hooks/useSwapCallback.tsx b/src/hooks/useSwapCallback.tsx index 046f6a30..015d80c8 100644 --- a/src/hooks/useSwapCallback.tsx +++ b/src/hooks/useSwapCallback.tsx @@ -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 @@ -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');