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,
|
2023-01-03 19:12:30 -08:00
|
|
|
PencilIcon,
|
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'
|
2023-01-03 19:12:30 -08: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'
|
2023-01-12 13:22:46 -08:00
|
|
|
import useQuoteRoutes from './useQuoteRoutes'
|
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-12-19 13:58:22 -08:00
|
|
|
import useIpAddress from 'hooks/useIpAddress'
|
2023-01-03 03:06:37 -08:00
|
|
|
import { useEnhancedWallet } from '@components/wallet/EnhancedWalletProvider'
|
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'
|
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-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')
|
2022-12-19 13:58:22 -08:00
|
|
|
const { ipAllowed, ipCountry } = useIpAddress()
|
2023-03-04 11:17:34 -08:00
|
|
|
const { isUnownedAccount } = 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)
|
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)
|
2023-03-04 11:17:34 -08:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
|
|
|
const { isDelegatedAccount } = useUnownedAccount()
|
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(() => {
|
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])
|
|
|
|
|
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,
|
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,
|
2023-01-12 13:22:46 -08:00
|
|
|
wallet: publicKey?.toBase58(),
|
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(() => {
|
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
|
|
|
},
|
2022-12-08 10:44:19 -08: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
|
|
|
},
|
2022-12-08 10:44:19 -08: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
|
|
|
}, [])
|
|
|
|
|
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-08 10:44:19 -08:00
|
|
|
}, [setAmountInFormValue, 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(() => {
|
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
|
|
|
|
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
|
2022-12-20 04:04:08 -08:00
|
|
|
className="absolute top-0 left-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"
|
|
|
|
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
|
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">
|
|
|
|
<div className="col-span-1 rounded-lg rounded-r-none border border-r-0 border-th-input-border bg-th-input-bkg">
|
|
|
|
<TokenSelect
|
|
|
|
bank={
|
|
|
|
inputBank ||
|
|
|
|
group?.banksMapByName.get(INPUT_TOKEN_DEFAULT)?.[0]
|
|
|
|
}
|
|
|
|
showTokenList={setShowTokenSelect}
|
|
|
|
type="input"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-span-1 flex h-[54px]">
|
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"
|
|
|
|
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-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
|
|
|
/>
|
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
|
|
|
|
className="rounded-full border border-th-bkg-4 p-1.5 text-th-fgd-3 md:hover:text-th-active"
|
|
|
|
onClick={handleSwitchTokens}
|
|
|
|
>
|
|
|
|
<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>
|
2022-12-29 02:59:56 -08:00
|
|
|
<p className="mb-2 text-th-fgd-2 lg:text-base">{t('swap:receive')}</p>
|
|
|
|
<div id="swap-step-three" className="mb-3 grid grid-cols-2">
|
|
|
|
<div className="col-span-1 rounded-lg rounded-r-none border border-r-0 border-th-input-border bg-th-input-bkg">
|
|
|
|
<TokenSelect
|
|
|
|
bank={
|
|
|
|
outputBank ||
|
|
|
|
group?.banksMapByName.get(OUTPUT_TOKEN_DEFAULT)?.[0]
|
|
|
|
}
|
|
|
|
showTokenList={setShowTokenSelect}
|
|
|
|
type="output"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<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">
|
|
|
|
{loadingSwapDetails ? (
|
|
|
|
<div className="w-full">
|
|
|
|
<SheenLoader className="flex flex-1 rounded-l-none">
|
|
|
|
<div className="h-[52px] w-full rounded-r-lg bg-th-bkg-4" />
|
|
|
|
</SheenLoader>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<NumberFormat
|
|
|
|
inputMode="decimal"
|
|
|
|
thousandSeparator=","
|
|
|
|
allowNegative={false}
|
|
|
|
isNumericString={true}
|
|
|
|
decimalScale={outputBank?.mintDecimals || 6}
|
|
|
|
name="amountOut"
|
|
|
|
id="amountOut"
|
|
|
|
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"
|
|
|
|
placeholder="0.00"
|
|
|
|
value={amountOutFormValue}
|
|
|
|
onValueChange={handleAmountOutChange}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{swapFormSizeUi === 'slider' ? (
|
|
|
|
<SwapSlider
|
|
|
|
useMargin={useMargin}
|
|
|
|
amount={amountInAsDecimal.toNumber()}
|
|
|
|
onChange={(v) => setAmountInFormValue(v, true)}
|
|
|
|
step={1 / 10 ** (inputBank?.mintDecimals || 6)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<PercentageSelectButtons
|
|
|
|
amountIn={amountInAsDecimal.toString()}
|
|
|
|
setAmountIn={(v) => setAmountInFormValue(v, true)}
|
|
|
|
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}
|
|
|
|
amountOut={
|
|
|
|
selectedRoute ? amountOutAsDecimal.toNumber() : undefined
|
|
|
|
}
|
2023-03-04 10:42:46 -08:00
|
|
|
isDelegatedAccount={isDelegatedAccount}
|
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-02-08 16:31:53 -08:00
|
|
|
{inputBank && inputBank.reduceOnly ? (
|
|
|
|
<div className="pb-4">
|
|
|
|
<InlineNotification
|
|
|
|
type="warning"
|
|
|
|
desc={t('swap:input-reduce-only-warning', {
|
|
|
|
symbol: inputBank.name,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
{outputBank && outputBank.reduceOnly ? (
|
|
|
|
<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-01-03 19:12:30 -08:00
|
|
|
<p className="text-sm text-th-fgd-3">{t('swap:max-slippage')}</p>
|
|
|
|
<div className="flex items-center space-x-1">
|
|
|
|
<p className="text-right font-mono text-sm text-th-fgd-2">
|
|
|
|
{slippage}%
|
|
|
|
</p>
|
|
|
|
<IconButton
|
|
|
|
className="text-th-fgd-3"
|
|
|
|
hideBg
|
|
|
|
onClick={() => setShowSettings(true)}
|
|
|
|
>
|
2023-01-11 13:34:59 -08:00
|
|
|
<PencilIcon className="ml-2 h-4 w-4" />
|
2023-01-03 19:12:30 -08:00
|
|
|
</IconButton>
|
|
|
|
</div>
|
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,
|
|
|
|
amountOut,
|
|
|
|
inputSymbol,
|
|
|
|
loadingSwapDetails,
|
2023-01-10 16:33:03 -08:00
|
|
|
selectedRoute,
|
2022-09-22 14:35:07 -07:00
|
|
|
setShowConfirm,
|
|
|
|
useMargin,
|
2023-03-04 10:42:46 -08:00
|
|
|
isDelegatedAccount,
|
2022-09-22 14:35:07 -07:00
|
|
|
}: {
|
|
|
|
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
|
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
|
2023-03-04 10:42:46 -08:00
|
|
|
isDelegatedAccount: boolean
|
2022-09-22 14:35:07 -07:00
|
|
|
}) => {
|
|
|
|
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)
|
2023-01-03 03:06:37 -08:00
|
|
|
const { handleConnect } = useEnhancedWallet()
|
2022-09-22 14:35:07 -07:00
|
|
|
|
|
|
|
const showInsufficientBalance = useMargin
|
|
|
|
? amountWithBorrow.lt(amountIn)
|
|
|
|
: tokenMax.lt(amountIn)
|
|
|
|
|
|
|
|
const disabled =
|
2023-01-10 16:33:03 -08:00
|
|
|
connected &&
|
|
|
|
(!amountIn.toNumber() ||
|
|
|
|
showInsufficientBalance ||
|
|
|
|
!amountOut ||
|
|
|
|
!selectedRoute)
|
2023-01-03 03:06:37 -08:00
|
|
|
|
|
|
|
const onClick = connected ? () => setShowConfirm(true) : handleConnect
|
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"
|
|
|
|
disabled={disabled}
|
|
|
|
size="large"
|
|
|
|
>
|
2023-03-04 10:42:46 -08:00
|
|
|
{isDelegatedAccount ? (
|
|
|
|
<div>Swap Unavailable for Delegates</div>
|
|
|
|
) : 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
|
|
|
)
|
|
|
|
}
|