Skip to content

Commit

Permalink
Request confirm if bridge module changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboydiamonds committed Sep 12, 2024
1 parent bd4294a commit c80c956
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { constructStringifiedBridgeSelections } from './useBridgeValidations'
import { BridgeQuote } from '@/utils/types'

export const useConfirmNewBridgePrice = () => {
const quoteRef = useRef<any>(null)
const triggerQuoteRef = useRef<any>(null)
const bpsThreshold = 0.0001 // 1bps

const [hasQuoteOutputChanged, setHasQuoteOutputChanged] =
Expand Down Expand Up @@ -51,17 +51,28 @@ export const useConfirmNewBridgePrice = () => {
const validQuotes =
bridgeQuote?.outputAmount && previousBridgeQuote?.outputAmount

const hasBridgeModuleChanged =
bridgeQuote?.bridgeModuleName !==
(triggerQuoteRef.current?.bridgeModuleName ??
previousBridgeQuote?.bridgeModuleName)

const outputAmountDiffMoreThanThreshold = validQuotes
? calculateOutputRelativeDifference(
bridgeQuote,
quoteRef.current ?? previousBridgeQuote
triggerQuoteRef.current ?? previousBridgeQuote
) > bpsThreshold
: false

if (
validQuotes &&
outputAmountDiffMoreThanThreshold &&
hasSameSelectionsAsPreviousQuote
hasSameSelectionsAsPreviousQuote &&
hasBridgeModuleChanged
) {
requestUserConfirmChange(previousBridgeQuote)
} else if (
validQuotes &&
hasSameSelectionsAsPreviousQuote &&
outputAmountDiffMoreThanThreshold
) {
requestUserConfirmChange(previousBridgeQuote)
} else {
Expand All @@ -71,22 +82,22 @@ export const useConfirmNewBridgePrice = () => {

const requestUserConfirmChange = (previousQuote: BridgeQuote) => {
if (!hasQuoteOutputChanged && !hasUserConfirmedChange) {
quoteRef.current = previousQuote
triggerQuoteRef.current = previousQuote
setHasQuoteOutputChanged(true)
}
setHasUserConfirmedChange(false)
}

const resetConfirm = () => {
if (hasUserConfirmedChange) {
quoteRef.current = null
triggerQuoteRef.current = null
setHasQuoteOutputChanged(false)
setHasUserConfirmedChange(false)
}
}

const onUserAcceptChange = () => {
quoteRef.current = null
triggerQuoteRef.current = null
setHasUserConfirmedChange(true)
}

Expand Down

0 comments on commit c80c956

Please sign in to comment.