2023-06-16 05:23:50 -07:00
|
|
|
import {
|
|
|
|
useState,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
Dispatch,
|
|
|
|
SetStateAction,
|
|
|
|
} from 'react'
|
2023-09-18 15:44:21 -07:00
|
|
|
import { ArrowDownIcon } from '@heroicons/react/20/solid'
|
2023-07-25 19:48:13 -07:00
|
|
|
import { NumberFormatValues, SourceInfo } from 'react-number-format'
|
2023-06-16 05:23:50 -07:00
|
|
|
import Decimal from 'decimal.js'
|
|
|
|
import mangoStore from '@store/mangoStore'
|
2023-10-26 21:30:17 -07:00
|
|
|
import { SIZE_INPUT_UI_KEY } from '../../utils/constants'
|
2023-06-16 05:23:50 -07:00
|
|
|
import { useWallet } from '@solana/wallet-adapter-react'
|
2023-10-16 21:12:46 -07:00
|
|
|
import { JupiterV6RouteInfo } from 'types/jupiter'
|
2023-06-16 05:23:50 -07:00
|
|
|
import useLocalStorageState from 'hooks/useLocalStorageState'
|
|
|
|
import SwapSlider from './SwapSlider'
|
|
|
|
import PercentageSelectButtons from './PercentageSelectButtons'
|
2023-07-25 19:48:13 -07:00
|
|
|
import BuyTokenInput from './BuyTokenInput'
|
|
|
|
import SellTokenInput from './SellTokenInput'
|
2023-08-03 19:06:09 -07:00
|
|
|
import Button from '@components/shared/Button'
|
|
|
|
import SwapReviewRouteInfo from './SwapReviewRouteInfo'
|
|
|
|
import useIpAddress from 'hooks/useIpAddress'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import useQuoteRoutes from './useQuoteRoutes'
|
|
|
|
import { useTokenMax } from './useTokenMax'
|
|
|
|
import Loading from '@components/shared/Loading'
|
|
|
|
import InlineNotification from '@components/shared/InlineNotification'
|
2023-08-10 17:52:42 -07:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
2023-08-15 22:27:45 -07:00
|
|
|
import Link from 'next/link'
|
2023-08-17 15:25:46 -07:00
|
|
|
import SecondaryConnectButton from '@components/shared/SecondaryConnectButton'
|
2023-08-27 22:59:56 -07:00
|
|
|
import useRemainingBorrowsInPeriod from 'hooks/useRemainingBorrowsInPeriod'
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
import relativeTime from 'dayjs/plugin/relativeTime'
|
2023-08-28 04:59:36 -07:00
|
|
|
import { formatCurrencyValue } from 'utils/numbers'
|
2023-09-06 06:06:57 -07:00
|
|
|
import { SwapFormTokenListType } from './SwapFormTokenList'
|
2023-09-08 02:39:56 -07:00
|
|
|
import useTokenPositionsFull from 'hooks/useTokenPositionsFull'
|
2023-10-20 08:11:19 -07:00
|
|
|
import TopBarStore from '@store/topBarStore'
|
2023-08-27 22:59:56 -07:00
|
|
|
|
|
|
|
dayjs.extend(relativeTime)
|
2023-06-16 05:23:50 -07:00
|
|
|
|
|
|
|
type MarketSwapFormProps = {
|
2023-09-06 06:06:57 -07:00
|
|
|
setShowTokenSelect: Dispatch<SetStateAction<SwapFormTokenListType>>
|
2023-10-12 14:38:44 -07:00
|
|
|
onSuccess?: () => void
|
2023-06-16 05:23:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const MAX_DIGITS = 11
|
|
|
|
export const withValueLimit = (values: NumberFormatValues): boolean => {
|
|
|
|
return values.floatValue
|
|
|
|
? values.floatValue.toFixed(0).length <= MAX_DIGITS
|
|
|
|
: true
|
|
|
|
}
|
|
|
|
|
|
|
|
export const NUMBER_FORMAT_CLASSNAMES =
|
|
|
|
'w-full rounded-r-lg h-[56px] box-border pb-4 border-l border-th-bkg-2 bg-th-input-bkg px-3 text-right font-mono text-xl text-th-fgd-1 focus:outline-none md:hover:bg-th-bkg-1'
|
|
|
|
|
|
|
|
const set = mangoStore.getState().set
|
|
|
|
|
2023-10-12 14:38:44 -07:00
|
|
|
const MarketSwapForm = ({
|
|
|
|
setShowTokenSelect,
|
|
|
|
onSuccess,
|
|
|
|
}: MarketSwapFormProps) => {
|
2023-08-03 19:06:09 -07:00
|
|
|
const { t } = useTranslation(['common', 'swap', 'trade'])
|
|
|
|
//initial state is undefined null is returned on error
|
2023-10-16 21:12:46 -07:00
|
|
|
const [selectedRoute, setSelectedRoute] =
|
|
|
|
useState<JupiterV6RouteInfo | null>()
|
2023-06-16 05:23:50 -07:00
|
|
|
const [animateSwitchArrow, setAnimateSwitchArrow] = useState(0)
|
2023-08-03 19:06:09 -07:00
|
|
|
const [showConfirm, setShowConfirm] = useState(false)
|
2023-06-16 05:23:50 -07:00
|
|
|
const [swapFormSizeUi] = useLocalStorageState(SIZE_INPUT_UI_KEY, 'slider')
|
|
|
|
const {
|
|
|
|
margin: useMargin,
|
2023-08-03 19:06:09 -07:00
|
|
|
slippage,
|
2023-06-16 05:23:50 -07:00
|
|
|
inputBank,
|
|
|
|
outputBank,
|
|
|
|
amountIn: amountInFormValue,
|
|
|
|
amountOut: amountOutFormValue,
|
|
|
|
swapMode,
|
|
|
|
} = mangoStore((s) => s.swap)
|
2023-10-26 21:30:17 -07:00
|
|
|
const [isDraggingSlider, setIsDraggingSlider] = useState(false)
|
2023-08-03 19:06:09 -07:00
|
|
|
const { connected, publicKey } = useWallet()
|
2023-10-24 11:51:18 -07:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
2023-10-26 21:30:17 -07:00
|
|
|
const quoteAmount =
|
|
|
|
swapMode === 'ExactIn' ? amountInFormValue : amountOutFormValue
|
2023-10-28 04:49:46 -07:00
|
|
|
const {
|
|
|
|
bestRoute,
|
|
|
|
isFetching: fetchingRoute,
|
|
|
|
refetch: refetchRoute,
|
|
|
|
} = useQuoteRoutes({
|
2023-10-26 21:30:17 -07:00
|
|
|
inputMint: inputBank?.mint.toString(),
|
|
|
|
outputMint: outputBank?.mint.toString(),
|
|
|
|
amount: quoteAmount,
|
2023-08-03 19:06:09 -07:00
|
|
|
slippage,
|
|
|
|
swapMode,
|
|
|
|
wallet: publicKey?.toBase58(),
|
2023-10-24 11:51:18 -07:00
|
|
|
mangoAccount,
|
2023-10-26 21:30:17 -07:00
|
|
|
enabled: () =>
|
|
|
|
!!(
|
|
|
|
inputBank?.mint &&
|
|
|
|
outputBank?.mint &&
|
|
|
|
quoteAmount &&
|
|
|
|
!isDraggingSlider
|
|
|
|
),
|
2023-08-03 19:06:09 -07:00
|
|
|
})
|
|
|
|
const { ipAllowed, ipCountry } = useIpAddress()
|
2023-06-16 05:23:50 -07:00
|
|
|
|
|
|
|
const amountInAsDecimal: Decimal | null = useMemo(() => {
|
2023-10-26 21:30:17 -07:00
|
|
|
return Number(amountInFormValue)
|
|
|
|
? new Decimal(amountInFormValue)
|
2023-06-16 05:23:50 -07:00
|
|
|
: new Decimal(0)
|
2023-10-26 21:30:17 -07:00
|
|
|
}, [amountInFormValue])
|
2023-06-16 05:23:50 -07:00
|
|
|
|
|
|
|
const amountOutAsDecimal: Decimal | null = useMemo(() => {
|
2023-10-26 21:30:17 -07:00
|
|
|
return Number(amountOutFormValue)
|
|
|
|
? new Decimal(amountOutFormValue)
|
2023-06-16 05:23:50 -07:00
|
|
|
: new Decimal(0)
|
2023-10-26 21:30:17 -07:00
|
|
|
}, [amountOutFormValue])
|
2023-06-16 05:23:50 -07:00
|
|
|
|
|
|
|
const setAmountInFormValue = useCallback(
|
|
|
|
(amountIn: string, setSwapMode?: boolean) => {
|
|
|
|
set((s) => {
|
|
|
|
s.swap.amountIn = amountIn
|
|
|
|
if (!parseFloat(amountIn)) {
|
|
|
|
s.swap.amountOut = ''
|
|
|
|
}
|
|
|
|
if (setSwapMode) {
|
|
|
|
s.swap.swapMode = 'ExactIn'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2023-07-25 19:48:13 -07:00
|
|
|
[],
|
2023-06-16 05:23:50 -07:00
|
|
|
)
|
|
|
|
|
2023-08-01 05:21:19 -07:00
|
|
|
const setAmountOutFormValue = useCallback(
|
|
|
|
(amountOut: string, setSwapMode?: boolean) => {
|
|
|
|
set((s) => {
|
|
|
|
s.swap.amountOut = amountOut
|
|
|
|
if (!parseFloat(amountOut)) {
|
|
|
|
s.swap.amountIn = ''
|
|
|
|
}
|
|
|
|
if (setSwapMode) {
|
|
|
|
s.swap.swapMode = 'ExactOut'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
)
|
2023-06-16 05:23:50 -07:00
|
|
|
|
2023-07-25 19:48:13 -07:00
|
|
|
const handleAmountInChange = useCallback(
|
|
|
|
(e: NumberFormatValues, info: SourceInfo) => {
|
|
|
|
if (info.source !== 'event') return
|
|
|
|
setAmountInFormValue(e.value)
|
2023-10-26 21:30:17 -07:00
|
|
|
set((s) => {
|
|
|
|
s.swap.amountOut = ''
|
|
|
|
})
|
2023-07-25 19:48:13 -07:00
|
|
|
if (swapMode === 'ExactOut') {
|
|
|
|
set((s) => {
|
|
|
|
s.swap.swapMode = 'ExactIn'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[outputBank, setAmountInFormValue, swapMode],
|
|
|
|
)
|
|
|
|
|
|
|
|
const handleAmountOutChange = useCallback(
|
|
|
|
(e: NumberFormatValues, info: SourceInfo) => {
|
|
|
|
if (info.source !== 'event') return
|
2023-10-26 21:30:17 -07:00
|
|
|
setAmountOutFormValue(e.value)
|
|
|
|
set((s) => {
|
|
|
|
s.swap.amountIn = ''
|
|
|
|
})
|
2023-07-25 19:48:13 -07:00
|
|
|
if (swapMode === 'ExactIn') {
|
|
|
|
set((s) => {
|
|
|
|
s.swap.swapMode = 'ExactOut'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[swapMode, setAmountOutFormValue],
|
|
|
|
)
|
|
|
|
|
2023-10-26 21:30:17 -07:00
|
|
|
const handleSliderDrag = useCallback(() => {
|
|
|
|
if (!isDraggingSlider) {
|
|
|
|
setIsDraggingSlider(true)
|
|
|
|
}
|
|
|
|
}, [isDraggingSlider])
|
|
|
|
|
|
|
|
const handleSliderDragEnd = useCallback(() => {
|
|
|
|
if (isDraggingSlider) {
|
|
|
|
setIsDraggingSlider(false)
|
|
|
|
}
|
|
|
|
}, [isDraggingSlider])
|
|
|
|
|
|
|
|
const handleSliderChange = useCallback(
|
|
|
|
(amountIn: string) => {
|
|
|
|
setAmountInFormValue(amountIn, true)
|
|
|
|
set((s) => {
|
|
|
|
s.swap.amountOut = ''
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[setAmountInFormValue],
|
|
|
|
)
|
|
|
|
|
2023-08-01 05:21:19 -07:00
|
|
|
const handleMax = useCallback(
|
|
|
|
(amountIn: string) => {
|
|
|
|
setAmountInFormValue(amountIn, true)
|
2023-10-26 21:30:17 -07:00
|
|
|
set((s) => {
|
|
|
|
s.swap.amountOut = ''
|
|
|
|
})
|
2023-08-01 05:21:19 -07:00
|
|
|
},
|
|
|
|
[setAmountInFormValue],
|
|
|
|
)
|
|
|
|
|
|
|
|
const handleRepay = useCallback(
|
|
|
|
(amountOut: string) => {
|
|
|
|
setAmountOutFormValue(amountOut, true)
|
2023-10-26 21:30:17 -07:00
|
|
|
set((s) => {
|
|
|
|
s.swap.amountIn = ''
|
|
|
|
})
|
2023-08-01 05:21:19 -07:00
|
|
|
},
|
|
|
|
[setAmountInFormValue],
|
|
|
|
)
|
|
|
|
|
2023-06-16 05:23:50 -07:00
|
|
|
/*
|
|
|
|
Once a route is returned from the Jupiter API, use the inAmount or outAmount
|
|
|
|
depending on the swapMode and set those values in state
|
|
|
|
*/
|
|
|
|
useEffect(() => {
|
|
|
|
if (typeof bestRoute !== 'undefined') {
|
|
|
|
setSelectedRoute(bestRoute)
|
|
|
|
|
2023-10-17 16:24:40 -07:00
|
|
|
if (inputBank && swapMode === 'ExactOut' && bestRoute?.inAmount) {
|
|
|
|
const inAmount = new Decimal(bestRoute.inAmount)
|
2023-06-16 05:23:50 -07:00
|
|
|
.div(10 ** inputBank.mintDecimals)
|
|
|
|
.toString()
|
|
|
|
setAmountInFormValue(inAmount)
|
2023-10-17 16:24:40 -07:00
|
|
|
} else if (outputBank && swapMode === 'ExactIn' && bestRoute?.outAmount) {
|
|
|
|
const outAmount = new Decimal(bestRoute.outAmount)
|
2023-06-16 05:23:50 -07:00
|
|
|
.div(10 ** outputBank.mintDecimals)
|
|
|
|
.toString()
|
|
|
|
setAmountOutFormValue(outAmount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [bestRoute, swapMode, inputBank, outputBank])
|
|
|
|
|
|
|
|
const handleSwitchTokens = useCallback(() => {
|
|
|
|
if (amountInAsDecimal?.gt(0) && amountOutAsDecimal.gte(0)) {
|
|
|
|
setAmountInFormValue(amountOutAsDecimal.toString())
|
|
|
|
}
|
|
|
|
const inputBank = mangoStore.getState().swap.inputBank
|
|
|
|
const outputBank = mangoStore.getState().swap.outputBank
|
|
|
|
set((s) => {
|
|
|
|
s.swap.inputBank = outputBank
|
|
|
|
s.swap.outputBank = inputBank
|
2023-10-26 21:30:17 -07:00
|
|
|
s.swap.amountIn = ''
|
|
|
|
s.swap.amountOut = ''
|
2023-06-16 05:23:50 -07:00
|
|
|
})
|
|
|
|
setAnimateSwitchArrow(
|
2023-07-25 19:48:13 -07:00
|
|
|
(prevanimateSwitchArrow) => prevanimateSwitchArrow + 1,
|
2023-06-16 05:23:50 -07:00
|
|
|
)
|
|
|
|
}, [setAmountInFormValue, amountOutAsDecimal, amountInAsDecimal])
|
|
|
|
|
2023-10-26 21:30:17 -07:00
|
|
|
const loadingExactIn: boolean = useMemo(() => {
|
|
|
|
return (
|
|
|
|
(!!(amountInAsDecimal.toNumber() || amountOutAsDecimal.toNumber()) &&
|
|
|
|
connected &&
|
|
|
|
typeof selectedRoute === 'undefined') ||
|
|
|
|
!!(
|
|
|
|
swapMode === 'ExactIn' &&
|
|
|
|
amountInAsDecimal.toNumber() &&
|
|
|
|
!amountOutAsDecimal.toNumber()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}, [
|
|
|
|
amountInAsDecimal,
|
|
|
|
amountOutAsDecimal,
|
|
|
|
connected,
|
|
|
|
selectedRoute,
|
|
|
|
swapMode,
|
|
|
|
])
|
|
|
|
|
|
|
|
const loadingExactOut: boolean = useMemo(() => {
|
2023-06-16 05:23:50 -07:00
|
|
|
return (
|
2023-10-26 21:30:17 -07:00
|
|
|
(!!(amountInAsDecimal.toNumber() || amountOutAsDecimal.toNumber()) &&
|
|
|
|
connected &&
|
|
|
|
typeof selectedRoute === 'undefined') ||
|
|
|
|
!!(
|
|
|
|
swapMode === 'ExactOut' &&
|
|
|
|
amountOutAsDecimal.toNumber() &&
|
|
|
|
!amountInAsDecimal.toNumber()
|
|
|
|
)
|
2023-06-16 05:23:50 -07:00
|
|
|
)
|
2023-10-26 21:30:17 -07:00
|
|
|
}, [
|
|
|
|
amountInAsDecimal,
|
|
|
|
amountOutAsDecimal,
|
|
|
|
connected,
|
|
|
|
selectedRoute,
|
|
|
|
swapMode,
|
|
|
|
])
|
2023-06-16 05:23:50 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-09-18 18:22:10 -07:00
|
|
|
<SwapReviewRouteInfo
|
|
|
|
amountIn={amountInAsDecimal}
|
2023-10-28 04:49:46 -07:00
|
|
|
loadingRoute={fetchingRoute}
|
2023-10-17 16:24:40 -07:00
|
|
|
onClose={() => setShowConfirm(false)}
|
|
|
|
onSuccess={onSuccess}
|
2023-10-28 04:49:46 -07:00
|
|
|
refetchRoute={refetchRoute}
|
2023-10-16 21:12:46 -07:00
|
|
|
routes={bestRoute ? [bestRoute] : undefined}
|
2023-09-18 18:22:10 -07:00
|
|
|
selectedRoute={selectedRoute}
|
|
|
|
setSelectedRoute={setSelectedRoute}
|
2023-10-17 16:24:40 -07:00
|
|
|
show={showConfirm}
|
|
|
|
slippage={slippage}
|
2023-09-18 18:22:10 -07:00
|
|
|
/>
|
2023-07-25 19:48:13 -07:00
|
|
|
<SellTokenInput
|
|
|
|
handleAmountInChange={handleAmountInChange}
|
|
|
|
setShowTokenSelect={setShowTokenSelect}
|
2023-08-01 05:21:19 -07:00
|
|
|
handleMax={handleMax}
|
2023-10-26 21:30:17 -07:00
|
|
|
loading={loadingExactOut}
|
2023-07-25 19:48:13 -07:00
|
|
|
/>
|
2023-08-23 15:17:04 -07:00
|
|
|
<div className="rounded-b-xl bg-th-bkg-2 p-3 pt-0">
|
|
|
|
{swapFormSizeUi === 'slider' ? (
|
|
|
|
<SwapSlider
|
|
|
|
useMargin={useMargin}
|
|
|
|
amount={amountInAsDecimal.toNumber()}
|
2023-10-26 21:30:17 -07:00
|
|
|
onChange={(v) => handleSliderChange(v)}
|
2023-08-23 15:17:04 -07:00
|
|
|
step={1 / 10 ** (inputBank?.mintDecimals || 6)}
|
2023-09-07 00:59:04 -07:00
|
|
|
maxAmount={useTokenMax}
|
2023-10-26 21:30:17 -07:00
|
|
|
handleStartDrag={handleSliderDrag}
|
|
|
|
handleEndDrag={handleSliderDragEnd}
|
2023-08-23 15:17:04 -07:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="-mt-2">
|
|
|
|
<PercentageSelectButtons
|
|
|
|
amountIn={amountInAsDecimal.toString()}
|
2023-10-26 21:30:17 -07:00
|
|
|
setAmountIn={(v) => handleSliderChange(v)}
|
2023-08-23 15:17:04 -07:00
|
|
|
useMargin={useMargin}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-06-16 05:23:50 -07:00
|
|
|
<div className="my-2 flex justify-center">
|
|
|
|
<button
|
|
|
|
className="rounded-full border border-th-fgd-4 p-1.5 text-th-fgd-3 focus-visible:border-th-active md:hover:border-th-active md:hover:text-th-active"
|
|
|
|
onClick={handleSwitchTokens}
|
|
|
|
>
|
|
|
|
<ArrowDownIcon
|
|
|
|
className="h-5 w-5"
|
|
|
|
style={
|
|
|
|
animateSwitchArrow % 2 == 0
|
|
|
|
? { transform: 'rotate(0deg)' }
|
|
|
|
: { transform: 'rotate(360deg)' }
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
</div>
|
2023-07-25 19:48:13 -07:00
|
|
|
<BuyTokenInput
|
|
|
|
handleAmountOutChange={handleAmountOutChange}
|
2023-10-26 21:30:17 -07:00
|
|
|
loading={loadingExactIn}
|
2023-07-25 19:48:13 -07:00
|
|
|
setShowTokenSelect={setShowTokenSelect}
|
2023-08-01 05:21:19 -07:00
|
|
|
handleRepay={handleRepay}
|
2023-07-25 19:48:13 -07:00
|
|
|
/>
|
2023-08-03 19:06:09 -07:00
|
|
|
{ipAllowed ? (
|
|
|
|
<SwapFormSubmitButton
|
2023-10-26 21:30:17 -07:00
|
|
|
loadingSwapDetails={loadingExactIn || loadingExactOut}
|
2023-08-03 19:06:09 -07:00
|
|
|
useMargin={useMargin}
|
|
|
|
selectedRoute={selectedRoute}
|
|
|
|
setShowConfirm={setShowConfirm}
|
|
|
|
amountIn={amountInAsDecimal}
|
|
|
|
inputSymbol={inputBank?.name}
|
|
|
|
amountOut={selectedRoute ? amountOutAsDecimal.toNumber() : undefined}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Button
|
|
|
|
disabled
|
2023-09-07 20:35:30 -07:00
|
|
|
className="mb-4 mt-6 flex w-full items-center justify-center text-base"
|
2023-08-03 19:06:09 -07:00
|
|
|
size="large"
|
|
|
|
>
|
|
|
|
{t('country-not-allowed', {
|
|
|
|
country: ipCountry ? `(${ipCountry})` : '',
|
|
|
|
})}
|
|
|
|
</Button>
|
|
|
|
)}
|
2023-06-16 05:23:50 -07:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MarketSwapForm
|
2023-08-03 19:06:09 -07:00
|
|
|
|
|
|
|
const SwapFormSubmitButton = ({
|
|
|
|
amountIn,
|
2023-09-18 15:44:21 -07:00
|
|
|
amountOut,
|
2023-08-03 19:06:09 -07:00
|
|
|
loadingSwapDetails,
|
|
|
|
selectedRoute,
|
|
|
|
setShowConfirm,
|
|
|
|
}: {
|
|
|
|
amountIn: Decimal
|
|
|
|
amountOut: number | undefined
|
|
|
|
inputSymbol: string | undefined
|
|
|
|
loadingSwapDetails: boolean
|
2023-10-16 21:12:46 -07:00
|
|
|
selectedRoute: JupiterV6RouteInfo | undefined | null
|
2023-08-03 19:06:09 -07:00
|
|
|
setShowConfirm: (x: boolean) => void
|
|
|
|
useMargin: boolean
|
|
|
|
}) => {
|
|
|
|
const { t } = useTranslation('common')
|
2023-08-10 17:52:42 -07:00
|
|
|
const { mangoAccountAddress } = useMangoAccount()
|
2023-08-17 15:25:46 -07:00
|
|
|
const { connected } = useWallet()
|
2023-10-20 08:11:19 -07:00
|
|
|
const { setShowSettingsModal } = TopBarStore()
|
2023-09-18 13:42:37 -07:00
|
|
|
// const { amount: tokenMax, amountWithBorrow } = useTokenMax(useMargin)
|
2023-08-15 22:27:45 -07:00
|
|
|
const { inputBank, outputBank } = mangoStore((s) => s.swap)
|
2023-08-27 22:59:56 -07:00
|
|
|
const { remainingBorrowsInPeriod, timeToNextPeriod } =
|
|
|
|
useRemainingBorrowsInPeriod(true)
|
2023-09-10 06:26:52 -07:00
|
|
|
const tokenPositionsFull = useTokenPositionsFull([outputBank, inputBank])
|
2023-08-10 17:52:42 -07:00
|
|
|
|
2023-08-27 22:59:56 -07:00
|
|
|
// check if the borrowed amount exceeds the net borrow limit in the current period
|
|
|
|
const borrowExceedsLimitInPeriod = useMemo(() => {
|
|
|
|
const mangoAccount = mangoStore.getState().mangoAccount.current
|
2023-09-09 04:32:28 -07:00
|
|
|
if (!mangoAccount || !inputBank || !remainingBorrowsInPeriod) return false
|
2023-08-27 22:59:56 -07:00
|
|
|
|
|
|
|
const balance = mangoAccount.getTokenDepositsUi(inputBank)
|
|
|
|
const remainingBalance = balance - amountIn.toNumber()
|
|
|
|
const borrowAmount = remainingBalance < 0 ? Math.abs(remainingBalance) : 0
|
2023-10-25 02:21:21 -07:00
|
|
|
const borrowAmountNotional = borrowAmount * inputBank.uiPrice
|
|
|
|
return borrowAmountNotional > remainingBorrowsInPeriod
|
2023-08-27 22:59:56 -07:00
|
|
|
}, [amountIn, inputBank, mangoAccountAddress, remainingBorrowsInPeriod])
|
|
|
|
|
2023-10-17 16:24:40 -07:00
|
|
|
const disabled =
|
|
|
|
!amountIn.toNumber() ||
|
|
|
|
!amountOut ||
|
|
|
|
!selectedRoute ||
|
|
|
|
!!selectedRoute.error
|
2023-08-03 19:06:09 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-08-17 15:25:46 -07:00
|
|
|
{connected ? (
|
|
|
|
<Button
|
2023-09-18 15:44:21 -07:00
|
|
|
onClick={() => setShowConfirm(true)}
|
2023-08-17 16:47:11 -07:00
|
|
|
className="mb-4 mt-6 flex w-full items-center justify-center text-base"
|
2023-09-18 15:44:21 -07:00
|
|
|
disabled={disabled}
|
2023-08-17 15:25:46 -07:00
|
|
|
size="large"
|
|
|
|
>
|
2023-09-18 15:44:21 -07:00
|
|
|
{loadingSwapDetails ? (
|
2023-08-03 19:06:09 -07:00
|
|
|
<Loading />
|
|
|
|
) : (
|
|
|
|
<span>{t('swap:review-swap')}</span>
|
2023-08-17 15:25:46 -07:00
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
) : (
|
|
|
|
<SecondaryConnectButton
|
2023-08-17 16:47:11 -07:00
|
|
|
className="mb-4 mt-6 flex w-full items-center justify-center"
|
2023-08-17 15:25:46 -07:00
|
|
|
isLarge
|
|
|
|
/>
|
|
|
|
)}
|
2023-08-15 22:27:45 -07:00
|
|
|
{tokenPositionsFull ? (
|
|
|
|
<div className="pb-4">
|
|
|
|
<InlineNotification
|
|
|
|
type="error"
|
|
|
|
desc={
|
|
|
|
<>
|
|
|
|
{t('error-token-positions-full')}{' '}
|
2023-10-20 08:11:19 -07:00
|
|
|
<Link href={''} onClick={() => setShowSettingsModal(true)}>
|
2023-08-15 22:27:45 -07:00
|
|
|
{t('manage')}
|
|
|
|
</Link>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2023-08-28 04:59:36 -07:00
|
|
|
{borrowExceedsLimitInPeriod &&
|
|
|
|
remainingBorrowsInPeriod &&
|
|
|
|
timeToNextPeriod ? (
|
2023-08-27 22:59:56 -07:00
|
|
|
<div className="mb-4">
|
|
|
|
<InlineNotification
|
|
|
|
type="error"
|
|
|
|
desc={t('error-borrow-exceeds-limit', {
|
2023-08-28 04:59:36 -07:00
|
|
|
remaining: formatCurrencyValue(remainingBorrowsInPeriod),
|
2023-08-27 22:59:56 -07:00
|
|
|
resetTime: dayjs().to(dayjs().add(timeToNextPeriod, 'second')),
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2023-10-17 16:24:40 -07:00
|
|
|
{(selectedRoute === null && amountIn.gt(0)) ||
|
|
|
|
(selectedRoute && !!selectedRoute.error) ? (
|
2023-08-03 19:06:09 -07:00
|
|
|
<div className="mb-4">
|
|
|
|
<InlineNotification type="error" desc={t('swap:no-swap-found')} />
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|