Skip to content

Commit

Permalink
fix: error captions (#323)
Browse files Browse the repository at this point in the history
* fix: do not display error caption for zero amounts

* fix: do not display insufficient liquidity caption when loading initial trade
  • Loading branch information
zzmp authored Dec 8, 2022
1 parent ec3659a commit c0740fb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/hooks/routing/clientSideSmartOrderRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
UniswapMulticallProvider,
} from '@uniswap/smart-order-router'
import JSBI from 'jsbi'
import { GetQuoteArgs, GetQuoteResult, NO_ROUTE } from 'state/routing/types'
import { GetQuoteArgs, GetQuoteResult, INITIALIZED, NO_ROUTE } from 'state/routing/types'
import { isExactInput } from 'utils/tradeType'

import { transformSwapRouteToGetQuoteResult } from './transformSwapRouteToGetQuoteResult'
Expand Down Expand Up @@ -103,7 +103,8 @@ async function getQuote(
const amount = CurrencyAmount.fromRawAmount(baseCurrency, JSBI.BigInt(amountRaw ?? '1')) // a null amountRaw should initialize the route
const route = await router.route(amount, quoteCurrency, tradeType, /*swapConfig=*/ undefined, routerConfig)

if (amountRaw === null || !route) return NO_ROUTE
if (!amountRaw) return INITIALIZED
if (!route) return NO_ROUTE

return transformSwapRouteToGetQuoteResult({ ...route, routeString: routeAmountsToString(route.route) })
}
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/swap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export function useIsSwapFieldIndependent(field: Field): boolean {

const amountAtom = pickAtom(swapAtom, 'amount')

// check if any amount has been entered by user
/** Returns true if the user has entered a non-zero amount. */
export function useIsAmountPopulated() {
return Boolean(useAtomValue(amountAtom))
return Boolean(Number(useAtomValue(amountAtom)))
}

export function useSwapAmount(field: Field): [string | undefined, (amount: string, origin?: 'max') => void] {
Expand Down
26 changes: 14 additions & 12 deletions src/state/routing/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export const routing = createApi({
async queryFn(args: GetQuoteArgs | SkipToken) {
if (args === skipToken) return { error: { status: 'CUSTOM_ERROR', error: 'Skipped' } }

// If enabled, try routing API, falling back to client-side SOR.
if (Boolean(args.routerUrl)) {
// amount may be null to initialize the client-side SOR. This should be skipped for the server.
if (args.amount === undefined) return { error: { status: 'CUSTOM_ERROR', error: 'Skipped' } }

if (
// If enabled, try the routing API, falling back to client-side routing.
Boolean(args.routerUrl) &&
// A null amount may be passed to initialize the client-side routing.
args.amount !== null
) {
try {
const { tokenInAddress, tokenInChainId, tokenOutAddress, tokenOutChainId, amount, tradeType } = args
const type = isExactInput(tradeType) ? 'exactIn' : 'exactOut'
Expand Down Expand Up @@ -61,20 +62,21 @@ export const routing = createApi({

const quote: GetQuoteResult = await response.json()
return { data: quote }
} catch (error) {
console.warn(`GetQuote failed on routing API, falling back to client: ${error}`)
} catch (error: any) {
console.warn(
`GetQuote failed on routing API, falling back to client: ${error?.message ?? error?.detail ?? error}`
)
}
}

// If integrator did not provide a routing API URL param, use clientside SOR
// Lazy-load the client-side router to improve initial pageload times.
const clientSideSmartOrderRouter = await import('../../hooks/routing/clientSideSmartOrderRouter')
try {
// Lazy-load the client-side router to improve initial pageload times.
const clientSideSmartOrderRouter = await import('../../hooks/routing/clientSideSmartOrderRouter')
const quote = await clientSideSmartOrderRouter.getClientSideQuote(args, { protocols })
return { data: quote }
} catch (error) {
} catch (error: any) {
console.warn(`GetQuote failed on client: ${error}`)
return { error: { status: 'CUSTOM_ERROR', error: error.message } }
return { error: { status: 'CUSTOM_ERROR', error: error?.message ?? error?.detail ?? error } }
}
},
keepUnusedDataFor: ms`10s`,
Expand Down
3 changes: 2 additions & 1 deletion src/state/routing/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ export interface QuoteResult {
routeString: string
}

export const INITIALIZED = 'Initialized'
export const NO_ROUTE = 'No Route'

export type GetQuoteResult = QuoteResult | typeof NO_ROUTE
export type GetQuoteResult = QuoteResult | typeof INITIALIZED | typeof NO_ROUTE

export class InterfaceTrade extends Trade<Currency, Currency, TradeType> {}

1 comment on commit c0740fb

@vercel
Copy link

@vercel vercel bot commented on c0740fb Dec 8, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

widgets – ./

widgets-seven-tau.vercel.app
widgets-git-main-uniswap.vercel.app
widgets-uniswap.vercel.app

Please sign in to comment.