-
Notifications
You must be signed in to change notification settings - Fork 101
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
383c1a0
fix(twap): display a warning when fbh was changed after twap order
shoom3301 66d5600
chore: add loading state
shoom3301 cb7d0e1
chore: fix pendingOrders
shoom3301 53a1ef0
Merge branch 'develop' into feat/twap-fbh-warning
shoom3301 1dbcf8d
feat: style TWAP warning banner (#5204)
fairlighteth 35f1802
fix: persist pendingTxHash in atom
shoom3301 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
apps/cowswap-frontend/src/modules/twap/containers/SetupFallbackHandlerWarning/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { atom, useAtom } from 'jotai' | ||
import { useSetAtom } from 'jotai/index' | ||
import { useEffect } 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; | ||
` | ||
|
||
const pendingTxHashAtom = atom<string | null>(null) | ||
|
||
export function SetupFallbackHandlerWarning() { | ||
const [pendingTxHash, setPendingTxHash] = useAtom(pendingTxHashAtom) | ||
|
||
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) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (!txWasMined) return | ||
|
||
setPendingTxHash(null) | ||
|
||
if (!extensibleFallbackContext || !account) return | ||
|
||
verifyExtensibleFallback(extensibleFallbackContext).then((result) => { | ||
updateFallbackHandlerVerification({ [account]: result }) | ||
}) | ||
}, [txWasMined, account, extensibleFallbackContext, updateFallbackHandlerVerification, setPendingTxHash]) | ||
|
||
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 | ||
}) | ||
} |
26 changes: 26 additions & 0 deletions
26
apps/cowswap-frontend/src/modules/twap/hooks/useSetupFallbackHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!