-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update widget with client side SOR (#3210)
* start SOR by creating custom widget hook * update best trade hook to use SOR in widget * update organization for client side SOR logic * fix auto router chain id import * remove dependency on react GA for widget * update dependencies for SOr * remove new useBestTrade.ts * update loading logic for fetching hook * update dependencies with import from ethersproject * update import version * add try catch on SOR usage * code cleanup, nit fixes
- Loading branch information
Showing
12 changed files
with
271 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/lib/hooks/routing/useClientSideSmartOrderRouterTrade.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core' | ||
import { useStablecoinAmountFromFiatValue } from 'hooks/useUSDCPrice' | ||
import { useEffect, useMemo, useState } from 'react' | ||
import { GetQuoteResult, InterfaceTrade, TradeState } from 'state/routing/types' | ||
import { computeRoutes, transformRoutesToTrade } from 'state/routing/utils' | ||
|
||
import { getClientSideQuote } from './clientSideSmartOrderRouter' | ||
import { useRoutingAPIArguments } from './useRoutingAPIArguments' | ||
|
||
export default function useClientSideSmartOrderRouterTrade<TTradeType extends TradeType>( | ||
tradeType: TTradeType, | ||
amountSpecified?: CurrencyAmount<Currency>, | ||
otherCurrency?: Currency | ||
): { | ||
state: TradeState | ||
trade: InterfaceTrade<Currency, Currency, TTradeType> | undefined | ||
} { | ||
const [currencyIn, currencyOut]: [Currency | undefined, Currency | undefined] = useMemo( | ||
() => | ||
tradeType === TradeType.EXACT_INPUT | ||
? [amountSpecified?.currency, otherCurrency] | ||
: [otherCurrency, amountSpecified?.currency], | ||
[amountSpecified, otherCurrency, tradeType] | ||
) | ||
|
||
const queryArgs = useRoutingAPIArguments({ | ||
tokenIn: currencyIn, | ||
tokenOut: currencyOut, | ||
amount: amountSpecified, | ||
tradeType, | ||
useClientSideRouter: true, | ||
}) | ||
|
||
const [loading, setLoading] = useState(false) | ||
const [{ quoteResult, error }, setFetchedResult] = useState<{ | ||
quoteResult: GetQuoteResult | undefined | ||
error: unknown | ||
}>({ | ||
quoteResult: undefined, | ||
error: undefined, | ||
}) | ||
|
||
// When arguments update, make a new call to SOR for updated quote | ||
useEffect(() => { | ||
setLoading(true) | ||
fetchQuote() | ||
|
||
async function fetchQuote() { | ||
try { | ||
if (queryArgs) { | ||
const result = await getClientSideQuote(queryArgs) | ||
setFetchedResult({ | ||
quoteResult: result.data, | ||
error: result.error, | ||
}) | ||
} | ||
} catch (e) { | ||
setFetchedResult({ | ||
quoteResult: undefined, | ||
error: true, | ||
}) | ||
} finally { | ||
setLoading(false) | ||
} | ||
} | ||
}, [queryArgs]) | ||
|
||
const route = useMemo( | ||
() => computeRoutes(currencyIn, currencyOut, tradeType, quoteResult), | ||
[currencyIn, currencyOut, quoteResult, tradeType] | ||
) | ||
|
||
// get USD gas cost of trade in active chains stablecoin amount | ||
const gasUseEstimateUSD = useStablecoinAmountFromFiatValue(quoteResult?.gasUseEstimateUSD) ?? null | ||
|
||
return useMemo(() => { | ||
if (!currencyIn || !currencyOut) { | ||
return { | ||
state: TradeState.INVALID, | ||
trade: undefined, | ||
} | ||
} | ||
|
||
if (loading && !quoteResult) { | ||
// only on first hook render | ||
return { | ||
state: TradeState.LOADING, | ||
trade: undefined, | ||
} | ||
} | ||
|
||
let otherAmount = undefined | ||
if (tradeType === TradeType.EXACT_INPUT && currencyOut && quoteResult) { | ||
otherAmount = CurrencyAmount.fromRawAmount(currencyOut, quoteResult.quote) | ||
} | ||
if (tradeType === TradeType.EXACT_OUTPUT && currencyIn && quoteResult) { | ||
otherAmount = CurrencyAmount.fromRawAmount(currencyIn, quoteResult.quote) | ||
} | ||
|
||
if (error || !otherAmount || !route || route.length === 0 || !queryArgs) { | ||
return { | ||
state: TradeState.NO_ROUTE_FOUND, | ||
trade: undefined, | ||
} | ||
} | ||
|
||
try { | ||
const trade = transformRoutesToTrade(route, tradeType, gasUseEstimateUSD) | ||
return { | ||
// always return VALID regardless of isFetching status | ||
state: TradeState.VALID, | ||
trade, | ||
} | ||
} catch (e) { | ||
console.debug('transformRoutesToTrade failed: ', e) | ||
return { state: TradeState.INVALID, trade: undefined } | ||
} | ||
}, [currencyIn, currencyOut, loading, quoteResult, tradeType, error, route, queryArgs, gasUseEstimateUSD]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core' | ||
import { useMemo } from 'react' | ||
|
||
/** | ||
* Returns query arguments for the Routing API query or undefined if the | ||
* query should be skipped. Input arguments do not need to be memoized, as they will | ||
* be destructured. | ||
*/ | ||
export function useRoutingAPIArguments({ | ||
tokenIn, | ||
tokenOut, | ||
amount, | ||
tradeType, | ||
useClientSideRouter, | ||
}: { | ||
tokenIn: Currency | undefined | ||
tokenOut: Currency | undefined | ||
amount: CurrencyAmount<Currency> | undefined | ||
tradeType: TradeType | ||
useClientSideRouter: boolean | ||
}) { | ||
return useMemo( | ||
() => | ||
!tokenIn || !tokenOut || !amount || tokenIn.equals(tokenOut) | ||
? undefined | ||
: { | ||
amount: amount.quotient.toString(), | ||
tokenInAddress: tokenIn.wrapped.address, | ||
tokenInChainId: tokenIn.wrapped.chainId, | ||
tokenInDecimals: tokenIn.wrapped.decimals, | ||
tokenInSymbol: tokenIn.wrapped.symbol, | ||
tokenOutAddress: tokenOut.wrapped.address, | ||
tokenOutChainId: tokenOut.wrapped.chainId, | ||
tokenOutDecimals: tokenOut.wrapped.decimals, | ||
tokenOutSymbol: tokenOut.wrapped.symbol, | ||
useClientSideRouter, | ||
type: (tradeType === TradeType.EXACT_INPUT ? 'exactIn' : 'exactOut') as 'exactIn' | 'exactOut', | ||
}, | ||
[amount, tokenIn, tokenOut, tradeType, useClientSideRouter] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.