Skip to content

Commit

Permalink
Removes unused fns
Browse files Browse the repository at this point in the history
  • Loading branch information
abtestingalpha committed Jun 27, 2024
1 parent 3edf444 commit 2b25ca3
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 297 deletions.
42 changes: 0 additions & 42 deletions packages/synapse-interface/slices/bridge/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { createAsyncThunk } from '@reduxjs/toolkit'

import { RootState } from '@/store/store'
import { useAppSelector } from '@/store/hooks'
import {
fetchBridgeQuote,
fetchBridgeQuotes,
BridgeQuoteRequest,
BridgeQuoteResponse,
} from '@/utils/actions/fetchBridgeQuotes'

export const useBridgeState = (): RootState['bridge'] => {
return useAppSelector((state) => state.bridge)
Expand All @@ -16,37 +8,3 @@ export const useBridgeState = (): RootState['bridge'] => {
export const useBridgeDisplayState = (): RootState['bridgeDisplay'] => {
return useAppSelector((state) => state.bridgeDisplay)
}

export const fetchAndStoreBridgeQuote = createAsyncThunk(
'bridge/fetchAndStoreBridgeQuote',
async ({
request,
synapseSDK,
}: {
request: BridgeQuoteRequest
synapseSDK: any
}) => {
const bridgeQuote: BridgeQuoteResponse = await fetchBridgeQuote(
request,
synapseSDK
)
return bridgeQuote
}
)

export const fetchAndStoreBridgeQuotes = createAsyncThunk(
'bridge/fetchAndStoreBridgeQuotes',
async ({
requests,
synapseSDK,
}: {
requests: BridgeQuoteRequest[]
synapseSDK: any
}) => {
const bridgeQuotes: BridgeQuoteResponse[] = await fetchBridgeQuotes(
requests,
synapseSDK
)
return bridgeQuotes
}
)
24 changes: 5 additions & 19 deletions packages/synapse-interface/slices/bridge/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { type Address } from 'viem'

import { EMPTY_BRIDGE_QUOTE } from '@/constants/bridge'
import { type BridgeQuote, type Token } from '@/utils/types'
import {
type BridgeQuote,
type BridgeQuoteResponse,
type Token,
} from '@/utils/types'
import {
getRoutePossibilities,
getSymbol,
Expand All @@ -12,8 +16,6 @@ import { getFromTokens } from '@/utils/routeMaker/getFromTokens'
import { getToChainIds } from '@/utils/routeMaker/getToChainIds'
import { getToTokens } from '@/utils/routeMaker/getToTokens'
import { findTokenByRouteSymbol } from '@/utils/findTokenByRouteSymbol'
import { fetchAndStoreBridgeQuotes } from './hooks'
import { BridgeQuoteResponse } from '@/utils/actions/fetchBridgeQuotes'
import { findValidToken } from '@/utils/findValidToken'
import { FetchState } from '../portfolio/actions'

Expand Down Expand Up @@ -483,22 +485,6 @@ export const bridgeSlice = createSlice({
state.toTokensBridgeQuotesStatus = initialState.toTokensBridgeQuotesStatus
},
},
extraReducers: (builder) => {
builder
.addCase(fetchAndStoreBridgeQuotes.pending, (state) => {
state.toTokensBridgeQuotesStatus = FetchState.LOADING
})
.addCase(
fetchAndStoreBridgeQuotes.fulfilled,
(state, action: PayloadAction<BridgeQuoteResponse[]>) => {
state.toTokensBridgeQuotes = action.payload
state.toTokensBridgeQuotesStatus = FetchState.VALID
}
)
.addCase(fetchAndStoreBridgeQuotes.rejected, (state) => {
state.toTokensBridgeQuotesStatus = FetchState.INVALID
})
},
})

export const {
Expand Down
162 changes: 0 additions & 162 deletions packages/synapse-interface/utils/actions/fetchBridgeQuotes.tsx
Original file line number Diff line number Diff line change
@@ -1,168 +1,6 @@
import { Address } from 'viem'
import { BridgeQuote, Token } from '@/utils/types'
import { formatBigIntToString } from '../bigint/format'
import { commify } from '@ethersproject/units'
import { calculateExchangeRate } from '../calculateExchangeRate'
import { getTimeMinutesFromNow } from '../time'

export interface BridgeQuoteResponse extends BridgeQuote {
destinationToken: Token
destinationChainId: number
}

export interface BridgeQuoteRequest {
originChainId: number
originToken: Token
destinationChainId: number
destinationTokenAddress: Address
destinationToken: Token
amount: bigint
}

export async function fetchBridgeQuote(
request: BridgeQuoteRequest,
synapseSDK: any
): Promise<BridgeQuoteResponse> {
if (request && synapseSDK) {
const currentTimestamp: number = getTimeMinutesFromNow(0)
try {
const {
originChainId,
originToken,
destinationChainId,
destinationTokenAddress,
destinationToken,
amount,
}: BridgeQuoteRequest = request

const {
feeAmount,
routerAddress,
maxAmountOut,
originQuery,
destQuery,
estimatedTime,
bridgeModuleName,
gasDropAmount,
} = await synapseSDK.bridgeQuote(
originChainId,
destinationChainId,
originToken.addresses[originChainId],
destinationTokenAddress,
amount
)

const toValueBigInt: bigint = BigInt(maxAmountOut.toString()) ?? 0n
// Bridge Lifecycle: originToken -> bridgeToken -> destToken
// amount is in originToken decimals
// originQuery.minAmountOut and feeAmount is in bridgeToken decimals
// Adjust feeAmount to be in originToken decimals
const adjustedFeeAmount =
(BigInt(feeAmount) * BigInt(amount)) / BigInt(originQuery.minAmountOut)

const {
originQuery: originQueryWithSlippage,
destQuery: destQueryWithSlippage,
} = synapseSDK.applyBridgeSlippage(
bridgeModuleName,
originQuery,
destQuery
)

return {
outputAmount: toValueBigInt,
outputAmountString: commify(
formatBigIntToString(
toValueBigInt,
destinationToken.decimals[destinationChainId],
8
)
),
routerAddress,
allowance: null, // update for allowances
exchangeRate: calculateExchangeRate(
amount - adjustedFeeAmount,
originToken.decimals[originChainId],
toValueBigInt,
destinationToken.decimals[destinationChainId]
),
feeAmount,
delta: BigInt(maxAmountOut.toString()),
originQuery: originQueryWithSlippage,
destQuery: destQueryWithSlippage,
destinationToken: request.destinationToken,
destinationChainId: destinationChainId,
estimatedTime: estimatedTime,
bridgeModuleName: bridgeModuleName,
gasDropAmount: BigInt(gasDropAmount.toString()),
timestamp: currentTimestamp,
}
} catch (error) {
console.error('Error fetching bridge quote:', error)
throw error
}
} else {
console.error('Missing request or synapse SDK')
}
}

export async function fetchBridgeQuotes(
requests: BridgeQuoteRequest[],
synapseSDK: any,
maxConcurrentRequests: number = 2, // Set the maximum number of concurrent requests
requestDelay: number = 1000 // Set the delay between requests in milliseconds (adjust as needed)
): Promise<BridgeQuoteResponse[]> {
try {
const bridgeQuotes: BridgeQuoteResponse[] = []

for (let i = 0; i < requests.length; i += maxConcurrentRequests) {
const batchRequests = requests.slice(i, i + maxConcurrentRequests)
const bridgeQuotesPromises: Promise<BridgeQuoteResponse>[] =
batchRequests.map(async (request: BridgeQuoteRequest) => {
try {
const results: BridgeQuoteResponse = await fetchBridgeQuote(
request,
synapseSDK
)
return results
} catch (error) {
console.error('Error in individual bridge quote request: ', error)
return null
}
})

const batchBridgeQuotes = (
await Promise.all(bridgeQuotesPromises)
).filter((quote) => quote !== null)
bridgeQuotes.push(...batchBridgeQuotes)

// Add a delay between batches of requests to avoid overloading the server
if (i + maxConcurrentRequests < requests.length) {
await new Promise((resolve) => setTimeout(resolve, requestDelay))
}
}

return bridgeQuotes
} catch (error) {
console.error('Error fetching bridge quotes: ', error)
return []
}
}

export function locateBestExchangeRateToken(
quotes: BridgeQuoteResponse[]
): Token | null {
if (quotes?.length === 0) {
return null
}

let bestQuote: BridgeQuoteResponse | null = null

quotes.forEach((quote) => {
if (!bestQuote || quote.exchangeRate > bestQuote.exchangeRate) {
bestQuote = quote
}
})

return bestQuote ? bestQuote.destinationToken : null
}
74 changes: 0 additions & 74 deletions packages/synapse-interface/utils/hooks/useAlternateBridgeQuotes.ts

This file was deleted.

5 changes: 5 additions & 0 deletions packages/synapse-interface/utils/types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export type BridgeQuote = {
destChainId: number
}

export interface BridgeQuoteResponse extends BridgeQuote {
destinationToken: Token
destinationChainId: number
}

interface TokensByChain {
[cID: string]: Token[]
}
Expand Down

0 comments on commit 2b25ca3

Please sign in to comment.