-
Notifications
You must be signed in to change notification settings - Fork 192
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
base: develop
Are you sure you want to change the base?
Changes from 2 commits
59e9a58
e2ee21d
98d42b4
2de18a2
17f168a
8ed7c3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,23 @@ | ||||||
import type { SwapperErrorMapping } from '../baseErrors' | ||||||
import { SWAPPER_ERRORS } from '../baseErrors' | ||||||
|
||||||
export const JUPITER_ERROR_VALUES = { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also looks better suited to an enum. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in 17f168a |
||||||
SLIPPAGE_EXCEEDED: 'SlippageToleranceExceeded', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in 2de18a2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. q: do we even feel like errors at execution time is something that generalizes to the point we shouls abstract this away? Feels like all we're doing here could be done wherever the error bubbles up ( From what I'm seeing, this is really solana/jupiter specific, and none of our other supported chains would return errors at Tx broadcast time, are you aware of other Solana swappers doing the same as jupiter @NeOMakinG ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way we've done it is now throwing specific error types from the swapper easing the handling and seems like the right home, it's also a good pattern we could take in the future, wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming we want to go from broadcast error to generalized error monad, 100%! |
||||||
UNDER_MINIMUM_AMOUNT: 'RequireGteViolated', | ||||||
CONSUMED_MORE_FEES: 'exceeded CUs meter', | ||||||
} as const | ||||||
|
||||||
export const JUPITER_ERRORS: SwapperErrorMapping[] = [ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. preferably-blocking: This is redundant.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in 17f168a |
||||||
{ | ||||||
key: SWAPPER_ERRORS.SLIPPAGE_EXCEEDED, | ||||||
value: JUPITER_ERROR_VALUES.SLIPPAGE_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, | ||||||
}, | ||||||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './utils/constants' | ||
export * from './errors' |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_EXCEEDED = 'trade.errors.slippageExceeded', | ||
UNDER_MINIMUM_AMOUNT = 'trade.errors.underMinimumAmount', | ||
CONSUMED_MORE_FEES = 'trade.errors.consumedMoreFees', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be weird to have translation keys in the swapper package, but for the sake of simplicity, I wanted to keep it here The very clean way would be to return error codes or strings, and map these errors codes/strings to our translation keys on react app side, but the swapper is not consumed outside of this scope Depending on reviews args, we might want to do it, but my choice has been done already 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the sake of simplicity, would actually prefer to keep just indexes. This is redeclaring keys from |
||
} | ||
|
||
export type SwapperErrorMapping = { | ||
key: SWAPPER_ERRORS | ||
value: string | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this is just a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in 17f168a |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: filename is confusing, wouldn't be sure what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file doesn't exists anymore, but changed to errorPatterns on jup side in 17f168a |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { SWAPPER_ERRORS } from './baseErrors' | ||
import { JUPITER_ERRORS } from './JupiterSwapper/errors' | ||
|
||
export { SWAPPER_ERRORS } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see https://github.com/shapeshift/web/pull/8433/files#r1894285675 re: import/export in the same file, feels like something we may not want to do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not relevant anymore, changed in 17f168a |
||
|
||
export const swapperErrors = [...JUPITER_ERRORS] |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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.", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"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", | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import type { | |
import { | ||
getHopByIndex, | ||
isExecutableTradeQuote, | ||
swapperErrors, | ||
SwapperName, | ||
TradeExecutionEvent, | ||
} from '@shapeshiftoss/swapper' | ||
|
@@ -132,18 +133,37 @@ export const useTradeExecution = ( | |
dispatch(tradeQuoteSlice.actions.setSwapTxPending({ hopIndex, id: confirmedTradeId })) | ||
|
||
const onFail = (e: unknown) => { | ||
const message = (() => { | ||
const { message, translationKey, error } = (() => { | ||
if (e instanceof ChainAdapterError) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another suggestion which would help achieving what we want to here (keep error-handling at broadcast time colocated with its swapper): solana's If you wrap diff --git a/packages/swapper/src/swappers/JupiterSwapper/JupiterSwapper.ts b/packages/swapper/src/swappers/JupiterSwapper/JupiterSwapper.ts
index eb1966aead..deeba7c500 100644
--- a/packages/swapper/src/swappers/JupiterSwapper/JupiterSwapper.ts
+++ b/packages/swapper/src/swappers/JupiterSwapper/JupiterSwapper.ts
@@ -1,0 +2 @@ import type { AssetId } from '@shapeshiftoss/caip'
+import { ChainAdapterError } from '@shapeshiftoss/chain-adapters'
@@ -7,0 +9,7 @@ import { jupiterSupportedChainIds } from './utils/constants'
+export class SolanaLogsError extends Error {
+ constructor(message: string) {
+ super(message)
+ this.name = 'SolanaLogsError'
+ }
+}
+
@@ -9 +17,16 @@ export const jupiterSwapper: Swapper = {
- executeSolanaTransaction,
+ executeSolanaTransaction: async (...args) => {
+ try {
+ const txid = await executeSolanaTransaction(...args)
+ return txid
+ } catch (e: unknown) {
+ if (e instanceof ChainAdapterError) {
+ // replace me with actual pattern matching
+ const isLogsError = true
+ // where errorMember is the actual error from enum
+ if (isLogsError) throw new SolanaLogsError(SLIPPAGE_EXCEEDED)
+ }
+
+ // rethrow is not instanceof ChainAdapterError, shouldn't be the case but jus to satisfy TS
+ throw e
+ }
+ },
diff --git a/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useTradeExecution.tsx b/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useTradeExecution.tsx
index c87f2e4589..ea4894c0e1 100644
--- a/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useTradeExecution.tsx
+++ b/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useTradeExecution.tsx
@@ -24,0 +25 @@ import { LIFI_TRADE_POLL_INTERVAL_MILLISECONDS } from '@shapeshiftoss/swapper/di
+import { SolanaLogsError } from '@shapeshiftoss/swapper/src/swappers/JupiterSwapper/JupiterSwapper'
@@ -136,0 +138,3 @@ export const useTradeExecution = (
+ if (e instanceof SolanaLogsError) {
+ // switch error enum member to translation here
+ } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implemented in 17f168a |
||
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, | ||
error: undefined, | ||
} | ||
} | ||
|
||
return { | ||
message: translate(e.metadata.translation, e.metadata.options), | ||
translationKey: e.metadata.translation, | ||
error: e, | ||
} | ||
} | ||
|
||
return { | ||
message: (e as Error).message ?? undefined, | ||
translationKey: undefined, | ||
error: e, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not relevant anymore, changed in 17f168a |
||
} | ||
return (e as Error).message ?? undefined | ||
})() | ||
|
||
dispatch( | ||
tradeQuoteSlice.actions.setSwapTxMessage({ hopIndex, message, id: confirmedTradeId }), | ||
) | ||
dispatch(tradeQuoteSlice.actions.setSwapTxFailed({ hopIndex, id: confirmedTradeId })) | ||
showErrorToast(e) | ||
showErrorToast(error, translationKey) | ||
|
||
if (!hasMixpanelSuccessOrFailFiredRef.current) { | ||
trackMixpanelEvent(MixPanelEvent.TradeFailed) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused
There was a problem hiding this comment.
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