-
Notifications
You must be signed in to change notification settings - Fork 30
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
feat(synapse-interface): confirm new price [SLT-150] #3084
Changes from 20 commits
38dec50
f44b62c
3e38869
bd29349
76663ba
18c8176
4e37c9a
6d14aca
cd75683
febd306
e7df238
69f4034
571b391
c7b2981
62d7735
93dc9da
e7e2f31
32de57c
d04b69a
f1118b1
f682d73
cd83681
c47a4b6
03549a6
e8eb2a0
5ad3190
0d729b1
e8000fc
ef5a504
9abf1b4
5c42768
10fd34c
eea56c7
835003c
c2c185b
194799b
1112571
54a9e01
fae7078
c4701d8
b383e86
e14285d
c7e3206
6f57fd6
bd4294a
c80c956
d580509
080c5ff
1c5414f
5623a0d
6436cab
6266b40
82d447a
383519d
f74ca92
c63eb5d
8627728
8dbadb9
907d0e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { BridgeQuote } from '@/utils/types' | ||
import { useState, useEffect } from 'react' | ||
|
||
export const BridgeQuoteResetTimer = ({ | ||
bridgeQuote, | ||
hasValidQuote, | ||
duration, // in ms | ||
}: { | ||
bridgeQuote: BridgeQuote | ||
hasValidQuote: boolean | ||
duration: number | ||
}) => { | ||
if (hasValidQuote) { | ||
return ( | ||
<AnimatedProgressCircle animateKey={bridgeQuote.id} duration={duration} /> | ||
) | ||
} | ||
} | ||
|
||
const AnimatedProgressCircle = ({ | ||
animateKey, | ||
duration, | ||
}: { | ||
animateKey: string | ||
duration: number | ||
}) => { | ||
const [animationKey, setAnimationKey] = useState(0) | ||
|
||
useEffect(() => { | ||
setAnimationKey((prevKey) => prevKey + 1) | ||
}, [animateKey]) | ||
|
||
return ( | ||
<svg | ||
key={animationKey} | ||
width="24" | ||
height="24" | ||
viewBox="-12 -12 24 24" | ||
stroke="currentcolor" | ||
strokeOpacity=".33" | ||
fill="none" | ||
className="absolute -rotate-90 -scale-y-100 right-4" | ||
> | ||
<circle r="8" /> | ||
<circle r="8" strokeDasharray="1" pathLength="1"> | ||
<animate | ||
attributeName="stroke-dashoffset" | ||
values="1; 2" | ||
dur={`${convertMsToSeconds(duration)}s`} | ||
/> | ||
</circle> | ||
</svg> | ||
) | ||
} | ||
|
||
const convertMsToSeconds = (ms: number) => { | ||
return Math.ceil(ms / 1000) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { useState, useEffect, useMemo, useRef } from 'react' | ||
|
||
import { useBridgeState } from '@/slices/bridge/hooks' | ||
import { useBridgeQuoteState } from '@/slices/bridgeQuote/hooks' | ||
import { constructStringifiedBridgeSelections } from './useBridgeValidations' | ||
import { BridgeQuote } from '@/utils/types' | ||
|
||
export const useConfirmNewBridgePrice = () => { | ||
const quoteRef = useRef<any>(null) | ||
|
||
const [hasQuoteOutputChanged, setHasQuoteOutputChanged] = | ||
useState<boolean>(false) | ||
const [hasUserConfirmedChange, setHasUserConfirmedChange] = | ||
useState<boolean>(false) | ||
|
||
const { bridgeQuote, previousBridgeQuote } = useBridgeQuoteState() | ||
const { debouncedFromValue, fromToken, toToken, fromChainId, toChainId } = | ||
useBridgeState() | ||
|
||
const currentBridgeQuoteSelections = useMemo( | ||
() => | ||
constructStringifiedBridgeSelections( | ||
debouncedFromValue, | ||
fromChainId, | ||
fromToken, | ||
toChainId, | ||
toToken | ||
), | ||
[debouncedFromValue, fromChainId, fromToken, toChainId, toToken] | ||
) | ||
|
||
const previousBridgeQuoteSelections = useMemo( | ||
() => | ||
constructStringifiedBridgeSelections( | ||
previousBridgeQuote?.inputAmountForQuote, | ||
previousBridgeQuote?.originChainId, | ||
previousBridgeQuote?.originTokenForQuote, | ||
previousBridgeQuote?.destChainId, | ||
previousBridgeQuote?.destTokenForQuote | ||
), | ||
[previousBridgeQuote] | ||
) | ||
|
||
const hasSameSelectionsAsPreviousQuote = useMemo( | ||
() => currentBridgeQuoteSelections === previousBridgeQuoteSelections, | ||
[currentBridgeQuoteSelections, previousBridgeQuoteSelections] | ||
) | ||
|
||
useEffect(() => { | ||
const validQuotes = | ||
bridgeQuote?.outputAmount && previousBridgeQuote?.outputAmount | ||
|
||
const outputAmountDiffMoreThan1bps = validQuotes | ||
? calculateOutputRelativeDifference( | ||
bridgeQuote, | ||
quoteRef.current ?? previousBridgeQuote | ||
) > 0.0001 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth putting this into a constant if we need to update |
||
: false | ||
|
||
if ( | ||
validQuotes && | ||
outputAmountDiffMoreThan1bps && | ||
hasSameSelectionsAsPreviousQuote | ||
) { | ||
requestUserConfirmChange(previousBridgeQuote) | ||
} else { | ||
resetConfirm() | ||
} | ||
}, [bridgeQuote, previousBridgeQuote, hasSameSelectionsAsPreviousQuote]) | ||
|
||
const requestUserConfirmChange = (previousQuote: BridgeQuote) => { | ||
if (!hasQuoteOutputChanged && !hasUserConfirmedChange) { | ||
quoteRef.current = previousQuote | ||
setHasQuoteOutputChanged(true) | ||
} | ||
setHasUserConfirmedChange(false) | ||
} | ||
|
||
const resetConfirm = () => { | ||
if (hasUserConfirmedChange) { | ||
quoteRef.current = null | ||
setHasQuoteOutputChanged(false) | ||
setHasUserConfirmedChange(false) | ||
} | ||
} | ||
|
||
const onUserAcceptChange = () => { | ||
quoteRef.current = null | ||
setHasUserConfirmedChange(true) | ||
} | ||
|
||
return { | ||
hasSameSelectionsAsPreviousQuote, | ||
hasQuoteOutputChanged, | ||
hasUserConfirmedChange, | ||
onUserAcceptChange, | ||
} | ||
} | ||
|
||
const calculateOutputRelativeDifference = ( | ||
quoteA?: BridgeQuote, | ||
quoteB?: BridgeQuote | ||
) => { | ||
if (!quoteA?.outputAmountString || !quoteB?.outputAmountString) return null | ||
|
||
const outputA = parseFloat(quoteA.outputAmountString) | ||
const outputB = parseFloat(quoteB.outputAmountString) | ||
|
||
return Math.abs(outputA - outputB) / outputB | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -67,6 +67,7 @@ import { useBridgeQuoteState } from '@/slices/bridgeQuote/hooks' | |||||||||||||
import { resetBridgeQuote } from '@/slices/bridgeQuote/reducer' | ||||||||||||||
import { fetchBridgeQuote } from '@/slices/bridgeQuote/thunks' | ||||||||||||||
import { useIsBridgeApproved } from '@/utils/hooks/useIsBridgeApproved' | ||||||||||||||
import { isTransactionUserRejectedError } from '@/utils/isTransactionUserRejectedError' | ||||||||||||||
|
||||||||||||||
const StateManagedBridge = () => { | ||||||||||||||
const dispatch = useAppDispatch() | ||||||||||||||
|
@@ -136,8 +137,6 @@ const StateManagedBridge = () => { | |||||||||||||
|
||||||||||||||
// will have to handle deadlineMinutes here at later time, gets passed as optional last arg in .bridgeQuote() | ||||||||||||||
|
||||||||||||||
/* clear stored bridge quote before requesting new bridge quote */ | ||||||||||||||
dispatch(resetBridgeQuote()) | ||||||||||||||
const currentTimestamp: number = getUnixTimeMinutesFromNow(0) | ||||||||||||||
|
||||||||||||||
try { | ||||||||||||||
|
@@ -198,7 +197,7 @@ const StateManagedBridge = () => { | |||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
useStaleQuoteUpdater( | ||||||||||||||
const isStale = useStaleQuoteUpdater( | ||||||||||||||
bridgeQuote, | ||||||||||||||
getAndSetBridgeQuote, | ||||||||||||||
isLoading, | ||||||||||||||
|
@@ -398,6 +397,10 @@ const StateManagedBridge = () => { | |||||||||||||
) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (isTransactionUserRejectedError) { | ||||||||||||||
getAndSetBridgeQuote() | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix incorrect condition in error handling The condition Apply this diff to fix the condition: -if (isTransactionUserRejectedError) {
+if (isTransactionUserRejectedError(error)) {
getAndSetBridgeQuote()
} Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
return txErrorHandler(error) | ||||||||||||||
} finally { | ||||||||||||||
dispatch(setIsWalletPending(false)) | ||||||||||||||
|
@@ -452,6 +455,8 @@ const StateManagedBridge = () => { | |||||||||||||
approveTxn={approveTxn} | ||||||||||||||
executeBridge={executeBridge} | ||||||||||||||
isBridgePaused={isBridgePaused} | ||||||||||||||
isQuoteStale={isStale} | ||||||||||||||
quoteTimeout={quoteTimeout} | ||||||||||||||
/> | ||||||||||||||
</> | ||||||||||||||
)} | ||||||||||||||
|
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.
bridgeModule
could change as well. Example: User first gets RFQ quote, and then inventory level changes. The quote is now throughCCTP
. Now the price/the module/ the fees are all different."Confirm new quote" (instead of price) covers both cases. Does the plumbing in the hook cover this as well?
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.
Good catch - confirming that the hook covers the scenario that
bridgeModule
changes while bridge selections remain the same as previous quote.The
useConfirmNewPrice()
hook will check for changes greater than 1bps given current and previous quotes share the same origin input amount, origin chainId, origin token, destination chainId, and destination token.Updated button text in f682d73