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

844: Fixed contract errors when switching wallets #914

Merged
8 commits merged into from
Jun 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
9 changes: 9 additions & 0 deletions src/components/@widgets/MakeWidget/MakeWidget.styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SelectLabel } from "../../../styled-components/Select/Select";
import Tooltip from "../../SwapInputs/subcomponents/Tooltip/Tooltip";
import ActionButtons from "./subcomponents/ActionButtons/ActionButtons";
import AddressInput from "./subcomponents/AddressInput/AddressInput";
import InfoSection from "./subcomponents/InfoSection/InfoSection";
import InputSection from "./subcomponents/InputSection/InputSection";
import OrderTypeSelector from "./subcomponents/OrderTypeSelector/OrderTypeSelector";
import { RateField } from "./subcomponents/RateField/RateField";
Expand Down Expand Up @@ -64,6 +65,14 @@ export const StyledAddressInput = styled(AddressInput)`

export const StyledActionButtons = styled(ActionButtons)``;

export const StyledInfoSection = styled(InfoSection)`
flex-grow: 1;

& + ${StyledActionButtons} {
margin-top: 1rem;
}
`;

export const StyledTooltip = styled(Tooltip)`
position: absolute;
right: 0;
Expand Down
17 changes: 14 additions & 3 deletions src/components/@widgets/MakeWidget/MakeWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ import switchToDefaultChain from "../../../helpers/switchToDefaultChain";
import toMaxAllowedDecimalsNumberString from "../../../helpers/toMaxAllowedDecimalsNumberString";
import toRoundedNumberString from "../../../helpers/toRoundedNumberString";
import useAllowance from "../../../hooks/useAllowance";
import useAllowancesOrBalancesFailed from "../../../hooks/useAllowancesOrBalancesFailed";
import useApprovalPending from "../../../hooks/useApprovalPending";
import useDepositPending from "../../../hooks/useDepositPending";
import useInsufficientBalance from "../../../hooks/useInsufficientBalance";
import useMaxAmount from "../../../hooks/useMaxAmount";
import useNativeWrappedToken from "../../../hooks/useNativeWrappedToken";
import useNetworkSupported from "../../../hooks/useNetworkSupported";
import useShouldDepositNativeToken from "../../../hooks/useShouldDepositNativeTokenAmount";
import useTokenInfo from "../../../hooks/useTokenInfo";
import useValidAddress from "../../../hooks/useValidAddress";
Expand All @@ -67,6 +69,7 @@ import {
OrderTypeSelectorAndRateFieldWrapper,
StyledActionButtons,
StyledAddressInput,
StyledInfoSection,
StyledOrderTypeSelector,
StyledRateField,
StyledTooltip,
Expand Down Expand Up @@ -156,6 +159,8 @@ const MakeWidget: FC = () => {
const shouldDepositNativeToken = !!shouldDepositNativeTokenAmount;
const hasDepositPending = useDepositPending();
const isValidAddress = useValidAddress(takerAddress);
const isAllowancesOrBalancesFailed = useAllowancesOrBalancesFailed();
const isNetworkSupported = useNetworkSupported();

// Modal states
const { setShowWalletList } = useContext(InterfaceContext);
Expand Down Expand Up @@ -433,8 +438,8 @@ const MakeWidget: FC = () => {
/>
<SwapInputs
canSetQuoteAmount
disabled={!isActive}
readOnly={!isActive}
disabled={!isActive || isAllowancesOrBalancesFailed}
readOnly={!isActive || !isNetworkSupported}
showMaxButton={showMaxButton}
showMaxInfoButton={showMaxInfoButton}
baseAmount={makerAmount}
Expand Down Expand Up @@ -487,6 +492,11 @@ const MakeWidget: FC = () => {
</TooltipContainer>
)}

<StyledInfoSection
isAllowancesFailed={isAllowancesOrBalancesFailed}
isNetworkUnsupported={!isNetworkSupported}
/>

<StyledActionButtons
hasInsufficientExpiry={expiry === 0}
hasInsufficientAllowance={!hasSufficientAllowance}
Expand All @@ -497,8 +507,9 @@ const MakeWidget: FC = () => {
hasMissingMakerToken={!makerTokenInfo}
hasMissingTakerAmount={hasMissingTakerAmount}
hasMissingTakerToken={!takerTokenInfo}
isNetworkUnsupported={false}
isNetworkUnsupported={!isNetworkSupported}
shouldDepositNativeToken={shouldDepositNativeToken}
shouldRefresh={isAllowancesOrBalancesFailed}
walletIsNotConnected={!isActive}
makerTokenSymbol={makerTokenInfo?.symbol}
onBackButtonClick={handleBackButtonClick}
Expand Down
7 changes: 6 additions & 1 deletion src/components/@widgets/MakeWidget/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const getActionButtonTranslation = (
hasMissingTakerToken: boolean,
networkIsUnsupported: boolean,
shouldDepositNativeToken: boolean,
shouldRefresh: boolean,
walletIsNotConnected: boolean,
makerTokenSymbol?: string
): string => {
Expand All @@ -22,7 +23,11 @@ export const getActionButtonTranslation = (
}

if (networkIsUnsupported) {
return i18n.t("wallet.unsupportedNetwork");
return i18n.t("wallet.switchNetwork");
}

if (shouldRefresh) {
return i18n.t("common.reloadPage");
}

if (hasInsufficientExpiry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ActionButtonsProps = {
hasMissingTakerToken: boolean;
isNetworkUnsupported: boolean;
shouldDepositNativeToken: boolean;
shouldRefresh: boolean;
walletIsNotConnected: boolean;
makerTokenSymbol?: string;
takerTokenSymbol?: string;
Expand All @@ -41,6 +42,7 @@ const ActionButtons: FC<ActionButtonsProps> = ({
hasMissingTakerToken,
isNetworkUnsupported,
shouldDepositNativeToken,
shouldRefresh,
walletIsNotConnected,
makerTokenSymbol,
onBackButtonClick,
Expand All @@ -57,6 +59,7 @@ const ActionButtons: FC<ActionButtonsProps> = ({
hasMissingTakerAmount ||
hasMissingTakerToken) &&
!walletIsNotConnected &&
!shouldRefresh &&
!isNetworkUnsupported;

const buttonText = getActionButtonTranslation(
Expand All @@ -69,6 +72,7 @@ const ActionButtons: FC<ActionButtonsProps> = ({
hasMissingTakerToken,
isNetworkUnsupported,
shouldDepositNativeToken,
shouldRefresh,
walletIsNotConnected,
makerTokenSymbol
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import styled from "styled-components/macro";

export const Container = styled.div`
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 0.5rem;
text-align: center;
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { FC } from "react";
import { useTranslation } from "react-i18next";

import {
StyledInfoHeading,
StyledInfoSubHeading,
} from "../../../../Typography/Typography.styles";
import { Container } from "./InfoSection.styles";

type ActionButtonsProps = {
isAllowancesFailed: boolean;
isNetworkUnsupported: boolean;
className?: string;
};

const InfoSection: FC<ActionButtonsProps> = ({
isAllowancesFailed,
isNetworkUnsupported,
className,
}) => {
const { t } = useTranslation();

if (isNetworkUnsupported) {
return (
<Container className={className}>
<StyledInfoHeading>{t("wallet.unsupportedNetwork")}</StyledInfoHeading>
</Container>
);
}

if (isAllowancesFailed) {
return (
<Container className={className}>
<StyledInfoHeading>
{t("balances.failedToFetchAllowances")}
</StyledInfoHeading>
<StyledInfoSubHeading>
{t("balances.failedToFetchAllowancesCta")}
</StyledInfoSubHeading>
</Container>
);
}

return null;
};

export default InfoSection;
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from "../../../features/takeOtc/takeOtcSlice";
import { compareAddresses } from "../../../helpers/string";
import useAllowance from "../../../hooks/useAllowance";
import useAllowancesOrBalancesFailed from "../../../hooks/useAllowancesOrBalancesFailed";
import useApprovalPending from "../../../hooks/useApprovalPending";
import useDepositPending from "../../../hooks/useDepositPending";
import useInsufficientBalance from "../../../hooks/useInsufficientBalance";
Expand Down Expand Up @@ -86,11 +87,13 @@ const OrderDetailWidget: FC<OrderDetailWidgetProps> = ({ order }) => {
const params = useParams<{ compressedOrder: string }>();
const { setShowWalletList, setTransactionsTabIsOpen } =
useContext(InterfaceContext);

const ordersStatus = useAppSelector(selectOrdersStatus);
const ordersErrors = useAppSelector(selectOrdersErrors);
const takeOtcErrors = useAppSelector(selectTakeOtcErrors);
const { userOrders } = useAppSelector(selectMyOrdersReducer);
const { indexerUrls } = useAppSelector(selectIndexerReducer);

const errors = [...ordersErrors, ...takeOtcErrors];

const [state, setState] = useState<OrderDetailWidgetState>(
Expand Down Expand Up @@ -135,6 +138,7 @@ const OrderDetailWidget: FC<OrderDetailWidgetProps> = ({ order }) => {
senderToken?.address,
senderAmount
);
const isAllowancesOrBalancesFailed = useAllowancesOrBalancesFailed();
const shouldDepositNativeToken = !!shouldDepositNativeTokenAmount;
const hasDepositPending = useDepositPending();
const orderTransactionLink = useOrderTransactionLink(order.nonce);
Expand Down Expand Up @@ -380,6 +384,7 @@ const OrderDetailWidget: FC<OrderDetailWidgetProps> = ({ order }) => {
onFeeButtonClick={toggleShowFeeInfo}
/>
<StyledInfoSection
isAllowancesFailed={isAllowancesOrBalancesFailed}
isExpired={orderStatus === OrderStatus.expired}
isDifferentChainId={walletChainIdIsDifferentThanOrderChainId}
isIntendedRecipient={userIsIntendedRecipient}
Expand All @@ -397,7 +402,7 @@ const OrderDetailWidget: FC<OrderDetailWidgetProps> = ({ order }) => {
isIntendedRecipient={userIsIntendedRecipient}
isMakerOfSwap={userIsMakerOfSwap}
isNotConnected={!isActive}
isNetworkUnsupported={false}
requiresReload={isAllowancesOrBalancesFailed}
shouldDepositNativeToken={shouldDepositNativeToken}
senderTokenSymbol={senderTokenSymbol}
onBackButtonClick={handleBackButtonClick}
Expand Down
27 changes: 23 additions & 4 deletions src/components/@widgets/OrderDetailWidget/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,47 @@ import { chainNames } from "@airswap/utils";

import i18n from "i18next";

interface FullOrderErc20WarningTranslation {
heading: string;
subHeading?: string;
}

export const getFullOrderERC20WarningTranslation = (
isAllowancesFailed: boolean,
isDifferentChainId: boolean,
isExpired: boolean,
isIntendedRecipient: boolean,
isMakerOfSwap: boolean,
isNotConnected: boolean,
orderChainId: number
): string | undefined => {
): FullOrderErc20WarningTranslation | undefined => {
const orderIsNotForConnectedWallet =
!isIntendedRecipient && !isMakerOfSwap && !isNotConnected;

if (isAllowancesFailed) {
return {
heading: i18n.t("balances.failedToFetchAllowances"),
subHeading: i18n.t("balances.failedToFetchAllowancesCta"),
};
}

if (isDifferentChainId) {
const chainName = chainNames[orderChainId] || i18n.t("common.unknown");
return i18n.t("orders.thisOrderIsForAnotherChain", { chainName });
return {
heading: i18n.t("orders.thisOrderIsForAnotherChain", { chainName }),
};
}

if (orderIsNotForConnectedWallet && isExpired) {
return i18n.t("orders.thisOrderWasForADifferentTaker");
return {
heading: i18n.t("orders.thisOrderWasForADifferentTaker"),
};
}

if (orderIsNotForConnectedWallet && !isExpired) {
return i18n.t("orders.thisOrderIsForADifferentTaker");
return {
heading: i18n.t("orders.thisOrderIsForADifferentTaker"),
};
}

return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type ActionButtonsProps = {
isDifferentChainId: boolean;
isIntendedRecipient: boolean;
isMakerOfSwap: boolean;
isNetworkUnsupported: boolean;
isNotConnected: boolean;
requiresReload: boolean;
shouldDepositNativeToken: boolean;
senderTokenSymbol?: string;
onBackButtonClick: () => void;
Expand All @@ -47,7 +47,7 @@ const ActionButtons: FC<ActionButtonsProps> = ({
isIntendedRecipient,
isMakerOfSwap,
isNotConnected,
isNetworkUnsupported,
requiresReload,
shouldDepositNativeToken,
senderTokenSymbol,
onBackButtonClick,
Expand All @@ -70,7 +70,7 @@ const ActionButtons: FC<ActionButtonsProps> = ({
);
}

if (isNetworkUnsupported || isDifferentChainId) {
if (isDifferentChainId) {
return (
<Container className={className}>
<SignButton
Expand All @@ -84,6 +84,19 @@ const ActionButtons: FC<ActionButtonsProps> = ({
);
}

if (requiresReload) {
return (
<Container className={className}>
<SignButton
intent="primary"
onClick={() => onActionButtonClick(ButtonActions.reloadPage)}
>
{t("common.reloadPage")}
</SignButton>
</Container>
);
}

if (isExpired || isTaken || isCanceled) {
return (
<Container className={className}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { useTranslation } from "react-i18next";

import useShouldDepositNativeTokenAmountInfo from "../../../../../hooks/useShouldDepositNativeTokenAmountInfo";
import { InfoSubHeading } from "../../../../Typography/Typography";
import {
StyledInfoHeading,
StyledInfoSubHeading,
} from "../../../../Typography/Typography.styles";
import { getFullOrderERC20WarningTranslation } from "../../helpers";
import { Container, Strong } from "./InfoSection.styles";

type ActionButtonsProps = {
isAllowancesFailed: boolean;
isDifferentChainId: boolean;
isExpired: boolean;
isIntendedRecipient: boolean;
Expand All @@ -17,6 +22,7 @@ type ActionButtonsProps = {
};

const InfoSection: FC<ActionButtonsProps> = ({
isAllowancesFailed,
isDifferentChainId,
isExpired,
isIntendedRecipient,
Expand All @@ -27,6 +33,7 @@ const InfoSection: FC<ActionButtonsProps> = ({
}) => {
const warningText = useMemo(() => {
return getFullOrderERC20WarningTranslation(
isAllowancesFailed,
isDifferentChainId,
isExpired,
isIntendedRecipient,
Expand All @@ -46,7 +53,11 @@ const InfoSection: FC<ActionButtonsProps> = ({
if (warningText) {
return (
<Container className={className}>
<InfoSubHeading>{warningText}</InfoSubHeading>
<StyledInfoHeading>{warningText.heading}</StyledInfoHeading>

{warningText.subHeading && (
<StyledInfoSubHeading>{warningText.subHeading}</StyledInfoSubHeading>
)}
</Container>
);
}
Expand Down
Loading
Loading