Skip to content

Commit

Permalink
Merge branch 'develop' into fix_wc_web3modal_z_index
Browse files Browse the repository at this point in the history
  • Loading branch information
NeOMakinG authored Dec 30, 2024
2 parents eff47db + 8008736 commit 43e9ada
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
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'
26 changes: 25 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,34 @@ import type { Asset } from '@shapeshiftoss/types'

import { executeSolanaTransaction } from '../..'
import type { BuyAssetBySellIdInput, Swapper } from '../../types'
import { JUPITER_ERRORS, SolanaLogsError } from './errorPatterns'
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) {
const errorMessage = e.message
const swapperErrorType = Object.keys(JUPITER_ERRORS).reduce(
(acc, errorPattern) => {
if (errorMessage.includes(errorPattern)) {
acc = JUPITER_ERRORS[errorPattern]
}

return acc
},
undefined as undefined | string,
)

if (swapperErrorType) throw new SolanaLogsError(swapperErrorType)
}

throw e
}
},

filterAssetIdsBySellable: (assets: Asset[]): Promise<AssetId[]> => {
return Promise.resolve(
Expand Down
28 changes: 28 additions & 0 deletions packages/swapper/src/swappers/JupiterSwapper/errorPatterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export class SolanaLogsError extends Error {
constructor(name: string) {
super(name)
this.name = name
}
}

export enum JUPITER_ERROR_PATTERNS {
SLIPPAGE_TOLERANCE_EXCEEDED = 'SlippageToleranceExceeded',
JUPITER_SLIPPAGE_ERROR_CODE = '0x1771',
UNDER_MINIMUM_AMOUNT = 'RequireGteViolated',
CONSUMED_MORE_FEES = 'exceeded CUs meter',
}

export enum JUPITER_ERROR_NAME {
SLIPPAGE_TOLERANCE_EXCEEDED = 'SlippageToleranceExceeded',
UNDER_MINIMUM_AMOUNT = 'UnderMinimumAmount',
CONSUMED_MORE_FEES = 'ConsumedMoreFees',
}

export const JUPITER_ERRORS: Record<JUPITER_ERROR_PATTERNS | string, string> = {
[JUPITER_ERROR_PATTERNS.SLIPPAGE_TOLERANCE_EXCEEDED]:
JUPITER_ERROR_NAME.SLIPPAGE_TOLERANCE_EXCEEDED,
[JUPITER_ERROR_PATTERNS.JUPITER_SLIPPAGE_ERROR_CODE]:
JUPITER_ERROR_NAME.SLIPPAGE_TOLERANCE_EXCEEDED,
[JUPITER_ERROR_PATTERNS.UNDER_MINIMUM_AMOUNT]: JUPITER_ERROR_NAME.UNDER_MINIMUM_AMOUNT,
[JUPITER_ERROR_PATTERNS.CONSUMED_MORE_FEES]: JUPITER_ERROR_NAME.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 './errorPatterns'
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.",
"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,12 +17,14 @@ import type {
import {
getHopByIndex,
isExecutableTradeQuote,
SolanaLogsError,
SwapperName,
TradeExecutionEvent,
} from '@shapeshiftoss/swapper'
import { LIFI_TRADE_POLL_INTERVAL_MILLISECONDS } from '@shapeshiftoss/swapper/dist/swappers/LifiSwapper/LifiSwapper'
import type { CosmosSdkChainId } from '@shapeshiftoss/types'
import type { TypedData } from 'eip-712'
import camelCase from 'lodash/camelCase'
import { useCallback, useEffect, useMemo, useRef } from 'react'
import { useTranslate } from 'react-polyglot'
import { useErrorToast } from 'hooks/useErrorToast/useErrorToast'
Expand Down Expand Up @@ -133,6 +135,10 @@ export const useTradeExecution = (

const onFail = (e: unknown) => {
const message = (() => {
if (e instanceof SolanaLogsError) {
return translate(`trade.errors.${camelCase(e.name)}`)
}

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

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

0 comments on commit 43e9ada

Please sign in to comment.