-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
458 additions
and
83 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
packages/synapse-interface/components/StateManagedBridge/BridgeQuoteResetTimer.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,67 @@ | ||
import { BridgeQuote } from '@/utils/types' | ||
import { useState, useEffect, useMemo } from 'react' | ||
|
||
export const BridgeQuoteResetTimer = ({ | ||
bridgeQuote, | ||
isActive, | ||
duration, // in ms | ||
}: { | ||
bridgeQuote: BridgeQuote | ||
isActive: boolean | ||
duration: number | ||
}) => { | ||
const memoizedTimer = useMemo(() => { | ||
if (isActive) { | ||
return ( | ||
<AnimatedProgressCircle | ||
animateKey={bridgeQuote.id} | ||
duration={duration} | ||
/> | ||
) | ||
} | ||
return null | ||
}, [bridgeQuote, duration, isActive]) | ||
|
||
return memoizedTimer | ||
} | ||
|
||
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" | ||
stroke-opacity=".33" | ||
fill="none" | ||
className="absolute -rotate-90" | ||
> | ||
<circle r="8" /> | ||
<circle r="8" stroke-dasharray="1" pathLength="1"> | ||
<animate | ||
attributeName="stroke-dashoffset" | ||
values="2; 1" | ||
dur={`${convertMsToSeconds(duration)}s`} | ||
fill="freeze" | ||
/> | ||
</circle> | ||
</svg> | ||
) | ||
} | ||
|
||
const convertMsToSeconds = (ms: number) => { | ||
return Math.ceil(ms / 1000) | ||
} |
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
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
122 changes: 122 additions & 0 deletions
122
packages/synapse-interface/components/StateManagedBridge/hooks/useConfirmNewBridgePrice.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,122 @@ | ||
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) | ||
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] | ||
) | ||
|
||
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 | ||
|
||
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 { | ||
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 | ||
} |
Oops, something went wrong.