Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse jupiter errors to human readable translations #8433

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/swapper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './thorchain-utils'
export * from './cowswap-utils'
export * from './safe-utils'
export * from './swappers/CowSwapper'
export * from './swappers/JupiterSwapper'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused

Suggested change
export * from './swappers/JupiterSwapper'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used again in 2de18a2

17 changes: 16 additions & 1 deletion packages/swapper/src/swappers/JupiterSwapper/JupiterSwapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@ import type { Asset } from '@shapeshiftoss/types'

import { executeSolanaTransaction } from '../..'
import type { BuyAssetBySellIdInput, Swapper } from '../../types'
import { JUPITER_ERRORS, SolanaLogsError } from './errors'
import { jupiterSupportedChainIds } from './utils/constants'

export const jupiterSwapper: Swapper = {
executeSolanaTransaction,
executeSolanaTransaction: async (...args) => {
try {
const txid = await executeSolanaTransaction(...args)
return txid
} catch (e) {
if (e instanceof Error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove me, all errors are instanceof Error

const errorMessage = e.message
const swapperError = JUPITER_ERRORS.find(error => errorMessage.includes(error.value))

if (swapperError) throw new SolanaLogsError(swapperError.key)
}

throw e
}
},

filterAssetIdsBySellable: (assets: Asset[]): Promise<AssetId[]> => {
return Promise.resolve(
Expand Down
30 changes: 30 additions & 0 deletions packages/swapper/src/swappers/JupiterSwapper/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { SwapperErrorMapping } from '../baseErrors'
import { SWAPPER_ERRORS } from '../baseErrors'

export class SolanaLogsError extends Error {
constructor(message: string) {
super(message)
this.name = 'SolanaLogsError'
}
}

export const JUPITER_ERROR_VALUES = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the naming here, this is rather a subtring check , maybe something like pattern vernacular would be a better fit?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also looks better suited to an enum.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in 17f168a

SLIPPAGE_TOLERANCE_EXCEEDED: 'SlippageToleranceExceeded',
UNDER_MINIMUM_AMOUNT: 'RequireGteViolated',
CONSUMED_MORE_FEES: 'exceeded CUs meter',
} as const

export const JUPITER_ERRORS: SwapperErrorMapping[] = [
Copy link
Contributor

@gomesalexandre gomesalexandre Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preferably-blocking: This is redundant.

  1. this is a K,V mapping which 1/1 maps the keys and values of JUPITER_ERROR_VALUES
  2. We're effectively reinventing the wheel and creating a poor man's flavour of Object

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in 17f168a

{
key: SWAPPER_ERRORS.SLIPPAGE_TOLERANCE_EXCEEDED,
value: JUPITER_ERROR_VALUES.SLIPPAGE_TOLERANCE_EXCEEDED,
},
{
key: SWAPPER_ERRORS.UNDER_MINIMUM_AMOUNT,
value: JUPITER_ERROR_VALUES.UNDER_MINIMUM_AMOUNT,
},
{
key: SWAPPER_ERRORS.CONSUMED_MORE_FEES,
value: JUPITER_ERROR_VALUES.CONSUMED_MORE_FEES,
},
]
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'
10 changes: 10 additions & 0 deletions packages/swapper/src/swappers/baseErrors.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if we use indexes this goes away, feels weird to import and export this guy in the same file (errors.ts)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 17f168a

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export enum SWAPPER_ERRORS {
SLIPPAGE_TOLERANCE_EXCEEDED = 'trade.errors.slippageExceeded',
UNDER_MINIMUM_AMOUNT = 'trade.errors.underMinimumAmount',
CONSUMED_MORE_FEES = 'trade.errors.consumedMoreFees',
}

export type SwapperErrorMapping = {
key: SWAPPER_ERRORS
value: string
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/shapeshift/web/pull/8433/files#r1894277094, we're effectively reinventing objects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is just a Record<SWAPPER_ERRORS, string>.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in 17f168a

5 changes: 4 additions & 1 deletion src/assets/translations/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,10 @@
"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.",
Copy link
Contributor

@gomesalexandre gomesalexandre Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"slippageExceeded": "Slippage tolerance exceeded.",
"slippageToleranceExceeded": "Slippage tolerance exceeded.",

"underMinimumAmount": "One or more of the assets in this trade are below the minimum amount required for this trade.",
"consumedMoreFees": "The transaction did revert because the fees consumed by this trade were higher than expected."
},
"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,
SolanaLogsError,
SwapperName,
TradeExecutionEvent,
} from '@shapeshiftoss/swapper'
Expand Down Expand Up @@ -133,6 +134,10 @@ export const useTradeExecution = (

const onFail = (e: unknown) => {
const message = (() => {
if (e instanceof SolanaLogsError) {
return translate(e.message)
}

if (e instanceof ChainAdapterError) {
return translate(e.metadata.translation, e.metadata.options)
}
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/useErrorToast/useErrorToast.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useToast } from '@chakra-ui/react'
import { ChainAdapterError } from '@shapeshiftoss/chain-adapters'
import { SolanaLogsError } from '@shapeshiftoss/swapper'
import type { InterpolationOptions } from 'node-polyglot'
import { useCallback } from 'react'
import { useTranslate } from 'react-polyglot'
Expand All @@ -19,6 +20,10 @@ export const useErrorToast = () => {
errorMsgTranslationOptions?: InterpolationOptions,
) => {
const translationArgs = (() => {
if (error instanceof SolanaLogsError) {
return [error.message]
}

// Chain adapter errors take priority
if (error instanceof ChainAdapterError) {
return [error.metadata.translation, error.metadata.options]
Expand Down