Skip to content

Commit db04176

Browse files
committed
disable swap if min amt not met
1 parent 3f213aa commit db04176

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

src/components/Swap.tsx

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
useRouteVerbose,
2525
useMarket,
2626
FEE_MULTIPLIER,
27+
useFairRoute,
2728
} from "../context/Dex";
2829
import { useTokenMap } from "../context/TokenList";
2930
import { useMint, useOwnedTokenAccount } from "../context/Token";
@@ -54,6 +55,10 @@ const useStyles = makeStyles((theme) => ({
5455
fontSize: 16,
5556
fontWeight: 700,
5657
padding: theme.spacing(1.5),
58+
"&:disabled": {
59+
cursor: "not-allowed",
60+
pointerEvents: "all !important",
61+
},
5762
},
5863
swapToFromButton: {
5964
display: "block",
@@ -324,6 +329,7 @@ export function SwapButton() {
324329
fromMint,
325330
toMint,
326331
fromAmount,
332+
toAmount,
327333
slippage,
328334
isClosingNewAccounts,
329335
isStrict,
@@ -341,14 +347,48 @@ export function SwapButton() {
341347
);
342348
const canSwap = useCanSwap();
343349
const referral = useReferral(fromMarket);
344-
const fair = useSwapFair();
350+
const fair = useSwapFair() ?? 0;
345351
let fromWallet = useOwnedTokenAccount(fromMint);
346352
let toWallet = useOwnedTokenAccount(toMint);
347-
const quoteMint = fromMarket && fromMarket.quoteMintAddress;
353+
const quoteMint = (fromMarket && fromMarket.quoteMintAddress) as PublicKey;
348354
const quoteMintInfo = useMint(quoteMint);
349355
const quoteWallet = useOwnedTokenAccount(quoteMint);
350356

351-
// Click handler.
357+
let minimumAmt = fromMarket?.minOrderSize ?? 0;
358+
let baseMint = fromMarket?.decoded.baseMint ?? null;
359+
360+
let isMultiRoute: boolean = (route?.markets?.length ?? 0) > 1;
361+
362+
let minAmtCondt = false;
363+
364+
let fromMinOrder = fromMarket?.minOrderSize ?? 0;
365+
let toMinOrder = toMarket?.minOrderSize ?? 0;
366+
const quoteExchangeRate = useFairRoute(fromMint, quoteMint) ?? 0;
367+
const toExchangeRate = useFairRoute(toMint, quoteMint) ?? 0;
368+
369+
if (isMultiRoute) {
370+
if (quoteExchangeRate > 1 && toExchangeRate > 1) {
371+
const toEffectiveAmount = fromAmount * (1 / fair);
372+
minAmtCondt =
373+
fromAmount >= fromMinOrder && toEffectiveAmount >= toMinOrder;
374+
} else {
375+
const quoteEffectiveAmount = fromAmount * (1 / quoteExchangeRate);
376+
minAmtCondt =
377+
fromAmount >= fromMinOrder &&
378+
quoteEffectiveAmount * toExchangeRate >= toMinOrder;
379+
}
380+
} else {
381+
if (baseMint?.toString() === fromMint.toString()) {
382+
minAmtCondt = fromAmount >= minimumAmt;
383+
} else {
384+
if (fair < 1) {
385+
minAmtCondt = fromAmount * (1 / fair) >= minimumAmt;
386+
} else {
387+
minAmtCondt = fromAmount >= minimumAmt * fair;
388+
}
389+
}
390+
}
391+
352392
const sendSwapTransaction = async () => {
353393
if (!fromMintInfo || !toMintInfo) {
354394
throw new Error("Unable to calculate mint decimals");
@@ -438,17 +478,16 @@ export function SwapButton() {
438478
txs[0].signers.push(...wrapSigners);
439479
txs[0].signers.push(...unwrapSigners);
440480
}
441-
442481
await swapClient.program.provider.sendAll(txs);
443482
};
444483
return (
445484
<Button
446485
variant="contained"
447486
className={styles.swapButton}
448487
onClick={sendSwapTransaction}
449-
disabled={!canSwap}
488+
disabled={!canSwap && !minAmtCondt}
450489
>
451-
Swap
490+
{(minAmtCondt && "Swap") || "Min Amount Required"}
452491
</Button>
453492
);
454493
}

0 commit comments

Comments
 (0)