Skip to content

Commit

Permalink
fix: parse jupiter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
NeOMakinG committed Dec 20, 2024
1 parent 38db4d0 commit 59e9a58
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/swapper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export * from './thorchain-utils'
export * from './cowswap-utils'
export * from './safe-utils'
export * from './swappers/CowSwapper'
export * from './swappers/JupiterSwapper'
export * from './swappers/errors'
18 changes: 18 additions & 0 deletions packages/swapper/src/swappers/JupiterSwapper/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { SwapperErrorMapping } from '../baseErrors'
import { SWAPPER_ERRORS } from '../baseErrors'

export const JUPITER_ERROR_VALUES = {
SLIPPAGE_EXCEEDED: 'SlippageToleranceExceeded',
UNDER_MINIMUM_AMOUNT: 'RequireGteViolated',
} as const

export const JUPITER_ERRORS: SwapperErrorMapping[] = [
{
key: SWAPPER_ERRORS.SLIPPAGE_EXCEEDED,
value: JUPITER_ERROR_VALUES.SLIPPAGE_EXCEEDED,
},
{
key: SWAPPER_ERRORS.UNDER_MINIMUM_AMOUNT,
value: JUPITER_ERROR_VALUES.UNDER_MINIMUM_AMOUNT,
},
]
2 changes: 2 additions & 0 deletions packages/swapper/src/swappers/JupiterSwapper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './utils/constants'
export * from './errors'
9 changes: 9 additions & 0 deletions packages/swapper/src/swappers/baseErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum SWAPPER_ERRORS {
SLIPPAGE_EXCEEDED = 'trade.errors.slippageExceeded',
UNDER_MINIMUM_AMOUNT = 'trade.errors.underMinimumAmount',
}

export type SwapperErrorMapping = {
key: SWAPPER_ERRORS
value: string
}
11 changes: 11 additions & 0 deletions packages/swapper/src/swappers/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SWAPPER_ERRORS } from './baseErrors'
import { JUPITER_ERRORS } from './JupiterSwapper/errors'

export { SWAPPER_ERRORS }

export const swapperErrors = [...JUPITER_ERRORS]

export const getSwapperErrorKey = (error: string): SWAPPER_ERRORS | null => {
const matchedError = swapperErrors.find(({ value }) => error.includes(value))
return matchedError ? matchedError.key : null
}
4 changes: 3 additions & 1 deletion src/assets/translations/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,9 @@
"unsafeQuote": "This amount is below the recommended minimum for this pair (%{recommendedMin} %{symbol}). This could cause your trade to fail or loss of funds.",
"rateLimitExceeded": "Rate limit exceeded, try again later",
"maxSlippageExceededWithExpectedSlippage": "The maximum allowed slippage tolerance for this trade has been exceeded during simulation. Expected slippage: %{expectedSlippage}%. Try again with a higher slipppage tolerance.",
"executionRevertedWithExpectedSlippage": "Execution reverted during execution. This may be due to insufficient slippage tolerance. Expected slippage: %{expectedSlippage}%. Try again with a higher slipppage tolerance."
"executionRevertedWithExpectedSlippage": "Execution reverted during execution. This may be due to insufficient slippage tolerance. Expected slippage: %{expectedSlippage}%. Try again with a higher slipppage tolerance.",
"slippageExceeded": "Slippage tolerance exceeded",
"underMinimumAmount": "One or more of the assets in this trade are below the minimum amount required for this trade"
},
"swappingComingSoonForWallet": "Swapping for %{walletName} is coming soon",
"recentTrades": "Recent Trades",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
import {
getHopByIndex,
isExecutableTradeQuote,
swapperErrors,
SwapperName,
TradeExecutionEvent,
} from '@shapeshiftoss/swapper'
Expand Down Expand Up @@ -132,18 +133,31 @@ export const useTradeExecution = (
dispatch(tradeQuoteSlice.actions.setSwapTxPending({ hopIndex, id: confirmedTradeId }))

const onFail = (e: unknown) => {
const message = (() => {
const { message, translationKey } = (() => {
if (e instanceof ChainAdapterError) {
return translate(e.metadata.translation, e.metadata.options)
const swapperError = swapperErrors.find(error => e.message.includes(error.value))

if (swapperError) {
return { message: translate(swapperError.key), translationKey: swapperError.key }
}

return {
message: translate(e.metadata.translation, e.metadata.options),
translationKey: e.metadata.translation,
}
}

return {
message: (e as Error).message ?? undefined,
translationKey: undefined,
}
return (e as Error).message ?? undefined
})()

dispatch(
tradeQuoteSlice.actions.setSwapTxMessage({ hopIndex, message, id: confirmedTradeId }),
)
dispatch(tradeQuoteSlice.actions.setSwapTxFailed({ hopIndex, id: confirmedTradeId }))
showErrorToast(e)
showErrorToast(e, translationKey)

if (!hasMixpanelSuccessOrFailFiredRef.current) {
trackMixpanelEvent(MixPanelEvent.TradeFailed)
Expand Down

0 comments on commit 59e9a58

Please sign in to comment.