Skip to content

Commit

Permalink
Merge pull request #2742 from synapsecns/fe/bridge-quote-validation
Browse files Browse the repository at this point in the history
fix(synapse-interface): RFQ input vs bridgeQuote validation
  • Loading branch information
abtestingalpha authored Jun 26, 2024
2 parents e8afb69 + b6461b6 commit f354813
Showing 1 changed file with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useSelector } from 'react-redux'
import { useMemo } from 'react'
import { TransactionButton } from '@/components/buttons/TransactionButton'
import { EMPTY_BRIDGE_QUOTE, EMPTY_BRIDGE_QUOTE_ZERO } from '@/constants/bridge'
import { RootState } from '@/store/store'
import { useAccount, useAccountEffect, useSwitchChain } from 'wagmi'
import { useEffect, useState } from 'react'
import { isAddress } from 'viem'
Expand All @@ -12,10 +10,7 @@ import { stringToBigInt } from '@/utils/bigint/format'
import { useBridgeDisplayState, useBridgeState } from '@/slices/bridge/hooks'
import { usePortfolioBalances } from '@/slices/portfolio/hooks'
import { useAppDispatch } from '@/store/hooks'
import {
setIsDestinationWarningAccepted,
setShowDestinationWarning,
} from '@/slices/bridgeDisplaySlice'
import { setIsDestinationWarningAccepted } from '@/slices/bridgeDisplaySlice'

export const BridgeTransactionButton = ({
approveTxn,
Expand Down Expand Up @@ -67,23 +62,31 @@ export const BridgeTransactionButton = ({
)
}, [balanceForToken, fromValue, fromChainId, toChainId, toToken])

const fromTokenDecimals: number | undefined =
fromToken && fromToken?.decimals[fromChainId]

const fromValueBigInt = useMemo(() => {
return fromTokenDecimals ? stringToBigInt(fromValue, fromTokenDecimals) : 0
}, [fromValue, fromTokenDecimals])

const bridgeQuoteAmountGreaterThanInputForRfq = useMemo(() => {
return (
bridgeQuote.bridgeModuleName === 'SynapseRFQ' &&
bridgeQuote.outputAmount > fromValueBigInt
)
}, [bridgeQuote.outputAmount, fromValueBigInt])

const isButtonDisabled =
isLoading ||
bridgeQuote === EMPTY_BRIDGE_QUOTE_ZERO ||
bridgeQuote === EMPTY_BRIDGE_QUOTE ||
(destinationAddress && !isAddress(destinationAddress)) ||
(isConnected && !sufficientBalance) ||
bridgeQuoteAmountGreaterThanInputForRfq ||
isBridgePaused

let buttonProperties

const fromTokenDecimals: number | undefined =
fromToken && fromToken?.decimals[fromChainId]

const fromValueBigInt = useMemo(() => {
return fromTokenDecimals ? stringToBigInt(fromValue, fromTokenDecimals) : 0
}, [fromValue, fromTokenDecimals])

if (isBridgePaused) {
buttonProperties = {
label: 'Bridge paused',
Expand Down Expand Up @@ -113,6 +116,11 @@ export const BridgeTransactionButton = ({
label: `Amount must be greater than fee`,
onClick: null,
}
} else if (bridgeQuoteAmountGreaterThanInputForRfq) {
buttonProperties = {
label: 'Invalid bridge quote',
onClick: null,
}
} else if (!isConnected && fromValueBigInt > 0) {
buttonProperties = {
label: `Connect Wallet to Bridge`,
Expand Down

0 comments on commit f354813

Please sign in to comment.