From 338ed2bd5b61fac94fbf8a3d760189410a5a1d87 Mon Sep 17 00:00:00 2001 From: Adam Clarke Date: Sun, 24 Sep 2023 18:03:38 +0100 Subject: [PATCH 1/2] fix(app): remove swap trade code (#1000) --- .../TradeConfirmationModal.tsx | 96 ++----------------- .../app/src/state/futures/common/types.ts | 1 - .../src/state/futures/smartMargin/actions.ts | 76 +-------------- .../src/state/futures/smartMargin/reducer.ts | 20 ---- .../state/futures/smartMargin/selectors.ts | 21 ---- .../src/state/futures/smartMargin/types.ts | 6 -- 6 files changed, 9 insertions(+), 211 deletions(-) diff --git a/packages/app/src/sections/futures/TradeConfirmation/TradeConfirmationModal.tsx b/packages/app/src/sections/futures/TradeConfirmation/TradeConfirmationModal.tsx index 6242e45ac..0eadd662b 100644 --- a/packages/app/src/sections/futures/TradeConfirmation/TradeConfirmationModal.tsx +++ b/packages/app/src/sections/futures/TradeConfirmation/TradeConfirmationModal.tsx @@ -1,5 +1,5 @@ import { MIN_MARGIN_AMOUNT, ZERO_WEI } from '@kwenta/sdk/constants' -import { PositionSide, SwapDepositToken } from '@kwenta/sdk/types' +import { PositionSide } from '@kwenta/sdk/types' import { OrderNameByType, formatCurrency, @@ -9,7 +9,7 @@ import { stripZeros, } from '@kwenta/sdk/utils' import Wei, { wei } from '@synthetixio/wei' -import { FC, useCallback, useMemo, useReducer, useState } from 'react' +import { FC, useCallback, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' @@ -18,12 +18,11 @@ import BaseModal from 'components/BaseModal' import Button from 'components/Button' import ErrorView from 'components/ErrorView' import { FlexDivRowCentered } from 'components/layout/flex' -import { ButtonLoader, MiniLoader } from 'components/Loader' +import { ButtonLoader } from 'components/Loader' import { StyledCaretDownIcon } from 'components/Select' import Spacer from 'components/Spacer' import Tooltip from 'components/Tooltip/Tooltip' import { NO_VALUE } from 'constants/placeholder' -import { SWAP_DEPOSIT_TRADE_ENABLED } from 'constants/ui' import { selectMarketAsset } from 'state/futures/common/selectors' import { selectLeverageSide, @@ -31,12 +30,7 @@ import { selectLeverageInput, selectTradePanelSLTPValidity, } from 'state/futures/selectors' -import { - calculateTradeSwapDeposit, - refetchTradePreview, - submitSmartMarginOrder, -} from 'state/futures/smartMargin/actions' -import { clearTradeSwapDepositQuote } from 'state/futures/smartMargin/reducer' +import { refetchTradePreview, submitSmartMarginOrder } from 'state/futures/smartMargin/actions' import { selectKeeperDepositExceedsBal, selectNewTradeHasSlTp, @@ -45,13 +39,8 @@ import { selectSelectedSwapDepositToken, selectSmartMarginOrderPrice, selectTradePreview, - selectSmartMarginQueryStatuses, - selectTradeSwapDepositQuote, - selectSwapDepositQuoteLoading, - selectQuoteInvalidReason, } from 'state/futures/smartMargin/selectors' -import { useAppDispatch, useAppSelector, useFetchAction, usePollAction } from 'state/hooks' -import { FetchStatus } from 'state/types' +import { useAppDispatch, useAppSelector, usePollAction } from 'state/hooks' import AcceptWarningView from '../../../components/AcceptWarningView' @@ -91,9 +80,6 @@ export default function TradeConfirmationModal({ const ethBalanceExceeded = useAppSelector(selectKeeperDepositExceedsBal) const hasSlTp = useAppSelector(selectNewTradeHasSlTp) const sltpValidity = useAppSelector(selectTradePanelSLTPValidity) - const quoteInvalidReason = useAppSelector(selectQuoteInvalidReason) - const quote = useAppSelector(selectTradeSwapDepositQuote) - const quoteLoading = useAppSelector(selectSwapDepositQuoteLoading) const swapToken = useAppSelector(selectSelectedSwapDepositToken) const [overridePriceProtection, setOverridePriceProtection] = useState(false) @@ -103,10 +89,6 @@ export default function TradeConfirmationModal({ const onConfirmOrder = useCallback(() => dispatch(submitSmartMarginOrder(true)), [dispatch]) - useFetchAction(calculateTradeSwapDeposit, { - dependencies: [potentialTradeDetails?.margin.toString()], - }) - const positionSide = useMemo(() => { if (potentialTradeDetails?.size.eq(ZERO_WEI)) { return position?.activePosition?.side === PositionSide.LONG @@ -139,19 +121,7 @@ export default function TradeConfirmationModal({ ? t('futures.market.trade.confirmation.modal.eth-bal-warning') : null - const quoteInvalidError = useMemo(() => { - return !!quoteInvalidReason - ? t(`futures.market.trade.confirmation.modal.quote-invalid-error-${quoteInvalidReason}`) - : null - }, [quoteInvalidReason, t]) - const disabledReason = useMemo(() => { - if (!!quoteInvalidReason) { - return t('futures.market.trade.confirmation.modal.disabled-quote-invalid') - } - if (!quote && quoteLoading) { - return t('futures.market.trade.confirmation.modal.disabled-quote-loading') - } if (showEthBalWarning) { return t('futures.market.trade.confirmation.modal.disabled-eth-bal', { depositAmount: formatNumber(stripZeros(keeperFee?.toString()), { suggestDecimals: true }), @@ -169,15 +139,11 @@ export default function TradeConfirmationModal({ keeperFee, overridePriceProtection, positionDetails?.exceedsPriceProtection, - quoteInvalidReason, - quote, - quoteLoading, ]) const handleDismiss = useCallback(() => { - dispatch(clearTradeSwapDepositQuote()) onDismiss() - }, [dispatch, onDismiss]) + }, [onDismiss]) const buttonText = allowanceValid ? t(`futures.market.trade.confirmation.modal.confirm-order.${leverageSide}`) @@ -211,7 +177,6 @@ export default function TradeConfirmationModal({ - {SWAP_DEPOSIT_TRADE_ENABLED && } {positionDetails?.exceedsPriceProtection && ( )} @@ -447,53 +412,6 @@ const GasFeeRow: FC = ({ gasFee }) => { return null } -const SwapRow = () => { - const swapDepositToken = useAppSelector(selectSelectedSwapDepositToken) - const [expanded, toggleExpanded] = useReducer((e) => !e, false) - - const { tradeSwapDepositQuote } = useAppSelector(selectSmartMarginQueryStatuses) - const quote = useAppSelector(selectTradeSwapDepositQuote) - - if (swapDepositToken === SwapDepositToken.SUSD) return null - - return ( - - ) : quote ? ( - `${formatNumber(quote?.amountIn ?? 0, { suggestDecimals: true })} ${ - quote?.token - } -> ${formatNumber(quote?.amountOut ?? 0, { suggestDecimals: true })} sUSD` - ) : ( - '-' - ) - } - > - - - ) -} - -const ExchangeRateRow = () => { - const swapDepositToken = useAppSelector(selectSelectedSwapDepositToken) - const quote = useAppSelector(selectTradeSwapDepositQuote) - const price = useMemo(() => { - if (!quote) return 0 - return quote.amountOut.div(quote.amountIn) - }, [quote]) - - return ( - - ) -} - const StyledBaseModal = styled(BaseModal)` [data-reach-dialog-content] { width: 400px; diff --git a/packages/app/src/state/futures/common/types.ts b/packages/app/src/state/futures/common/types.ts index c30a735df..e918eb0d7 100644 --- a/packages/app/src/state/futures/common/types.ts +++ b/packages/app/src/state/futures/common/types.ts @@ -64,7 +64,6 @@ export type SmartMarginQueryStatuses = FuturesQueryStatuses & { smartMarginBalanceInfo: QueryStatus swapDepositBalanceQuote: QueryStatus swapDepositQuote: QueryStatus - tradeSwapDepositQuote: QueryStatus } export type TradeSizeInputs = { diff --git a/packages/app/src/state/futures/smartMargin/actions.ts b/packages/app/src/state/futures/smartMargin/actions.ts index 050e97eb3..90bcfd3ea 100644 --- a/packages/app/src/state/futures/smartMargin/actions.ts +++ b/packages/app/src/state/futures/smartMargin/actions.ts @@ -51,6 +51,7 @@ import { import { fetchBalances } from 'state/balances/actions' import { EST_KEEPER_GAS_FEE, ZERO_CM_FEES, ZERO_STATE_TRADE_INPUTS } from 'state/constants' import { serializeWeiObject } from 'state/helpers' +import { selectSelectedEpoch } from 'state/staking/selectors' import { AppDispatch, AppThunk, RootState } from 'state/store' import { ThunkConfig } from 'state/types' import { selectNetwork, selectWallet } from 'state/wallet/selectors' @@ -98,13 +99,7 @@ import { clearSmartMarginTradePreviews, setKeeperDeposit, } from './reducer' -import { - selectIdleAccountMargin, - selectSelectedSwapDepositToken, - selectSwapDepositBalance, - selectSwapDepositBalanceQuote, - selectTradeSwapDepositQuote, -} from './selectors' +import { selectSelectedSwapDepositToken, selectSwapDepositBalance } from './selectors' import { selectSmartMarginAccount, selectSmartMarginMarginDelta, @@ -137,7 +132,6 @@ import { selectAllSmartMarginPositions, } from './selectors' import { SmartMarginBalanceInfo } from './types' -import { selectSelectedEpoch } from 'state/staking/selectors' export const fetchMarketsV2 = createAsyncThunk< { markets: PerpsMarketV2[]; networkId: NetworkId } | undefined, @@ -429,64 +423,6 @@ export const fetchSmartMarginTradePreview = createAsyncThunk< } ) -export const calculateTradeSwapDeposit = createAsyncThunk< - | { - token: SwapDepositToken - amountIn: string - amountOut: string - quoteInvalidReason?: `insufficient-${'balance' | 'quote'}` - } - | undefined, - void, - ThunkConfig ->('futures/calculateTradeSwapDeposit', async (_, { getState }) => { - const state = getState() - const wallet = selectWallet(state) - const marketInfo = selectV2MarketInfo(state) - const swapDepositToken = selectSelectedSwapDepositToken(state) - const marginDelta = selectSmartMarginMarginDelta(state) - const idleMargin = selectIdleAccountMargin(state) - const swapDepositBalance = selectSwapDepositBalance(state) - const balanceQuote = selectSwapDepositBalanceQuote(state) - - if (!wallet || !marketInfo || !swapDepositToken || swapDepositToken === SwapDepositToken.SUSD) - return - - try { - const requiredSwapDeposit = marginDelta.sub(idleMargin) - - if (requiredSwapDeposit.lte(0)) { - return - } - - // Add some buffer to account for price change since quote - // but keeping within the bounds of original balance quote - - const depositWithBuffer = requiredSwapDeposit.add( - requiredSwapDeposit.mul(SWAP_QUOTE_BUFFER).div(100) - ) - - const tokenInAmount = depositWithBuffer.div(balanceQuote?.rate || 0) - - let quoteInvalidReason: `insufficient-${'balance' | 'quote'}` | undefined = undefined - - if (tokenInAmount.gt(swapDepositBalance)) { - quoteInvalidReason = 'insufficient-balance' - } - - return { - token: swapDepositToken, - amountIn: tokenInAmount.toString(), - amountOut: requiredSwapDeposit.toString(), - quoteInvalidReason, - } - } catch (err) { - logError(err) - notifyError('Failed to calculate swap deposit', err) - throw err - } -}) - export const clearTradeInputs = createAsyncThunk( 'futures/clearTradeInputs', async (_, { dispatch }) => { @@ -1079,7 +1015,6 @@ export const submitSmartMarginOrder = createAsyncThunk { smartMargin.swapDepositCustomSlippage = action.payload }, - clearTradeSwapDepositQuote: (smartMargin) => { - smartMargin.tradeSwapDepositQuote = undefined - }, }, extraReducers: (builder) => { // Markets @@ -549,20 +544,6 @@ const smartMarginSlice = createSlice({ error: 'Failed to fetch quote for the swap deposit token', } }) - - builder.addCase(calculateTradeSwapDeposit.pending, (futuresState) => { - futuresState.queryStatuses.tradeSwapDepositQuote = LOADING_STATUS - }) - builder.addCase(calculateTradeSwapDeposit.fulfilled, (futuresState, action) => { - futuresState.queryStatuses.tradeSwapDepositQuote = SUCCESS_STATUS - futuresState.tradeSwapDepositQuote = action.payload - }) - builder.addCase(calculateTradeSwapDeposit.rejected, (futuresState) => { - futuresState.queryStatuses.tradeSwapDepositQuote = { - status: FetchStatus.Error, - error: 'Failed to fetch quote for the swap deposit', - } - }) }, }) @@ -596,7 +577,6 @@ export const { setSLTPModalTakeProfit, setSwapDepositSlippage, setSwapDepositCustomSlippage, - clearTradeSwapDepositQuote, } = smartMarginSlice.actions const findWalletForAccount = ( diff --git a/packages/app/src/state/futures/smartMargin/selectors.ts b/packages/app/src/state/futures/smartMargin/selectors.ts index a643f0772..16507915d 100644 --- a/packages/app/src/state/futures/smartMargin/selectors.ts +++ b/packages/app/src/state/futures/smartMargin/selectors.ts @@ -614,14 +614,6 @@ export const selectSmartMarginAllowanceValid = createSelector( } ) -export const selectQuoteInvalidReason = (state: RootState) => - state.smartMargin.tradeSwapDepositQuote?.quoteInvalidReason - -export const selectSwapDepositQuoteLoading = createSelector( - (state: RootState) => state.smartMargin.queryStatuses.tradeSwapDepositQuote, - ({ status }) => status === FetchStatus.Loading -) - export const selectWithdrawableSmartMargin = createSelector( selectAvailableMarginInMarkets, selectSmartMarginBalanceInfo, @@ -1254,16 +1246,3 @@ export const selectSwapDepositSlippage = createSelector( (state: RootState) => state.smartMargin.swapDepositSlippage, (customSlippage, slippage) => (customSlippage ? Number(customSlippage) : slippage ?? 0) ) - -export const selectTradeSwapDepositQuote = createSelector( - (state: RootState) => state.smartMargin.tradeSwapDepositQuote, - (quote) => { - return quote - ? { - token: quote.token, - amountIn: wei(quote.amountIn), - amountOut: wei(quote.amountOut), - } - : null - } -) diff --git a/packages/app/src/state/futures/smartMargin/types.ts b/packages/app/src/state/futures/smartMargin/types.ts index 22c7563d2..e60c31d40 100644 --- a/packages/app/src/state/futures/smartMargin/types.ts +++ b/packages/app/src/state/futures/smartMargin/types.ts @@ -161,10 +161,4 @@ export type SmartMarginState = { } swapDepositSlippage: number swapDepositCustomSlippage: string - tradeSwapDepositQuote?: { - token: SwapDepositToken - amountIn: string - amountOut: string - quoteInvalidReason?: `insufficient-${'balance' | 'quote'}` - } } From 993b2b5ad9e8bff3677032df530162988a642ba5 Mon Sep 17 00:00:00 2001 From: platschi Date: Sun, 24 Sep 2023 16:14:46 -0300 Subject: [PATCH 2/2] chore(*): bump version --- package.json | 2 +- packages/app/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 49db722a7..76904dc65 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kwenta", - "version": "7.9.0", + "version": "7.9.3", "description": "Kwenta", "main": "index.js", "scripts": { diff --git a/packages/app/package.json b/packages/app/package.json index 8fcd34108..d33452144 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -1,6 +1,6 @@ { "name": "@kwenta/app", - "version": "7.9.0", + "version": "7.9.3", "scripts": { "dev": "next", "build": "next build",