Skip to content

Commit

Permalink
hotfix: ETH swap token address (#1355)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LeifuChen authored Sep 5, 2022
1 parent d81d792 commit 337472f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 39 deletions.
3 changes: 2 additions & 1 deletion constants/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export const INDEX_SYNTHS = new Set<CurrencyKey | 'DebtRatio'>(['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',
Expand Down
5 changes: 5 additions & 0 deletions containers/Convert/Convert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -68,6 +71,7 @@ const useConvert = () => {
amount: params.amount,
fromAddress: walletAddress,
slippage,
protocols,
referrerAddress: KWENTA_REFERRAL_ADDRESS,
disableEstimate: true,
},
Expand Down Expand Up @@ -99,6 +103,7 @@ const useConvert = () => {
toTokenAddress: params.toTokenAddress,
amount: params.amount,
disableEstimate: true,
protocols,
},
});
return ethers.utils
Expand Down
74 changes: 42 additions & 32 deletions hooks/useExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -320,53 +321,62 @@ 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,
baseCurrencyKey,
selectedPriceCurrency.name
);
}, [
isQuoteCurrencyETH,
baseCurrencyTokenAddress,
txProvider,
baseCurrency,
coinGeckoPrices,
baseCurrencyTokenAddress,
selectPriceCurrencyRate,
exchangeRates,
baseCurrencyKey,
Expand Down
2 changes: 1 addition & 1 deletion pages/exchange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Exchange: ExchangeComponent = () => {
const exchangeData = useExchange({
footerCardAttached: false,
routingEnabled: true,
showNoSynthsCard: true,
showNoSynthsCard: false,
});

const { baseCurrencyKey, quoteCurrencyKey, inverseRate } = exchangeData;
Expand Down
7 changes: 4 additions & 3 deletions queries/coingecko/useCoinGeckoTokenPricesQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -19,9 +20,9 @@ const useCoinGeckoTokenPricesQuery = (
QUERY_KEYS.CoinGecko.TokenPrices(tokenAddresses, platform),
async () => {
const response = await axios.get<PriceResponse>(
`${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;
},
Expand Down
14 changes: 12 additions & 2 deletions utils/currencies.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down

1 comment on commit 337472f

@vercel
Copy link

@vercel vercel bot commented on 337472f Sep 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kwenta – ./

kwenta-kwenta.vercel.app
kwenta-git-main-kwenta.vercel.app
kwenta.io
v2.beta.kwenta.io

Please sign in to comment.