From 337472f602e2e67d31b197273147033aabbc8af2 Mon Sep 17 00:00:00 2001 From: leifu Date: Tue, 6 Sep 2022 00:11:19 +0300 Subject: [PATCH] hotfix: ETH swap token address (#1355) * Disabled no synth card * Added ETH approval * Limit the liquidity source * Update the token address * Fixed the lint error and update the token address * Fix the swap pricing display --- constants/currency.ts | 3 +- containers/Convert/Convert.tsx | 5 ++ hooks/useExchange.ts | 74 +++++++++++-------- pages/exchange.tsx | 2 +- .../coingecko/useCoinGeckoTokenPricesQuery.ts | 7 +- utils/currencies.ts | 14 +++- 6 files changed, 66 insertions(+), 39 deletions(-) diff --git a/constants/currency.ts b/constants/currency.ts index 2a0ae30e4e..571ff6ce64 100644 --- a/constants/currency.ts +++ b/constants/currency.ts @@ -78,7 +78,8 @@ export const INDEX_SYNTHS = new Set(['DebtRatio']); export const sUSD_EXCHANGE_RATE = new Wei(1); export const SYNTH_DECIMALS = 18; -export const ETH_ADDRESS = '0x4200000000000000000000000000000000000006'; +export const ETH_ADDRESS = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'; // For 1inch API +export const ETH_COINGECKO_ADDRESS = '0x4200000000000000000000000000000000000006'; // For coingecko API export const ATOMIC_EXCHANGES_L1 = [ 'sBTC', diff --git a/containers/Convert/Convert.tsx b/containers/Convert/Convert.tsx index f6fae53e84..b12721752b 100644 --- a/containers/Convert/Convert.tsx +++ b/containers/Convert/Convert.tsx @@ -47,6 +47,9 @@ type OneInchApproveSpenderResponse = { address: string; }; +const protocols = + 'OPTIMISM_UNISWAP_V3, OPTIMISM_SYNTHETIX, OPTIMISM_SYNTHETIX_WRAPPER, OPTIMISM_ONE_INCH_LIMIT_ORDER, OPTIMISM_ONE_INCH_LIMIT_ORDER_V2, OPTIMISM_CURVE, OPTIMISM_BALANCER_V2, OPTIMISM_VELODROME, OPTIMISM_KYBERSWAP_ELASTIC'; + const useConvert = () => { const { signer, network, tokensMap, synthetixjs } = Connector.useContainer(); const walletAddress = useRecoilValue(walletAddressState); @@ -68,6 +71,7 @@ const useConvert = () => { amount: params.amount, fromAddress: walletAddress, slippage, + protocols, referrerAddress: KWENTA_REFERRAL_ADDRESS, disableEstimate: true, }, @@ -99,6 +103,7 @@ const useConvert = () => { toTokenAddress: params.toTokenAddress, amount: params.amount, disableEstimate: true, + protocols, }, }); return ethers.utils diff --git a/hooks/useExchange.ts b/hooks/useExchange.ts index 4b644f3543..987b577133 100644 --- a/hooks/useExchange.ts +++ b/hooks/useExchange.ts @@ -14,6 +14,7 @@ import { CRYPTO_CURRENCY_MAP, CurrencyKey, ETH_ADDRESS, + ETH_COINGECKO_ADDRESS, } from 'constants/currency'; import { DEFAULT_CRYPTO_DECIMALS } from 'constants/defaults'; import ROUTES from 'constants/routes'; @@ -76,7 +77,7 @@ export type SwapRatio = 25 | 50 | 75 | 100; const useExchange = ({ footerCardAttached = false, routingEnabled = false, - showNoSynthsCard = true, + showNoSynthsCard = false, }: ExchangeCardProps) => { const { t } = useTranslation(); const { monitorTransaction } = TransactionNotifier.useContainer(); @@ -320,42 +321,50 @@ const useExchange = ({ }, [getBalance, baseCurrencyKey, isBaseCurrencyETH]); // TODO: Fix coingecko prices (optimism issue maybe?) - const quotePriceRate = useMemo( - () => - txProvider !== 'synthetix' && !quoteCurrency - ? coinGeckoPrices != null && - quoteCurrencyTokenAddress != null && - selectPriceCurrencyRate != null && - coinGeckoPrices[quoteCurrencyTokenAddress.toLowerCase()] != null - ? coinGeckoPrices[quoteCurrencyTokenAddress.toLowerCase()].usd / - selectPriceCurrencyRate.toNumber() - : wei(0) - : newGetExchangeRatesForCurrencies( - exchangeRates, - quoteCurrencyKey, - selectedPriceCurrency.name - ), - [ - txProvider, - quoteCurrency, - coinGeckoPrices, - quoteCurrencyTokenAddress, - selectPriceCurrencyRate, - exchangeRates, - quoteCurrencyKey, - selectedPriceCurrency.name, - ] - ); + const quotePriceRate = useMemo(() => { + const quoteCurrencyTokenAddresLower = (isQuoteCurrencyETH + ? ETH_COINGECKO_ADDRESS + : quoteCurrencyTokenAddress + )?.toLowerCase(); + + return txProvider !== 'synthetix' && !quoteCurrency + ? coinGeckoPrices != null && + quoteCurrencyTokenAddress != null && + selectPriceCurrencyRate != null && + quoteCurrencyTokenAddresLower != null && + coinGeckoPrices[quoteCurrencyTokenAddresLower] != null + ? coinGeckoPrices[quoteCurrencyTokenAddresLower].usd / selectPriceCurrencyRate.toNumber() + : wei(0) + : newGetExchangeRatesForCurrencies( + exchangeRates, + quoteCurrencyKey, + selectedPriceCurrency.name + ); + }, [ + isQuoteCurrencyETH, + quoteCurrencyTokenAddress, + txProvider, + quoteCurrency, + coinGeckoPrices, + selectPriceCurrencyRate, + exchangeRates, + quoteCurrencyKey, + selectedPriceCurrency.name, + ]); const basePriceRate = useMemo(() => { + const baseCurrencyTokenAddresLower = (isQuoteCurrencyETH + ? ETH_COINGECKO_ADDRESS + : baseCurrencyTokenAddress + )?.toLowerCase(); + return txProvider !== 'synthetix' && !baseCurrency ? coinGeckoPrices != null && baseCurrencyTokenAddress != null && selectPriceCurrencyRate != null && - coinGeckoPrices[baseCurrencyTokenAddress.toLowerCase()] != null - ? wei(coinGeckoPrices[baseCurrencyTokenAddress.toLowerCase()].usd).div( - selectPriceCurrencyRate - ) + baseCurrencyTokenAddresLower != null && + coinGeckoPrices[baseCurrencyTokenAddresLower] != null + ? wei(coinGeckoPrices[baseCurrencyTokenAddresLower].usd).div(selectPriceCurrencyRate) : wei(0) : newGetExchangeRatesForCurrencies( exchangeRates, @@ -363,10 +372,11 @@ const useExchange = ({ selectedPriceCurrency.name ); }, [ + isQuoteCurrencyETH, + baseCurrencyTokenAddress, txProvider, baseCurrency, coinGeckoPrices, - baseCurrencyTokenAddress, selectPriceCurrencyRate, exchangeRates, baseCurrencyKey, diff --git a/pages/exchange.tsx b/pages/exchange.tsx index d021bf267e..48bd5441cd 100644 --- a/pages/exchange.tsx +++ b/pages/exchange.tsx @@ -20,7 +20,7 @@ const Exchange: ExchangeComponent = () => { const exchangeData = useExchange({ footerCardAttached: false, routingEnabled: true, - showNoSynthsCard: true, + showNoSynthsCard: false, }); const { baseCurrencyKey, quoteCurrencyKey, inverseRate } = exchangeData; diff --git a/queries/coingecko/useCoinGeckoTokenPricesQuery.ts b/queries/coingecko/useCoinGeckoTokenPricesQuery.ts index 7732507d7f..59834da2c1 100644 --- a/queries/coingecko/useCoinGeckoTokenPricesQuery.ts +++ b/queries/coingecko/useCoinGeckoTokenPricesQuery.ts @@ -2,6 +2,7 @@ import axios from 'axios'; import { UseQueryOptions, useQuery } from 'react-query'; import { useRecoilValue } from 'recoil'; +import { ETH_ADDRESS, ETH_COINGECKO_ADDRESS } from 'constants/currency'; import QUERY_KEYS from 'constants/queryKeys'; import { isL2State } from 'store/wallet'; @@ -19,9 +20,9 @@ const useCoinGeckoTokenPricesQuery = ( QUERY_KEYS.CoinGecko.TokenPrices(tokenAddresses, platform), async () => { const response = await axios.get( - `${CG_BASE_API_URL}/simple/token_price/${platform}?contract_addresses=${tokenAddresses.join( - ',' - )}&vs_currencies=usd` + `${CG_BASE_API_URL}/simple/token_price/${platform}?contract_addresses=${tokenAddresses + .join(',') + .replace(ETH_ADDRESS, ETH_COINGECKO_ADDRESS)}&vs_currencies=usd` ); return response.data; }, diff --git a/utils/currencies.ts b/utils/currencies.ts index 97a567294e..c1691b2a48 100644 --- a/utils/currencies.ts +++ b/utils/currencies.ts @@ -1,7 +1,14 @@ import { Rates, Token } from '@synthetixio/queries'; import { wei } from '@synthetixio/wei'; -import { CurrencyKey, Synths, CRYPTO_CURRENCY_MAP, FIAT_SYNTHS } from 'constants/currency'; +import { + CurrencyKey, + Synths, + CRYPTO_CURRENCY_MAP, + FIAT_SYNTHS, + ETH_ADDRESS, + ETH_COINGECKO_ADDRESS, +} from 'constants/currency'; import { PriceResponse } from '../queries/coingecko/types'; import { FuturesMarketKey } from './futures'; @@ -83,7 +90,10 @@ export const newGetCoinGeckoPricesForCurrencies = ( if (!coinGeckoPrices || !baseCurrencyTokenAddress) { return wei(0); } - const base = baseCurrencyTokenAddress.toLowerCase(); + const base = (baseCurrencyTokenAddress === ETH_ADDRESS + ? ETH_COINGECKO_ADDRESS + : baseCurrencyTokenAddress + ).toLowerCase(); if (!coinGeckoPrices[base]) { return wei(0);