From 095b155de62eb753c574a36dece254cf23da9ff0 Mon Sep 17 00:00:00 2001 From: Alfetopito Date: Tue, 15 Oct 2024 16:38:15 +0100 Subject: [PATCH] feat: use widget deadline on twap form --- .../twap/containers/TwapFormWidget/index.tsx | 50 +++++++++++++++++-- .../twap/pure/DeadlineSelector/index.tsx | 40 +++++++++++---- 2 files changed, 77 insertions(+), 13 deletions(-) diff --git a/apps/cowswap-frontend/src/modules/twap/containers/TwapFormWidget/index.tsx b/apps/cowswap-frontend/src/modules/twap/containers/TwapFormWidget/index.tsx index fb804210f3..f96ae76a33 100644 --- a/apps/cowswap-frontend/src/modules/twap/containers/TwapFormWidget/index.tsx +++ b/apps/cowswap-frontend/src/modules/twap/containers/TwapFormWidget/index.tsx @@ -3,9 +3,11 @@ import { useEffect, useLayoutEffect, useMemo, useState } from 'react' import { renderTooltip } from '@cowprotocol/ui' import { useWalletInfo } from '@cowprotocol/wallet' +import { TradeType } from '@cowprotocol/widget-lib' import { useAdvancedOrdersDerivedState } from 'modules/advancedOrders' import { openAdvancedOrdersTabAnalytics, twapWalletCompatibilityAnalytics } from 'modules/analytics' +import { useInjectedWidgetDeadline } from 'modules/injectedWidget' import { useReceiveAmountInfo } from 'modules/trade' import { useIsWrapOrUnwrap } from 'modules/trade/hooks/useIsWrapOrUnwrap' import { useTradeState } from 'modules/trade/hooks/useTradeState' @@ -21,7 +23,14 @@ import { useRateInfoParams } from 'common/hooks/useRateInfoParams' import * as styledEl from './styled' import { LABELS_TOOLTIPS } from './tooltips' -import { DEFAULT_NUM_OF_PARTS, DEFAULT_TWAP_SLIPPAGE, MAX_TWAP_SLIPPAGE, ORDER_DEADLINES } from '../../const' +import { + DEFAULT_NUM_OF_PARTS, + DEFAULT_TWAP_SLIPPAGE, + MAX_PART_TIME, + MAX_TWAP_SLIPPAGE, + MINIMUM_PART_TIME, + ORDER_DEADLINES, +} from '../../const' import { useFallbackHandlerVerification, useIsFallbackHandlerCompatible, @@ -65,9 +74,43 @@ export function TwapFormWidget() { const limitPriceAfterSlippage = usePrice( receiveAmountInfo?.afterSlippage.sellAmount, - receiveAmountInfo?.afterSlippage.buyAmount + receiveAmountInfo?.afterSlippage.buyAmount, ) + const widgetDeadline = useInjectedWidgetDeadline(TradeType.ADVANCED) + + useEffect(() => { + if (widgetDeadline) { + // Ensure min part duration + const minDuration = Math.floor(MINIMUM_PART_TIME / 60) * 2 // it must have at least 2 parts + + const maxDuration = Math.floor(MAX_PART_TIME / 60) * numberOfPartsValue + + let minutes = widgetDeadline + if (widgetDeadline < minDuration) { + minutes = minDuration + } else if (widgetDeadline > maxDuration) { + minutes = maxDuration + } + + console.log( + `fuck:twap deadline`, + widgetDeadline, + minDuration, + maxDuration, + minutes, + new Date(widgetDeadline * 60 * 1000 + Date.now()), + ) + + updateSettingsState({ + customDeadline: { hours: 0, minutes }, + isCustomDeadline: true, + }) + } + }, [widgetDeadline, updateSettingsState, numberOfPartsValue]) + + const isDeadlineDisabled = !!widgetDeadline + const deadlineState = { deadline, customDeadline, @@ -166,8 +209,9 @@ export function TwapFormWidget() { updateSettingsState(value)} + setDeadline={updateSettingsState} label={LABELS_TOOLTIPS.totalDuration.label} tooltip={renderTooltip(LABELS_TOOLTIPS.totalDuration.tooltip, { parts: numberOfPartsValue, diff --git a/apps/cowswap-frontend/src/modules/twap/pure/DeadlineSelector/index.tsx b/apps/cowswap-frontend/src/modules/twap/pure/DeadlineSelector/index.tsx index 8169b99b0a..6999fd7636 100644 --- a/apps/cowswap-frontend/src/modules/twap/pure/DeadlineSelector/index.tsx +++ b/apps/cowswap-frontend/src/modules/twap/pure/DeadlineSelector/index.tsx @@ -1,7 +1,6 @@ import React, { useCallback, useMemo, useState } from 'react' -import { UI } from '@cowprotocol/ui' -import { renderTooltip } from '@cowprotocol/ui' +import { renderTooltip, UI } from '@cowprotocol/ui' import styled from 'styled-components/macro' @@ -10,14 +9,17 @@ import { Content } from 'modules/trade/pure/TradeWidgetField/styled' import { LabelTooltip } from 'modules/twap' import { customDeadlineToSeconds, deadlinePartsDisplay } from 'modules/twap/utils/deadlinePartsDisplay' +import { TradeWidgetField } from '../../../trade/pure/TradeWidgetField' import { defaultCustomDeadline, TwapOrdersDeadline } from '../../state/twapOrdersSettingsAtom' import { CustomDeadlineSelector } from '../CustomDeadlineSelector' interface DeadlineSelectorProps { items: TradeSelectItem[] deadline: TwapOrdersDeadline + isDeadlineDisabled: boolean label: LabelTooltip['label'] tooltip: LabelTooltip['tooltip'] + setDeadline(value: TwapOrdersDeadline): void } @@ -48,10 +50,22 @@ const StyledTradeSelect = styled(TradeSelect)` } ` +const StyledTradeField = styled(TradeWidgetField)` + ${Content} { + width: 100%; + color: inherit; + } + + ${Content} > div { + width: 100%; + } +` + export function DeadlineSelector(props: DeadlineSelectorProps) { const { items, deadline: { deadline, customDeadline, isCustomDeadline }, + isDeadlineDisabled, label, tooltip, setDeadline, @@ -74,7 +88,7 @@ export function DeadlineSelector(props: DeadlineSelectorProps) { }) } }, - [setIsCustomModalOpen, setDeadline] + [setIsCustomModalOpen, setDeadline], ) const activeLabel = useMemo(() => { @@ -87,13 +101,19 @@ export function DeadlineSelector(props: DeadlineSelectorProps) { return ( <> - + {isDeadlineDisabled ? ( + +
{activeLabel}
+
+ ) : ( + + )} setDeadline({ isCustomDeadline: true, customDeadline: value, deadline: 0 })} customDeadline={customDeadline}