Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOV-4524: Show USD values on Convert page #1038

Merged
merged 8 commits into from
Nov 12, 2024
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
5 changes: 5 additions & 0 deletions .changeset/gentle-schools-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"frontend": patch
---

chore: fix asset icon styles
5 changes: 5 additions & 0 deletions .changeset/slimy-tigers-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"frontend": patch
---

SOV-4524: Show USD values on Convert page
130 changes: 97 additions & 33 deletions apps/frontend/src/app/5_pages/ConvertPage/ConvertPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ import { AmountRenderer } from '../../2_molecules/AmountRenderer/AmountRenderer'
import { AssetRenderer } from '../../2_molecules/AssetRenderer/AssetRenderer';
import { MaxButton } from '../../2_molecules/MaxButton/MaxButton';
import { TradingChart } from '../../2_molecules/TradingChart/TradingChart';
import { TOKEN_RENDER_PRECISION } from '../../../constants/currencies';
import { TOKEN_RENDER_PRECISION, USD } from '../../../constants/currencies';
import { getTokenDisplayName } from '../../../constants/tokens';
import { useAccount } from '../../../hooks/useAccount';
import { useAssetBalance } from '../../../hooks/useAssetBalance';
import { useCurrentChain } from '../../../hooks/useChainStore';
import { useDollarValue } from '../../../hooks/useDollarValue';
import { useWeiAmountInput } from '../../../hooks/useWeiAmountInput';
import { translations } from '../../../locales/i18n';
import {
Expand All @@ -50,7 +51,7 @@ import {
listAssetsOfChain,
} from '../../../utils/asset';
import { removeTrailingZerosFromString } from '../../../utils/helpers';
import { decimalic, fromWei } from '../../../utils/math';
import { decimalic, fromWei, toWei } from '../../../utils/math';
import {
CATEGORY_TOKENS,
DEFAULT_SWAP_DESTINATIONS,
Expand Down Expand Up @@ -329,6 +330,36 @@ const ConvertPage: FC = () => {
.toString();
}, [quote, route, slippageTolerance]);

const { usdValue: minimumReceivedUsdValue } = useDollarValue(
destinationToken,
minimumReceived !== ''
? toWei(minimumReceived).toString()
: Decimal.ZERO.toString(),
);

const renderMinimumReceived = useMemo(
() => (
<>
<AmountRenderer
value={minimumReceived}
suffix={getTokenDisplayName(destinationToken)}
precision={TOKEN_RENDER_PRECISION}
/>

<span className="opacity-75">
{' ('}
<AmountRenderer
value={minimumReceivedUsdValue}
suffix={USD}
showRoundingPrefix={false}
/>
{')'}
</span>
</>
),
[destinationToken, minimumReceived, minimumReceivedUsdValue],
);

const priceToken = useMemo<string>(() => {
if (!destinationToken) {
return sourceToken;
Expand Down Expand Up @@ -479,6 +510,11 @@ const ConvertPage: FC = () => {
[quote],
);

const { usdValue: priceUsdValue } = useDollarValue(
priceToken,
price !== '' ? toWei(price).toString() : Decimal.ZERO.toString(),
);

const renderPriceAmount = useMemo(() => {
if (price) {
return (
Expand All @@ -489,11 +525,21 @@ const ConvertPage: FC = () => {
precision={TOKEN_RENDER_PRECISION}
trigger={TooltipTrigger.hover}
/>

<span className="opacity-75">
{' ('}
<AmountRenderer
value={priceUsdValue}
suffix={USD}
showRoundingPrefix={false}
/>
{')'}
</span>
</>
);
}
return t(commonTranslations.na);
}, [price, priceToken]);
}, [price, priceToken, priceUsdValue]);

const renderPair = useMemo(
() => `${sourceToken}/${destinationToken}/${currentChainId}`,
Expand Down Expand Up @@ -609,6 +655,16 @@ const ConvertPage: FC = () => {
}
}, [account, setAmount]);

const { usdValue: sourceUsdValue } = useDollarValue(
sourceToken,
weiAmount.toString(),
);

const { usdValue: destinationUsdValue } = useDollarValue(
destinationToken,
quote !== '' ? toWei(renderDestinationAmount).toString() : '0',
);

return (
<>
<Helmet>
Expand All @@ -625,10 +681,10 @@ const ConvertPage: FC = () => {
{t(pageTranslations.subtitle)}
</Paragraph>

<div className="flex flex-col-reverse lg:flex-row lg:space-x-6 xl:w-9/12 w-full h-full mt-6 lg:mt-12">
<div className="flex flex-col-reverse items-center lg:items-stretch gap-y-12 lg:flex-row lg:space-x-6 xl:w-9/12 w-full h-full mt-6 lg:mt-12">
<TradingChart pair={renderPair} />

<div className="p-0 sm:border sm:border-gray-50 sm:rounded lg:min-w-[28rem] sm:p-6 sm:bg-gray-90 self-start h-full">
<div className="p-0 sm:border sm:border-gray-50 sm:rounded lg:min-w-[28rem] sm:p-6 sm:bg-gray-90 lg:self-start h-full w-full sm:w-auto">
<div className="bg-gray-80 rounded p-6">
<div className="w-full flex flex-row justify-between items-center">
<Paragraph size={ParagraphSize.base} className="font-medium">
Expand All @@ -644,18 +700,25 @@ const ConvertPage: FC = () => {
/>
</div>

<div className="w-full flex flex-row justify-between items-center gap-3 mt-3.5">
<AmountInput
value={amount}
onChangeText={setAmount}
label={t(commonTranslations.amount)}
min={0}
invalid={!isValidAmount}
disabled={!account}
className="w-full flex-grow-0 flex-shrink"
dataAttribute="convert-from-amount"
placeholder="0"
/>
<div className="w-full flex flex-row justify-between items-start gap-3 mt-3.5">
<div>
<AmountInput
value={amount}
onChangeText={setAmount}
label={t(commonTranslations.amount)}
min={0}
invalid={!isValidAmount}
disabled={!account}
className="w-full flex-grow-0 flex-shrink"
dataAttribute="convert-from-amount"
placeholder="0"
/>

<div className="flex justify-end text-tiny text-gray-30 mt-1">
<AmountRenderer value={sourceUsdValue} suffix={USD} />
</div>
</div>

<AssetDropdownWithFilters
token={sourceToken}
selectedCategories={sourceCategories}
Expand Down Expand Up @@ -696,15 +759,22 @@ const ConvertPage: FC = () => {
{t(pageTranslations.form.convertTo)}
</Paragraph>

<div className="w-full flex flex-row justify-between items-center gap-3 mt-3.5">
<AmountInput
value={renderDestinationAmount}
label={t(commonTranslations.amount)}
readOnly
placeholder={t(commonTranslations.na)}
className="w-full flex-grow-0 flex-shrink"
dataAttribute="convert-to-amount"
/>
<div className="w-full flex flex-row justify-between items-start gap-3 mt-3.5">
<div>
<AmountInput
value={renderDestinationAmount}
label={t(commonTranslations.amount)}
readOnly
placeholder={t(commonTranslations.na)}
className="w-full flex-grow-0 flex-shrink"
dataAttribute="convert-to-amount"
/>

<div className="flex justify-end text-tiny text-gray-30 mt-1">
<AmountRenderer value={destinationUsdValue} suffix={USD} />
</div>
</div>

<AssetDropdownWithFilters
token={destinationToken}
selectedCategories={destinationCategories}
Expand Down Expand Up @@ -745,13 +815,7 @@ const ConvertPage: FC = () => {
<SimpleTableRow
label={t(pageTranslations.minimumReceived)}
valueClassName="text-primary-10"
value={
<AmountRenderer
value={minimumReceived}
suffix={getTokenDisplayName(destinationToken)}
precision={TOKEN_RENDER_PRECISION}
/>
}
value={renderMinimumReceived}
/>
<SimpleTableRow
label={t(pageTranslations.maximumPrice)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { getContract } from '@sovryn/contracts';
import { getProvider } from '@sovryn/ethers-provider';
import { SmartRouter, SwapRoute } from '@sovryn/sdk';

import { SWAP_ROUTES } from './ConvertPage.constants';
import { getCurrentChain } from '../../../hooks/useChainStore';
import { SWAP_ROUTES } from './ConvertPage.constants';

export const getRouteContract = async (
route: SwapRoute,
Expand Down
Loading