Skip to content

Commit

Permalink
disable swap if min amt not met
Browse files Browse the repository at this point in the history
  • Loading branch information
munanadi committed Sep 28, 2021
1 parent 3f213aa commit db04176
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions src/components/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
useRouteVerbose,
useMarket,
FEE_MULTIPLIER,
useFairRoute,
} from "../context/Dex";
import { useTokenMap } from "../context/TokenList";
import { useMint, useOwnedTokenAccount } from "../context/Token";
Expand Down Expand Up @@ -54,6 +55,10 @@ const useStyles = makeStyles((theme) => ({
fontSize: 16,
fontWeight: 700,
padding: theme.spacing(1.5),
"&:disabled": {
cursor: "not-allowed",
pointerEvents: "all !important",
},
},
swapToFromButton: {
display: "block",
Expand Down Expand Up @@ -324,6 +329,7 @@ export function SwapButton() {
fromMint,
toMint,
fromAmount,
toAmount,
slippage,
isClosingNewAccounts,
isStrict,
Expand All @@ -341,14 +347,48 @@ export function SwapButton() {
);
const canSwap = useCanSwap();
const referral = useReferral(fromMarket);
const fair = useSwapFair();
const fair = useSwapFair() ?? 0;
let fromWallet = useOwnedTokenAccount(fromMint);
let toWallet = useOwnedTokenAccount(toMint);
const quoteMint = fromMarket && fromMarket.quoteMintAddress;
const quoteMint = (fromMarket && fromMarket.quoteMintAddress) as PublicKey;
const quoteMintInfo = useMint(quoteMint);
const quoteWallet = useOwnedTokenAccount(quoteMint);

// Click handler.
let minimumAmt = fromMarket?.minOrderSize ?? 0;
let baseMint = fromMarket?.decoded.baseMint ?? null;

let isMultiRoute: boolean = (route?.markets?.length ?? 0) > 1;

let minAmtCondt = false;

let fromMinOrder = fromMarket?.minOrderSize ?? 0;
let toMinOrder = toMarket?.minOrderSize ?? 0;
const quoteExchangeRate = useFairRoute(fromMint, quoteMint) ?? 0;
const toExchangeRate = useFairRoute(toMint, quoteMint) ?? 0;

if (isMultiRoute) {
if (quoteExchangeRate > 1 && toExchangeRate > 1) {
const toEffectiveAmount = fromAmount * (1 / fair);
minAmtCondt =
fromAmount >= fromMinOrder && toEffectiveAmount >= toMinOrder;
} else {
const quoteEffectiveAmount = fromAmount * (1 / quoteExchangeRate);
minAmtCondt =
fromAmount >= fromMinOrder &&
quoteEffectiveAmount * toExchangeRate >= toMinOrder;
}
} else {
if (baseMint?.toString() === fromMint.toString()) {
minAmtCondt = fromAmount >= minimumAmt;
} else {
if (fair < 1) {
minAmtCondt = fromAmount * (1 / fair) >= minimumAmt;
} else {
minAmtCondt = fromAmount >= minimumAmt * fair;
}
}
}

const sendSwapTransaction = async () => {
if (!fromMintInfo || !toMintInfo) {
throw new Error("Unable to calculate mint decimals");
Expand Down Expand Up @@ -438,17 +478,16 @@ export function SwapButton() {
txs[0].signers.push(...wrapSigners);
txs[0].signers.push(...unwrapSigners);
}

await swapClient.program.provider.sendAll(txs);
};
return (
<Button
variant="contained"
className={styles.swapButton}
onClick={sendSwapTransaction}
disabled={!canSwap}
disabled={!canSwap && !minAmtCondt}
>
Swap
{(minAmtCondt && "Swap") || "Min Amount Required"}
</Button>
);
}
Expand Down

0 comments on commit db04176

Please sign in to comment.