-
Notifications
You must be signed in to change notification settings - Fork 33
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
9 changed files
with
134 additions
and
8 deletions.
There are no files selected for viewing
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
import { useState, useEffect, useMemo, useRef } from 'react' | ||
|
||
import { useBridgeQuoteState } from '@/slices/bridgeQuote/hooks' | ||
import { constructStringifiedBridgeSelections } from './useBridgeValidations' | ||
|
||
export const useConfirmNewBridgePrice = () => { | ||
const [hasQuoteOutputChanged, setHasQuoteOutputChanged] = | ||
useState<boolean>(false) | ||
const [hasUserConfirmedChange, setHasUserConfirmedChange] = | ||
useState<boolean>(false) | ||
|
||
const triggeredQuoteRef = useRef<any>(null) | ||
|
||
const { bridgeQuote, previousBridgeQuote } = useBridgeQuoteState() | ||
|
||
const currentBridgeQuoteSelections = useMemo( | ||
() => | ||
constructStringifiedBridgeSelections( | ||
bridgeQuote?.inputAmountForQuote, | ||
bridgeQuote?.originChainId, | ||
bridgeQuote?.originTokenForQuote, | ||
bridgeQuote?.destChainId, | ||
bridgeQuote?.destTokenForQuote | ||
), | ||
[bridgeQuote] | ||
) | ||
|
||
const previousBridgeQuoteSelections = useMemo( | ||
() => | ||
constructStringifiedBridgeSelections( | ||
previousBridgeQuote?.inputAmountForQuote, | ||
previousBridgeQuote?.originChainId, | ||
previousBridgeQuote?.originTokenForQuote, | ||
previousBridgeQuote?.destChainId, | ||
previousBridgeQuote?.destTokenForQuote | ||
), | ||
[previousBridgeQuote] | ||
) | ||
|
||
useEffect(() => { | ||
const validQuotes = | ||
bridgeQuote?.outputAmount && previousBridgeQuote?.outputAmount | ||
|
||
const selectionsMatch = | ||
currentBridgeQuoteSelections === previousBridgeQuoteSelections | ||
|
||
const outputAmountChanged = | ||
bridgeQuote?.outputAmount !== previousBridgeQuote?.outputAmount | ||
|
||
if (selectionsMatch && validQuotes && outputAmountChanged) { | ||
// Ref quote that triggered the change | ||
triggeredQuoteRef.current = bridgeQuote | ||
setHasQuoteOutputChanged(true) | ||
setHasUserConfirmedChange(false) | ||
} else if ( | ||
selectionsMatch && | ||
bridgeQuote?.outputAmount === triggeredQuoteRef?.current?.outputAmount | ||
) { | ||
// Maintain status until User confirms ref quote update | ||
setHasQuoteOutputChanged(true) | ||
} else { | ||
setHasQuoteOutputChanged(false) | ||
} | ||
}, [ | ||
bridgeQuote, | ||
previousBridgeQuote, | ||
currentBridgeQuoteSelections, | ||
previousBridgeQuoteSelections, | ||
]) | ||
|
||
return { | ||
hasQuoteOutputChanged, | ||
hasUserConfirmedChange, | ||
setHasUserConfirmedChange, | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
packages/synapse-interface/store/middleware/bridgeQuoteHistoryMiddleware.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,25 @@ | ||
import { | ||
Middleware, | ||
MiddlewareAPI, | ||
Dispatch, | ||
AnyAction, | ||
} from '@reduxjs/toolkit' | ||
|
||
export const bridgeQuoteHistoryMiddleware: Middleware = | ||
(store: MiddlewareAPI) => (next: Dispatch) => (action: AnyAction) => { | ||
const previousState = store.getState() | ||
const result = next(action) | ||
const currentState = store.getState() | ||
|
||
if ( | ||
previousState.bridgeQuote.bridgeQuote !== | ||
currentState.bridgeQuote.bridgeQuote | ||
) { | ||
store.dispatch({ | ||
type: 'bridgeQuote/setPreviousBridgeQuote', | ||
payload: previousState.bridgeQuote.bridgeQuote, | ||
}) | ||
} | ||
|
||
return result | ||
} |
File renamed without changes.
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