Skip to content

Commit

Permalink
feat: add native currency check
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Oct 2, 2023
1 parent 2893203 commit 71d0f52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 10 additions & 1 deletion libs/oeth/swap/src/views/SwapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '@origin/shared/providers';
import { composeContexts, isNilOrEmpty } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { useAccount, useBalance } from 'wagmi';
import { mainnet, useAccount, useBalance, useNetwork } from 'wagmi';

import { ApyHeader } from '../components/ApyHeader';
import { SwapRoute } from '../components/SwapRoute';
Expand Down Expand Up @@ -74,6 +74,7 @@ function SwapViewWrapped() {
const intl = useIntl();
const { value: slippage, set: setSlippage } = useSlippage();
const { address, isConnected } = useAccount();
const { chain } = useNetwork();
const [tokenSource, setTokenSource] = useState<TokenSource | null>(null);
const [
{
Expand Down Expand Up @@ -194,6 +195,10 @@ function SwapViewWrapped() {
onTokenClick={() => {
setTokenSource('tokenIn');
}}
isNativeCurrency={
tokenIn.symbol ===
(chain?.nativeCurrency.symbol ?? mainnet.nativeCurrency.symbol)
}
tokenPriceUsd={prices?.[tokenIn.symbol]}
isPriceLoading={isPriceLoading}
isConnected={isConnected}
Expand Down Expand Up @@ -239,6 +244,10 @@ function SwapViewWrapped() {
onTokenClick={() => {
setTokenSource('tokenOut');
}}
isNativeCurrency={
tokenOut.symbol ===
(chain?.nativeCurrency.symbol ?? mainnet.nativeCurrency.symbol)
}
tokenPriceUsd={prices?.[tokenOut.symbol]}
isPriceLoading={isSwapRoutesLoading || isPriceLoading}
isConnected={isConnected}
Expand Down
11 changes: 6 additions & 5 deletions libs/shared/components/src/Inputs/TokenInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type TokenInputProps = {
disableMaxButton?: boolean;
token: Token;
onTokenClick?: () => void;
isNativeCurrency?: boolean;
isTokenClickDisabled?: boolean;
tokenPriceUsd?: number;
isPriceLoading?: boolean;
Expand All @@ -58,6 +59,7 @@ export const TokenInput = forwardRef<HTMLInputElement, TokenInputProps>(
disableMaxButton,
token,
onTokenClick,
isNativeCurrency = false,
isTokenClickDisabled,
tokenPriceUsd = 0,
isPriceLoading,
Expand All @@ -70,17 +72,16 @@ export const TokenInput = forwardRef<HTMLInputElement, TokenInputProps>(
const intl = useIntl();

const handleMaxClick = () => {
const max =
token.symbol === 'ETH'
? balance - parseEther(MIN_ETH_FOR_GAS)
: balance;
const max = isNativeCurrency
? balance - parseEther(MIN_ETH_FOR_GAS)
: balance;
onAmountChange(max);
};

const amountUsd = +formatUnits(amount, decimals) * tokenPriceUsd;
const maxVisible =
!hideMaxButton &&
(token.symbol === 'ETH' ? balance > parseEther(MIN_ETH_FOR_GAS) : true);
(isNativeCurrency ? balance > parseEther(MIN_ETH_FOR_GAS) : true);
const maxDisabled = disableMaxButton || !isConnected || isBalanceLoading;

return (
Expand Down

0 comments on commit 71d0f52

Please sign in to comment.