From fab687ac87e4ca958324e7cc97ede4ba3975658f Mon Sep 17 00:00:00 2001 From: tjs Date: Mon, 8 Aug 2022 10:42:18 -0700 Subject: [PATCH] swap settings modal --- components/SideNav.tsx | 16 +++---- components/swap/Swap.tsx | 23 ++++++---- components/swap/SwapSettings.tsx | 75 ++++++++++++++++++++++++++++++++ store/state.ts | 4 ++ 4 files changed, 102 insertions(+), 16 deletions(-) create mode 100644 components/swap/SwapSettings.tsx diff --git a/components/SideNav.tsx b/components/SideNav.tsx index e2503b73..dfc1f985 100644 --- a/components/SideNav.tsx +++ b/components/SideNav.tsx @@ -65,34 +65,34 @@ const SideNav = ({ collapsed }: { collapsed: boolean }) => { } + icon={} title={t('portfolio')} pagePath="/" /> } + icon={} title={t('trade')} pagePath="/trade" /> } + icon={} title={t('stats')} pagePath="/stats" /> } + icon={} title={t('settings')} pagePath="/settings" /> } + icon={} title={t('more')} > {icon} @@ -261,7 +261,7 @@ const ExpandableMenuItem = ({ className={` ${ hideIconBg ? '' - : 'flex h-10 w-10 items-center justify-center rounded-full bg-th-bkg-3' + : 'flex h-9 w-9 items-center justify-center rounded-full bg-th-bkg-3' } ${ alignBottom ? 'flex h-[72px] w-[72px] items-center justify-center hover:bg-th-bkg-2' @@ -310,7 +310,7 @@ const ExpandableMenuItem = ({ className={ hideIconBg ? '' - : 'flex h-10 w-10 items-center justify-center rounded-full bg-th-bkg-3' + : 'flex h-9 w-9 items-center justify-center rounded-full bg-th-bkg-3' } > {icon} diff --git a/components/swap/Swap.tsx b/components/swap/Swap.tsx index b9a0962e..63679688 100644 --- a/components/swap/Swap.tsx +++ b/components/swap/Swap.tsx @@ -1,6 +1,6 @@ import { useState, ChangeEvent, useCallback, useEffect, useMemo } from 'react' import { TransactionInstruction } from '@solana/web3.js' -import { ArrowDownIcon } from '@heroicons/react/solid' +import { ArrowDownIcon, CogIcon } from '@heroicons/react/solid' import mangoStore from '../../store/state' import { RouteInfo } from '@jup-ag/core' import { Token } from '../../types/jupiter' @@ -26,6 +26,7 @@ import { toUiDecimals } from '@blockworks-foundation/mango-v4' import Loading from '../shared/Loading' import { EnterBottomExitBottom } from '../shared/Transitions' import useJupiter from './useJupiter' +import SwapSettings from './SwapSettings' const getBestRoute = (routesInfos: RouteInfo[]) => { return routesInfos[0] @@ -72,6 +73,7 @@ const Swap = () => { const [submitting, setSubmitting] = useState(false) const [animateSwitchArrow, setAnimateSwitchArrow] = useState(0) const [showTokenSelect, setShowTokenSelect] = useState('') + const [showSettings, setShowSettings] = useState(false) const [useMargin, setUseMargin] = useState(true) const [sizePercentage, setSizePercentage] = useState('') const [showConfirm, setShowConfirm] = useState(false) @@ -82,7 +84,7 @@ const Swap = () => { const outputToken = mangoStore((s) => s.swap.outputToken) const jupiterTokens = mangoStore((s) => s.jupiterTokens) const connected = mangoStore((s) => s.connected) - const debouncedAmountIn = useDebounce(amountIn, 400) + const debouncedAmountIn = useDebounce(amountIn, 300) const { amountOut, jupiter, outputTokenInfo, routes } = useJupiter({ inputTokenSymbol: inputToken, @@ -235,14 +237,19 @@ const Swap = () => {

{t('trade')}

- setUseMargin(!useMargin)} +
setShowSettings(true)} > - {t('margin')} - + +
+ + setShowSettings(false)} /> +

{t('sell')}

diff --git a/components/swap/SwapSettings.tsx b/components/swap/SwapSettings.tsx new file mode 100644 index 00000000..32a3c2c4 --- /dev/null +++ b/components/swap/SwapSettings.tsx @@ -0,0 +1,75 @@ +import { XIcon } from '@heroicons/react/solid' +import { useTranslation } from 'next-i18next' +import { useState } from 'react' +import mangoStore from '../../store/state' +import ButtonGroup from '../forms/ButtonGroup' +import Input from '../forms/Input' +import Switch from '../forms/Switch' +import { IconButton } from '../shared/Button' + +const slippagePresets = ['0.1', '0.5', '1', '2'] + +const SwapSettings = ({ onClose }: { onClose: () => void }) => { + const { t } = useTranslation('common') + const margin = mangoStore((s) => s.swap.margin) + const slippage = mangoStore((s) => s.swap.slippage) + const set = mangoStore((s) => s.set) + + const [showCustomSlippageForm, setShowCustomSlippageForm] = useState(false) + const [inputValue, setInputValue] = useState('') + + const handleSetMargin = () => { + set((s) => { + s.swap.margin = !s.swap.margin + }) + } + + const handleSetSlippage = (slippage: string) => { + set((s) => { + s.swap.slippage = parseFloat(slippage) + }) + } + + console.log('slippage', slippage) + + return ( + <> +

{t('settings')}

+ + + + +
+
Slippage
+ {showCustomSlippageForm ? ( + setInputValue(e.target.value)} + suffix="%" + /> + ) : ( + handleSetSlippage(v)} + unit="%" + values={slippagePresets} + /> + )} +
+
+ + {t('margin')} + +
+ + ) +} + +export default SwapSettings diff --git a/store/state.ts b/store/state.ts index 22a7a0f1..e6b6f203 100644 --- a/store/state.ts +++ b/store/state.ts @@ -73,6 +73,8 @@ export type MangoStore = { outputToken: string inputTokenInfo: Token | undefined outputTokenInfo: Token | undefined + margin: boolean + slippage: number } set: (x: (x: MangoStore) => void) => void wallet: { @@ -126,6 +128,8 @@ const mangoStore = create( outputToken: 'USDC', inputTokenInfo: undefined, outputTokenInfo: undefined, + margin: true, + slippage: 0.1, }, wallet: { loadProfilePic: true,