Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(twap): display a warning when fbh was changed after twap order #5202

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAtomValue, useSetAtom } from 'jotai'
import { useCallback, useEffect, useMemo } from 'react'
import { ReactNode, useCallback, useEffect, useMemo } from 'react'

import { useTokensAllowances, useTokensBalances } from '@cowprotocol/balances-and-allowances'
import { useIsSafeViaWc, useWalletDetails, useWalletInfo } from '@cowprotocol/wallet'
Expand Down Expand Up @@ -60,12 +60,14 @@ interface OrdersTableWidgetProps {
displayOrdersOnlyForSafeApp: boolean
orders: Order[]
orderType: TabOrderTypes
children?: ReactNode
}

export function OrdersTableWidget({
orders: allOrders,
orderType,
displayOrdersOnlyForSafeApp,
children,
}: OrdersTableWidgetProps) {
const { chainId, account } = useWalletInfo()
const location = useLocation()
Expand Down Expand Up @@ -122,14 +124,14 @@ export function OrdersTableWidget({
(orders: ParsedOrder[]) => {
updateOrdersToCancel(orders)
},
[updateOrdersToCancel]
[updateOrdersToCancel],
)

const toggleOrderForCancellation = useCallback(
(order: ParsedOrder) => {
updateOrdersToCancel(toggleOrderInCancellationList(ordersToCancel, order))
},
[ordersToCancel, updateOrdersToCancel]
[ordersToCancel, updateOrdersToCancel],
)

const getShowCancellationModal = useCallback(
Expand All @@ -138,7 +140,7 @@ export function OrdersTableWidget({

return rawOrder ? cancelOrder(rawOrder) : null
},
[allOrders, cancelOrder]
[allOrders, cancelOrder],
)

const getAlternativeOrderModalContext = useGetAlternativeOrderModalContextCallback()
Expand Down Expand Up @@ -167,6 +169,7 @@ export function OrdersTableWidget({
<>
<PendingPermitUpdater orders={ordersToCheckPendingPermit} />
<ContentWrapper>
{children}
<OrdersTableContainer
chainId={chainId}
tabs={tabs}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { useSetAtom } from 'jotai/index'
import { useEffect, useState } from 'react'

import { usePrevious } from '@cowprotocol/common-hooks'
import { ButtonPrimary, InlineBanner, Loader, BannerOrientation, UI } from '@cowprotocol/ui'
import { useWalletInfo } from '@cowprotocol/wallet'

import styled from 'styled-components/macro'

import { useAllTransactions } from 'legacy/state/enhancedTransactions/hooks'

import { useExtensibleFallbackContext } from '../../hooks/useExtensibleFallbackContext'
import { useSetupFallbackHandler } from '../../hooks/useSetupFallbackHandler'
import { verifyExtensibleFallback } from '../../services/verifyExtensibleFallback'
import { updateFallbackHandlerVerificationAtom } from '../../state/fallbackHandlerVerificationAtom'

const Banner = styled(InlineBanner)`
/* TODO: Make all these part of the InlineBanner props */
position: relative;
font-size: 15px;

> span {
gap: 20px;
}

> span > span {
gap: 20px;
}

&::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: var(${UI.COLOR_PAPER});
z-index: -1;
border-radius: inherit;
}

&::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: inherit;
z-index: -1;
border-radius: inherit;
}
`

const ActionButton = styled(ButtonPrimary)`
display: inline-block;
width: 100%;
font-size: 16px;
padding: 16px 24px;
min-height: auto;
`

export function SetupFallbackHandlerWarning() {
const [pendingTxHash, setPendingTxHash] = useState<string | null>(null)

const { account } = useWalletInfo()
const setupFallbackHandler = useSetupFallbackHandler()
const isTransactionPending = useIsTransactionPending(pendingTxHash)
const prevIsTransactionPending = usePrevious(isTransactionPending)
const txWasMined = prevIsTransactionPending === true && isTransactionPending === false

const updateFallbackHandlerVerification = useSetAtom(updateFallbackHandlerVerificationAtom)

const extensibleFallbackContext = useExtensibleFallbackContext()

const handleUpdateClick = async () => {
const txHash = await setupFallbackHandler()

if (txHash) {
setPendingTxHash(txHash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A re-render of this would loose the posted TX, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!
Fixed it: 35f1802
Thanks!

}
}

useEffect(() => {
if (!txWasMined) return

if (!extensibleFallbackContext || !account) return

verifyExtensibleFallback(extensibleFallbackContext).then((result) => {
updateFallbackHandlerVerification({ [account]: result })
})
}, [txWasMined, account, extensibleFallbackContext, updateFallbackHandlerVerification])

return (
<Banner
bannerType="danger"
backDropBlur
orientation={BannerOrientation.Vertical}
iconSize={46}
noWrapContent
padding="20px"
>
<span>
<p>
Your Safe fallback handler was changed after TWAP orders were placed. All open TWAP orders are not getting
created because of that. Please, update the fallback handler in order to make the orders work again.
</p>
<ActionButton disabled={isTransactionPending} onClick={handleUpdateClick}>
{isTransactionPending ? <Loader /> : 'Update fallback handler'}
</ActionButton>
</span>
</Banner>
)
}

function useIsTransactionPending(txHash: string | null): boolean {
const allTransactions = useAllTransactions()

if (!txHash) return false

return Object.keys(allTransactions).some((hash) => {
const tx = allTransactions[hash]

if (!tx || tx.receipt || tx.replacementType || tx.errorMessage) return false

return tx.hash === txHash
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useSafeAppsSdk } from '@cowprotocol/wallet'

import { useExtensibleFallbackContext } from './useExtensibleFallbackContext'

import { useTransactionAdder } from '../../../legacy/state/enhancedTransactions/hooks'
import { extensibleFallbackSetupTxs } from '../services/extensibleFallbackSetupTxs'

export function useSetupFallbackHandler() {
const safeAppsSdk = useSafeAppsSdk()
const extensibleFallbackContext = useExtensibleFallbackContext()
const addTransaction = useTransactionAdder()

return async () => {
if (!safeAppsSdk || !extensibleFallbackContext) return

const fallbackSetupTxs = await extensibleFallbackSetupTxs(extensibleFallbackContext)
const { safeTxHash } = await safeAppsSdk.txs.send({ txs: fallbackSetupTxs })

addTransaction({
hash: safeTxHash,
summary: 'Setup TWAP fallback handler',
})

return safeTxHash
}
}
2 changes: 2 additions & 0 deletions apps/cowswap-frontend/src/modules/twap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ export * from './hooks/useCancelTwapOrder'
export * from './hooks/useMapTwapCurrencyInfo'
export * from './hooks/useTwapFormState'
export * from './hooks/useTwapSlippage'
export { useIsFallbackHandlerRequired } from './hooks/useFallbackHandlerVerification'
export { SetupFallbackHandlerWarning } from './containers/SetupFallbackHandlerWarning'
export * from './updaters/index'
export * from './types'
12 changes: 8 additions & 4 deletions apps/cowswap-frontend/src/pages/AdvancedOrders/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useAtomValue } from 'jotai'

import { PENDING_STATES } from 'legacy/state/orders/actions'

import {
advancedOrdersAtom,
AdvancedOrdersWidget,
Expand All @@ -10,38 +12,40 @@ import { useInjectedWidgetParams } from 'modules/injectedWidget'
import { OrdersTableWidget, TabOrderTypes } from 'modules/ordersTable'
import * as styledEl from 'modules/trade/pure/TradePageLayout'
import {
SetupFallbackHandlerWarning,
TwapConfirmModal,
TwapFormWidget,
TwapUpdaters,
useAllEmulatedOrders,
useIsFallbackHandlerRequired,
useMapTwapCurrencyInfo,
useTwapFormState,
useTwapSlippage,
} from 'modules/twap'
import { TwapFormState } from 'modules/twap/pure/PrimaryActionButton/getTwapFormState'


export default function AdvancedOrdersPage() {
const { isUnlocked } = useAtomValue(advancedOrdersAtom)

const allEmulatedOrders = useAllEmulatedOrders()
const isFallbackHandlerRequired = useIsFallbackHandlerRequired()

const twapFormValidation = useTwapFormState()
const twapSlippage = useTwapSlippage()
const mapTwapCurrencyInfo = useMapTwapCurrencyInfo()
const { hideOrdersTable } = useInjectedWidgetParams()

const disablePriceImpact = twapFormValidation === TwapFormState.SELL_AMOUNT_TOO_SMALL

const advancedWidgetParams = { disablePriceImpact }

const { hideOrdersTable } = useInjectedWidgetParams()
const pendingOrders = allEmulatedOrders.filter((order) => PENDING_STATES.includes(order.status))

return (
<>
<FillAdvancedOrdersDerivedStateUpdater slippage={twapSlippage} />
<SetupAdvancedOrderAmountsFromUrlUpdater />
<styledEl.PageWrapper isUnlocked={isUnlocked}>
<styledEl.PrimaryWrapper>
{isFallbackHandlerRequired && pendingOrders.length > 0 && <SetupFallbackHandlerWarning />}
<AdvancedOrdersWidget
updaters={<TwapUpdaters />}
confirmContent={<TwapConfirmModal />}
Expand Down
Loading