Skip to content

Commit

Permalink
feat(synapse-interface): validate rfq quotes (#2971)
Browse files Browse the repository at this point in the history
* feat: reject rfq quotes if differing by more than 30% wrt synapse bridge

* fix: directional comparison

* Remove log

* feat: compare RFQ against nonRFQ quotes

* Track nonRFQ quote used over RFQ

* Remove testing var

* Remove unnecessary segment fields

* Remove address field in Segment tracking since already tracked

* Remove unnecessary address field in other segmentAnalyticsEvent() instances

* feat: track specific quote details in segment event

* fix: clearer patterns

* fix: remove bigint typecast
  • Loading branch information
bigboydiamonds authored Aug 8, 2024
1 parent b4b6948 commit f8a0e13
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { useAccount } from 'wagmi'

import { segmentAnalyticsEvent } from '@/contexts/SegmentAnalyticsProvider'

Expand All @@ -10,12 +9,10 @@ import NavMenu from './NavMenu'
import Ticker from './Ticker'

const Wrapper = ({ children }) => {
const { address: currentAddress } = useAccount()
const router = useRouter()

useEffect(() => {
segmentAnalyticsEvent(`[Teaser] arrives`, {
address: currentAddress,
query: router.query,
pathname: router.pathname,
})
Expand Down
3 changes: 0 additions & 3 deletions packages/synapse-interface/pages/landing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ import ResourcesSection from './sections/ResourcesSection'

import { useRouter } from 'next/router'
import { useEffect } from 'react'
import { useAccount } from 'wagmi'

import { segmentAnalyticsEvent } from '@/contexts/SegmentAnalyticsProvider'

const LandingPage = () => {
const { address: currentAddress } = useAccount()
const router = useRouter()

useEffect(() => {
segmentAnalyticsEvent(`[Landing] arrives`, {
address: currentAddress,
query: router.query,
pathname: router.pathname,
})
Expand Down
37 changes: 30 additions & 7 deletions packages/synapse-interface/pages/state-managed-bridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,39 @@ const StateManagedBridge = () => {
(quote) => quote.bridgeModuleName === 'SynapseRFQ'
)

const nonRfqQuote = activeQuotes.find(
(quote) => quote.bridgeModuleName !== 'SynapseRFQ'
)

let quote

if (rfqQuote) {
quote = rfqQuote
if (rfqQuote && nonRfqQuote) {
const rfqMaxAmountOut = BigInt(rfqQuote.maxAmountOut.toString())
const nonRfqMaxAmountOut = BigInt(nonRfqQuote.maxAmountOut.toString())

const allowedPercentileDifference = 30n
const maxDifference =
(nonRfqMaxAmountOut * allowedPercentileDifference) / 100n

if (rfqMaxAmountOut > nonRfqMaxAmountOut - maxDifference) {
quote = rfqQuote
} else {
quote = nonRfqQuote

segmentAnalyticsEvent(`[Bridge] use non-RFQ quote over RFQ`, {
bridgeModuleName: nonRfqQuote.bridgeModuleName,
originChainId: fromChainId,
originToken: fromToken.symbol,
originTokenAddress: fromToken.addresses[fromChainId],
destinationChainId: toChainId,
destinationToken: toToken.symbol,
destinationTokenAddress: toToken.addresses[toChainId],
rfqQuoteAmountOut: rfqQuote.maxAmountOut.toString(),
nonRfqMaxAmountOut: nonRfqQuote.maxAmountOut.toString(),
})
}
} else {
/* allBridgeQuotes returns sorted quotes by maxAmountOut descending */
quote = activeQuotes[0]
quote = rfqQuote ?? nonRfqQuote
}

const {
Expand Down Expand Up @@ -367,7 +393,6 @@ const StateManagedBridge = () => {
segmentAnalyticsEvent(
`[Bridge] initiates bridge`,
{
address,
originChainId: fromChainId,
destinationChainId: toChainId,
inputAmount: debouncedFromValue,
Expand Down Expand Up @@ -461,7 +486,6 @@ const StateManagedBridge = () => {
{ id: 'bridge-in-progress-popup', duration: Infinity }
)
segmentAnalyticsEvent(`[Bridge] bridges successfully`, {
address,
originChainId: fromChainId,
destinationChainId: toChainId,
inputAmount: debouncedFromValue,
Expand Down Expand Up @@ -524,7 +548,6 @@ const StateManagedBridge = () => {
return tx
} catch (error) {
segmentAnalyticsEvent(`[Bridge] error bridging`, {
address,
errorCode: error.code,
})
dispatch(removePendingBridgeTransaction(currentTimestamp))
Expand Down
2 changes: 0 additions & 2 deletions packages/synapse-interface/pages/swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ const StateManagedSwap = () => {
segmentAnalyticsEvent(
`[Swap] initiates swap`,
{
address,
chainId: swapChainId,
swapFromToken: swapFromToken.symbol,
swapToToken: swapToToken.symbol,
Expand Down Expand Up @@ -309,7 +308,6 @@ const StateManagedSwap = () => {
onSuccessSwap()

segmentAnalyticsEvent(`[Swap] swaps successfully`, {
address,
originChainId: swapChainId,
inputAmount: swapFromValue,
expectedReceivedAmount: swapQuote.outputAmountString,
Expand Down
2 changes: 0 additions & 2 deletions packages/synapse-interface/pages/teaser/#index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import ValueProps from './ValueProps'
import Wrapper from '@/components/WipWrapperComponents/Wrapper'

const LandingPage = () => {
const { address: currentAddress } = useAccount()
const router = useRouter()

useEffect(() => {
segmentAnalyticsEvent(`[Teaser] arrives`, {
address: currentAddress,
query: router.query,
pathname: router.pathname,
})
Expand Down

0 comments on commit f8a0e13

Please sign in to comment.