Skip to content

Commit

Permalink
Add extra checks to get usd value from output token and explicitly ch…
Browse files Browse the repository at this point in the history
…eck for invalid values (#4183)
  • Loading branch information
Sam Mason de Caires authored and nickbytes committed Sep 21, 2022
1 parent a69ca46 commit 1f1d10c
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/screens/ExchangeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ import Routes from '@/navigation/routesNames';
import { ethereumUtils, gasUtils } from '@/utils';
import { useEthUSDPrice } from '@/utils/ethereumUtils';
import logger from 'logger';
import { assert } from 'chai';

export const DEFAULT_SLIPPAGE_BIPS = {
[Network.mainnet]: 100,
Expand Down Expand Up @@ -684,14 +683,28 @@ export default function ExchangeModal({
} else {
const ethPriceInNativeCurrency =
genericAssets[ETH_ADDRESS]?.price?.value ?? 0;
const tokenPriceInNativeCurrency =
const inputTokenPriceInNativeCurrency =
genericAssets[inputCurrency?.address]?.price?.value ?? 0;
const tokensPerEth = divide(
tokenPriceInNativeCurrency,
const outputTokenPriceInNativeCurrency =
genericAssets[outputCurrency?.address]?.price?.value ?? 0;
const inputTokensPerEth = divide(
inputTokenPriceInNativeCurrency,
ethPriceInNativeCurrency
);
const inputTokensInEth = multiply(tokensPerEth, inputAmount!);
amountInUSD = multiply(priceOfEther, inputTokensInEth);
const outputTokensPerEth = divide(
outputTokenPriceInNativeCurrency,
ethPriceInNativeCurrency
);
const inputTokensInEth = multiply(inputTokensPerEth, inputAmount!);
const outputTokensInEth = multiply(outputTokensPerEth, outputAmount!);

const availableTokenPrice = inputTokensInEth ?? outputTokensInEth;
const maybeResultAmount = multiply(priceOfEther, availableTokenPrice);
// We have to use string matching here because the multiply helper will return the value as a string from the helpers
// If we pass a empty string value to segment it gets ignored
amountInUSD = ['NaN', '0'].includes(maybeResultAmount)
? ''
: maybeResultAmount;
}
} catch (e) {
logger.log('error getting the swap amount in USD price', e);
Expand Down

0 comments on commit 1f1d10c

Please sign in to comment.