@@ -24,6 +24,7 @@ import {
24
24
useRouteVerbose ,
25
25
useMarket ,
26
26
FEE_MULTIPLIER ,
27
+ useFairRoute ,
27
28
} from "../context/Dex" ;
28
29
import { useTokenMap } from "../context/TokenList" ;
29
30
import { useMint , useOwnedTokenAccount } from "../context/Token" ;
@@ -54,6 +55,10 @@ const useStyles = makeStyles((theme) => ({
54
55
fontSize : 16 ,
55
56
fontWeight : 700 ,
56
57
padding : theme . spacing ( 1.5 ) ,
58
+ "&:disabled" : {
59
+ cursor : "not-allowed" ,
60
+ pointerEvents : "all !important" ,
61
+ } ,
57
62
} ,
58
63
swapToFromButton : {
59
64
display : "block" ,
@@ -324,6 +329,7 @@ export function SwapButton() {
324
329
fromMint,
325
330
toMint,
326
331
fromAmount,
332
+ toAmount,
327
333
slippage,
328
334
isClosingNewAccounts,
329
335
isStrict,
@@ -341,14 +347,48 @@ export function SwapButton() {
341
347
) ;
342
348
const canSwap = useCanSwap ( ) ;
343
349
const referral = useReferral ( fromMarket ) ;
344
- const fair = useSwapFair ( ) ;
350
+ const fair = useSwapFair ( ) ?? 0 ;
345
351
let fromWallet = useOwnedTokenAccount ( fromMint ) ;
346
352
let toWallet = useOwnedTokenAccount ( toMint ) ;
347
- const quoteMint = fromMarket && fromMarket . quoteMintAddress ;
353
+ const quoteMint = ( fromMarket && fromMarket . quoteMintAddress ) as PublicKey ;
348
354
const quoteMintInfo = useMint ( quoteMint ) ;
349
355
const quoteWallet = useOwnedTokenAccount ( quoteMint ) ;
350
356
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
+
352
392
const sendSwapTransaction = async ( ) => {
353
393
if ( ! fromMintInfo || ! toMintInfo ) {
354
394
throw new Error ( "Unable to calculate mint decimals" ) ;
@@ -438,17 +478,16 @@ export function SwapButton() {
438
478
txs [ 0 ] . signers . push ( ...wrapSigners ) ;
439
479
txs [ 0 ] . signers . push ( ...unwrapSigners ) ;
440
480
}
441
-
442
481
await swapClient . program . provider . sendAll ( txs ) ;
443
482
} ;
444
483
return (
445
484
< Button
446
485
variant = "contained"
447
486
className = { styles . swapButton }
448
487
onClick = { sendSwapTransaction }
449
- disabled = { ! canSwap }
488
+ disabled = { ! canSwap && ! minAmtCondt }
450
489
>
451
- Swap
490
+ { ( minAmtCondt && " Swap" ) || "Min Amount Required" }
452
491
</ Button >
453
492
) ;
454
493
}
0 commit comments