-
Notifications
You must be signed in to change notification settings - Fork 32
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 51 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,113 @@ | ||
import { useState, useEffect, useMemo } from 'react' | ||
|
||
import { BridgeQuote } from '@/utils/types' | ||
import { convertMsToSeconds } from '@/utils/time' | ||
|
||
export const BridgeQuoteResetTimer = ({ | ||
bridgeQuote, | ||
isLoading, | ||
isActive, | ||
duration, // in ms | ||
}: { | ||
bridgeQuote: BridgeQuote | ||
isLoading: boolean | ||
isActive: boolean | ||
duration: number | ||
}) => { | ||
const memoizedTimer = useMemo(() => { | ||
if (!isActive) return null | ||
|
||
if (isLoading) { | ||
return <AnimatedLoadingCircle /> | ||
} else { | ||
return ( | ||
<AnimatedProgressCircle | ||
animateKey={bridgeQuote.id} | ||
duration={duration} | ||
/> | ||
) | ||
} | ||
}, [bridgeQuote, duration, isActive]) | ||
|
||
return memoizedTimer | ||
} | ||
|
||
const AnimatedLoadingCircle = () => { | ||
return ( | ||
<svg | ||
width="24" | ||
height="24" | ||
viewBox="-12 -12 24 24" | ||
stroke="currentcolor" | ||
fill="none" | ||
className="absolute block -rotate-90" | ||
> | ||
<circle r="8" pathLength="1" stroke-dashArray="0.05" stroke-opacity=".5"> | ||
<animate | ||
attributeName="stroke-dashoffset" | ||
to="-1" | ||
dur="2.5s" | ||
repeatCount="indefinite" | ||
/> | ||
</circle> | ||
</svg> | ||
) | ||
} | ||
|
||
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" | ||
fill="none" | ||
className="absolute block -rotate-90" | ||
> | ||
<circle r="8" pathLength="1" stroke-opacity=".25"> | ||
<animate | ||
attributeName="stroke-dashoffset" | ||
to="-1" | ||
dur="2.5s" | ||
repeatCount="indefinite" | ||
/> | ||
<set | ||
attributeName="stroke-dasharray" | ||
to="0.05" | ||
begin={`${convertMsToSeconds(duration)}s`} | ||
/> | ||
<set | ||
attributeName="stroke-opacity" | ||
to="0.5" | ||
begin={`${convertMsToSeconds(duration)}s`} | ||
/> | ||
</circle> | ||
<circle r="8" stroke-dasharray="1" pathLength="1"> | ||
<animate | ||
attributeName="stroke-dashoffset" | ||
values="2; 1" | ||
dur={`${convertMsToSeconds(duration)}s`} | ||
fill="freeze" | ||
/> | ||
<animate | ||
attributeName="stroke-opacity" | ||
values="0; 1" | ||
dur={`${convertMsToSeconds(duration)}s`} | ||
/> | ||
</circle> | ||
</svg> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,125 @@ | ||||||
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 triggerQuoteRef = useRef<any>(null) | ||||||
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. Specify explicit type for 'triggerQuoteRef' to enhance type safety Currently, Update the declaration to use the appropriate type: -const triggerQuoteRef = useRef<any>(null)
+const triggerQuoteRef = useRef<BridgeQuote | null>(null) Committable suggestion
Suggested change
|
||||||
const bpsThreshold = 0.0001 // 1bps | ||||||
|
||||||
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] | ||||||
) | ||||||
|
||||||
const isPendingConfirmChange = | ||||||
hasQuoteOutputChanged && | ||||||
hasSameSelectionsAsPreviousQuote && | ||||||
!hasUserConfirmedChange | ||||||
|
||||||
useEffect(() => { | ||||||
const validQuotes = | ||||||
bridgeQuote?.outputAmount && previousBridgeQuote?.outputAmount | ||||||
|
||||||
const hasBridgeModuleChanged = | ||||||
bridgeQuote?.bridgeModuleName !== | ||||||
(triggerQuoteRef.current?.bridgeModuleName ?? | ||||||
previousBridgeQuote?.bridgeModuleName) | ||||||
|
||||||
const outputAmountDiffMoreThanThreshold = validQuotes | ||||||
? calculateOutputRelativeDifference( | ||||||
bridgeQuote, | ||||||
triggerQuoteRef.current ?? previousBridgeQuote | ||||||
) > bpsThreshold | ||||||
: false | ||||||
|
||||||
Comment on lines
+64
to
+70
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. Ensure safe comparison when 'calculateOutputRelativeDifference' may return null The function Apply the following changes to handle the const diff = calculateOutputRelativeDifference(
bridgeQuote,
triggerQuoteRef.current ?? previousBridgeQuote
)
const outputAmountDiffMoreThanThreshold = validQuotes
- ? diff > bpsThreshold
+ ? diff !== null && diff > bpsThreshold
: false
|
||||||
if ( | ||||||
validQuotes && | ||||||
hasSameSelectionsAsPreviousQuote && | ||||||
hasBridgeModuleChanged | ||||||
) { | ||||||
requestUserConfirmChange(previousBridgeQuote) | ||||||
} else if ( | ||||||
validQuotes && | ||||||
hasSameSelectionsAsPreviousQuote && | ||||||
outputAmountDiffMoreThanThreshold | ||||||
) { | ||||||
requestUserConfirmChange(previousBridgeQuote) | ||||||
} else { | ||||||
resetConfirm() | ||||||
} | ||||||
}, [bridgeQuote, previousBridgeQuote, hasSameSelectionsAsPreviousQuote]) | ||||||
|
||||||
const requestUserConfirmChange = (previousQuote: BridgeQuote) => { | ||||||
if (!hasQuoteOutputChanged && !hasUserConfirmedChange) { | ||||||
triggerQuoteRef.current = previousQuote | ||||||
setHasQuoteOutputChanged(true) | ||||||
} | ||||||
setHasUserConfirmedChange(false) | ||||||
} | ||||||
|
||||||
const resetConfirm = () => { | ||||||
if (hasUserConfirmedChange) { | ||||||
triggerQuoteRef.current = null | ||||||
setHasQuoteOutputChanged(false) | ||||||
setHasUserConfirmedChange(false) | ||||||
} | ||||||
} | ||||||
|
||||||
const onUserAcceptChange = () => { | ||||||
triggerQuoteRef.current = null | ||||||
setHasUserConfirmedChange(true) | ||||||
} | ||||||
|
||||||
return { | ||||||
isPendingConfirmChange, | ||||||
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 | ||||||
} |
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.
Adjust 'isButtonDisabled' logic to enable user confirmation
Including
(isConnected && isQuoteStale)
inisButtonDisabled
disables the button when the quote is stale, even when user confirmation is required. Modify the condition to exclude cases whereisPendingConfirmChange
is true, allowing the user to confirm the new quote.Apply this diff to adjust the disabling logic: