Skip to content

Commit

Permalink
hide swap button for token if it can't be swapped (#3657)
Browse files Browse the repository at this point in the history
if there are no Jupiter routes for the token, or if there is a zero balance of the token, then hide the swap button on the token info page
  • Loading branch information
wentokay authored Apr 10, 2023
1 parent a87a7d9 commit 4f47ec5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
STRIPE_ENABLED,
} from "@coral-xyz/common";
import { Dollar } from "@coral-xyz/react-common";
import { SwapProvider, useFeatureGates } from "@coral-xyz/recoil";
import {
SwapProvider,
useFeatureGates,
useSwapContext,
} from "@coral-xyz/recoil";
import { useCustomTheme } from "@coral-xyz/themes";
import { ArrowDownward, ArrowUpward, SwapHoriz } from "@mui/icons-material";
import { Typography } from "@mui/material";
Expand All @@ -18,7 +22,6 @@ import { Swap, SwapSelectToken } from "../../Unlocked/Swap";

import {
AddressSelectorLoader,
NftAddressSelector,
TokenAddressSelector,
} from "./TokensWidget/AddressSelector";
import { Deposit } from "./TokensWidget/Deposit";
Expand Down Expand Up @@ -101,27 +104,39 @@ function SwapButton({
/>
);

// Wrap in Suspense so it doesn't block if Jupiter is slow or down.
const SwapButtonIfTheTokenIsSwappable = () => {
// This component loads inside Suspense, so it should not block
// rendering as we wait for Jupiter Routes to be downloaded and parsed
const { canSwap } = useSwapContext();
return canSwap ? (
<SwapButtonComponent
routes={[
{
name: "swap",
component: (props: any) => <Swap {...props} />,
title: `Swap`,
props: {
blockchain,
},
},
{
title: `Select Token`,
name: "select-token",
component: (props: any) => <SwapSelectToken {...props} />,
},
]}
/>
) : // There are no Jupiter Routes for this token, so hide the button
null;
};

// This displays a semi-transparent Swap button while Jupiter routes are
// first downloaded and parsed. After that it will either make the button
// fully opaque and clickable or hide it if the token isn't supported
return (
<React.Suspense fallback={<SwapButtonComponent />}>
<SwapProvider tokenAddress={address}>
<SwapButtonComponent
routes={[
{
name: "swap",
component: (props: any) => <Swap {...props} />,
title: `Swap`,
props: {
blockchain,
},
},
{
title: `Select Token`,
name: "select-token",
component: (props: any) => <SwapSelectToken {...props} />,
},
]}
/>
<SwapButtonIfTheTokenIsSwappable />
</SwapProvider>
</React.Suspense>
);
Expand Down
4 changes: 3 additions & 1 deletion packages/recoil/src/context/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getAssociatedTokenAddress } from "@solana/spl-token";
import type { TokenInfo } from "@solana/spl-token-registry";
import { PublicKey, Transaction } from "@solana/web3.js";
import * as bs58 from "bs58";
import { BigNumber, ethers, FixedNumber } from "ethers";
import { BigNumber, ethers,FixedNumber } from "ethers";

import { blockchainTokenData } from "../atoms/balance";
import { jupiterInputTokens } from "../atoms/solana/jupiter";
Expand Down Expand Up @@ -99,6 +99,7 @@ export type SwapContext = {
isLoadingRoutes: boolean;
isLoadingTransactions: boolean;
isJupiterError: boolean;
canSwap: boolean;
};

const _SwapContext = React.createContext<SwapContext | null>(null);
Expand Down Expand Up @@ -568,6 +569,7 @@ export function SwapProvider({
availableForSwap,
exceedsBalance,
feeExceedsBalance,
canSwap: !availableForSwap.isZero(),
}}
>
{children}
Expand Down

1 comment on commit 4f47ec5

@vercel
Copy link

@vercel vercel bot commented on 4f47ec5 Apr 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.