From 19d4d1691df7102acf86dc3ff655f940dc7e3667 Mon Sep 17 00:00:00 2001 From: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com> Date: Fri, 2 Feb 2024 14:05:40 +0100 Subject: [PATCH] perps: remove modify module and adjust main module (#761) * perps: remove modify module and adjust main module * fix build --- .../perps/BalancesTable/Columns/Manage.tsx | 1 - .../perps/Module/PerpsManageModule/index.tsx | 109 ------------------ src/components/perps/Module/PerpsModule.tsx | 90 +++++++++++---- src/components/perps/Module/Summary.tsx | 14 ++- ...PerpsManageModule.ts => usePerpsModule.ts} | 41 +++---- .../AssetSelector/AssetSelectorPerps.tsx | 23 +++- src/hooks/perps/usePerpsAsset.ts | 9 +- src/hooks/perps/usePerpsMarket.ts | 4 +- src/pages/PerpsPage.tsx | 10 +- src/types/enums/searchParams.ts | 1 - 10 files changed, 125 insertions(+), 177 deletions(-) delete mode 100644 src/components/perps/Module/PerpsManageModule/index.tsx rename src/components/perps/Module/{PerpsManageModule/usePerpsManageModule.ts => usePerpsModule.ts} (59%) diff --git a/src/components/perps/BalancesTable/Columns/Manage.tsx b/src/components/perps/BalancesTable/Columns/Manage.tsx index c005a9aa..4c23b44d 100644 --- a/src/components/perps/BalancesTable/Columns/Manage.tsx +++ b/src/components/perps/BalancesTable/Columns/Manage.tsx @@ -29,7 +29,6 @@ export default function Manage(props: Props) { setSearchParams({ ...params, [SearchParams.PERPS_MARKET]: props.perpPosition.asset.denom, - [SearchParams.PERPS_MANAGE]: 'true', }) }, }, diff --git a/src/components/perps/Module/PerpsManageModule/index.tsx b/src/components/perps/Module/PerpsManageModule/index.tsx deleted file mode 100644 index b1cbb2d8..00000000 --- a/src/components/perps/Module/PerpsManageModule/index.tsx +++ /dev/null @@ -1,109 +0,0 @@ -import classNames from 'classnames' -import debounce from 'lodash.debounce' -import { useEffect, useMemo, useState } from 'react' - -import { Cross } from 'components/common/Icons' -import LeverageSlider from 'components/common/LeverageSlider' -import { Spacer } from 'components/common/Spacer' -import Text from 'components/common/Text' -import { TradeDirectionSelector } from 'components/common/TradeDirectionSelector' -import { LeverageButtons } from 'components/perps/Module/LeverageButtons' -import { Or } from 'components/perps/Module/Or' -import usePerpsManageModule from 'components/perps/Module/PerpsManageModule/usePerpsManageModule' -import PerpsSummary from 'components/perps/Module/Summary' -import AssetAmountInput from 'components/trade/TradeModule/SwapForm/AssetAmountInput' -import useCurrentAccount from 'hooks/accounts/useCurrentAccount' -import { useUpdatedAccount } from 'hooks/useUpdatedAccount' -import getPerpsPosition from 'utils/getPerpsPosition' -import { BN } from 'utils/helpers' - -export function PerpsManageModule() { - const [tradeDirection, setTradeDirection] = useState(null) - const [amount, setAmount] = useState(null) - const account = useCurrentAccount() - const { simulatePerps, addedPerps } = useUpdatedAccount(account) - const { - closeManagePerpModule, - previousAmount, - previousTradeDirection, - previousLeverage, - leverage, - asset, - } = usePerpsManageModule(amount) - - const debouncedUpdateAccount = useMemo( - () => - debounce((perpsPosition: PerpsPosition) => { - if ( - addedPerps && - perpsPosition.amount === addedPerps.amount && - perpsPosition.tradeDirection === addedPerps.tradeDirection - ) - return - simulatePerps(perpsPosition) - }, 100), - [simulatePerps, addedPerps], - ) - - useEffect(() => { - const perpsPosition = getPerpsPosition( - asset, - amount ?? previousAmount, - tradeDirection ?? previousTradeDirection, - ) - debouncedUpdateAccount(perpsPosition) - }, [ - debouncedUpdateAccount, - asset, - amount, - previousAmount, - tradeDirection, - previousTradeDirection, - ]) - - if (!asset) return null - - return ( -
-
- Manage Position - -
- - - - Position Leverage - {}} type={tradeDirection || 'long'} /> - - - -
- ) -} diff --git a/src/components/perps/Module/PerpsModule.tsx b/src/components/perps/Module/PerpsModule.tsx index 437fff86..19f95050 100644 --- a/src/components/perps/Module/PerpsModule.tsx +++ b/src/components/perps/Module/PerpsModule.tsx @@ -1,5 +1,5 @@ import debounce from 'lodash.debounce' -import { useEffect, useMemo, useState } from 'react' +import { useCallback, useEffect, useMemo, useState } from 'react' import Card from 'components/common/Card' import LeverageSlider from 'components/common/LeverageSlider' @@ -9,6 +9,7 @@ import { TradeDirectionSelector } from 'components/common/TradeDirectionSelector import { LeverageButtons } from 'components/perps/Module/LeverageButtons' import { Or } from 'components/perps/Module/Or' import PerpsSummary from 'components/perps/Module/Summary' +import usePerpsModule from 'components/perps/Module/usePerpsModule' import AssetSelectorPerps from 'components/trade/TradeModule/AssetSelector/AssetSelectorPerps' import AssetAmountInput from 'components/trade/TradeModule/SwapForm/AssetAmountInput' import OrderTypeSelector from 'components/trade/TradeModule/SwapForm/OrderTypeSelector' @@ -24,10 +25,11 @@ export function PerpsModule() { const [selectedOrderType, setSelectedOrderType] = useState('Market') const [tradeDirection, setTradeDirection] = useState('long') const { perpsAsset } = usePerpsAsset() - const [leverage, setLeverage] = useState(1) const account = useCurrentAccount() const { simulatePerps, addedPerps } = useUpdatedAccount(account) const [amount, setAmount] = useState(BN_ZERO) + const { previousAmount, previousTradeDirection, previousLeverage, leverage, hasActivePosition } = + usePerpsModule(amount) const debouncedUpdateAccount = useMemo( () => @@ -44,46 +46,92 @@ export function PerpsModule() { ) useEffect(() => { - const perpsPosition = getPerpsPosition(perpsAsset, amount, tradeDirection) + const perpsPosition = getPerpsPosition( + perpsAsset, + amount ?? previousAmount, + tradeDirection ?? previousTradeDirection, + ) debouncedUpdateAccount(perpsPosition) - }, [debouncedUpdateAccount, amount, perpsAsset, tradeDirection]) + }, [ + debouncedUpdateAccount, + amount, + perpsAsset, + tradeDirection, + previousAmount, + previousTradeDirection, + ]) + + const setLeverage = useCallback((leverage: number) => { + // TODO: Implement leverage setting + }, []) + + const onChangeTradeDirection = useCallback( + (tradeDirection: TradeDirection) => { + setAmount(amount.times(-1)) + setTradeDirection(tradeDirection) + }, + [amount], + ) + + const onChangeAmount = useCallback( + (amount: BigNumber) => { + if (tradeDirection === 'short') { + setAmount(amount.times(-1)) + return + } + + setAmount(amount) + }, + [tradeDirection], + ) if (!perpsAsset) return null return ( } + title={} className='h-full mb-4' > - - + - - Position Leverage - - + {!hasActivePosition && ( + <> + + Position Leverage + + + + )} + ) diff --git a/src/components/perps/Module/Summary.tsx b/src/components/perps/Module/Summary.tsx index 3c2ba493..926dc297 100644 --- a/src/components/perps/Module/Summary.tsx +++ b/src/components/perps/Module/Summary.tsx @@ -18,10 +18,10 @@ type Props = { amount: BigNumber tradeDirection: TradeDirection asset: Asset - changeTradeDirection?: boolean previousAmount?: BigNumber previousTradeDirection?: 'long' | 'short' previousLeverage?: number + hasActivePosition: boolean } export default function PerpsSummary(props: Props) { @@ -70,14 +70,13 @@ export default function PerpsSummary(props: Props) { function ManageSummary(props: Props) { const showTradeDirection = props.previousTradeDirection && props.previousTradeDirection !== props.tradeDirection - const showAmount = - props.previousAmount && props.amount && !props.previousAmount.isEqualTo(props.amount) + const showAmount = !props.amount.isZero() && props.previousAmount const showLeverage = props.previousLeverage && props.leverage && props.previousLeverage.toFixed(2) !== props.leverage.toFixed(2) - if (!showTradeDirection && !showLeverage && !showAmount) return null + if ((!showTradeDirection && !showLeverage && !showAmount) || !props.hasActivePosition) return null return (
@@ -95,14 +94,17 @@ function ManageSummary(props: Props) { {showAmount && props.previousAmount && ( - + - + )} diff --git a/src/components/perps/Module/PerpsManageModule/usePerpsManageModule.ts b/src/components/perps/Module/usePerpsModule.ts similarity index 59% rename from src/components/perps/Module/PerpsManageModule/usePerpsManageModule.ts rename to src/components/perps/Module/usePerpsModule.ts index 25ecd7d4..34e8b9f3 100644 --- a/src/components/perps/Module/PerpsManageModule/usePerpsManageModule.ts +++ b/src/components/perps/Module/usePerpsModule.ts @@ -1,5 +1,4 @@ -import { useCallback, useMemo } from 'react' -import { useSearchParams } from 'react-router-dom' +import { useMemo } from 'react' import { BN_ZERO } from 'constants/math' import useCurrentAccount from 'hooks/accounts/useCurrentAccount' @@ -8,34 +7,33 @@ import usePerpPosition from 'hooks/perps/usePerpPosition' import usePerpsAsset from 'hooks/perps/usePerpsAsset' import usePrice from 'hooks/usePrice' import usePrices from 'hooks/usePrices' -import { SearchParams } from 'types/enums/searchParams' import { getAccountNetValue } from 'utils/accounts' +import { byDenom } from 'utils/array' import { demagnify } from 'utils/formatters' -import { getSearchParamsObject } from 'utils/route' -export default function usePerpsManageModule(amount: BigNumber | null) { +export default function usePerpsModule(amount: BigNumber | null) { const { perpsAsset } = usePerpsAsset() - const [searchParams, setSearchParams] = useSearchParams() const perpPosition = usePerpPosition(perpsAsset.denom) const { data: prices } = usePrices() const assets = useAllAssets() const account = useCurrentAccount() const price = usePrice(perpsAsset.denom) + const hasActivePosition = useMemo( + () => !!account?.perps.find(byDenom(perpsAsset.denom)), + [account?.perps, perpsAsset.denom], + ) + const accountNetValue = useMemo(() => { if (!account || !prices || !assets) return BN_ZERO return getAccountNetValue(account, prices, assets) }, [account, assets, prices]) - const closeManagePerpModule = useCallback(() => { - const params = getSearchParamsObject(searchParams) - delete params[SearchParams.PERPS_MANAGE] - setSearchParams({ - ...params, - }) - }, [searchParams, setSearchParams]) - - const previousAmount = useMemo(() => perpPosition?.amount ?? BN_ZERO, [perpPosition?.amount]) + const previousAmount = useMemo( + () => + (perpPosition?.amount ?? BN_ZERO).times(perpPosition?.tradeDirection === 'short' ? -1 : 1), + [perpPosition?.amount, perpPosition?.tradeDirection], + ) const previousTradeDirection = useMemo( () => perpPosition?.tradeDirection || 'long', [perpPosition?.tradeDirection], @@ -43,26 +41,29 @@ export default function usePerpsManageModule(amount: BigNumber | null) { const previousLeverage = useMemo( () => - price.times(demagnify(previousAmount, perpsAsset)).div(accountNetValue).plus(1).toNumber(), + price + .times(demagnify(previousAmount.abs(), perpsAsset)) + .div(accountNetValue) + .plus(1) + .toNumber(), [accountNetValue, perpsAsset, previousAmount, price], ) const leverage = useMemo( () => price - .times(demagnify(amount ?? BN_ZERO, perpsAsset)) + .times(demagnify(previousAmount.plus(amount ?? BN_ZERO).abs(), perpsAsset)) .div(accountNetValue) .plus(1) .toNumber(), - [accountNetValue, amount, perpsAsset, price], + [accountNetValue, amount, perpsAsset, previousAmount, price], ) return { - closeManagePerpModule, previousAmount, previousTradeDirection, previousLeverage, leverage, - asset: perpsAsset, + hasActivePosition, } } diff --git a/src/components/trade/TradeModule/AssetSelector/AssetSelectorPerps.tsx b/src/components/trade/TradeModule/AssetSelector/AssetSelectorPerps.tsx index 02b59e86..b21995e0 100644 --- a/src/components/trade/TradeModule/AssetSelector/AssetSelectorPerps.tsx +++ b/src/components/trade/TradeModule/AssetSelector/AssetSelectorPerps.tsx @@ -4,25 +4,32 @@ import Button from 'components/common/Button' import { ChevronDown } from 'components/common/Icons' import Text from 'components/common/Text' import AssetOverlay from 'components/trade/TradeModule/AssetSelector/AssetOverlay' +import useCurrentAccount from 'hooks/accounts/useCurrentAccount' import usePerpsEnabledAssets from 'hooks/assets/usePerpsEnabledAssets' import usePerpsAsset from 'hooks/perps/usePerpsAsset' import useStore from 'store' interface Props { asset: Asset + hasActivePosition: boolean } export default function AssetSelectorPerps(props: Props) { const assetOverlayState = useStore((s) => s.assetOverlayState) const { perpsAsset, updatePerpsAsset } = usePerpsAsset() + const currentAccount = useCurrentAccount() const perpAssets = usePerpsEnabledAssets() const onChangePerpsAsset = useCallback( (asset: Asset) => { - updatePerpsAsset(asset.denom) + let hasPosition = false + if (currentAccount && currentAccount.perps.find((perp) => perp.denom === asset.denom)) { + hasPosition = true + } + updatePerpsAsset(asset.denom, hasPosition) }, - [updatePerpsAsset], + [currentAccount, updatePerpsAsset], ) const handleChangeState = useCallback(() => { @@ -37,9 +44,15 @@ export default function AssetSelectorPerps(props: Props) { onClick={() => useStore.setState({ assetOverlayState: 'pair' })} className='flex items-center justify-between w-full py-5 bg-white/5' > - - {perpsAsset.symbol}/USD - +
+ + {perpsAsset.symbol}/USD + + + {props.hasActivePosition && ( +
Active
+ )} +
All markets diff --git a/src/hooks/perps/usePerpsAsset.ts b/src/hooks/perps/usePerpsAsset.ts index aa9947ac..be54cc21 100644 --- a/src/hooks/perps/usePerpsAsset.ts +++ b/src/hooks/perps/usePerpsAsset.ts @@ -6,6 +6,7 @@ import { LocalStorageKeys } from 'constants/localStorageKeys' import usePerpsEnabledAssets from 'hooks/assets/usePerpsEnabledAssets' import useLocalStorage from 'hooks/localStorage/useLocalStorage' import useChainConfig from 'hooks/useChainConfig' +import { SearchParams } from 'types/enums/searchParams' import { getSearchParamsObject } from 'utils/route' export default function usePerpsAsset() { @@ -18,10 +19,12 @@ export default function usePerpsAsset() { >(chainConfig.id + '/' + LocalStorageKeys.PERPS_ASSET, DEFAULT_SETTINGS.perpsAsset) const updatePerpsAsset = useCallback( - (denom: string) => { + (denom: string, manage?: boolean) => { const params = getSearchParamsObject(searchParams) - params.perpsMarket = denom - setSearchParams(params) + setSearchParams({ + ...params, + [SearchParams.PERPS_MARKET]: denom, + }) setPerpsAssetInLocalStorage(denom) }, [searchParams, setPerpsAssetInLocalStorage, setSearchParams], diff --git a/src/hooks/perps/usePerpsMarket.ts b/src/hooks/perps/usePerpsMarket.ts index 87f3cbf9..af65a7b1 100644 --- a/src/hooks/perps/usePerpsMarket.ts +++ b/src/hooks/perps/usePerpsMarket.ts @@ -15,8 +15,8 @@ export default function usePerpsMarket() { clients && perpsAsset && `chains/${chainConfig.id}/perps/${perpsAsset.denom}`, () => getPerpsMarket(clients!, perpsAsset!), { - refreshInterval: 1000, - dedupingInterval: 1000, + refreshInterval: 5000, + dedupingInterval: 5000, }, ) } diff --git a/src/pages/PerpsPage.tsx b/src/pages/PerpsPage.tsx index 10e6fec2..131a1fd0 100644 --- a/src/pages/PerpsPage.tsx +++ b/src/pages/PerpsPage.tsx @@ -1,22 +1,14 @@ -import { useSearchParams } from 'react-router-dom' - -import { PerpsManageModule } from 'components/perps/Module/PerpsManageModule' import { PerpsModule } from 'components/perps/Module/PerpsModule' import { PerpsChart } from 'components/perps/PerpsChart' import { PerpsInfo } from 'components/perps/PerpsInfo' import { PerpsPositions } from 'components/perps/PerpsPositions' -import { SearchParams } from 'types/enums/searchParams' export default function PerpsPage() { - const [searchParams] = useSearchParams() - - const isManagingPosition = searchParams.get(SearchParams.PERPS_MANAGE) === 'true' - return (
- {isManagingPosition ? : } +
diff --git a/src/types/enums/searchParams.ts b/src/types/enums/searchParams.ts index b526bdc3..a3952c99 100644 --- a/src/types/enums/searchParams.ts +++ b/src/types/enums/searchParams.ts @@ -1,5 +1,4 @@ export enum SearchParams { ACCOUNT_ID = 'accountId', PERPS_MARKET = 'perpsMarket', - PERPS_MANAGE = 'perpsManage', }