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

Commit

Permalink
[CLEANUP] useUSDCPrice speedup + logs clean (#1968)
Browse files Browse the repository at this point in the history
* 1.7.0-rc.0

* ci(Mergify): configuration update (#1969)

Signed-off-by: David <null>

* dont log trade details - quiet down logs

* simpler usdc price return and ref currency

Co-authored-by: Anxo Rodriguez <[email protected]>
  • Loading branch information
W3stside and anxolin authored Dec 7, 2021
1 parent f23d98f commit 3c62d9b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 80 deletions.
15 changes: 11 additions & 4 deletions .mergify.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
queue_rules:
- name: default
conditions:
- check-success=Deploy
- check-success=Cypress

pull_request_rules:
- name: Merge approved and green PRs when tagged with 'Auto-merge'
conditions:
Expand All @@ -6,10 +12,11 @@ pull_request_rules:
- check-success=Deploy
- check-success=Cypress
actions:
merge:
queue:
method: squash
strict: smart+fasttrack
name: default
commit_message: title+body
# DEPENDABOT PRs
- name: automatic merge for Dependabot pull requests
conditions:
- author~=^dependabot(|-preview)\[bot\]$
Expand All @@ -18,7 +25,7 @@ pull_request_rules:
- check-success=Deploy
- base=develop
actions:
merge:
queue:
method: squash
strict: smart+fasttrack
name: default
commit_message: title+body
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
133 changes: 60 additions & 73 deletions src/custom/hooks/useUSDCPrice/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CurrencyAmount, Currency, Price, Token } from '@uniswap/sdk-core'
import { useEffect, useMemo, useState } from 'react'
import { useEffect, useMemo, useRef, useState } from 'react'
import { SupportedChainId } from 'constants/chains'
/* import { DAI_OPTIMISM, USDC, USDC_ARBITRUM } from '../constants/tokens'
import { useV2TradeExactOut } from './useV2Trade'
Expand Down Expand Up @@ -167,62 +167,67 @@ export function useCoingeckoUsdPrice(currency?: Currency) {
const [price, setPrice] = useState<Price<Token, Currency> | null>(null)
const [error, setError] = useState<Error | null>(null)

useEffect(() => {
const isSupportedChainId = supportedChainId(chainId)
if (!isSupportedChainId || !currency) return
// Currency is deep nested and we only really care about token address changing
// so we ref it here as to avoid updating useEffect
const currencyRef = useRef(currency)
currencyRef.current = currency

const baseAmount = tryParseAmount('1', currency)
const tokenAddress = currencyId(currency)
const tokenAddress = currencyRef.current ? currencyId(currencyRef.current) : undefined

if (baseAmount) {
getUSDPriceQuote({
chainId: isSupportedChainId,
tokenAddress,
useEffect(() => {
const isSupportedChainId = supportedChainId(chainId)
const baseAmount = tryParseAmount('1', currencyRef.current)

if (!isSupportedChainId || !tokenAddress || !baseAmount) return

getUSDPriceQuote({
chainId: isSupportedChainId,
tokenAddress,
})
.then(toPriceInformation)
.then((priceResponse) => {
setError(null)

if (!priceResponse?.amount) return

const { amount: apiUsdPrice } = priceResponse
// api returns converted units e.g $2.25 instead of 2255231233312312 (atoms)
// we need to parse all USD returned amounts
// and convert to the same currencyRef.current for both sides (SDK math invariant)
// in our case we stick to the USDC paradigm
const quoteAmount = tryParseAmount(apiUsdPrice.toString(), USDC)
// parse failure is unlikely - type safe
if (!quoteAmount) return
// create a new Price object
// we need to invert here as it is
// constructed based on the coingecko USD price response
// e.g 1 unit of USER'S TOKEN represented in USD
const usdPrice = new Price({
baseAmount,
quoteAmount,
}).invert()

console.debug(
'[useCoingeckoUsdPrice] Best Coingecko USD price amount',
usdPrice.toSignificant(12),
usdPrice.invert().toSignificant(12)
)

return setPrice(usdPrice)
})
.then(toPriceInformation)
.then((priceResponse) => {
setError(null)

if (!priceResponse?.amount) return

const { amount: apiUsdPrice } = priceResponse
// api returns converted units e.g $2.25 instead of 2255231233312312 (atoms)
// we need to parse all USD returned amounts
// and convert to the same currency for both sides (SDK math invariant)
// in our case we stick to the USDC paradigm
const quoteAmount = tryParseAmount(apiUsdPrice.toString(), USDC)
// parse failure is unlikely - type safe
if (!quoteAmount) return
// create a new Price object
// we need to invert here as it is
// constructed based on the coingecko USD price response
// e.g 1 unit of USER'S TOKEN represented in USD
const usdPrice = new Price({
baseAmount,
quoteAmount,
}).invert()

console.debug(
'[useCoingeckoUsdPrice] Best Coingecko USD price amount',
usdPrice.toSignificant(12),
usdPrice.invert().toSignificant(12)
)

return setPrice(usdPrice)
.catch((error) => {
console.error(
'[useUSDCPrice::useCoingeckoUsdPrice]::Error getting USD price from Coingecko for token',
tokenAddress,
error
)
return batchedUpdate(() => {
setError(new Error(error))
setPrice(null)
})
.catch((error) => {
console.error(
'[useUSDCPrice::useCoingeckoUsdPrice]::Error getting USD price from Coingecko for token',
currency.symbol,
error
)
return batchedUpdate(() => {
setError(new Error(error))
setPrice(null)
})
})
}
}, [chainId, currency, blockNumber])
})
// don't depend on Currency (deep nested object)
}, [chainId, blockNumber, tokenAddress])

return { price, error }
}
Expand All @@ -234,26 +239,8 @@ export function useCoingeckoUsdValue(currencyAmount: CurrencyAmount<Currency> |
}

export function useHigherUSDValue(currencyAmount: CurrencyAmount<Currency> | undefined) {
const usdcValue = useUSDCValue(currencyAmount)
const gpUsdPrice = useUSDCValue(currencyAmount)
const coingeckoUsdPrice = useCoingeckoUsdValue(currencyAmount)

return useMemo(() => {
// USDC PRICE UNAVAILABLE
if (!usdcValue && coingeckoUsdPrice) {
console.debug('[USD Estimation]::COINGECKO')
return coingeckoUsdPrice
// COINGECKO PRICE UNAVAILABLE
} else if (usdcValue && !coingeckoUsdPrice) {
console.debug('[USD Estimation]::UNIv2')
return usdcValue
// BOTH AVAILABLE
} else if (usdcValue && coingeckoUsdPrice) {
console.debug('[USD Estimation]::[BOTH] ==> COIN')
// coingecko logic takes precedence
return coingeckoUsdPrice
} else {
console.debug('[USD Estimation]::None found')
return null
}
}, [usdcValue, coingeckoUsdPrice])
return coingeckoUsdPrice || gpUsdPrice
}
4 changes: 2 additions & 2 deletions src/custom/pages/Swap/SwapMod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import { computeSlippageAdjustedAmounts } from 'utils/prices'
import FeeInformationTooltip from 'components/swap/FeeInformationTooltip'
import { useWalletInfo } from 'hooks/useWalletInfo'
import { HashLink } from 'react-router-hash-link'
import { logTradeDetails } from 'state/swap/utils'
// import { logTradeDetails } from 'state/swap/utils'
import { useGetQuoteAndStatus } from 'state/price/hooks'
import { SwapProps, ButtonError, ButtonPrimary } from '.' // mod
import TradeGp from 'state/swap/TradeGp'
Expand Down Expand Up @@ -189,7 +189,7 @@ export default function Swap({
const { quote, isGettingNewQuote } = useGetQuoteAndStatus({ token: INPUT.currencyId, chainId })

// Log all trade information
logTradeDetails(v2Trade, allowedSlippage)
// logTradeDetails(v2Trade, allowedSlippage)

// Checks if either currency is native ETH
// MOD: adds this hook
Expand Down

0 comments on commit 3c62d9b

Please sign in to comment.