-
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
22 changed files
with
497 additions
and
83 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
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,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> | ||
) | ||
} |
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
127 changes: 127 additions & 0 deletions
127
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,127 @@ | ||
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] | ||
) | ||
|
||
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 | ||
|
||
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 = ( | ||
currentQuote?: BridgeQuote, | ||
previousQuote?: BridgeQuote | ||
) => { | ||
if (!currentQuote?.outputAmountString || !previousQuote?.outputAmountString) { | ||
return null | ||
} | ||
|
||
const currentOutput = parseFloat(currentQuote.outputAmountString) | ||
const previousOutput = parseFloat(currentQuote.outputAmountString) | ||
|
||
return previousOutput - currentOutput / previousOutput | ||
} |
Oops, something went wrong.