-
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
475 additions
and
83 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
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,86 @@ | ||
import { useState, useEffect, useMemo } from 'react' | ||
|
||
import { BridgeQuote } from '@/utils/types' | ||
import { convertMsToSeconds } from '@/utils/time' | ||
|
||
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" | ||
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
125 changes: 125 additions & 0 deletions
125
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,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) | ||
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 = ( | ||
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.