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

Don't call fee/price endpoint on Wrapping (ETH/WETH || XDAI/WXDAI etc) #1920

Merged
merged 6 commits into from
Dec 8, 2021
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "CowSwap - Gnosis Protocol",
"homepage": ".",
"private": true,
"version": "1.6.0",
"version": "1.7.0-rc.0",
"engines": {
"node": ">=14.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/custom/hooks/usePriceImpact/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type ParsedAmounts = {
export interface FallbackPriceImpactParams {
abTrade?: TradeGp
fiatPriceImpact?: Percent
isWrapping: boolean
}
8 changes: 5 additions & 3 deletions src/custom/hooks/usePriceImpact/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ import useFallbackPriceImpact from './useFallbackPriceImpact'
import { FallbackPriceImpactParams, ParsedAmounts } from './commonTypes'
import { QuoteError } from 'state/price/actions'

type PriceImpactParams = Omit<FallbackPriceImpactParams, 'fiatPriceImpact'> & { parsedAmounts: ParsedAmounts }
type PriceImpactParams = Omit<FallbackPriceImpactParams, 'fiatPriceImpact'> & {
parsedAmounts: ParsedAmounts
}

export interface PriceImpact {
priceImpact: Percent | undefined
error: QuoteError | undefined
loading: boolean
}

export default function usePriceImpact({ abTrade, parsedAmounts }: PriceImpactParams): PriceImpact {
export default function usePriceImpact({ abTrade, parsedAmounts, isWrapping }: PriceImpactParams): PriceImpact {
/* const fiatPriceImpact = */ useFiatValuePriceImpact(parsedAmounts)
// TODO: remove this - testing only - forces fallback price impact
const {
impact: fallbackPriceImpact,
error,
loading,
} = useFallbackPriceImpact({ abTrade, fiatPriceImpact: undefined })
} = useFallbackPriceImpact({ abTrade, fiatPriceImpact: undefined, isWrapping })

const priceImpact = /* fiatPriceImpact || */ fallbackPriceImpact

Expand Down
12 changes: 4 additions & 8 deletions src/custom/hooks/usePriceImpact/useFallbackPriceImpact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useEffect, useMemo, useState } from 'react'
import { Percent } from '@uniswap/sdk-core'

import { useSwapState } from 'state/swap/hooks'
import { Field } from 'state/swap/actions'

import useExactInSwap, { useCalculateQuote } from './useQuoteAndSwap'
import { FallbackPriceImpactParams } from './commonTypes'
Expand Down Expand Up @@ -40,18 +39,15 @@ function _getBaTradeParsedAmount(abTrade: TradeGp | undefined, shouldCalculate:
return abTrade.outputAmountWithoutFee
}

export default function useFallbackPriceImpact({ abTrade, fiatPriceImpact }: FallbackPriceImpactParams) {
export default function useFallbackPriceImpact({ abTrade, fiatPriceImpact, isWrapping }: FallbackPriceImpactParams) {
const {
typedValue,
INPUT: { currencyId: sellToken },
OUTPUT: { currencyId: buyToken },
independentField,
} = useSwapState()

const abTradeIsSell = independentField === Field.INPUT

// Should we even calc this? Check if fiatPriceImpact exists
const shouldCalculate = !Boolean(fiatPriceImpact)
// Should we even calc this? Check if fiatPriceImpact exists OR user is wrapping token
const shouldCalculate = !Boolean(fiatPriceImpact) || !isWrapping

// Calculate the necessary params to get the inverse trade impact
const { parsedAmount, outputCurrency, ...swapQuoteParams } = useMemo(
Expand Down Expand Up @@ -103,7 +99,7 @@ export default function useFallbackPriceImpact({ abTrade, fiatPriceImpact }: Fal
setImpact(undefined)
setError(undefined)
}
}, [abIn, abOut, baOut, quoteError, abTradeIsSell, loading, typedValue])
}, [abIn, abOut, baOut, quoteError, loading, typedValue])

return { impact, error, loading }
}
6 changes: 6 additions & 0 deletions src/custom/hooks/usePriceImpact/useQuoteAndSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ZERO_ADDRESS } from 'constants/misc'
import { SupportedChainId } from 'constants/chains'
import { DEFAULT_DECIMALS } from 'constants/index'
import { QuoteError } from 'state/price/actions'
import { isWrappingTrade } from 'state/swap/utils'

type ExactInSwapParams = {
parsedAmount: CurrencyAmount<Currency> | undefined
Expand Down Expand Up @@ -107,10 +108,15 @@ export function useCalculateQuote(params: GetQuoteParams) {

// calculates a new Quote and inverse swap values
export default function useExactInSwap({ quote, outputCurrency, parsedAmount }: ExactInSwapParams) {
const { chainId } = useActiveWeb3React()

const isWrapping = isWrappingTrade(parsedAmount?.currency, outputCurrency, chainId)

const bestTradeExactIn = useTradeExactInWithFee({
parsedAmount,
outputCurrency,
quote,
isWrapping,
})

return bestTradeExactIn
Expand Down
2 changes: 1 addition & 1 deletion src/custom/pages/Swap/SwapMod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export default function Swap({
[independentField, parsedAmount, showWrap, trade]
)

const priceImpactParams = usePriceImpact({ abTrade: v2Trade, parsedAmounts })
const priceImpactParams = usePriceImpact({ abTrade: v2Trade, parsedAmounts, isWrapping: !!onWrap })
const { priceImpact, error: priceImpactError, loading: priceImpactLoading } = priceImpactParams

const { feeWarningAccepted, setFeeWarningAccepted } = useHighFeeWarning(trade)
Expand Down
5 changes: 5 additions & 0 deletions src/custom/state/price/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useActiveWeb3React } from 'hooks/web3'
import useDebounce from 'hooks/useDebounce'
import useIsOnline from 'hooks/useIsOnline'
import { QuoteInformationObject } from './reducer'
import { isWrappingTrade } from 'state/swap/utils'

const DEBOUNCE_TIME = 350
const REFETCH_CHECK_INTERVAL = 10000 // Every 10s
Expand Down Expand Up @@ -146,8 +147,12 @@ export default function FeesUpdater(): null {
// Don't refetch if:
// - window is not visible
// - some parameter is missing
// - it is a wrapping operation
if (!chainId || !sellToken || !buyToken || !typedValue || !windowVisible) return

// Native wrap trade, return
if (isWrappingTrade(sellCurrency, buyCurrency, chainId)) return

// Don't refetch if the amount is missing
const kind = independentField === Field.INPUT ? OrderKind.SELL : OrderKind.BUY
const amount = tryParseAmount(typedValue, (kind === OrderKind.SELL ? sellCurrency : buyCurrency) ?? undefined)
Expand Down
7 changes: 5 additions & 2 deletions src/custom/state/swap/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface TradeParams {
inputCurrency?: Currency | null
outputCurrency?: Currency | null
quote?: QuoteInformationObject
isWrapping: boolean
}

export const stringToCurrency = (amount: string, currency: Currency) =>
Expand All @@ -22,10 +23,11 @@ export function useTradeExactInWithFee({
parsedAmount: parsedInputAmount,
outputCurrency,
quote,
isWrapping,
}: Omit<TradeParams, 'inputCurrency'>) {
// make sure we have a typed in amount, a fee, and a price
// else we can assume the trade will be null
if (!parsedInputAmount || !outputCurrency || !quote?.fee || !quote?.price?.amount) return null
if (!parsedInputAmount || !outputCurrency || isWrapping || !quote?.fee || !quote?.price?.amount) return null

const feeAsCurrency = stringToCurrency(quote.fee.amount, parsedInputAmount.currency)
// Check that fee amount is not greater than the user's input amt
Expand Down Expand Up @@ -80,8 +82,9 @@ export function useTradeExactOutWithFee({
parsedAmount: parsedOutputAmount,
inputCurrency,
quote,
isWrapping,
}: Omit<TradeParams, 'outputCurrency'>) {
if (!parsedOutputAmount || !inputCurrency || !quote?.fee || !quote?.price?.amount) return null
if (!parsedOutputAmount || !inputCurrency || isWrapping || !quote?.fee || !quote?.price?.amount) return null

const feeAsCurrency = stringToCurrency(quote.fee.amount, inputCurrency)
// set final fee object
Expand Down
5 changes: 5 additions & 0 deletions src/custom/state/swap/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { WETH9_EXTENDED as WETH, GpEther as ETHER } from 'constants/tokens'
import { BAD_RECIPIENT_ADDRESSES } from 'state/swap/hooks'
import { useIsExpertMode, useUserSlippageToleranceWithDefault } from '@src/state/user/hooks'
import { PriceImpact } from 'hooks/usePriceImpact'
import { isWrappingTrade } from './utils'

export * from '@src/state/swap/hooks'

Expand Down Expand Up @@ -295,15 +296,19 @@ export function useDerivedSwapInfo(): /* {
console.debug('[useDerivedSwapInfo] Fee quote: ', quote?.fee?.amount)
}, [quote])

const isWrapping = isWrappingTrade(inputCurrency, outputCurrency, chainId)

const bestTradeExactIn = useTradeExactInWithFee({
parsedAmount: isExactIn ? parsedAmount : undefined,
outputCurrency,
quote,
isWrapping,
})
const bestTradeExactOut = useTradeExactOutWithFee({
parsedAmount: isExactIn ? undefined : parsedAmount,
inputCurrency,
quote,
isWrapping,
})

// TODO: rename v2Trade to just "trade" we dont have versions
Expand Down
15 changes: 14 additions & 1 deletion src/custom/state/swap/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { Percent, TradeType } from '@uniswap/sdk-core'
import { SupportedChainId } from 'constants/chains'
import { Currency, Percent, TradeType } from '@uniswap/sdk-core'
import TradeGp from './TradeGp'
import { WETH9_EXTENDED } from 'constants/tokens'

export function isWrappingTrade(
sellCurrency: Currency | null | undefined,
buyCurrency: Currency | null | undefined,
chainId?: SupportedChainId
): boolean {
return Boolean(
(sellCurrency?.isNative && buyCurrency?.wrapped.equals(WETH9_EXTENDED[chainId || SupportedChainId.MAINNET])) ||
(sellCurrency?.wrapped.equals(WETH9_EXTENDED[chainId || SupportedChainId.MAINNET]) && buyCurrency?.isNative)
)
}

export function logTradeDetails(trade: TradeGp | undefined, allowedSlippage: Percent) {
// don't do anything outside of dev env
Expand Down