Gracefully handle non existant trade routes

This commit is contained in:
armaniferrante 2021-05-15 19:54:25 -07:00
parent 79c87882cd
commit 4a25be0fb3
No known key found for this signature in database
GPG Key ID: 58BEF301E91F7828
6 changed files with 32 additions and 16 deletions

View File

@ -10,7 +10,7 @@
"@project-serum/anchor": "^0.5.1-beta.2",
"@project-serum/serum": "^0.13.34",
"@project-serum/sol-wallet-adapter": "^0.2.0",
"@project-serum/swap": "^0.1.0-alpha.4",
"@project-serum/swap": "^0.1.0-alpha.5",
"@solana/spl-token": "^0.1.4",
"@solana/spl-token-registry": "^0.2.86",
"@solana/web3.js": "^1.10.1",

View File

@ -108,6 +108,7 @@ function InfoDetails() {
{ ticker: fromMintTicker, mint: fromMint },
{ ticker: toMintTicker, mint: toMint },
];
const route = swapClient.route(fromMint, toMint);
return (
<div style={{ padding: "15px", width: "250px" }}>
<div>
@ -117,9 +118,13 @@ function InfoDetails() {
>
Trade Route
</Typography>
{swapClient.route(fromMint, toMint).map((market: PublicKey) => {
return <MarketRoute key={market.toString()} market={market} />;
})}
{route ? (
route.map((market: PublicKey) => {
return <MarketRoute key={market.toString()} market={market} />;
})
) : (
<Typography color="textSecondary">Route not found</Typography>
)}
</div>
<div style={{ marginTop: "15px" }}>
<Typography

View File

@ -270,8 +270,8 @@ function SwapButton() {
const toMintInfo = useMint(toMint);
const openOrders = useOpenOrders();
const route = useRoute(fromMint, toMint);
const fromMarket = useMarket(route[0]);
const toMarket = useMarket(route[1]);
const fromMarket = useMarket(route ? route[0] : undefined);
const toMarket = useMarket(route ? route[1] : undefined);
const sendSwapTransaction = async () => {
if (!fromMintInfo || !toMintInfo) {
@ -307,7 +307,9 @@ function SwapButton() {
variant="contained"
className={styles.swapButton}
onClick={sendSwapTransaction}
disabled={swapClient.program.provider.wallet.publicKey === null}
disabled={
swapClient.program.provider.wallet.publicKey === null || route === null
}
>
Swap
</Button>

View File

@ -40,7 +40,12 @@ export default function TokenDialog({
const styles = useStyles();
const { swapClient } = useDexContext();
const tokens = useMemo(() => {
return swapClient.tokens().concat([USDC_MINT, USDT_MINT]);
return swapClient
.tokens()
.concat([USDC_MINT, USDT_MINT])
.filter(
(t) => t.toString() !== "So11111111111111111111111111111111111111112"
);
}, [swapClient]);
return (
<Dialog

View File

@ -228,9 +228,13 @@ export function useFairRoute(
toMint: PublicKey
): number | undefined {
const route = useRoute(fromMint, toMint);
const fromFair = useFair(route[0]);
const fromMarket = useMarket(route[0]);
const toFair = useFair(route[1]);
const fromFair = useFair(route ? route[0] : undefined);
const fromMarket = useMarket(route ? route[0] : undefined);
const toFair = useFair(route ? route[1] : undefined);
if (route === null) {
return undefined;
}
if (route.length === 1 && fromFair !== undefined) {
if (fromMarket === undefined) {
@ -251,7 +255,7 @@ export function useFairRoute(
export function useRoute(
fromMint: PublicKey,
toMint: PublicKey
): Array<PublicKey> {
): Array<PublicKey> | null {
const { swapClient } = useDexContext();
return useMemo(
() => swapClient.route(fromMint, toMint),

View File

@ -1588,10 +1588,10 @@
bs58 "^4.0.1"
eventemitter3 "^4.0.4"
"@project-serum/swap@^0.1.0-alpha.4":
version "0.1.0-alpha.4"
resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.1.0-alpha.4.tgz#f5035218173d7f8ccb0d53c637c10f50b4734c3f"
integrity sha512-5O6rJjLmaZTiPJUZq5iHBV1c3LQ11ZBv4wD1ol5rIPxDkHlmxqfH23XQVkC3aEJXIry5ffRf7hL/e1VgSW5C3A==
"@project-serum/swap@^0.1.0-alpha.5":
version "0.1.0-alpha.5"
resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.1.0-alpha.5.tgz#106fdf5354c3c17f1832ab623122739fd45e2e52"
integrity sha512-ZJW9XNlZyhIq/C8pwKhFvf7duKth8dfu7vkgVtfUp281dBgSXx6IwrigpYcJ9x/VZMr9LMrkUVW1LiXd8XZdEQ==
dependencies:
"@project-serum/anchor" "^0.5.1-beta.2"
"@project-serum/serum" "^0.13.34"