2022-11-22 19:31:50 -08:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
|
|
|
import LeverageSlider from '../shared/LeverageSlider'
|
2023-09-07 00:59:04 -07:00
|
|
|
import { TokenMaxResults } from './useTokenMax'
|
2023-10-19 18:44:28 -07:00
|
|
|
import mangoStore from '@store/mangoStore'
|
2023-12-08 04:26:20 -08:00
|
|
|
import { useMemo } from 'react'
|
|
|
|
import { floorToDecimal } from 'utils/numbers'
|
2022-11-22 19:31:50 -08:00
|
|
|
|
|
|
|
const SwapSlider = ({
|
|
|
|
amount,
|
|
|
|
onChange,
|
|
|
|
useMargin,
|
|
|
|
step,
|
2023-09-07 00:59:04 -07:00
|
|
|
maxAmount,
|
2023-10-26 21:30:17 -07:00
|
|
|
handleStartDrag,
|
|
|
|
handleEndDrag,
|
2022-11-22 19:31:50 -08:00
|
|
|
}: {
|
|
|
|
amount: number
|
|
|
|
onChange: (x: string) => void
|
|
|
|
useMargin: boolean
|
|
|
|
step: number
|
2023-09-07 00:59:04 -07:00
|
|
|
maxAmount: (useMargin: boolean) => TokenMaxResults
|
2023-10-26 21:30:17 -07:00
|
|
|
handleStartDrag?: () => void
|
|
|
|
handleEndDrag?: () => void
|
2022-11-22 19:31:50 -08:00
|
|
|
}) => {
|
|
|
|
const { mangoAccount } = useMangoAccount()
|
2023-09-07 00:59:04 -07:00
|
|
|
const { amount: tokenMax, amountWithBorrow } = maxAmount(useMargin)
|
2023-10-19 18:44:28 -07:00
|
|
|
const { inputBank } = mangoStore((s) => s.swap)
|
2022-12-07 18:49:01 -08:00
|
|
|
|
2023-12-08 04:26:20 -08:00
|
|
|
const max = useMemo(() => {
|
|
|
|
if (!inputBank) return 0
|
|
|
|
return useMargin
|
|
|
|
? floorToDecimal(amountWithBorrow, inputBank.mintDecimals).toNumber()
|
|
|
|
: floorToDecimal(tokenMax, inputBank.mintDecimals).toNumber()
|
|
|
|
}, [tokenMax, amountWithBorrow, inputBank, useMargin])
|
|
|
|
|
2022-11-22 19:31:50 -08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{!mangoAccount ? (
|
|
|
|
<LeverageSlider
|
|
|
|
amount={amount}
|
|
|
|
leverageMax={100}
|
|
|
|
onChange={onChange}
|
|
|
|
step={step}
|
2023-10-26 21:30:17 -07:00
|
|
|
handleStartDrag={handleStartDrag}
|
|
|
|
handleEndDrag={handleEndDrag}
|
2022-11-22 19:31:50 -08:00
|
|
|
/>
|
|
|
|
) : (
|
2022-12-08 03:15:16 -08:00
|
|
|
<LeverageSlider
|
|
|
|
amount={amount}
|
2023-10-19 18:44:28 -07:00
|
|
|
decimals={inputBank?.mintDecimals}
|
2023-12-08 04:26:20 -08:00
|
|
|
leverageMax={max}
|
2023-07-16 20:41:13 -07:00
|
|
|
onChange={onChange}
|
2022-12-08 03:15:16 -08:00
|
|
|
step={step}
|
2023-10-26 21:30:17 -07:00
|
|
|
handleStartDrag={handleStartDrag}
|
|
|
|
handleEndDrag={handleEndDrag}
|
2022-12-08 03:15:16 -08:00
|
|
|
/>
|
2022-11-22 19:31:50 -08:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SwapSlider
|