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,
|
2023-01-03 19:12:30 -08:00
|
|
|
|
Cog8ToothIcon,
|
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-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'
|
2023-06-01 17:55:24 -07:00
|
|
|
|
import Button, { IconButton, LinkButton } 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'
|
2023-01-12 13:22:46 -08:00
|
|
|
|
import useQuoteRoutes from './useQuoteRoutes'
|
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,
|
2023-07-26 17:37:35 -07:00
|
|
|
|
SWAP_MARGIN_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-12-19 13:58:22 -08:00
|
|
|
|
import useIpAddress from 'hooks/useIpAddress'
|
2023-01-03 19:12:30 -08:00
|
|
|
|
import SwapSettings from './SwapSettings'
|
2023-02-04 13:55:42 -08:00
|
|
|
|
import InlineNotification from '@components/shared/InlineNotification'
|
2023-02-12 18:46:41 -08:00
|
|
|
|
import useUnownedAccount from 'hooks/useUnownedAccount'
|
2023-06-01 17:55:24 -07:00
|
|
|
|
import Tooltip from '@components/shared/Tooltip'
|
2023-06-15 21:08:45 -07:00
|
|
|
|
import { formatCurrencyValue } from 'utils/numbers'
|
2023-07-04 22:38:14 -07:00
|
|
|
|
import Switch from '@components/forms/Switch'
|
2023-07-17 22:22:13 -07:00
|
|
|
|
import MaxAmountButton from '@components/shared/MaxAmountButton'
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 05:22:29 -07:00
|
|
|
|
const NUMBER_FORMAT_CLASSNAMES =
|
2023-06-15 21:08:45 -07:00
|
|
|
|
'w-full rounded-r-lg border h-[56px] border-th-input-border bg-th-input-bkg px-3 pb-4 border-box text-right font-mono text-xl text-th-fgd-1 focus:border-th-fgd-4 focus:outline-none md:hover:border-th-input-border-hover md:hover:focus-visible:border-th-fgd-4'
|
2023-03-24 05:22:29 -07:00
|
|
|
|
|
2022-12-08 10:44:19 -08:00
|
|
|
|
const set = mangoStore.getState().set
|
|
|
|
|
|
2022-08-19 21:03:26 -07:00
|
|
|
|
const SwapForm = () => {
|
2022-12-08 03:15:16 -08:00
|
|
|
|
const { t } = useTranslation(['common', 'swap', 'trade'])
|
2023-02-04 13:55:42 -08:00
|
|
|
|
//initial state is undefined null is returned on error
|
|
|
|
|
const [selectedRoute, setSelectedRoute] = useState<RouteInfo | null>()
|
2022-07-22 08:40:53 -07:00
|
|
|
|
const [animateSwitchArrow, setAnimateSwitchArrow] = useState(0)
|
2023-02-27 23:20:11 -08:00
|
|
|
|
const [showTokenSelect, setShowTokenSelect] = useState<'input' | 'output'>()
|
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()
|
2023-01-19 12:41:11 -08:00
|
|
|
|
const [swapFormSizeUi] = useLocalStorageState(SIZE_INPUT_UI_KEY, 'slider')
|
2023-07-26 17:37:35 -07:00
|
|
|
|
const [, setSavedSwapMargin] = useLocalStorageState<boolean>(
|
|
|
|
|
SWAP_MARGIN_KEY,
|
|
|
|
|
true,
|
|
|
|
|
)
|
2022-12-19 13:58:22 -08:00
|
|
|
|
const { ipAllowed, ipCountry } = useIpAddress()
|
2023-06-28 10:19:02 -07:00
|
|
|
|
const { isUnownedAccount, isDelegatedAccount } = useUnownedAccount()
|
2022-10-07 05:22:18 -07:00
|
|
|
|
|
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)
|
2023-03-04 11:17:34 -08:00
|
|
|
|
const { mangoAccount } = useMangoAccount()
|
2023-01-12 13:22:46 -08:00
|
|
|
|
const { connected, publicKey } = useWallet()
|
2022-09-14 17:02:16 -07:00
|
|
|
|
|
2022-12-07 13:33:35 -08:00
|
|
|
|
const amountInAsDecimal: Decimal | null = useMemo(() => {
|
2023-07-16 20:41:13 -07:00
|
|
|
|
return Number(amountInFormValue)
|
|
|
|
|
? new Decimal(amountInFormValue)
|
2022-09-01 10:33:29 -07:00
|
|
|
|
: new Decimal(0)
|
2023-07-16 20:41:13 -07:00
|
|
|
|
}, [amountInFormValue])
|
2022-09-01 10:33:29 -07:00
|
|
|
|
|
2022-12-07 13:33:35 -08:00
|
|
|
|
const amountOutAsDecimal: Decimal | null = useMemo(() => {
|
2023-07-16 20:41:13 -07:00
|
|
|
|
return Number(amountOutFormValue)
|
|
|
|
|
? new Decimal(amountOutFormValue)
|
2022-12-07 13:33:35 -08:00
|
|
|
|
: new Decimal(0)
|
2023-07-16 20:41:13 -07:00
|
|
|
|
}, [amountOutFormValue])
|
2022-12-07 13:33:35 -08:00
|
|
|
|
|
2023-01-12 13:22:46 -08:00
|
|
|
|
const { bestRoute, routes } = useQuoteRoutes({
|
2022-11-18 11:11:06 -08:00
|
|
|
|
inputMint: inputBank?.mint.toString() || USDC_MINT,
|
|
|
|
|
outputMint: outputBank?.mint.toString() || MANGO_MINT,
|
2023-07-16 20:41:13 -07:00
|
|
|
|
amount: swapMode === 'ExactIn' ? amountInFormValue : amountOutFormValue,
|
2022-08-03 14:46:37 -07:00
|
|
|
|
slippage,
|
2022-12-07 13:33:35 -08:00
|
|
|
|
swapMode,
|
2023-01-12 13:22:46 -08:00
|
|
|
|
wallet: publicKey?.toBase58(),
|
2023-06-28 10:19:02 -07:00
|
|
|
|
mode: isDelegatedAccount ? 'JUPITER' : 'ALL',
|
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'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
2023-07-21 11:47:53 -07:00
|
|
|
|
[],
|
2022-12-07 17:14:29 -08:00
|
|
|
|
)
|
2022-12-07 13:33:35 -08:00
|
|
|
|
|
2023-07-16 20:41:13 -07:00
|
|
|
|
const setAmountFromSlider = useCallback(
|
|
|
|
|
(amount: string) => {
|
|
|
|
|
setAmountInFormValue(amount, true)
|
|
|
|
|
},
|
2023-07-21 11:47:53 -07:00
|
|
|
|
[setAmountInFormValue],
|
2023-07-16 20:41:13 -07:00
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
2023-07-17 22:22:13 -07:00
|
|
|
|
const setBorrowAmountOut = useCallback(
|
|
|
|
|
(borrowAmount: string) => {
|
|
|
|
|
if (swapMode === 'ExactIn') {
|
|
|
|
|
set((s) => {
|
|
|
|
|
s.swap.swapMode = 'ExactOut'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
setAmountOutFormValue(borrowAmount.toString())
|
|
|
|
|
},
|
2023-07-21 11:47:53 -07:00
|
|
|
|
[setAmountOutFormValue],
|
2023-07-17 22:22:13 -07: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(() => {
|
2023-02-04 13:55:42 -08:00
|
|
|
|
if (typeof bestRoute !== 'undefined') {
|
2022-11-18 11:11:06 -08:00
|
|
|
|
setSelectedRoute(bestRoute)
|
2022-12-07 13:33:35 -08:00
|
|
|
|
|
2023-02-04 13:55:42 -08:00
|
|
|
|
if (inputBank && swapMode === 'ExactOut' && bestRoute) {
|
|
|
|
|
const inAmount = new Decimal(bestRoute!.inAmount)
|
2022-12-07 13:33:35 -08:00
|
|
|
|
.div(10 ** inputBank.mintDecimals)
|
|
|
|
|
.toString()
|
|
|
|
|
setAmountInFormValue(inAmount)
|
2023-02-04 13:55:42 -08:00
|
|
|
|
} else if (outputBank && swapMode === 'ExactIn' && bestRoute) {
|
|
|
|
|
const outAmount = new Decimal(bestRoute!.outAmount)
|
2022-12-07 13:33:35 -08:00
|
|
|
|
.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-12-08 10:44:19 -08:00
|
|
|
|
}, [useMargin, setAmountInFormValue, setAmountOutFormValue])
|
2022-08-25 14:24:10 -07:00
|
|
|
|
|
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
|
|
|
|
},
|
2023-07-21 11:47:53 -07:00
|
|
|
|
[swapMode, setAmountInFormValue],
|
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
|
|
|
|
},
|
2023-07-21 11:47:53 -07:00
|
|
|
|
[swapMode, setAmountOutFormValue],
|
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
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-02-08 16:31:53 -08:00
|
|
|
|
setShowTokenSelect(undefined)
|
2022-12-07 13:33:35 -08:00
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-02-08 16:31:53 -08:00
|
|
|
|
setShowTokenSelect(undefined)
|
2022-12-07 13:33:35 -08:00
|
|
|
|
}, [])
|
|
|
|
|
|
2023-07-16 20:41:13 -07:00
|
|
|
|
const handleSwitchTokens = useCallback(
|
|
|
|
|
(amountIn: Decimal, amountOut: Decimal) => {
|
|
|
|
|
if (amountIn?.gt(0) && amountOut.gte(0)) {
|
|
|
|
|
setAmountInFormValue(amountOut.toString())
|
|
|
|
|
}
|
|
|
|
|
const inputBank = mangoStore.getState().swap.inputBank
|
|
|
|
|
const outputBank = mangoStore.getState().swap.outputBank
|
|
|
|
|
set((s) => {
|
|
|
|
|
s.swap.inputBank = outputBank
|
|
|
|
|
s.swap.outputBank = inputBank
|
|
|
|
|
})
|
|
|
|
|
setAnimateSwitchArrow(
|
2023-07-21 11:47:53 -07:00
|
|
|
|
(prevanimateSwitchArrow) => prevanimateSwitchArrow + 1,
|
2023-07-16 20:41:13 -07:00
|
|
|
|
)
|
|
|
|
|
},
|
2023-07-21 11:47:53 -07:00
|
|
|
|
[setAmountInFormValue],
|
2023-07-16 20:41:13 -07:00
|
|
|
|
)
|
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
|
|
|
|
},
|
|
|
|
|
],
|
2023-07-21 11:47:53 -07:00
|
|
|
|
HealthType.maint,
|
2022-08-25 06:00:42 -07:00
|
|
|
|
)
|
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
|
|
|
|
|
2023-07-17 22:22:13 -07:00
|
|
|
|
const outputTokenBalanceBorrow = useMemo(() => {
|
|
|
|
|
if (!outputBank) return 0
|
|
|
|
|
const balance = mangoAccount?.getTokenBalanceUi(outputBank)
|
|
|
|
|
return balance && balance < 0 ? Math.abs(balance) : 0
|
|
|
|
|
}, [outputBank])
|
|
|
|
|
|
2022-09-22 14:35:07 -07:00
|
|
|
|
const loadingSwapDetails: boolean = useMemo(() => {
|
2023-01-09 02:01:21 -08:00
|
|
|
|
return (
|
|
|
|
|
!!(amountInAsDecimal.toNumber() || amountOutAsDecimal.toNumber()) &&
|
|
|
|
|
connected &&
|
2023-02-04 13:55:42 -08:00
|
|
|
|
typeof selectedRoute === 'undefined'
|
2023-01-09 02:01:21 -08:00
|
|
|
|
)
|
|
|
|
|
}, [amountInAsDecimal, amountOutAsDecimal, connected, selectedRoute])
|
2022-08-25 12:44:02 -07:00
|
|
|
|
|
2023-07-04 22:38:14 -07:00
|
|
|
|
const handleSetMargin = () => {
|
|
|
|
|
set((s) => {
|
|
|
|
|
s.swap.margin = !s.swap.margin
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 17:37:35 -07:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
setSavedSwapMargin(useMargin)
|
|
|
|
|
}, [useMargin])
|
|
|
|
|
|
2022-07-05 20:37:49 -07:00
|
|
|
|
return (
|
2022-09-14 14:57:12 -07:00
|
|
|
|
<ContentBox
|
|
|
|
|
hidePadding
|
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
|
|
|
|
>
|
2023-01-03 19:12:30 -08:00
|
|
|
|
<div>
|
2022-08-25 06:00:42 -07:00
|
|
|
|
<Transition
|
2023-04-20 23:10:09 -07:00
|
|
|
|
className="absolute top-0 right-0 z-10 h-full w-full bg-th-bkg-1 pb-0"
|
2022-08-25 06:00:42 -07:00
|
|
|
|
show={showConfirm}
|
|
|
|
|
enter="transition ease-in duration-300"
|
2023-04-20 23:10:09 -07:00
|
|
|
|
enterFrom="-translate-x-full"
|
2022-08-25 06:00:42 -07:00
|
|
|
|
enterTo="translate-x-0"
|
|
|
|
|
leave="transition ease-out duration-300"
|
|
|
|
|
leaveFrom="translate-x-0"
|
2023-04-20 23:10:09 -07:00
|
|
|
|
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
|
2023-07-19 20:19:03 -07:00
|
|
|
|
className="thin-scroll absolute bottom-0 left-0 z-10 h-full w-full overflow-hidden bg-th-bkg-1 p-6 pb-0"
|
2022-08-25 06:00:42 -07:00
|
|
|
|
show={!!showTokenSelect}
|
|
|
|
|
>
|
|
|
|
|
<SwapFormTokenList
|
2023-02-08 16:31:53 -08:00
|
|
|
|
onClose={() => setShowTokenSelect(undefined)}
|
2022-08-25 06:00:42 -07:00
|
|
|
|
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}
|
|
|
|
|
>
|
2023-01-03 19:12:30 -08:00
|
|
|
|
<SwapSettings onClose={() => setShowSettings(false)} />
|
2022-08-25 12:44:02 -07:00
|
|
|
|
</EnterBottomExitBottom>
|
2023-01-03 19:12:30 -08:00
|
|
|
|
<div className="relative p-6 pt-10">
|
2023-01-11 13:34:59 -08:00
|
|
|
|
<div className="absolute right-4 top-4">
|
2023-01-03 19:12:30 -08:00
|
|
|
|
<IconButton
|
|
|
|
|
className="text-th-fgd-3"
|
|
|
|
|
hideBg
|
|
|
|
|
onClick={() => setShowSettings(true)}
|
|
|
|
|
>
|
|
|
|
|
<Cog8ToothIcon className="h-5 w-5" />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</div>
|
2022-12-29 02:59:56 -08:00
|
|
|
|
<div className="mb-2 flex items-end justify-between">
|
|
|
|
|
<p className="text-th-fgd-2 lg:text-base">{t('swap:pay')}</p>
|
2023-02-12 18:46:41 -08:00
|
|
|
|
{!isUnownedAccount ? (
|
|
|
|
|
<MaxSwapAmount
|
|
|
|
|
useMargin={useMargin}
|
|
|
|
|
setAmountIn={(v) => setAmountInFormValue(v, true)}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
2022-08-25 06:00:42 -07:00
|
|
|
|
</div>
|
2022-12-29 02:59:56 -08:00
|
|
|
|
<div className="mb-3 grid grid-cols-2" id="swap-step-two">
|
2023-03-24 05:22:29 -07:00
|
|
|
|
<div className="col-span-1">
|
2022-12-29 02:59:56 -08:00
|
|
|
|
<TokenSelect
|
|
|
|
|
bank={
|
|
|
|
|
inputBank ||
|
|
|
|
|
group?.banksMapByName.get(INPUT_TOKEN_DEFAULT)?.[0]
|
|
|
|
|
}
|
|
|
|
|
showTokenList={setShowTokenSelect}
|
|
|
|
|
type="input"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2023-06-15 21:08:45 -07:00
|
|
|
|
<div className="relative col-span-1">
|
2022-10-30 22:00:21 -07:00
|
|
|
|
<NumberFormat
|
|
|
|
|
inputMode="decimal"
|
|
|
|
|
thousandSeparator=","
|
|
|
|
|
allowNegative={false}
|
|
|
|
|
isNumericString={true}
|
2022-12-29 02:59:56 -08:00
|
|
|
|
decimalScale={inputBank?.mintDecimals || 6}
|
|
|
|
|
name="amountIn"
|
|
|
|
|
id="amountIn"
|
2023-03-24 05:22:29 -07:00
|
|
|
|
className={NUMBER_FORMAT_CLASSNAMES}
|
2022-10-30 22:00:21 -07:00
|
|
|
|
placeholder="0.00"
|
2022-12-29 02:59:56 -08:00
|
|
|
|
value={amountInFormValue}
|
|
|
|
|
onValueChange={handleAmountInChange}
|
|
|
|
|
isAllowed={withValueLimit}
|
2022-10-30 22:00:21 -07:00
|
|
|
|
/>
|
2023-06-15 21:08:45 -07:00
|
|
|
|
<span className="absolute right-3 bottom-1.5 text-xxs text-th-fgd-4">
|
|
|
|
|
{inputBank
|
|
|
|
|
? formatCurrencyValue(
|
2023-07-21 11:47:53 -07:00
|
|
|
|
inputBank.uiPrice * Number(amountInFormValue),
|
2023-06-15 21:08:45 -07:00
|
|
|
|
)
|
|
|
|
|
: '–'}
|
|
|
|
|
</span>
|
2022-12-19 13:58:22 -08:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2022-12-29 02:59:56 -08:00
|
|
|
|
<div className="-mb-2 flex justify-center">
|
|
|
|
|
<button
|
2023-04-19 17:43:33 -07:00
|
|
|
|
className="rounded-full border border-th-bkg-4 p-1.5 text-th-fgd-3 focus-visible:border-th-fgd-4 md:hover:text-th-active"
|
2023-07-16 20:41:13 -07:00
|
|
|
|
onClick={() =>
|
|
|
|
|
handleSwitchTokens(amountInAsDecimal, amountOutAsDecimal)
|
|
|
|
|
}
|
2022-12-29 02:59:56 -08:00
|
|
|
|
>
|
|
|
|
|
<ArrowDownIcon
|
|
|
|
|
className="h-5 w-5"
|
|
|
|
|
style={
|
|
|
|
|
animateSwitchArrow % 2 == 0
|
|
|
|
|
? { transform: 'rotate(0deg)' }
|
|
|
|
|
: { transform: 'rotate(360deg)' }
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
2022-12-07 14:44:39 -08:00
|
|
|
|
</div>
|
2023-07-17 22:22:13 -07:00
|
|
|
|
<div className="mb-2 flex items-end justify-between">
|
|
|
|
|
<p className="text-th-fgd-2 lg:text-base">{t('swap:receive')}</p>
|
|
|
|
|
{outputTokenBalanceBorrow ? (
|
|
|
|
|
<MaxAmountButton
|
|
|
|
|
className="mb-0.5 text-xs"
|
|
|
|
|
decimals={outputBank?.mintDecimals || 9}
|
|
|
|
|
label={t('repay')}
|
|
|
|
|
onClick={() =>
|
|
|
|
|
setBorrowAmountOut(
|
|
|
|
|
outputTokenBalanceBorrow.toFixed(
|
2023-07-21 11:47:53 -07:00
|
|
|
|
outputBank?.mintDecimals || 9,
|
|
|
|
|
),
|
2023-07-17 22:22:13 -07:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
value={outputTokenBalanceBorrow}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
2022-12-29 02:59:56 -08:00
|
|
|
|
<div id="swap-step-three" className="mb-3 grid grid-cols-2">
|
2023-03-24 05:22:29 -07:00
|
|
|
|
<div className="col-span-1">
|
2022-12-29 02:59:56 -08:00
|
|
|
|
<TokenSelect
|
|
|
|
|
bank={
|
|
|
|
|
outputBank ||
|
|
|
|
|
group?.banksMapByName.get(OUTPUT_TOKEN_DEFAULT)?.[0]
|
|
|
|
|
}
|
|
|
|
|
showTokenList={setShowTokenSelect}
|
|
|
|
|
type="output"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2023-06-15 21:08:45 -07:00
|
|
|
|
<div className="relative col-span-1">
|
2022-12-29 02:59:56 -08:00
|
|
|
|
{loadingSwapDetails ? (
|
2023-06-15 21:08:45 -07:00
|
|
|
|
<div className="flex h-[56px] w-full items-center justify-center rounded-l-none rounded-r-lg border border-th-input-border bg-th-bkg-2">
|
2023-03-24 05:22:29 -07:00
|
|
|
|
<Loading />
|
2022-12-29 02:59:56 -08:00
|
|
|
|
</div>
|
|
|
|
|
) : (
|
2023-06-15 21:08:45 -07:00
|
|
|
|
<>
|
|
|
|
|
<NumberFormat
|
|
|
|
|
inputMode="decimal"
|
|
|
|
|
thousandSeparator=","
|
|
|
|
|
allowNegative={false}
|
|
|
|
|
isNumericString={true}
|
|
|
|
|
decimalScale={outputBank?.mintDecimals || 6}
|
|
|
|
|
name="amountOut"
|
|
|
|
|
id="amountOut"
|
|
|
|
|
className={NUMBER_FORMAT_CLASSNAMES}
|
|
|
|
|
placeholder="0.00"
|
|
|
|
|
value={amountOutFormValue}
|
|
|
|
|
onValueChange={handleAmountOutChange}
|
|
|
|
|
/>
|
|
|
|
|
<span className="absolute right-3 bottom-1.5 text-xxs text-th-fgd-4">
|
|
|
|
|
{outputBank
|
|
|
|
|
? formatCurrencyValue(
|
2023-07-21 11:47:53 -07:00
|
|
|
|
outputBank.uiPrice * Number(amountOutFormValue),
|
2023-06-15 21:08:45 -07:00
|
|
|
|
)
|
|
|
|
|
: '–'}
|
|
|
|
|
</span>
|
|
|
|
|
</>
|
2022-12-29 02:59:56 -08:00
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{swapFormSizeUi === 'slider' ? (
|
|
|
|
|
<SwapSlider
|
|
|
|
|
useMargin={useMargin}
|
|
|
|
|
amount={amountInAsDecimal.toNumber()}
|
2023-07-16 20:41:13 -07:00
|
|
|
|
onChange={setAmountFromSlider}
|
2022-12-29 02:59:56 -08:00
|
|
|
|
step={1 / 10 ** (inputBank?.mintDecimals || 6)}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<PercentageSelectButtons
|
|
|
|
|
amountIn={amountInAsDecimal.toString()}
|
2023-07-16 20:41:13 -07:00
|
|
|
|
setAmountIn={setAmountFromSlider}
|
2022-12-29 02:59:56 -08:00
|
|
|
|
useMargin={useMargin}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{ipAllowed ? (
|
|
|
|
|
<SwapFormSubmitButton
|
|
|
|
|
loadingSwapDetails={loadingSwapDetails}
|
|
|
|
|
useMargin={useMargin}
|
2023-01-10 16:33:03 -08:00
|
|
|
|
selectedRoute={selectedRoute}
|
2022-12-29 02:59:56 -08:00
|
|
|
|
setShowConfirm={setShowConfirm}
|
|
|
|
|
amountIn={amountInAsDecimal}
|
|
|
|
|
inputSymbol={inputBank?.name}
|
2023-04-18 23:49:16 -07:00
|
|
|
|
amountOut={
|
|
|
|
|
selectedRoute ? amountOutAsDecimal.toNumber() : undefined
|
|
|
|
|
}
|
2022-12-29 02:59:56 -08:00
|
|
|
|
/>
|
|
|
|
|
) : (
|
2023-01-06 02:55:51 -08:00
|
|
|
|
<Button
|
|
|
|
|
disabled
|
|
|
|
|
className="mt-6 mb-4 w-full leading-tight"
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
|
|
|
|
{t('country-not-allowed', {
|
|
|
|
|
country: ipCountry ? `(${ipCountry})` : '',
|
|
|
|
|
})}
|
|
|
|
|
</Button>
|
2022-12-29 02:59:56 -08:00
|
|
|
|
)}
|
2023-01-09 15:47:18 -08:00
|
|
|
|
{group && inputBank ? (
|
|
|
|
|
<TokenVaultWarnings bank={inputBank} type="swap" />
|
|
|
|
|
) : null}
|
2023-05-11 19:25:16 -07:00
|
|
|
|
{inputBank &&
|
|
|
|
|
inputBank.areBorrowsReduceOnly() &&
|
|
|
|
|
inputBank.areDepositsReduceOnly() ? (
|
2023-02-08 16:31:53 -08:00
|
|
|
|
<div className="pb-4">
|
|
|
|
|
<InlineNotification
|
|
|
|
|
type="warning"
|
|
|
|
|
desc={t('swap:input-reduce-only-warning', {
|
|
|
|
|
symbol: inputBank.name,
|
|
|
|
|
})}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2023-05-11 19:25:16 -07:00
|
|
|
|
{outputBank &&
|
|
|
|
|
outputBank.areBorrowsReduceOnly() &&
|
|
|
|
|
outputBank.areDepositsReduceOnly() ? (
|
2023-02-08 16:31:53 -08:00
|
|
|
|
<div className="pb-4">
|
|
|
|
|
<InlineNotification
|
|
|
|
|
type="warning"
|
|
|
|
|
desc={t('swap:output-reduce-only-warning', {
|
|
|
|
|
symbol: outputBank.name,
|
|
|
|
|
})}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2022-12-29 02:59:56 -08:00
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<div id="swap-step-four">
|
|
|
|
|
<HealthImpact maintProjectedHealth={maintProjectedHealth} />
|
|
|
|
|
</div>
|
2023-01-11 13:34:59 -08:00
|
|
|
|
<div className="flex items-center justify-between">
|
2023-06-01 17:55:24 -07:00
|
|
|
|
<Tooltip content={t('swap:tooltip-margin')}>
|
|
|
|
|
<p className="tooltip-underline text-sm text-th-fgd-3">
|
|
|
|
|
{t('swap:margin')}
|
2023-01-03 19:12:30 -08:00
|
|
|
|
</p>
|
2023-06-01 17:55:24 -07:00
|
|
|
|
</Tooltip>
|
2023-07-04 22:38:14 -07:00
|
|
|
|
<Switch
|
|
|
|
|
className="text-th-fgd-3"
|
|
|
|
|
checked={useMargin}
|
|
|
|
|
onChange={handleSetMargin}
|
2023-07-20 03:40:27 -07:00
|
|
|
|
small
|
2023-07-04 22:38:14 -07:00
|
|
|
|
/>
|
2023-06-01 17:55:24 -07:00
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<p className="text-sm text-th-fgd-3">{t('swap:max-slippage')}</p>
|
|
|
|
|
<LinkButton
|
|
|
|
|
className="text-right font-mono text-sm font-normal text-th-fgd-2 underline underline-offset-2 md:hover:no-underline"
|
|
|
|
|
onClick={() => setShowSettings(true)}
|
|
|
|
|
>
|
|
|
|
|
{slippage}%
|
|
|
|
|
</LinkButton>
|
2022-12-28 20:17:38 -08:00
|
|
|
|
</div>
|
2022-12-07 13:33:35 -08:00
|
|
|
|
</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,
|
2023-04-18 23:49:16 -07:00
|
|
|
|
amountOut,
|
2022-09-22 14:35:07 -07:00
|
|
|
|
inputSymbol,
|
|
|
|
|
loadingSwapDetails,
|
2023-01-10 16:33:03 -08:00
|
|
|
|
selectedRoute,
|
2022-09-22 14:35:07 -07:00
|
|
|
|
setShowConfirm,
|
|
|
|
|
useMargin,
|
|
|
|
|
}: {
|
|
|
|
|
amountIn: Decimal
|
2023-04-18 23:49:16 -07:00
|
|
|
|
amountOut: number | undefined
|
2022-09-22 14:35:07 -07:00
|
|
|
|
inputSymbol: string | undefined
|
|
|
|
|
loadingSwapDetails: boolean
|
2023-02-04 13:55:42 -08:00
|
|
|
|
selectedRoute: RouteInfo | undefined | null
|
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')
|
2023-07-20 16:07:59 -07:00
|
|
|
|
const { connected, connect } = useWallet()
|
2022-09-22 14:35:07 -07:00
|
|
|
|
const { amount: tokenMax, amountWithBorrow } = useTokenMax(useMargin)
|
|
|
|
|
|
|
|
|
|
const showInsufficientBalance = useMargin
|
|
|
|
|
? amountWithBorrow.lt(amountIn)
|
|
|
|
|
: tokenMax.lt(amountIn)
|
|
|
|
|
|
2023-04-18 23:49:16 -07:00
|
|
|
|
const disabled =
|
|
|
|
|
connected &&
|
|
|
|
|
(!amountIn.toNumber() ||
|
|
|
|
|
showInsufficientBalance ||
|
|
|
|
|
!amountOut ||
|
2023-06-28 10:19:02 -07:00
|
|
|
|
!selectedRoute)
|
2023-01-03 03:06:37 -08:00
|
|
|
|
|
2023-07-20 16:07:59 -07:00
|
|
|
|
const onClick = connected ? () => setShowConfirm(true) : connect
|
2022-09-22 14:35:07 -07:00
|
|
|
|
|
|
|
|
|
return (
|
2023-02-04 13:55:42 -08:00
|
|
|
|
<>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={onClick}
|
|
|
|
|
className="mt-6 mb-4 flex w-full items-center justify-center text-base"
|
2023-04-18 23:49:16 -07:00
|
|
|
|
disabled={disabled}
|
2023-02-04 13:55:42 -08:00
|
|
|
|
size="large"
|
|
|
|
|
>
|
2023-06-28 10:19:02 -07:00
|
|
|
|
{connected ? (
|
2023-02-04 13:55:42 -08:00
|
|
|
|
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 />
|
|
|
|
|
) : (
|
|
|
|
|
<span>{t('swap:review-swap')}</span>
|
|
|
|
|
)
|
|
|
|
|
) : (
|
2022-09-22 14:35:07 -07:00
|
|
|
|
<div className="flex items-center">
|
2023-02-04 13:55:42 -08:00
|
|
|
|
<LinkIcon className="mr-2 h-5 w-5" />
|
|
|
|
|
{t('connect')}
|
2022-09-22 14:35:07 -07:00
|
|
|
|
</div>
|
2023-02-04 13:55:42 -08:00
|
|
|
|
)}
|
|
|
|
|
</Button>
|
2023-02-22 10:49:03 -08:00
|
|
|
|
{selectedRoute === null && amountIn.gt(0) ? (
|
2023-02-04 13:55:42 -08:00
|
|
|
|
<div className="mb-4">
|
|
|
|
|
<InlineNotification type="error" desc={t('swap:no-swap-found')} />
|
2022-09-22 14:35:07 -07:00
|
|
|
|
</div>
|
2023-02-22 10:49:03 -08:00
|
|
|
|
) : null}
|
2023-02-04 13:55:42 -08:00
|
|
|
|
</>
|
2022-09-22 14:35:07 -07:00
|
|
|
|
)
|
|
|
|
|
}
|