diff --git a/src/components/AssetNumericInput/index.tsx b/src/components/AssetNumericInput/index.tsx index 13a28a00..e9b35755 100644 --- a/src/components/AssetNumericInput/index.tsx +++ b/src/components/AssetNumericInput/index.tsx @@ -1,48 +1,29 @@ import { FC, useMemo } from 'preact/compat'; import { UseFormRegisterReturn } from 'react-hook-form'; -import { ConnectButton } from '@rainbow-me/rainbowkit'; -import { InputTokenType, OutputTokenType } from '../../constants/tokenConfig'; import { AssetButton } from '../buttons/AssetButton'; import { SwapFormValues } from '../Nabla/schema'; import { NumericInput } from '../NumericInput'; - -export const ChainName = () => ( - - {({ account, chain, openChainModal, openConnectModal, authenticationStatus, mounted }) => { - const ready = mounted && authenticationStatus !== 'loading'; - const connected = - ready && account && chain && (!authenticationStatus || authenticationStatus === 'authenticated'); - - return ( - - ); - }} - -); +import { AssetIconType } from '../../hooks/useGetIcon'; interface AssetNumericInputProps { - tokenType?: InputTokenType | OutputTokenType | 'eur'; - tokenSymbol?: string; + assetIcon: AssetIconType; + tokenSymbol: string; onClick: () => void; - additionalText?: string; disabled?: boolean; readOnly?: boolean; registerInput: UseFormRegisterReturn; } export const AssetNumericInput: FC = ({ - additionalText, - tokenType, + assetIcon, tokenSymbol, onClick, registerInput, ...rest }) => { const memoizedAssetButton = useMemo( - () => , - [tokenType, tokenSymbol, onClick], + () => , + [assetIcon, tokenSymbol, onClick], ); return ( @@ -55,7 +36,6 @@ export const AssetNumericInput: FC = ({
{memoizedAssetButton}
- {additionalText ?

{additionalText}

: }
= ({ tokenOutData, fromToken, toTokenSymbol }) => { const exchangeRate = - fromToken !== undefined && toTokenSymbol !== undefined && !tokenOutData.isLoading && tokenOutData.data ? ( + fromToken !== undefined && !tokenOutData.isLoading && tokenOutData.data ? ( <>{`1 ${fromToken.assetSymbol} = ${Number(tokenOutData.data.effectiveExchangeRate).toFixed( 2, )} ${toTokenSymbol}`} diff --git a/src/components/FeeCollapse/index.tsx b/src/components/FeeCollapse/index.tsx index c2585583..b11cabfc 100644 --- a/src/components/FeeCollapse/index.tsx +++ b/src/components/FeeCollapse/index.tsx @@ -23,7 +23,7 @@ function calculateFeesUSD(fromAmount: string): string { interface CollapseProps { fromAmount?: string; toAmount?: string; - toTokenSymbol?: string; + toTokenSymbol: string; } export const FeeCollapse: FC = ({ fromAmount, toAmount, toTokenSymbol }) => { diff --git a/src/components/InputKeys/PoolListItem/index.tsx b/src/components/InputKeys/PoolListItem/index.tsx index 71945cfb..19ecf805 100644 --- a/src/components/InputKeys/PoolListItem/index.tsx +++ b/src/components/InputKeys/PoolListItem/index.tsx @@ -1,14 +1,14 @@ import { Avatar, AvatarProps, Button } from 'react-daisyui'; import { CheckIcon } from '@heroicons/react/20/solid'; -import { useGetIcon } from '../../../hooks/useGetIcon'; -import { Fiat, InputTokenType, OutputTokenType } from '../../../constants/tokenConfig'; +import { AssetIconType, useGetIcon } from '../../../hooks/useGetIcon'; +import { InputTokenType, OutputTokenType } from '../../../constants/tokenConfig'; interface PoolListItemProps { tokenType: T; tokenSymbol: string; isSelected?: boolean; onSelect: (tokenType: T) => void; - fiat?: Fiat; + assetIcon: AssetIconType; } export function PoolListItem({ @@ -16,11 +16,9 @@ export function PoolListItem({ tokenSymbol, isSelected, onSelect, - fiat, + assetIcon, }: PoolListItemProps) { - const formattedTokenType = fiat ? fiat.symbol : tokenType; - const formattedTokenSymbol = fiat ? fiat.string : tokenSymbol; - const tokenIcon = useGetIcon(formattedTokenType); + const tokenIcon = useGetIcon(assetIcon); return ( ); diff --git a/src/components/InputKeys/SelectionModal.tsx b/src/components/InputKeys/SelectionModal.tsx index eea73e86..ef4f1440 100644 --- a/src/components/InputKeys/SelectionModal.tsx +++ b/src/components/InputKeys/SelectionModal.tsx @@ -4,6 +4,7 @@ import { Fiat, InputTokenType, OutputTokenType } from '../../constants/tokenConf import { Dialog } from '../Dialog'; import { Skeleton } from '../Skeleton'; import { PoolListItem } from './PoolListItem'; +import { AssetIconType } from '../../hooks/useGetIcon'; interface PoolSelectorModalProps extends PoolListProps { isLoading?: boolean; @@ -12,9 +13,9 @@ interface PoolSelectorModalProps ext } interface PoolListProps { - definitions: { assetSymbol: string; type: T; fiat?: Fiat }[]; + definitions: { assetSymbol: string; type: T; assetIcon: AssetIconType }[]; onSelect: (tokenType: InputTokenType | OutputTokenType) => void; - selected: InputTokenType | OutputTokenType | undefined; + selected: InputTokenType | OutputTokenType; } export function PoolSelectorModal({ @@ -45,14 +46,14 @@ function PoolList({ onSelect, defini placeholder="Find by name or address" />
- {definitions.map(({ fiat, assetSymbol, type }) => ( + {definitions.map(({ assetIcon, assetSymbol, type }) => ( ))}
diff --git a/src/components/buttons/AssetButton/index.tsx b/src/components/buttons/AssetButton/index.tsx index 1df21d5a..29bd703e 100644 --- a/src/components/buttons/AssetButton/index.tsx +++ b/src/components/buttons/AssetButton/index.tsx @@ -1,13 +1,12 @@ -import { InputTokenType, OutputTokenType } from '../../../constants/tokenConfig'; -import { useGetIcon } from '../../../hooks/useGetIcon'; +import { AssetIconType, useGetIcon } from '../../../hooks/useGetIcon'; interface AssetButtonProps { - tokenType?: InputTokenType | OutputTokenType | 'eur'; - tokenSymbol?: string; + assetIcon: AssetIconType; + tokenSymbol: string; onClick: () => void; } -export function AssetButton({ tokenType, tokenSymbol, onClick }: AssetButtonProps) { - const icon = useGetIcon(tokenType); +export function AssetButton({ assetIcon, tokenSymbol, onClick }: AssetButtonProps) { + const icon = useGetIcon(assetIcon); return ( ); } diff --git a/src/constants/tokenConfig.ts b/src/constants/tokenConfig.ts index 83a00388..47e4e2d8 100644 --- a/src/constants/tokenConfig.ts +++ b/src/constants/tokenConfig.ts @@ -1,3 +1,5 @@ +import { AssetIconType } from '../hooks/useGetIcon'; + export interface InputTokenDetails { assetSymbol: string; erc20AddressSourceChain: `0x${string}`; @@ -6,20 +8,21 @@ export interface InputTokenDetails { pendulumCurrencyId: { XCM: number }; pendulumAssetSymbol: string; }; + polygonAssetIcon: AssetIconType; decimals: number; } export type InputTokenType = 'usdc' | 'usdce'; export interface Fiat { - symbol: 'eur'; - string: 'EUR'; + assetIcon: AssetIconType; + symbol: string; } export interface OutputTokenDetails { tomlFileUrl: string; decimals: number; - fiat?: Fiat; + fiat: Fiat; stellarAsset: { code: { hex: string; @@ -44,6 +47,7 @@ export const INPUT_TOKEN_CONFIG: Record = { pendulumCurrencyId: { XCM: 12 }, pendulumAssetSymbol: 'USDC.axl', }, + polygonAssetIcon: 'polygonUSDC', decimals: 6, }, usdce: { @@ -54,6 +58,7 @@ export const INPUT_TOKEN_CONFIG: Record = { pendulumCurrencyId: { XCM: 12 }, pendulumAssetSymbol: 'USDC.axl', }, + polygonAssetIcon: 'polygonUSDC', decimals: 6, }, }; @@ -64,8 +69,8 @@ export const OUTPUT_TOKEN_CONFIG: Record = tomlFileUrl: 'https://mykobo.co/.well-known/stellar.toml', decimals: 12, fiat: { - symbol: 'eur', - string: 'EUR', + assetIcon: 'eur', + symbol: 'EUR', }, stellarAsset: { code: { diff --git a/src/hooks/useGetIcon.tsx b/src/hooks/useGetIcon.tsx index a1f53f53..40343de5 100644 --- a/src/hooks/useGetIcon.tsx +++ b/src/hooks/useGetIcon.tsx @@ -1,6 +1,3 @@ -import { useAccount, useChainId } from 'wagmi'; -import { polygon } from 'wagmi/chains'; - import EURC from '../assets/coins/EURC.png'; import EUR from '../assets/coins/EUR.svg'; import USDC from '../assets/coins/USDC.png'; @@ -8,25 +5,14 @@ import USDC_POLYGON from '../assets/coins/USDC_POLYGON.svg'; import DefaultIcon from '../assets/coins/PEN.png'; const ICONS = { - default: { - eurc: EURC, - eur: EUR, - usdc: USDC, - usdce: USDC, - }, - [polygon.id]: { - usdc: USDC_POLYGON, - usdce: USDC_POLYGON, - }, + eurc: EURC, + eur: EUR, + usdc: USDC, + polygonUSDC: USDC_POLYGON, }; -type TokenType = keyof typeof ICONS.default | keyof (typeof ICONS)[typeof polygon.id]; - -export function useGetIcon(token?: TokenType, defaultIcon = DefaultIcon) { - const chainId = useChainId(); - const { isConnected } = useAccount(); - - const iconMap = isConnected ? ICONS[chainId as keyof typeof ICONS] : ICONS.default; +export type AssetIconType = keyof typeof ICONS; - return token ? iconMap[token as keyof typeof iconMap] || ICONS.default[token] || defaultIcon : defaultIcon; +export function useGetIcon(assetIcon: AssetIconType, defaultIcon = DefaultIcon) { + return ICONS[assetIcon]; } diff --git a/src/pages/swap/index.tsx b/src/pages/swap/index.tsx index 7b3f44d6..6b7b4fd7 100644 --- a/src/pages/swap/index.tsx +++ b/src/pages/swap/index.tsx @@ -23,8 +23,6 @@ import { ProgressPage } from '../progress'; import { SuccessPage } from '../success'; import { FailurePage } from '../failure'; -const SHOW_RECEIVED_CURRENCY_IN_FIAT = true; - const Arrow = () => (
@@ -70,8 +68,8 @@ export const SwapPage = () => { to, } = useSwapForm(); - const fromToken = from ? INPUT_TOKEN_CONFIG[from] : undefined; - const toToken = to ? OUTPUT_TOKEN_CONFIG[to] : undefined; + const fromToken = INPUT_TOKEN_CONFIG[from]; + const toToken = OUTPUT_TOKEN_CONFIG[to]; const tokenOutData = useTokenOutAmount({ wantsSwap: true, @@ -130,57 +128,47 @@ export const SwapPage = () => { const ReceiveNumericInput = useMemo( () => ( setModalType('to')} registerInput={form.register('toAmount')} disabled={isQuoteSubmitted || tokenOutData.isLoading} readOnly={true} /> ), - [ - toToken?.fiat?.symbol, - toToken?.fiat?.string, - toToken?.stellarAsset.code.string, - to, - form, - isQuoteSubmitted, - tokenOutData.isLoading, - setModalType, - ], + [toToken.fiat.symbol, toToken.fiat.assetIcon, to, form, isQuoteSubmitted, tokenOutData.isLoading, setModalType], ); const WidthrawNumericInput = useMemo( () => ( setIsQuoteSubmitted(true) })} - tokenType={from} - tokenSymbol={fromToken?.assetSymbol} + tokenSymbol={fromToken.assetSymbol} + assetIcon={fromToken.polygonAssetIcon} onClick={() => setModalType('from')} /> ), - [form, from, fromToken?.assetSymbol, setModalType], + [form, fromToken.polygonAssetIcon, fromToken.polygonAssetIcon, setModalType], ); function getCurrentErrorMessage() { const amountOut = tokenOutData.data?.amountOut; - if (amountOut !== undefined && toToken !== undefined) { + if (amountOut !== undefined) { const maxAmountRaw = Big(toToken.maxWithdrawalAmountRaw); const minAmountRaw = Big(toToken.minWithdrawalAmountRaw); if (maxAmountRaw.lt(Big(amountOut.rawBalance))) { const maxAmountUnits = multiplyByPowerOfTen(maxAmountRaw, -toToken.decimals); return `Maximum withdrawal amount is ${stringifyBigWithSignificantDecimals(maxAmountUnits, 2)} ${ - SHOW_RECEIVED_CURRENCY_IN_FIAT ? toToken.fiat?.string : toToken.stellarAsset.code.string + toToken.fiat.symbol }.`; } if (config.test.overwriteMinimumTransferAmount === false && minAmountRaw.gt(Big(amountOut.rawBalance))) { const minAmountUnits = multiplyByPowerOfTen(minAmountRaw, -toToken.decimals); return `Minimum withdrawal amount is ${stringifyBigWithSignificantDecimals(minAmountUnits, 2)} ${ - SHOW_RECEIVED_CURRENCY_IN_FIAT ? toToken.fiat?.string : toToken.stellarAsset.code.string + toToken.fiat.symbol }.`; } } @@ -193,12 +181,12 @@ export const SwapPage = () => { ? Object.entries(INPUT_TOKEN_CONFIG).map(([key, value]) => ({ type: key as InputTokenType, assetSymbol: value.assetSymbol, - fiat: undefined, + assetIcon: value.polygonAssetIcon, })) : Object.entries(OUTPUT_TOKEN_CONFIG).map(([key, value]) => ({ type: key as OutputTokenType, - assetSymbol: value.stellarAsset.code.string, - fiat: SHOW_RECEIVED_CURRENCY_IN_FIAT ? value.fiat : undefined, + assetSymbol: value.fiat.symbol, + assetIcon: value.fiat.assetIcon, })); const modals = ( @@ -239,13 +227,13 @@ export const SwapPage = () => { {...{ tokenOutData, fromToken, - toTokenSymbol: SHOW_RECEIVED_CURRENCY_IN_FIAT ? toToken?.fiat?.string : toToken?.stellarAsset.code.string, + toTokenSymbol: toToken.fiat.symbol, }} />