2022-08-17 18:26:38 -07:00
|
|
|
import { useState, useCallback, useEffect, useMemo } from 'react'
|
2022-08-19 21:03:26 -07:00
|
|
|
import { PublicKey } from '@solana/web3.js'
|
2022-08-25 17:27:05 -07:00
|
|
|
import {
|
|
|
|
ArrowDownIcon,
|
2022-09-07 17:47:59 -07:00
|
|
|
Cog8ToothIcon,
|
|
|
|
MagnifyingGlassIcon,
|
2022-08-25 17:27:05 -07:00
|
|
|
ExclamationCircleIcon,
|
2022-10-22 05:14:25 -07:00
|
|
|
LinkIcon,
|
2022-09-06 21:36:35 -07:00
|
|
|
} from '@heroicons/react/20/solid'
|
2022-12-07 13:33:35 -08:00
|
|
|
import NumberFormat, {
|
|
|
|
NumberFormatValues,
|
|
|
|
SourceInfo,
|
|
|
|
} from 'react-number-format'
|
2022-08-17 18:26:38 -07:00
|
|
|
import Decimal from 'decimal.js'
|
2022-09-12 08:53:57 -07:00
|
|
|
import mangoStore from '@store/mangoStore'
|
2022-07-05 20:37:49 -07:00
|
|
|
import ContentBox from '../shared/ContentBox'
|
2022-12-07 13:33:35 -08:00
|
|
|
import SwapReviewRouteInfo from './SwapReviewRouteInfo'
|
2022-09-01 10:33:29 -07:00
|
|
|
import TokenSelect from './TokenSelect'
|
2022-07-05 20:37:49 -07:00
|
|
|
import useDebounce from '../shared/useDebounce'
|
2022-07-15 05:50:29 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-08-20 11:54:38 -07:00
|
|
|
import SwapFormTokenList from './SwapFormTokenList'
|
2022-07-16 04:22:59 -07:00
|
|
|
import { Transition } from '@headlessui/react'
|
2022-11-04 11:55:21 -07:00
|
|
|
import Button, { IconButton } from '../shared/Button'
|
2022-07-18 03:02:43 -07:00
|
|
|
import Loading from '../shared/Loading'
|
2022-07-23 19:48:26 -07:00
|
|
|
import { EnterBottomExitBottom } from '../shared/Transitions'
|
2022-11-18 11:11:06 -08:00
|
|
|
import useJupiterRoutes from './useJupiterRoutes'
|
2022-08-08 10:42:18 -07:00
|
|
|
import SwapSettings from './SwapSettings'
|
2022-08-15 04:28:36 -07:00
|
|
|
import SheenLoader from '../shared/SheenLoader'
|
2022-08-25 06:00:42 -07:00
|
|
|
import { HealthType } from '@blockworks-foundation/mango-v4'
|
2022-08-20 11:17:57 -07:00
|
|
|
import {
|
|
|
|
INPUT_TOKEN_DEFAULT,
|
2022-11-18 11:11:06 -08:00
|
|
|
MANGO_MINT,
|
2022-08-20 11:17:57 -07:00
|
|
|
OUTPUT_TOKEN_DEFAULT,
|
2022-11-22 19:31:50 -08:00
|
|
|
SIZE_INPUT_UI_KEY,
|
2022-11-18 11:11:06 -08:00
|
|
|
USDC_MINT,
|
2022-08-20 11:17:57 -07:00
|
|
|
} from '../../utils/constants'
|
2022-10-05 04:25:26 -07:00
|
|
|
import { useTokenMax } from './useTokenMax'
|
2022-11-04 11:55:21 -07:00
|
|
|
import HealthImpact from '@components/shared/HealthImpact'
|
2022-11-15 20:12:51 -08:00
|
|
|
import { useWallet } from '@solana/wallet-adapter-react'
|
2022-11-18 09:09:39 -08:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
2022-11-18 11:11:06 -08:00
|
|
|
import { RouteInfo } from 'types/jupiter'
|
2022-11-21 20:20:05 -08:00
|
|
|
import useMangoGroup from 'hooks/useMangoGroup'
|
2022-11-22 19:31:50 -08:00
|
|
|
import useLocalStorageState from 'hooks/useLocalStorageState'
|
|
|
|
import SwapSlider from './SwapSlider'
|
2022-11-27 04:36:12 -08:00
|
|
|
import TokenVaultWarnings from '@components/shared/TokenVaultWarnings'
|
2022-12-07 13:33:35 -08:00
|
|
|
import MaxSwapAmount from './MaxSwapAmount'
|
|
|
|
import PercentageSelectButtons from './PercentageSelectButtons'
|
2022-07-18 03:02:43 -07:00
|
|
|
|
2022-08-17 18:26:38 -07:00
|
|
|
const MAX_DIGITS = 11
|
2022-09-02 11:36:57 -07:00
|
|
|
export const withValueLimit = (values: NumberFormatValues): boolean => {
|
2022-08-17 18:26:38 -07:00
|
|
|
return values.floatValue
|
|
|
|
? values.floatValue.toFixed(0).length <= MAX_DIGITS
|
|
|
|
: true
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:03:26 -07:00
|
|
|
const SwapForm = () => {
|
2022-09-14 18:06:00 -07:00
|
|
|
const { t } = useTranslation(['common', 'swap'])
|
2022-07-18 03:02:43 -07:00
|
|
|
const [selectedRoute, setSelectedRoute] = useState<RouteInfo>()
|
2022-07-22 08:40:53 -07:00
|
|
|
const [animateSwitchArrow, setAnimateSwitchArrow] = useState(0)
|
2022-07-16 04:22:59 -07:00
|
|
|
const [showTokenSelect, setShowTokenSelect] = useState('')
|
2022-08-08 10:42:18 -07:00
|
|
|
const [showSettings, setShowSettings] = useState(false)
|
2022-07-17 19:49:14 -07:00
|
|
|
const [showConfirm, setShowConfirm] = useState(false)
|
2022-11-21 20:20:05 -08:00
|
|
|
const { group } = useMangoGroup()
|
2022-11-22 19:31:50 -08:00
|
|
|
const [swapFormSizeUi] = useLocalStorageState(SIZE_INPUT_UI_KEY, 'Slider')
|
2022-10-07 05:22:18 -07:00
|
|
|
|
2022-07-10 19:01:16 -07:00
|
|
|
const set = mangoStore.getState().set
|
2022-11-18 11:11:06 -08:00
|
|
|
const {
|
|
|
|
margin: useMargin,
|
|
|
|
slippage,
|
|
|
|
inputBank,
|
|
|
|
outputBank,
|
2022-12-07 13:33:35 -08:00
|
|
|
amountIn: amountInFormValue,
|
|
|
|
amountOut: amountOutFormValue,
|
|
|
|
swapMode,
|
2022-11-18 11:11:06 -08:00
|
|
|
} = mangoStore((s) => s.swap)
|
2022-08-18 13:50:34 -07:00
|
|
|
const [debouncedAmountIn] = useDebounce(amountInFormValue, 300)
|
2022-12-07 13:33:35 -08:00
|
|
|
const [debouncedAmountOut] = useDebounce(amountOutFormValue, 300)
|
2022-11-18 09:09:39 -08:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
2022-11-15 20:12:51 -08:00
|
|
|
const { connected } = useWallet()
|
2022-09-14 17:02:16 -07:00
|
|
|
|
2022-12-07 13:33:35 -08:00
|
|
|
const amountInAsDecimal: Decimal | null = useMemo(() => {
|
2022-09-01 10:33:29 -07:00
|
|
|
return Number(debouncedAmountIn)
|
|
|
|
? new Decimal(debouncedAmountIn)
|
|
|
|
: new Decimal(0)
|
|
|
|
}, [debouncedAmountIn])
|
|
|
|
|
2022-12-07 13:33:35 -08:00
|
|
|
const amountOutAsDecimal: Decimal | null = useMemo(() => {
|
|
|
|
return Number(debouncedAmountOut)
|
|
|
|
? new Decimal(debouncedAmountOut)
|
|
|
|
: new Decimal(0)
|
|
|
|
}, [debouncedAmountOut])
|
|
|
|
|
2022-11-18 11:11:06 -08:00
|
|
|
const { bestRoute, routes } = useJupiterRoutes({
|
|
|
|
inputMint: inputBank?.mint.toString() || USDC_MINT,
|
|
|
|
outputMint: outputBank?.mint.toString() || MANGO_MINT,
|
2022-12-07 13:33:35 -08:00
|
|
|
amount: swapMode === 'ExactIn' ? debouncedAmountIn : debouncedAmountOut,
|
2022-08-03 14:46:37 -07:00
|
|
|
slippage,
|
2022-12-07 13:33:35 -08:00
|
|
|
swapMode,
|
2022-08-03 14:46:37 -07:00
|
|
|
})
|
2022-07-18 03:02:43 -07:00
|
|
|
|
2022-12-07 17:14:29 -08: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'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
2022-12-07 13:33:35 -08:00
|
|
|
|
|
|
|
const setAmountOutFormValue = useCallback((amountOut: string) => {
|
|
|
|
set((s) => {
|
|
|
|
s.swap.amountOut = amountOut
|
2022-12-07 17:04:48 -08:00
|
|
|
if (!parseFloat(amountOut)) {
|
|
|
|
s.swap.amountIn = ''
|
|
|
|
}
|
2022-12-07 13:33:35 -08:00
|
|
|
})
|
|
|
|
}, [])
|
2022-11-18 11:11:06 -08:00
|
|
|
|
2022-12-07 13:33:35 -08: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
|
|
|
|
*/
|
2022-07-18 03:02:43 -07:00
|
|
|
useEffect(() => {
|
2022-11-18 11:11:06 -08:00
|
|
|
if (bestRoute) {
|
|
|
|
setSelectedRoute(bestRoute)
|
2022-12-07 13:33:35 -08:00
|
|
|
|
|
|
|
if (inputBank && swapMode === 'ExactOut') {
|
|
|
|
const inAmount = new Decimal(bestRoute.inAmount)
|
|
|
|
.div(10 ** inputBank.mintDecimals)
|
|
|
|
.toString()
|
|
|
|
setAmountInFormValue(inAmount)
|
|
|
|
} else if (outputBank && swapMode === 'ExactIn') {
|
|
|
|
const outAmount = new Decimal(bestRoute.outAmount)
|
|
|
|
.div(10 ** outputBank.mintDecimals)
|
|
|
|
.toString()
|
|
|
|
setAmountOutFormValue(outAmount)
|
|
|
|
}
|
2022-11-18 11:11:06 -08:00
|
|
|
}
|
2022-12-07 13:33:35 -08:00
|
|
|
}, [bestRoute, swapMode, inputBank, outputBank])
|
2022-07-18 03:02:43 -07:00
|
|
|
|
2022-12-07 13:33:35 -08:00
|
|
|
/*
|
|
|
|
If the use margin setting is toggled, clear the form values
|
|
|
|
*/
|
2022-08-25 14:24:10 -07:00
|
|
|
useEffect(() => {
|
2022-09-02 16:52:07 -07:00
|
|
|
setAmountInFormValue('')
|
2022-12-07 13:33:35 -08:00
|
|
|
setAmountOutFormValue('')
|
2022-08-25 14:24:10 -07:00
|
|
|
}, [useMargin])
|
|
|
|
|
2022-12-07 13:33:35 -08:00
|
|
|
const handleAmountInChange = useCallback(
|
|
|
|
(e: NumberFormatValues, info: SourceInfo) => {
|
|
|
|
if (info.source !== 'event') return
|
|
|
|
if (swapMode === 'ExactOut') {
|
2022-08-17 18:26:38 -07:00
|
|
|
set((s) => {
|
2022-12-07 13:33:35 -08:00
|
|
|
s.swap.swapMode = 'ExactIn'
|
2022-08-17 18:26:38 -07:00
|
|
|
})
|
|
|
|
}
|
2022-12-07 13:33:35 -08:00
|
|
|
setAmountInFormValue(e.value)
|
2022-08-17 18:26:38 -07:00
|
|
|
},
|
2022-12-07 13:33:35 -08:00
|
|
|
[swapMode]
|
2022-08-17 18:26:38 -07:00
|
|
|
)
|
2022-08-10 13:23:19 -07:00
|
|
|
|
2022-12-07 13:33:35 -08:00
|
|
|
const handleAmountOutChange = useCallback(
|
|
|
|
(e: NumberFormatValues, info: SourceInfo) => {
|
|
|
|
if (info.source !== 'event') return
|
|
|
|
if (swapMode === 'ExactIn') {
|
2022-08-17 18:26:38 -07:00
|
|
|
set((s) => {
|
2022-12-07 13:33:35 -08:00
|
|
|
s.swap.swapMode = 'ExactOut'
|
2022-08-17 18:26:38 -07:00
|
|
|
})
|
|
|
|
}
|
2022-12-07 13:33:35 -08:00
|
|
|
setAmountOutFormValue(e.value)
|
2022-08-17 18:26:38 -07:00
|
|
|
},
|
2022-12-07 13:33:35 -08:00
|
|
|
[swapMode]
|
2022-08-17 18:26:38 -07:00
|
|
|
)
|
2022-07-05 20:37:49 -07:00
|
|
|
|
2022-12-07 13:33:35 -08:00
|
|
|
const handleTokenInSelect = useCallback((mintAddress: string) => {
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
if (group) {
|
|
|
|
const bank = group.getFirstBankByMint(new PublicKey(mintAddress))
|
|
|
|
set((s) => {
|
|
|
|
s.swap.inputBank = bank
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setShowTokenSelect('')
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleTokenOutSelect = useCallback((mintAddress: string) => {
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
if (group) {
|
|
|
|
const bank = group.getFirstBankByMint(new PublicKey(mintAddress))
|
|
|
|
set((s) => {
|
|
|
|
s.swap.outputBank = bank
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setShowTokenSelect('')
|
|
|
|
}, [])
|
|
|
|
|
2022-08-17 18:26:38 -07:00
|
|
|
const handleSwitchTokens = useCallback(() => {
|
2022-12-07 13:33:35 -08:00
|
|
|
if (amountInAsDecimal?.gt(0) && amountOutAsDecimal.gte(0)) {
|
|
|
|
setAmountInFormValue(amountOutAsDecimal.toString())
|
2022-08-31 08:10:34 -07:00
|
|
|
}
|
2022-08-18 13:50:34 -07:00
|
|
|
const inputBank = mangoStore.getState().swap.inputBank
|
|
|
|
const outputBank = mangoStore.getState().swap.outputBank
|
2022-07-11 20:00:22 -07:00
|
|
|
set((s) => {
|
2022-08-18 13:50:34 -07:00
|
|
|
s.swap.inputBank = outputBank
|
|
|
|
s.swap.outputBank = inputBank
|
2022-07-11 20:00:22 -07:00
|
|
|
})
|
2022-08-17 18:26:38 -07:00
|
|
|
setAnimateSwitchArrow(
|
|
|
|
(prevanimateSwitchArrow) => prevanimateSwitchArrow + 1
|
|
|
|
)
|
2022-12-07 13:33:35 -08:00
|
|
|
}, [set, amountOutAsDecimal, amountInAsDecimal])
|
2022-08-25 06:00:42 -07:00
|
|
|
|
|
|
|
const maintProjectedHealth = useMemo(() => {
|
|
|
|
const group = mangoStore.getState().group
|
2022-12-07 13:33:35 -08:00
|
|
|
if (
|
|
|
|
!inputBank ||
|
|
|
|
!mangoAccount ||
|
|
|
|
!outputBank ||
|
|
|
|
!amountOutAsDecimal ||
|
|
|
|
!group
|
|
|
|
)
|
2022-08-25 06:00:42 -07:00
|
|
|
return 0
|
|
|
|
|
|
|
|
const simulatedHealthRatio =
|
|
|
|
mangoAccount.simHealthRatioWithTokenPositionUiChanges(
|
|
|
|
group,
|
|
|
|
[
|
|
|
|
{
|
2022-11-18 11:11:06 -08:00
|
|
|
mintPk: inputBank.mint,
|
2022-12-07 13:33:35 -08:00
|
|
|
uiTokenAmount: amountInAsDecimal.toNumber() * -1,
|
2022-08-25 06:00:42 -07:00
|
|
|
},
|
|
|
|
{
|
2022-11-18 11:11:06 -08:00
|
|
|
mintPk: outputBank.mint,
|
2022-12-07 15:09:15 -08:00
|
|
|
uiTokenAmount: amountOutAsDecimal.toNumber(),
|
2022-08-25 06:00:42 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
HealthType.maint
|
|
|
|
)
|
2022-12-07 13:33:35 -08:00
|
|
|
return simulatedHealthRatio > 100
|
2022-08-25 06:00:42 -07:00
|
|
|
? 100
|
2022-12-07 13:33:35 -08:00
|
|
|
: simulatedHealthRatio < 0
|
2022-08-25 06:00:42 -07:00
|
|
|
? 0
|
2022-12-07 13:33:35 -08:00
|
|
|
: Math.trunc(simulatedHealthRatio)
|
|
|
|
}, [
|
|
|
|
mangoAccount,
|
|
|
|
inputBank,
|
|
|
|
outputBank,
|
|
|
|
amountInAsDecimal,
|
|
|
|
amountOutAsDecimal,
|
|
|
|
])
|
2022-08-25 06:00:42 -07:00
|
|
|
|
2022-09-22 14:35:07 -07:00
|
|
|
const loadingSwapDetails: boolean = useMemo(() => {
|
2022-12-07 13:33:35 -08:00
|
|
|
return !!amountInAsDecimal.toNumber() && connected && !selectedRoute
|
|
|
|
}, [amountInAsDecimal, connected, selectedRoute])
|
2022-08-25 12:44:02 -07:00
|
|
|
|
2022-07-05 20:37:49 -07:00
|
|
|
return (
|
2022-09-14 14:57:12 -07:00
|
|
|
<ContentBox
|
|
|
|
hidePadding
|
2022-09-14 21:51:19 -07:00
|
|
|
// showBackground
|
2022-10-30 22:02:55 -07:00
|
|
|
className="relative overflow-hidden border-x-0 md:border-l md:border-r-0 md:border-t-0 md:border-b-0"
|
2022-09-14 14:57:12 -07:00
|
|
|
>
|
2022-09-14 23:06:23 -07:00
|
|
|
<div className="p-6 pt-3">
|
2022-08-25 06:00:42 -07:00
|
|
|
<Transition
|
2022-10-08 03:15:03 -07:00
|
|
|
className="thin-scroll absolute top-0 left-0 z-10 h-full w-full overflow-auto bg-th-bkg-1 p-6 pb-0"
|
2022-08-25 06:00:42 -07:00
|
|
|
show={showConfirm}
|
|
|
|
enter="transition ease-in duration-300"
|
|
|
|
enterFrom="translate-x-full"
|
|
|
|
enterTo="translate-x-0"
|
|
|
|
leave="transition ease-out duration-300"
|
|
|
|
leaveFrom="translate-x-0"
|
|
|
|
leaveTo="translate-x-full"
|
2022-07-17 04:48:33 -07:00
|
|
|
>
|
2022-12-07 13:33:35 -08:00
|
|
|
<SwapReviewRouteInfo
|
2022-08-25 06:00:42 -07:00
|
|
|
onClose={() => setShowConfirm(false)}
|
2022-12-07 13:33:35 -08:00
|
|
|
amountIn={amountInAsDecimal}
|
2022-08-25 06:00:42 -07:00
|
|
|
slippage={slippage}
|
|
|
|
routes={routes}
|
|
|
|
selectedRoute={selectedRoute}
|
|
|
|
setSelectedRoute={setSelectedRoute}
|
2022-07-16 04:22:59 -07:00
|
|
|
/>
|
2022-08-25 06:00:42 -07:00
|
|
|
</Transition>
|
|
|
|
<EnterBottomExitBottom
|
2022-10-08 03:15:03 -07:00
|
|
|
className="thin-scroll absolute bottom-0 left-0 z-10 h-full w-full overflow-auto bg-th-bkg-1 p-6 pb-0"
|
2022-08-25 06:00:42 -07:00
|
|
|
show={!!showTokenSelect}
|
|
|
|
>
|
|
|
|
<SwapFormTokenList
|
|
|
|
onClose={() => setShowTokenSelect('')}
|
|
|
|
onTokenSelect={
|
|
|
|
showTokenSelect === 'input'
|
|
|
|
? handleTokenInSelect
|
|
|
|
: handleTokenOutSelect
|
|
|
|
}
|
|
|
|
type={showTokenSelect}
|
2022-09-14 03:37:45 -07:00
|
|
|
useMargin={useMargin}
|
2022-07-15 05:50:29 -07:00
|
|
|
/>
|
2022-08-25 06:00:42 -07:00
|
|
|
</EnterBottomExitBottom>
|
2022-08-25 12:44:02 -07:00
|
|
|
<EnterBottomExitBottom
|
2022-10-08 03:15:03 -07:00
|
|
|
className="thin-scroll absolute bottom-0 left-0 z-10 h-full w-full overflow-auto bg-th-bkg-1 p-6 pb-0"
|
2022-08-25 12:44:02 -07:00
|
|
|
show={showSettings}
|
|
|
|
>
|
|
|
|
<SwapSettings onClose={() => setShowSettings(false)} />
|
|
|
|
</EnterBottomExitBottom>
|
2022-10-29 19:46:59 -07:00
|
|
|
<div className="flex items-center justify-end">
|
2022-09-21 21:25:24 -07:00
|
|
|
<div id="swap-step-one">
|
2022-09-11 17:22:37 -07:00
|
|
|
<IconButton
|
2022-10-29 04:38:40 -07:00
|
|
|
className="-mr-2 text-th-fgd-3"
|
2022-09-14 19:41:55 -07:00
|
|
|
hideBg
|
2022-09-11 17:22:37 -07:00
|
|
|
onClick={() => setShowSettings(true)}
|
|
|
|
size="small"
|
|
|
|
>
|
2022-10-30 04:19:04 -07:00
|
|
|
<Cog8ToothIcon className="h-4 w-4" />
|
2022-09-11 17:22:37 -07:00
|
|
|
</IconButton>
|
|
|
|
</div>
|
2022-07-10 19:01:16 -07:00
|
|
|
</div>
|
2022-11-13 19:33:45 -08:00
|
|
|
<div className="mb-2 flex items-end justify-between">
|
2022-12-07 18:34:24 -08:00
|
|
|
<p className="text-base text-th-fgd-2">{t('swap:pay')}</p>
|
2022-08-25 06:00:42 -07:00
|
|
|
<MaxSwapAmount
|
|
|
|
useMargin={useMargin}
|
2022-12-07 17:14:29 -08:00
|
|
|
setAmountIn={(v) => setAmountInFormValue(v, true)}
|
2022-08-23 05:41:37 -07:00
|
|
|
/>
|
2022-07-15 05:50:29 -07:00
|
|
|
</div>
|
2022-11-13 19:33:45 -08:00
|
|
|
<div className="mb-3 grid grid-cols-2" id="swap-step-two">
|
2022-12-07 18:34:24 -08:00
|
|
|
<div className="col-span-1 rounded-lg rounded-r-none border border-r-0 border-th-input-border bg-th-input-bkg">
|
2022-08-25 06:00:42 -07:00
|
|
|
<TokenSelect
|
2022-11-21 20:20:05 -08:00
|
|
|
bank={
|
2022-12-07 13:33:35 -08:00
|
|
|
inputBank || group?.banksMapByName.get(INPUT_TOKEN_DEFAULT)?.[0]
|
2022-11-21 20:20:05 -08:00
|
|
|
}
|
2022-08-25 06:00:42 -07:00
|
|
|
showTokenList={setShowTokenSelect}
|
|
|
|
type="input"
|
|
|
|
/>
|
|
|
|
</div>
|
2022-11-04 12:08:27 -07:00
|
|
|
<div className="col-span-1 flex h-[54px]">
|
2022-08-25 06:00:42 -07:00
|
|
|
<NumberFormat
|
|
|
|
inputMode="decimal"
|
|
|
|
thousandSeparator=","
|
2022-08-31 08:54:43 -07:00
|
|
|
allowNegative={false}
|
|
|
|
isNumericString={true}
|
2022-11-18 11:11:06 -08:00
|
|
|
decimalScale={inputBank?.mintDecimals || 6}
|
2022-08-25 06:00:42 -07:00
|
|
|
name="amountIn"
|
|
|
|
id="amountIn"
|
2022-12-07 18:34:24 -08:00
|
|
|
className="w-full rounded-l-none rounded-r-lg border border-th-input-border bg-th-input-bkg p-3 text-right font-mono text-base font-bold text-th-fgd-1 focus:outline-none lg:text-lg xl:text-xl"
|
2022-08-25 06:00:42 -07:00
|
|
|
placeholder="0.00"
|
|
|
|
value={amountInFormValue}
|
|
|
|
onValueChange={handleAmountInChange}
|
|
|
|
isAllowed={withValueLimit}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-29 21:21:23 -07:00
|
|
|
<div className="-mb-2 flex justify-center">
|
2022-08-25 06:00:42 -07:00
|
|
|
<button
|
2022-11-30 19:32:32 -08:00
|
|
|
className="rounded-full border border-th-bkg-4 p-1.5 text-th-fgd-3 md:hover:text-th-active"
|
2022-08-25 06:00:42 -07:00
|
|
|
onClick={handleSwitchTokens}
|
|
|
|
>
|
|
|
|
<ArrowDownIcon
|
|
|
|
className="h-5 w-5"
|
|
|
|
style={
|
|
|
|
animateSwitchArrow % 2 == 0
|
|
|
|
? { transform: 'rotate(0deg)' }
|
|
|
|
: { transform: 'rotate(360deg)' }
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
</div>
|
2022-12-07 18:34:24 -08:00
|
|
|
<p className="mb-2 text-base text-th-fgd-2">{t('swap:receive')}</p>
|
2022-09-21 21:25:24 -07:00
|
|
|
<div id="swap-step-three" className="mb-3 grid grid-cols-2">
|
2022-12-07 18:34:24 -08:00
|
|
|
<div className="col-span-1 rounded-lg rounded-r-none border border-r-0 border-th-input-border bg-th-input-bkg">
|
2022-08-25 06:00:42 -07:00
|
|
|
<TokenSelect
|
2022-11-21 20:20:05 -08:00
|
|
|
bank={
|
|
|
|
outputBank ||
|
2022-12-07 13:33:35 -08:00
|
|
|
group?.banksMapByName.get(OUTPUT_TOKEN_DEFAULT)?.[0]
|
2022-11-21 20:20:05 -08:00
|
|
|
}
|
2022-08-25 06:00:42 -07:00
|
|
|
showTokenList={setShowTokenSelect}
|
|
|
|
type="output"
|
|
|
|
/>
|
|
|
|
</div>
|
2022-12-07 18:34:24 -08:00
|
|
|
<div className="flex h-[54px] w-full items-center justify-end rounded-r-lg border border-th-input-border text-right text-lg font-bold text-th-fgd-3 xl:text-xl">
|
2022-09-22 14:35:07 -07:00
|
|
|
{loadingSwapDetails ? (
|
2022-08-25 06:00:42 -07:00
|
|
|
<div className="w-full">
|
2022-09-08 18:00:47 -07:00
|
|
|
<SheenLoader className="flex flex-1 rounded-l-none">
|
|
|
|
<div className="h-[52px] w-full rounded-r-lg bg-th-bkg-4" />
|
2022-08-25 06:00:42 -07:00
|
|
|
</SheenLoader>
|
|
|
|
</div>
|
|
|
|
) : (
|
2022-10-30 22:00:21 -07:00
|
|
|
<NumberFormat
|
|
|
|
inputMode="decimal"
|
|
|
|
thousandSeparator=","
|
|
|
|
allowNegative={false}
|
|
|
|
isNumericString={true}
|
2022-11-18 11:11:06 -08:00
|
|
|
decimalScale={outputBank?.mintDecimals || 6}
|
2022-11-04 10:41:33 -07:00
|
|
|
name="amountOut"
|
|
|
|
id="amountOut"
|
2022-12-07 18:34:24 -08:00
|
|
|
className="w-full rounded-l-none rounded-r-lg bg-th-input-bkg p-3 text-right font-mono text-base font-bold text-th-fgd-1 focus:outline-none lg:text-lg xl:text-xl"
|
2022-10-30 22:00:21 -07:00
|
|
|
placeholder="0.00"
|
2022-12-07 13:33:35 -08:00
|
|
|
value={amountOutFormValue}
|
|
|
|
onValueChange={handleAmountOutChange}
|
2022-10-30 22:00:21 -07:00
|
|
|
/>
|
2022-08-25 06:00:42 -07:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-22 19:31:50 -08:00
|
|
|
{swapFormSizeUi === 'Slider' ? (
|
|
|
|
<SwapSlider
|
|
|
|
useMargin={useMargin}
|
2022-12-07 13:33:35 -08:00
|
|
|
amount={amountInAsDecimal.toNumber()}
|
2022-12-07 17:14:29 -08:00
|
|
|
onChange={(v) => setAmountInFormValue(v, true)}
|
2022-11-22 19:31:50 -08:00
|
|
|
step={1 / 10 ** (inputBank?.mintDecimals || 6)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<PercentageSelectButtons
|
2022-12-07 13:33:35 -08:00
|
|
|
amountIn={amountInAsDecimal.toString()}
|
2022-12-07 17:14:29 -08:00
|
|
|
setAmountIn={(v) => setAmountInFormValue(v, true)}
|
2022-11-22 19:31:50 -08:00
|
|
|
useMargin={useMargin}
|
|
|
|
/>
|
|
|
|
)}
|
2022-09-22 14:35:07 -07:00
|
|
|
<SwapFormSubmitButton
|
|
|
|
loadingSwapDetails={loadingSwapDetails}
|
|
|
|
useMargin={useMargin}
|
|
|
|
setShowConfirm={setShowConfirm}
|
2022-12-07 13:33:35 -08:00
|
|
|
amountIn={amountInAsDecimal}
|
2022-11-18 11:11:06 -08:00
|
|
|
inputSymbol={inputBank?.name}
|
2022-12-07 13:33:35 -08:00
|
|
|
amountOut={selectedRoute ? amountOutAsDecimal.toNumber() : undefined}
|
2022-09-22 14:35:07 -07:00
|
|
|
/>
|
2022-12-07 13:33:35 -08:00
|
|
|
{group && inputBank ? (
|
2022-11-27 04:36:12 -08:00
|
|
|
<div className="pt-4">
|
2022-12-07 13:33:35 -08:00
|
|
|
<TokenVaultWarnings bank={inputBank} />
|
2022-11-27 04:36:12 -08:00
|
|
|
</div>
|
|
|
|
) : null}
|
2022-12-07 14:44:39 -08:00
|
|
|
<div className="space-y-2 pt-2">
|
|
|
|
<div id="swap-step-four">
|
|
|
|
<HealthImpact maintProjectedHealth={maintProjectedHealth} />
|
|
|
|
</div>
|
2022-12-07 13:33:35 -08:00
|
|
|
<div className="flex justify-between">
|
|
|
|
<p className="text-sm text-th-fgd-3">Est. {t('swap:slippage')}</p>
|
|
|
|
<p className="text-right font-mono text-sm text-th-fgd-3">
|
|
|
|
{selectedRoute?.priceImpactPct
|
|
|
|
? selectedRoute?.priceImpactPct * 100 < 0.1
|
2022-12-07 14:44:39 -08:00
|
|
|
? '<0.1%'
|
2022-12-07 13:33:35 -08:00
|
|
|
: `${(selectedRoute?.priceImpactPct * 100).toFixed(2)}%`
|
|
|
|
: '0.00%'}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-11 17:22:37 -07:00
|
|
|
</div>
|
2022-07-05 20:37:49 -07:00
|
|
|
</ContentBox>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:03:26 -07:00
|
|
|
export default SwapForm
|
2022-08-15 09:02:46 -07:00
|
|
|
|
2022-09-22 14:35:07 -07:00
|
|
|
const SwapFormSubmitButton = ({
|
|
|
|
amountIn,
|
|
|
|
amountOut,
|
|
|
|
inputSymbol,
|
|
|
|
loadingSwapDetails,
|
|
|
|
setShowConfirm,
|
|
|
|
useMargin,
|
|
|
|
}: {
|
|
|
|
amountIn: Decimal
|
2022-11-18 11:11:06 -08:00
|
|
|
amountOut: number | undefined
|
2022-09-22 14:35:07 -07:00
|
|
|
inputSymbol: string | undefined
|
|
|
|
loadingSwapDetails: boolean
|
2022-12-07 13:33:35 -08:00
|
|
|
setShowConfirm: (x: boolean) => void
|
2022-09-22 14:35:07 -07:00
|
|
|
useMargin: boolean
|
|
|
|
}) => {
|
|
|
|
const { t } = useTranslation('common')
|
2022-11-15 20:12:51 -08:00
|
|
|
const { connected } = useWallet()
|
2022-09-22 14:35:07 -07:00
|
|
|
const { amount: tokenMax, amountWithBorrow } = useTokenMax(useMargin)
|
|
|
|
|
|
|
|
const showInsufficientBalance = useMargin
|
|
|
|
? amountWithBorrow.lt(amountIn)
|
|
|
|
: tokenMax.lt(amountIn)
|
|
|
|
|
|
|
|
const disabled =
|
2022-11-18 11:11:06 -08:00
|
|
|
!amountIn.toNumber() || !connected || showInsufficientBalance || !amountOut
|
2022-09-22 14:35:07 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
onClick={() => setShowConfirm(true)}
|
|
|
|
className="mt-6 flex w-full items-center justify-center text-base"
|
|
|
|
disabled={disabled}
|
|
|
|
size="large"
|
|
|
|
>
|
|
|
|
{connected ? (
|
|
|
|
showInsufficientBalance ? (
|
|
|
|
<div className="flex items-center">
|
|
|
|
<ExclamationCircleIcon className="mr-2 h-5 w-5 flex-shrink-0" />
|
|
|
|
{t('swap:insufficient-balance', {
|
|
|
|
symbol: inputSymbol,
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
) : loadingSwapDetails ? (
|
|
|
|
<Loading />
|
|
|
|
) : disabled ? (
|
|
|
|
<div className="flex items-center">
|
|
|
|
<ExclamationCircleIcon className="mr-2 h-5 w-5 flex-shrink-0" />
|
|
|
|
No routes found
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="flex items-center">
|
|
|
|
<MagnifyingGlassIcon className="mr-2 h-5 w-5" />
|
|
|
|
{t('swap:review-swap')}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
) : (
|
|
|
|
<div className="flex items-center">
|
2022-10-22 05:14:25 -07:00
|
|
|
<LinkIcon className="mr-2 h-5 w-5" />
|
2022-09-22 14:35:07 -07:00
|
|
|
{t('connect')}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
)
|
|
|
|
}
|