Skip to content

Commit

Permalink
Merge 4e37c9a into 252fb54
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboydiamonds authored Aug 30, 2024
2 parents 252fb54 + 4e37c9a commit 18c5172
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
"solidity.compileUsingRemoteVersion": "v0.8.17+commit.8df45f5f",
"solidity.formatter": "prettier",
"solidity.defaultCompiler": "remote"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import { useBridgeQuoteState } from '@/slices/bridgeQuote/hooks'
import { setIsDestinationWarningAccepted } from '@/slices/bridgeDisplaySlice'
import { useBridgeDisplayState, useBridgeState } from '@/slices/bridge/hooks'
import { TransactionButton } from '@/components/buttons/TransactionButton'
import { useBridgeValidations } from './hooks/useBridgeValidations'
import {
useBridgeValidations,
constructStringifiedBridgeSelections,
} from './hooks/useBridgeValidations'
import { segmentAnalyticsEvent } from '@/contexts/SegmentAnalyticsProvider'
import { useConfirmNewBridgePrice } from './hooks/useConfirmNewBridgePrice'

export const BridgeTransactionButton = ({
approveTxn,
Expand Down Expand Up @@ -45,6 +49,11 @@ export const BridgeTransactionButton = ({
debouncedFromValue,
} = useBridgeState()
const { bridgeQuote, isLoading } = useBridgeQuoteState()
const {
hasQuoteOutputChanged,
hasUserConfirmedChange,
setHasUserConfirmedChange,
} = useConfirmNewBridgePrice()

const { isWalletPending } = useWalletState()
const { showDestinationWarning, isDestinationWarningAccepted } =
Expand Down Expand Up @@ -162,6 +171,13 @@ export const BridgeTransactionButton = ({
onClick: () => switchChain({ chainId: fromChainId }),
pendingLabel: 'Switching chains',
}
} else if (hasQuoteOutputChanged && !hasUserConfirmedChange) {
buttonProperties = {
label: 'Confirm new price',
onClick: () => setHasUserConfirmedChange(true),
className:
'!border !border-synapsePurple !from-bgLight !to-bgLight !animate-pulse',
}
} else if (!isApproved && hasValidInput && hasValidQuote) {
buttonProperties = {
onClick: approveTxn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const useBridgeValidations = () => {
}
}

const constructStringifiedBridgeSelections = (
export const constructStringifiedBridgeSelections = (
originAmount,
originChainId,
originToken,
Expand Down
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,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ const StateManagedBridge = () => {

// will have to handle deadlineMinutes here at later time, gets passed as optional last arg in .bridgeQuote()

/* clear stored bridge quote before requesting new bridge quote */
dispatch(resetBridgeQuote())
const currentTimestamp: number = getUnixTimeMinutesFromNow(0)

try {
Expand Down
8 changes: 7 additions & 1 deletion packages/synapse-interface/slices/bridgeQuote/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { fetchBridgeQuote } from './thunks'

export interface BridgeQuoteState {
bridgeQuote: BridgeQuote
previousBridgeQuote: BridgeQuote | null
isLoading: boolean
}

export const initialState: BridgeQuoteState = {
bridgeQuote: EMPTY_BRIDGE_QUOTE,
previousBridgeQuote: null,
isLoading: false,
}

Expand All @@ -24,6 +26,9 @@ export const bridgeQuoteSlice = createSlice({
resetBridgeQuote: (state) => {
state.bridgeQuote = initialState.bridgeQuote
},
setPreviousBridgeQuote: (state, action: PayloadAction<any>) => {
state.previousBridgeQuote = action.payload
},
},
extraReducers: (builder) => {
builder
Expand All @@ -44,6 +49,7 @@ export const bridgeQuoteSlice = createSlice({
},
})

export const { resetBridgeQuote, setIsLoading } = bridgeQuoteSlice.actions
export const { resetBridgeQuote, setIsLoading, setPreviousBridgeQuote } =
bridgeQuoteSlice.actions

export default bridgeQuoteSlice.reducer
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
}
9 changes: 7 additions & 2 deletions packages/synapse-interface/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { api } from '@/slices/api/slice'
import { segmentAnalyticsEvent } from '@/contexts/SegmentAnalyticsProvider'
import { storageKey, persistConfig, persistedReducer } from './reducer'
import { resetReduxCache } from '@/slices/application/actions'
import { destinationAddressMiddleware } from '@/store/destinationAddressMiddleware'
import { destinationAddressMiddleware } from '@/store/middleware/destinationAddressMiddleware'
import { bridgeQuoteHistoryMiddleware } from './middleware/bridgeQuoteHistoryMiddleware'

const checkVersionAndResetCache = (): boolean => {
if (typeof window !== 'undefined') {
Expand All @@ -28,7 +29,11 @@ export const store = configureStore({
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false,
}).concat(api.middleware, destinationAddressMiddleware),
}).concat(
api.middleware,
destinationAddressMiddleware,
bridgeQuoteHistoryMiddleware
),
})

if (checkVersionAndResetCache()) {
Expand Down

0 comments on commit 18c5172

Please sign in to comment.