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

Improve max capacity error message #352

Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/nine-emus-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@reservoir0x/relay-kit-ui': patch
---

Fix capacity exceeded error to find max capacity
2 changes: 1 addition & 1 deletion packages/ui/src/components/widgets/SwapWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ const SwapWidget: FC<SwapWidgetProps> = ({
}}
/>
{promptSwitchRoute ? (
<Flex css={{ mt: '6px', gap: '3' }}>
<Flex css={{ gap: '3' }}>
{isCapacityExceededError &&
maxCapacityFormatted &&
maxCapacityFormatted != '0' ? (
Expand Down
31 changes: 23 additions & 8 deletions packages/ui/src/components/widgets/SwapWidgetRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { Execute } from '@reservoir0x/relay-sdk'
import {
calculatePriceTimeEstimate,
calculateRelayerFeeProportionUsd,
extractMaxCapacity,
isHighRelayerServiceFeeUsd,
parseFees
} from '../../utils/quote.js'
Expand Down Expand Up @@ -436,8 +437,21 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({

//Here we fetch the price data and quote data in parallel and then merge into one data model
const isFetchingPrice = isFetchingQuote ?? _isFetchingPrice
const error = _quoteData || isFetchingQuote ? null : quoteError ?? priceError
const price = error ? undefined : _quoteData ?? _priceData
let error = _quoteData || isFetchingQuote ? null : quoteError ?? priceError
let price = error ? undefined : _quoteData ?? _priceData
//The only exception to the rule is a capacity exceeded error, in that case we want to use the error from the price api instead
if (
priceError &&
((priceError as any)?.response?.data?.message?.includes(
'Insufficient relayer liquidity'
) ||
(priceError as any)?.response?.data?.message?.includes(
'Amount is higher than the available liquidity'
))
) {
error = priceError
price = undefined
}

useDisconnected(address, () => {
setCustomToAddress(undefined)
Expand Down Expand Up @@ -536,13 +550,14 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
address === recipient

const operation = price?.details?.operation || 'swap'
const maxCapacityWei =
isCapacityExceededError && fetchQuoteDataErrorMessage
? fetchQuoteDataErrorMessage.match(/(\d+)/)?.[0]
: undefined
const maxCapacityFormatted = maxCapacityWei
? formatBN(BigInt(maxCapacityWei), 2, toToken?.decimals ?? 18)
const maxCapacity = isCapacityExceededError
? extractMaxCapacity(
fetchQuoteDataErrorMessage ?? undefined,
toToken?.decimals
)
: undefined
const maxCapacityWei = maxCapacity?.value
const maxCapacityFormatted = maxCapacity?.formatted

let ctaCopy: string = context || 'Swap'

Expand Down
13 changes: 13 additions & 0 deletions packages/ui/src/utils/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ export const isHighRelayerServiceFeeUsd = (quote?: QuoteResponse) => {
)
}

export const extractMaxCapacity = (
errorMessage?: string,
decimals: number = 18
) => {
const value = errorMessage?.match(/(\d+)/)?.[0]
const formatted = value ? formatBN(BigInt(value), 2, decimals) : undefined

return {
value,
formatted
}
}

export const extractQuoteId = (steps?: Execute['steps']) => {
if (
steps &&
Expand Down