Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
better types/param name
Browse files Browse the repository at this point in the history
  • Loading branch information
W3stside committed Dec 7, 2021
1 parent e2a949c commit c3d23a8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/custom/hooks/useGetGpPriceStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export async function checkGpPriceStrategy(): Promise<GpPriceStrategy> {
const GP_PRICE_STRATEGY_INTERVAL_TIME = ms`30 minutes`

export default function useGetGpPriceStrategy(
defaultApiToUse: GpPriceStrategy = DEFAULT_GP_PRICE_STRATEGY
defaultStrategy: GpPriceStrategy = DEFAULT_GP_PRICE_STRATEGY
): GpPriceStrategy {
const [gpPriceStrategy, setGpPriceStrategy] = useState<GpPriceStrategy>(defaultApiToUse)
const [gpPriceStrategy, setGpPriceStrategy] = useState<GpPriceStrategy>(defaultStrategy)

useEffect(() => {
console.debug('[useGetGpPriceStrategy::GP Price Strategy]::', gpPriceStrategy)
Expand Down
10 changes: 5 additions & 5 deletions src/custom/hooks/useRefetchPriceCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export function useRefetchQuoteCallback() {
const { getNewQuote, refreshQuote, updateQuote, setQuoteError } = useQuoteDispatchers()
const addUnsupportedToken = useAddGpUnsupportedToken()
const removeGpUnsupportedToken = useRemoveGpUnsupportedToken()
// check which GP Quote API to use (NEW/LEGACY)
const gpApiStatus = useGetGpPriceStrategy()
// check which price strategy to use (COWSWAP/LEGACY)
const priceStrategy = useGetGpPriceStrategy()

registerOnWindow({
getNewQuote,
Expand All @@ -128,7 +128,7 @@ export function useRefetchQuoteCallback() {
})

return useCallback(
async (params: QuoteParams) => {
async (params: Omit<QuoteParams, 'strategy'>) => {
const { quoteParams, isPriceRefresh } = params
let quoteData: FeeQuoteParams | QuoteInformationObject = quoteParams

Expand All @@ -145,7 +145,7 @@ export function useRefetchQuoteCallback() {

// Get the quote
// price can be null if fee > price
const { cancelled, data } = await getBestQuoteResolveOnlyLastCall({ ...params, apiStatus: gpApiStatus })
const { cancelled, data } = await getBestQuoteResolveOnlyLastCall({ ...params, strategy: priceStrategy })
if (cancelled) {
// Cancellation can happen if a new request is made, then any ongoing query is canceled
console.debug('[useRefetchPriceCallback] Canceled get quote price for', params)
Expand Down Expand Up @@ -207,7 +207,7 @@ export function useRefetchQuoteCallback() {
}
},
[
gpApiStatus,
priceStrategy,
isUnsupportedTokenGp,
updateQuote,
refreshQuote,
Expand Down
12 changes: 6 additions & 6 deletions src/custom/state/orders/updaters/UnfillableOrdersUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SupportedChainId as ChainId } from 'constants/chains'

import { getBestQuote, PriceInformation } from 'utils/price'
import { isOrderUnfillable } from 'state/orders/utils'
import useGetGpPriceStrategy, { GpPriceStrategy } from '@src/custom/hooks/useGetGpPriceStrategy'
import useGetGpPriceStrategy, { GpPriceStrategy } from 'hooks/useGetGpPriceStrategy'
import { getPromiseFulfilledValue } from 'utils/misc'

/**
Expand All @@ -19,7 +19,7 @@ import { getPromiseFulfilledValue } from 'utils/misc'
* @param chainId
* @param order
*/
async function _getOrderPrice(chainId: ChainId, order: Order, apiStatus: GpPriceStrategy) {
async function _getOrderPrice(chainId: ChainId, order: Order, strategy: GpPriceStrategy) {
let amount, baseToken, quoteToken

if (order.kind === 'sell') {
Expand All @@ -46,7 +46,7 @@ async function _getOrderPrice(chainId: ChainId, order: Order, apiStatus: GpPrice
}

try {
return getBestQuote({ apiStatus, quoteParams, fetchFee: false, isPriceRefresh: false })
return getBestQuote({ strategy, quoteParams, fetchFee: false, isPriceRefresh: false })
} catch (e) {
return null
}
Expand All @@ -60,7 +60,7 @@ export function UnfillableOrdersUpdater(): null {
const pending = usePendingOrders({ chainId })
const setIsOrderUnfillable = useSetIsOrderUnfillable()
// check which GP Quote API to use (NEW/LEGACY)
const gpApiStatus = useGetGpPriceStrategy()
const strategy = useGetGpPriceStrategy()

// Ref, so we don't rerun useEffect
const pendingRef = useRef(pending)
Expand Down Expand Up @@ -101,7 +101,7 @@ export function UnfillableOrdersUpdater(): null {
}

pending.forEach((order, index) =>
_getOrderPrice(chainId, order, gpApiStatus).then((quote) => {
_getOrderPrice(chainId, order, strategy).then((quote) => {
if (quote) {
const [promisedPrice] = quote
const price = getPromiseFulfilledValue(promisedPrice, null)
Expand All @@ -117,7 +117,7 @@ export function UnfillableOrdersUpdater(): null {
isUpdating.current = false
console.debug(`[UnfillableOrdersUpdater] Checked canceled orders in ${Date.now() - startTime}ms`)
}
}, [account, chainId, gpApiStatus, updateIsUnfillableFlag])
}, [account, chainId, strategy, updateIsUnfillableFlag])

useEffect(() => {
updatePending()
Expand Down
21 changes: 12 additions & 9 deletions src/custom/utils/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { OptimalRate } from 'paraswap-core'
import { GetQuoteResponse, OrderKind } from '@gnosis.pm/gp-v2-contracts'
import { ChainId } from 'state/lists/actions'
import { toErc20Address } from 'utils/tokens'
import { GpPriceStrategy } from '@src/custom/hooks/useGetGpPriceStrategy'
import { GpPriceStrategy } from 'hooks/useGetGpPriceStrategy'

const FEE_EXCEEDS_FROM_ERROR = new GpQuoteError({
errorType: GpQuoteErrorCodes.FeeExceedsFrom,
Expand All @@ -28,6 +28,7 @@ const FEE_EXCEEDS_FROM_ERROR = new GpQuoteError({

export interface QuoteParams {
quoteParams: FeeQuoteParams
strategy: GpPriceStrategy
fetchFee: boolean
previousFee?: FeeInformation
isPriceRefresh: boolean
Expand Down Expand Up @@ -275,7 +276,11 @@ export async function getFullQuote({ quoteParams }: { quoteParams: FeeQuoteParam
* (LEGACY) Will be overwritten in the near future
* Return the best quote considering all price feeds. The quote contains information about the price and fee
*/
export async function getBestQuoteLegacy({ quoteParams, fetchFee, previousFee }: QuoteParams): Promise<QuoteResult> {
export async function getBestQuoteLegacy({
quoteParams,
fetchFee,
previousFee,
}: Omit<QuoteParams, 'strategy'>): Promise<QuoteResult> {
const { sellToken, buyToken, fromDecimals, toDecimals, amount, kind, chainId, userAddress, validTo } = quoteParams
const { baseToken, quoteToken } = getCanonicalMarket({ sellToken, buyToken, kind })
// Get a new fee quote (if required)
Expand Down Expand Up @@ -327,19 +332,17 @@ export async function getBestQuote({
quoteParams,
fetchFee,
previousFee,
apiStatus,
}: QuoteParams & {
apiStatus: GpPriceStrategy
}): Promise<QuoteResult> {
if (apiStatus === 'COWSWAP') {
strategy,
}: QuoteParams): Promise<QuoteResult> {
if (strategy === 'COWSWAP') {
return getFullQuote({ quoteParams }).catch((err) => {
console.error(
'[PRICE::API] getBestQuote - error in COWSWAP full quote endpoint, reason: <',
'[PRICE::API] getBestQuote - error using COWSWAP price strategy, reason: <',
err,
'> - trying back up price sources...'
)
// ATTEMPT LEGACY CALL
return getBestQuote({ apiStatus: 'LEGACY', quoteParams, fetchFee, previousFee, isPriceRefresh: false })
return getBestQuote({ strategy: 'LEGACY', quoteParams, fetchFee, previousFee, isPriceRefresh: false })
})
} else {
return getBestQuoteLegacy({ quoteParams, fetchFee, previousFee, isPriceRefresh: false })
Expand Down

0 comments on commit c3d23a8

Please sign in to comment.