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-08 10:42:18 -07:00
|
|
|
import { ArrowDownIcon, CogIcon } from '@heroicons/react/solid'
|
2022-08-03 14:46:37 -07:00
|
|
|
import { RouteInfo } from '@jup-ag/core'
|
2022-08-17 18:26:38 -07:00
|
|
|
import NumberFormat, { NumberFormatValues } from 'react-number-format'
|
|
|
|
import Decimal from 'decimal.js'
|
|
|
|
|
2022-08-20 11:17:57 -07:00
|
|
|
import mangoStore from '../../store/mangoStore'
|
2022-07-05 20:37:49 -07:00
|
|
|
import ContentBox from '../shared/ContentBox'
|
2022-08-17 13:06:25 -07:00
|
|
|
import JupiterRouteInfo from './JupiterRouteInfo'
|
2022-07-05 20:37:49 -07:00
|
|
|
import TokenSelect from '../TokenSelect'
|
|
|
|
import useDebounce from '../shared/useDebounce'
|
2022-08-11 19:27:36 -07:00
|
|
|
import { floorToDecimal, numberFormat } from '../../utils/numbers'
|
2022-08-15 15:18:23 -07:00
|
|
|
import { SwapLeverageSlider } from './LeverageSlider'
|
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-08-12 05:40:26 -07:00
|
|
|
import Button, { IconButton, LinkButton } from '../shared/Button'
|
2022-07-17 04:48:33 -07:00
|
|
|
import ButtonGroup from '../forms/ButtonGroup'
|
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-08-03 14:46:37 -07:00
|
|
|
import useJupiter from './useJupiter'
|
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-17 21:13:50 -07:00
|
|
|
import { toUiDecimals } from '@blockworks-foundation/mango-v4'
|
2022-08-20 11:17:57 -07:00
|
|
|
import {
|
|
|
|
INPUT_TOKEN_DEFAULT,
|
|
|
|
OUTPUT_TOKEN_DEFAULT,
|
|
|
|
} from '../../utils/constants'
|
2022-08-24 03:51:06 -07:00
|
|
|
import InlineNotification from '../shared/InlineNotification'
|
2022-07-18 03:02:43 -07:00
|
|
|
|
2022-08-17 18:26:38 -07:00
|
|
|
const MAX_DIGITS = 11
|
|
|
|
const withValueLimit = (values: NumberFormatValues): boolean => {
|
|
|
|
return values.floatValue
|
|
|
|
? values.floatValue.toFixed(0).length <= MAX_DIGITS
|
|
|
|
: true
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:03:26 -07:00
|
|
|
const SwapForm = () => {
|
2022-07-15 05:50:29 -07:00
|
|
|
const { t } = useTranslation('common')
|
2022-07-18 03:02:43 -07:00
|
|
|
const [selectedRoute, setSelectedRoute] = useState<RouteInfo>()
|
2022-08-17 21:13:50 -07:00
|
|
|
const [amountInFormValue, setAmountInFormValue] = useState('')
|
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-07-22 08:40:53 -07:00
|
|
|
|
2022-07-10 19:01:16 -07:00
|
|
|
const set = mangoStore.getState().set
|
2022-08-09 15:44:01 -07:00
|
|
|
const useMargin = mangoStore((s) => s.swap.margin)
|
|
|
|
const slippage = mangoStore((s) => s.swap.slippage)
|
2022-08-18 13:50:34 -07:00
|
|
|
const inputTokenInfo = mangoStore((s) => s.swap.inputTokenInfo)
|
|
|
|
const outputTokenInfo = mangoStore((s) => s.swap.outputTokenInfo)
|
2022-08-02 11:04:00 -07:00
|
|
|
const jupiterTokens = mangoStore((s) => s.jupiterTokens)
|
2022-07-17 19:49:14 -07:00
|
|
|
const connected = mangoStore((s) => s.connected)
|
2022-08-18 13:50:34 -07:00
|
|
|
const [debouncedAmountIn] = useDebounce(amountInFormValue, 300)
|
2022-08-24 03:51:06 -07:00
|
|
|
const { amount: tokenMax, amountWithBorrow, decimals } = useTokenMax()
|
2022-07-05 20:37:49 -07:00
|
|
|
|
2022-08-18 13:50:34 -07:00
|
|
|
const { amountOut, jupiter, routes } = useJupiter({
|
|
|
|
inputTokenInfo,
|
|
|
|
outputTokenInfo,
|
2022-08-17 13:06:25 -07:00
|
|
|
inputAmount: debouncedAmountIn,
|
2022-08-03 14:46:37 -07:00
|
|
|
slippage,
|
|
|
|
})
|
2022-07-18 03:02:43 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-08-03 14:46:37 -07:00
|
|
|
setSelectedRoute(routes[0])
|
|
|
|
}, [routes])
|
2022-07-18 03:02:43 -07:00
|
|
|
|
2022-08-17 18:26:38 -07:00
|
|
|
const handleAmountInChange = useCallback((e: NumberFormatValues) => {
|
2022-08-17 21:13:50 -07:00
|
|
|
setAmountInFormValue(e.value)
|
2022-08-17 18:26:38 -07:00
|
|
|
}, [])
|
2022-07-05 20:37:49 -07:00
|
|
|
|
2022-08-17 18:26:38 -07:00
|
|
|
const handleTokenInSelect = useCallback(
|
|
|
|
(mintAddress: string) => {
|
|
|
|
const inputTokenInfo = jupiterTokens.find(
|
|
|
|
(t: any) => t.address === mintAddress
|
|
|
|
)
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
if (group) {
|
2022-08-18 13:50:34 -07:00
|
|
|
const bank = group.getFirstBankByMint(new PublicKey(mintAddress))
|
2022-08-17 18:26:38 -07:00
|
|
|
set((s) => {
|
2022-08-18 13:50:34 -07:00
|
|
|
s.swap.inputBank = bank
|
2022-08-17 18:26:38 -07:00
|
|
|
s.swap.inputTokenInfo = inputTokenInfo
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setShowTokenSelect('')
|
|
|
|
},
|
|
|
|
[jupiterTokens, set]
|
|
|
|
)
|
2022-08-10 13:23:19 -07:00
|
|
|
|
2022-08-17 18:26:38 -07:00
|
|
|
const handleTokenOutSelect = useCallback(
|
|
|
|
(mintAddress: string) => {
|
|
|
|
const outputTokenInfo = jupiterTokens.find(
|
|
|
|
(t: any) => t.address === mintAddress
|
|
|
|
)
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
if (group) {
|
2022-08-18 13:50:34 -07:00
|
|
|
const bank = group.getFirstBankByMint(new PublicKey(mintAddress))
|
2022-08-17 18:26:38 -07:00
|
|
|
set((s) => {
|
2022-08-18 13:50:34 -07:00
|
|
|
s.swap.outputBank = bank
|
2022-08-17 18:26:38 -07:00
|
|
|
s.swap.outputTokenInfo = outputTokenInfo
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setShowTokenSelect('')
|
|
|
|
},
|
|
|
|
[jupiterTokens, set]
|
|
|
|
)
|
2022-07-05 20:37:49 -07:00
|
|
|
|
2022-08-17 18:26:38 -07:00
|
|
|
const handleSwitchTokens = useCallback(() => {
|
2022-08-17 21:13:50 -07:00
|
|
|
setAmountInFormValue(amountOut.toString())
|
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-25 22:27:53 -07:00
|
|
|
s.swap.inputTokenInfo = outputTokenInfo
|
|
|
|
s.swap.outputTokenInfo = inputTokenInfo
|
2022-07-11 20:00:22 -07:00
|
|
|
})
|
2022-08-17 18:26:38 -07:00
|
|
|
setAnimateSwitchArrow(
|
|
|
|
(prevanimateSwitchArrow) => prevanimateSwitchArrow + 1
|
|
|
|
)
|
2022-08-18 13:50:34 -07:00
|
|
|
}, [inputTokenInfo, outputTokenInfo, set, amountOut])
|
2022-07-10 19:01:16 -07:00
|
|
|
|
2022-08-17 18:26:38 -07:00
|
|
|
const amountIn: Decimal | null = useMemo(() => {
|
|
|
|
return Number(debouncedAmountIn)
|
|
|
|
? new Decimal(debouncedAmountIn)
|
|
|
|
: new Decimal(0)
|
|
|
|
}, [debouncedAmountIn])
|
|
|
|
|
|
|
|
const isLoadingTradeDetails = useMemo(() => {
|
|
|
|
return (
|
|
|
|
amountIn.toNumber() && connected && (!selectedRoute || !outputTokenInfo)
|
|
|
|
)
|
|
|
|
}, [amountIn, connected, selectedRoute, outputTokenInfo])
|
2022-07-18 03:02:43 -07:00
|
|
|
|
2022-07-05 20:37:49 -07:00
|
|
|
return (
|
2022-07-17 19:49:14 -07:00
|
|
|
<ContentBox showBackground className="relative overflow-hidden">
|
2022-07-23 19:51:54 -07:00
|
|
|
<Transition
|
2022-07-17 19:49:14 -07:00
|
|
|
className="thin-scroll absolute top-0 left-0 z-20 h-full w-full overflow-auto bg-th-bkg-2 p-6 pb-0"
|
|
|
|
show={showConfirm}
|
2022-08-16 15:58:03 -07:00
|
|
|
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 19:49:14 -07:00
|
|
|
>
|
2022-08-17 13:06:25 -07:00
|
|
|
<JupiterRouteInfo
|
2022-08-18 13:50:34 -07:00
|
|
|
inputTokenInfo={inputTokenInfo}
|
2022-07-17 19:49:14 -07:00
|
|
|
onClose={() => setShowConfirm(false)}
|
2022-08-17 18:26:38 -07:00
|
|
|
amountIn={amountIn}
|
2022-07-17 19:49:14 -07:00
|
|
|
slippage={slippage}
|
2022-07-18 03:02:43 -07:00
|
|
|
outputTokenInfo={outputTokenInfo}
|
|
|
|
jupiter={jupiter}
|
|
|
|
routes={routes}
|
|
|
|
selectedRoute={selectedRoute}
|
|
|
|
setSelectedRoute={setSelectedRoute}
|
2022-07-17 19:49:14 -07:00
|
|
|
/>
|
2022-07-23 19:51:54 -07:00
|
|
|
</Transition>
|
2022-07-23 19:48:26 -07:00
|
|
|
<EnterBottomExitBottom
|
2022-08-02 11:04:00 -07:00
|
|
|
className="thin-scroll absolute bottom-0 left-0 z-20 h-full w-full overflow-auto bg-th-bkg-2 p-6 pb-0"
|
2022-07-16 04:22:59 -07:00
|
|
|
show={!!showTokenSelect}
|
|
|
|
>
|
2022-08-20 11:54:38 -07:00
|
|
|
<SwapFormTokenList
|
2022-07-16 04:22:59 -07:00
|
|
|
onClose={() => setShowTokenSelect('')}
|
|
|
|
onTokenSelect={
|
|
|
|
showTokenSelect === 'input'
|
|
|
|
? handleTokenInSelect
|
|
|
|
: handleTokenOutSelect
|
|
|
|
}
|
|
|
|
type={showTokenSelect}
|
|
|
|
/>
|
2022-07-23 19:48:26 -07:00
|
|
|
</EnterBottomExitBottom>
|
2022-07-17 04:48:33 -07:00
|
|
|
<div className="mb-4 flex items-center justify-between">
|
|
|
|
<h3>{t('trade')}</h3>
|
2022-08-12 05:40:26 -07:00
|
|
|
<IconButton
|
2022-08-14 04:56:19 -07:00
|
|
|
className="text-th-fgd-3"
|
2022-08-08 10:42:18 -07:00
|
|
|
onClick={() => setShowSettings(true)}
|
2022-08-14 04:56:19 -07:00
|
|
|
size="small"
|
2022-07-17 04:48:33 -07:00
|
|
|
>
|
2022-08-12 05:40:26 -07:00
|
|
|
<CogIcon className="h-5 w-5" />
|
|
|
|
</IconButton>
|
2022-07-17 04:48:33 -07:00
|
|
|
</div>
|
2022-08-08 10:42:18 -07:00
|
|
|
<EnterBottomExitBottom
|
|
|
|
className="thin-scroll absolute bottom-0 left-0 z-20 h-full w-full overflow-auto bg-th-bkg-2 p-6 pb-0"
|
|
|
|
show={showSettings}
|
|
|
|
>
|
|
|
|
<SwapSettings onClose={() => setShowSettings(false)} />
|
|
|
|
</EnterBottomExitBottom>
|
2022-07-17 04:48:33 -07:00
|
|
|
<div className="mb-2 flex items-center justify-between">
|
|
|
|
<p className="text-th-fgd-3">{t('sell')}</p>
|
2022-08-17 21:13:50 -07:00
|
|
|
<MaxSwapAmount
|
2022-08-24 03:51:06 -07:00
|
|
|
amountWithBorrow={amountWithBorrow}
|
2022-08-17 21:13:50 -07:00
|
|
|
useMargin={useMargin}
|
|
|
|
setAmountIn={setAmountInFormValue}
|
2022-08-24 03:51:06 -07:00
|
|
|
tokenMax={tokenMax}
|
2022-08-17 18:26:38 -07:00
|
|
|
/>
|
2022-07-17 04:48:33 -07:00
|
|
|
</div>
|
2022-07-15 05:50:29 -07:00
|
|
|
<div className="mb-3 grid grid-cols-2">
|
2022-07-23 19:48:26 -07:00
|
|
|
<div className="col-span-1 rounded-lg rounded-r-none border border-r-0 border-th-bkg-4 bg-th-bkg-1">
|
2022-07-16 04:22:59 -07:00
|
|
|
<TokenSelect
|
2022-08-18 13:50:34 -07:00
|
|
|
tokenSymbol={inputTokenInfo?.symbol || INPUT_TOKEN_DEFAULT}
|
2022-07-16 04:22:59 -07:00
|
|
|
showTokenList={setShowTokenSelect}
|
|
|
|
type="input"
|
|
|
|
/>
|
2022-07-05 20:37:49 -07:00
|
|
|
</div>
|
2022-07-15 05:50:29 -07:00
|
|
|
<div className="col-span-1">
|
2022-08-17 18:26:38 -07:00
|
|
|
<NumberFormat
|
|
|
|
inputMode="decimal"
|
|
|
|
thousandSeparator=","
|
2022-08-18 13:50:34 -07:00
|
|
|
decimalScale={inputTokenInfo?.decimals || 6}
|
2022-07-15 05:50:29 -07:00
|
|
|
name="amountIn"
|
|
|
|
id="amountIn"
|
2022-07-23 19:48:26 -07:00
|
|
|
className="w-full rounded-lg rounded-l-none border border-th-bkg-4 bg-th-bkg-1 p-3 text-right text-xl font-bold tracking-wider text-th-fgd-1 focus:outline-none"
|
2022-07-15 05:50:29 -07:00
|
|
|
placeholder="0.00"
|
2022-08-17 18:26:38 -07:00
|
|
|
value={amountInFormValue}
|
|
|
|
onValueChange={handleAmountInChange}
|
|
|
|
isAllowed={withValueLimit}
|
2022-07-15 05:50:29 -07:00
|
|
|
/>
|
2022-07-10 19:01:16 -07:00
|
|
|
</div>
|
2022-07-17 04:48:33 -07:00
|
|
|
{!useMargin ? (
|
2022-08-23 05:41:37 -07:00
|
|
|
<PercentageSelectButtons
|
|
|
|
amountIn={amountInFormValue}
|
2022-08-24 03:51:06 -07:00
|
|
|
decimals={decimals}
|
2022-08-23 05:41:37 -07:00
|
|
|
setAmountIn={setAmountInFormValue}
|
2022-08-24 03:51:06 -07:00
|
|
|
tokenMax={tokenMax}
|
2022-08-23 05:41:37 -07:00
|
|
|
/>
|
2022-07-17 04:48:33 -07:00
|
|
|
) : null}
|
2022-07-15 05:50:29 -07:00
|
|
|
</div>
|
|
|
|
<div className="flex justify-center">
|
|
|
|
<button
|
|
|
|
className="rounded-full border border-th-bkg-4 p-1.5 text-th-fgd-3 md:hover:text-th-primary"
|
|
|
|
onClick={handleSwitchTokens}
|
|
|
|
>
|
2022-07-16 21:41:20 -07:00
|
|
|
<ArrowDownIcon
|
|
|
|
className="h-5 w-5"
|
|
|
|
style={
|
2022-07-22 08:40:53 -07:00
|
|
|
animateSwitchArrow % 2 == 0
|
2022-07-16 21:41:20 -07:00
|
|
|
? { transform: 'rotate(0deg)' }
|
|
|
|
: { transform: 'rotate(360deg)' }
|
|
|
|
}
|
|
|
|
/>
|
2022-07-15 05:50:29 -07:00
|
|
|
</button>
|
|
|
|
</div>
|
2022-07-17 04:48:33 -07:00
|
|
|
<p className="mb-2 text-th-fgd-3">{t('buy')}</p>
|
2022-07-15 05:50:29 -07:00
|
|
|
<div className="mb-3 grid grid-cols-2">
|
2022-07-23 19:48:26 -07:00
|
|
|
<div className="col-span-1 rounded-lg rounded-r-none border border-r-0 border-th-bkg-4 bg-th-bkg-1">
|
2022-07-16 04:22:59 -07:00
|
|
|
<TokenSelect
|
2022-08-18 13:50:34 -07:00
|
|
|
tokenSymbol={outputTokenInfo?.symbol || OUTPUT_TOKEN_DEFAULT}
|
2022-07-16 04:22:59 -07:00
|
|
|
showTokenList={setShowTokenSelect}
|
|
|
|
type="output"
|
|
|
|
/>
|
2022-07-15 05:50:29 -07:00
|
|
|
</div>
|
2022-08-23 04:13:17 -07:00
|
|
|
<div className="flex h-[54px] w-full items-center justify-end rounded-r-lg border border-th-bkg-4 bg-th-bkg-3 text-right text-xl font-bold tracking-wider text-th-fgd-3">
|
2022-08-15 04:28:36 -07:00
|
|
|
{isLoadingTradeDetails ? (
|
|
|
|
<div className="w-full">
|
|
|
|
<SheenLoader className="rounded-l-none">
|
|
|
|
<div className="h-[52px] w-full rounded-r-lg bg-th-bkg-3" />
|
|
|
|
</SheenLoader>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<span className="p-3">
|
2022-08-18 13:50:34 -07:00
|
|
|
{amountOut ? numberFormat.format(amountOut) : ''}
|
2022-08-15 04:28:36 -07:00
|
|
|
</span>
|
|
|
|
)}
|
2022-07-05 20:37:49 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-07-17 04:48:33 -07:00
|
|
|
{useMargin ? (
|
|
|
|
<>
|
2022-08-16 20:42:16 -07:00
|
|
|
<div className="mb-1 flex items-center justify-between">
|
2022-07-17 04:48:33 -07:00
|
|
|
<p className="text-th-fgd-3">{t('leverage')}</p>
|
2022-08-22 19:26:59 -07:00
|
|
|
{/* <p className="text-th-fgd-1">0.00x</p> */}
|
2022-07-17 04:48:33 -07:00
|
|
|
</div>
|
2022-08-15 15:18:23 -07:00
|
|
|
<SwapLeverageSlider
|
2022-08-17 21:13:50 -07:00
|
|
|
amount={amountIn.toNumber()}
|
|
|
|
onChange={setAmountInFormValue}
|
2022-07-17 04:48:33 -07:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : null}
|
2022-07-17 19:49:14 -07:00
|
|
|
<Button
|
|
|
|
onClick={() => setShowConfirm(true)}
|
2022-07-23 19:51:54 -07:00
|
|
|
className="mt-6 flex w-full items-center justify-center text-base"
|
2022-07-18 03:02:43 -07:00
|
|
|
disabled={
|
2022-08-17 23:10:43 -07:00
|
|
|
!amountIn.toNumber() ||
|
|
|
|
!connected ||
|
|
|
|
!routes?.length ||
|
|
|
|
!selectedRoute ||
|
2022-08-24 03:51:06 -07:00
|
|
|
!outputTokenInfo ||
|
|
|
|
(!useMargin && amountIn.toNumber() > tokenMax)
|
2022-07-18 03:02:43 -07:00
|
|
|
}
|
2022-07-20 21:50:56 -07:00
|
|
|
size="large"
|
2022-07-17 19:49:14 -07:00
|
|
|
>
|
2022-07-18 03:02:43 -07:00
|
|
|
{connected ? (
|
|
|
|
isLoadingTradeDetails ? (
|
|
|
|
<Loading />
|
|
|
|
) : (
|
2022-08-17 18:26:38 -07:00
|
|
|
<div className="flex items-center">{t('trade:review-trade')}</div>
|
2022-07-18 03:02:43 -07:00
|
|
|
)
|
|
|
|
) : (
|
|
|
|
t('connect')
|
|
|
|
)}
|
2022-07-17 19:49:14 -07:00
|
|
|
</Button>
|
2022-08-24 03:51:06 -07:00
|
|
|
{!useMargin && amountIn.toNumber() > tokenMax ? (
|
|
|
|
<div className="pt-4">
|
|
|
|
<InlineNotification
|
|
|
|
type="error"
|
|
|
|
desc={t('trade:insufficient-balance', {
|
|
|
|
symbol: inputTokenInfo?.symbol,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
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-08-18 13:50:34 -07:00
|
|
|
export const useTokenMax = () => {
|
2022-08-15 09:02:46 -07:00
|
|
|
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
|
2022-08-18 13:50:34 -07:00
|
|
|
const inputBank = mangoStore((s) => s.swap.inputBank)
|
|
|
|
const outputBank = mangoStore((s) => s.swap.outputBank)
|
2022-08-19 21:03:26 -07:00
|
|
|
const slippage = mangoStore((s) => s.swap.slippage)
|
2022-08-15 09:02:46 -07:00
|
|
|
|
|
|
|
const tokenInMax = useMemo(() => {
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
|
2022-08-18 13:50:34 -07:00
|
|
|
if (!group || !inputBank || !mangoAccount || !outputBank)
|
2022-08-17 21:13:50 -07:00
|
|
|
return { amount: 0.0, decimals: 6, amountWithBorrow: 0.0 }
|
|
|
|
|
2022-08-20 17:09:36 -07:00
|
|
|
const inputBankLiquidity = floorToDecimal(
|
|
|
|
inputBank.uiDeposits() - inputBank.uiBorrows(),
|
|
|
|
inputBank.mintDecimals
|
|
|
|
)
|
|
|
|
const maxWithdrawableAmount = Math.min(
|
|
|
|
mangoAccount.getTokenBalanceUi(inputBank),
|
|
|
|
inputBankLiquidity
|
|
|
|
)
|
2022-08-24 03:51:06 -07:00
|
|
|
const maxUiAmountWithBorrow = mangoAccount?.getMaxSourceUiForTokenSwap(
|
|
|
|
group,
|
|
|
|
inputBank.mint,
|
|
|
|
outputBank.mint,
|
|
|
|
0.98 - slippage / 10
|
|
|
|
)
|
2022-08-15 09:02:46 -07:00
|
|
|
|
2022-08-15 21:19:09 -07:00
|
|
|
return {
|
2022-08-20 17:09:36 -07:00
|
|
|
amount:
|
|
|
|
maxWithdrawableAmount > 0
|
|
|
|
? floorToDecimal(maxWithdrawableAmount, inputBank.mintDecimals)
|
|
|
|
: 0,
|
2022-08-17 21:13:50 -07:00
|
|
|
amountWithBorrow:
|
2022-08-20 17:09:36 -07:00
|
|
|
maxUiAmountWithBorrow > 0
|
|
|
|
? Math.min(
|
|
|
|
floorToDecimal(maxUiAmountWithBorrow, inputBank.mintDecimals),
|
|
|
|
inputBankLiquidity
|
2022-08-17 21:13:50 -07:00
|
|
|
)
|
|
|
|
: 0,
|
2022-08-18 13:50:34 -07:00
|
|
|
decimals: inputBank.mintDecimals,
|
2022-08-15 21:19:09 -07:00
|
|
|
}
|
2022-08-19 21:03:26 -07:00
|
|
|
}, [inputBank, mangoAccount, outputBank, slippage])
|
2022-08-15 09:02:46 -07:00
|
|
|
|
|
|
|
return tokenInMax
|
|
|
|
}
|
|
|
|
|
2022-08-17 21:13:50 -07:00
|
|
|
const MaxSwapAmount = ({
|
2022-08-24 03:51:06 -07:00
|
|
|
amountWithBorrow,
|
2022-08-15 09:02:46 -07:00
|
|
|
setAmountIn,
|
2022-08-24 03:51:06 -07:00
|
|
|
tokenMax,
|
2022-08-17 21:13:50 -07:00
|
|
|
useMargin,
|
2022-08-15 09:02:46 -07:00
|
|
|
}: {
|
2022-08-24 03:51:06 -07:00
|
|
|
amountWithBorrow: number
|
2022-08-15 09:02:46 -07:00
|
|
|
setAmountIn: (x: any) => void
|
2022-08-24 03:51:06 -07:00
|
|
|
tokenMax: number
|
2022-08-17 21:13:50 -07:00
|
|
|
useMargin: boolean
|
2022-08-15 09:02:46 -07:00
|
|
|
}) => {
|
2022-08-19 14:25:31 -07:00
|
|
|
const mangoAccountLoading = mangoStore((s) => s.mangoAccount.initialLoad)
|
2022-08-15 09:02:46 -07:00
|
|
|
const { t } = useTranslation('common')
|
|
|
|
|
|
|
|
const setMaxInputAmount = () => {
|
2022-08-17 21:13:50 -07:00
|
|
|
const amountIn = useMargin ? amountWithBorrow : tokenMax
|
|
|
|
setAmountIn(amountIn)
|
2022-08-15 09:02:46 -07:00
|
|
|
}
|
|
|
|
|
2022-08-19 14:25:31 -07:00
|
|
|
if (mangoAccountLoading) return null
|
|
|
|
|
2022-08-15 09:02:46 -07:00
|
|
|
return (
|
|
|
|
<LinkButton className="no-underline" onClick={setMaxInputAmount}>
|
2022-08-17 21:13:50 -07:00
|
|
|
<span className="font-normal text-th-fgd-4">{t('max')}:</span>
|
|
|
|
<span className="mx-1 text-th-fgd-3 underline">
|
|
|
|
{useMargin ? amountWithBorrow : tokenMax}
|
|
|
|
</span>
|
2022-08-15 09:02:46 -07:00
|
|
|
</LinkButton>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const PercentageSelectButtons = ({
|
2022-08-23 05:41:37 -07:00
|
|
|
amountIn,
|
2022-08-24 03:51:06 -07:00
|
|
|
decimals,
|
2022-08-15 09:02:46 -07:00
|
|
|
setAmountIn,
|
2022-08-24 03:51:06 -07:00
|
|
|
tokenMax,
|
2022-08-15 09:02:46 -07:00
|
|
|
}: {
|
2022-08-23 05:41:37 -07:00
|
|
|
amountIn: string
|
2022-08-24 03:51:06 -07:00
|
|
|
decimals: number
|
2022-08-15 20:16:20 -07:00
|
|
|
setAmountIn: (x: any) => any
|
2022-08-24 03:51:06 -07:00
|
|
|
tokenMax: number
|
2022-08-15 09:02:46 -07:00
|
|
|
}) => {
|
|
|
|
const [sizePercentage, setSizePercentage] = useState('')
|
|
|
|
|
2022-08-23 05:41:37 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (tokenMax > 0 && Number(amountIn) === tokenMax) {
|
|
|
|
setSizePercentage('100')
|
|
|
|
}
|
|
|
|
}, [amountIn, tokenMax])
|
|
|
|
|
2022-08-15 09:02:46 -07:00
|
|
|
const handleSizePercentage = (percentage: string) => {
|
|
|
|
setSizePercentage(percentage)
|
|
|
|
if (tokenMax > 0) {
|
2022-08-15 21:19:09 -07:00
|
|
|
let amount = (Number(percentage) / 100) * tokenMax
|
|
|
|
if (percentage !== '100') {
|
|
|
|
amount = floorToDecimal(amount, decimals)
|
|
|
|
}
|
|
|
|
setAmountIn(amount.toString())
|
2022-08-15 20:16:20 -07:00
|
|
|
} else {
|
|
|
|
setAmountIn('0')
|
2022-08-15 09:02:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="col-span-2 mt-2">
|
|
|
|
<ButtonGroup
|
|
|
|
activeValue={sizePercentage}
|
|
|
|
onChange={(p) => handleSizePercentage(p)}
|
|
|
|
values={['10', '25', '50', '75', '100']}
|
|
|
|
unit="%"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|