Skip to content

Commit

Permalink
Move finalizeBridgeRoute to ModuleSet
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiTimesChi committed Dec 27, 2023
1 parent c940412 commit 6722fa7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
43 changes: 40 additions & 3 deletions packages/sdk-router/src/module/synapseModuleSet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BigNumber } from '@ethersproject/bignumber'
import invariant from 'tiny-invariant'

import { BigintIsh } from '../constants'
import { BridgeQuote, BridgeRoute } from './types'
import { BridgeQuote, BridgeRoute, FeeConfig } from './types'
import { SynapseModule } from './synapseModule'
import { ONE_WEEK, TEN_MINUTES, calculateDeadline } from '../utils/deadlines'

export abstract class SynapseModuleSet {
abstract readonly bridgeModuleName: string
Expand Down Expand Up @@ -106,6 +108,17 @@ export abstract class SynapseModuleSet {
amountIn: BigintIsh
): Promise<BridgeRoute[]>

/**
* Retrieves the fee data for a given bridge route.
*
* @param bridgeRoute - The bridge route to get fee data for.
* @returns A promise that resolves to the fee data.
*/
abstract getFeeData(bridgeRoute: BridgeRoute): Promise<{
feeAmount: BigNumber
feeConfig: FeeConfig
}>

/**
* Finalizes the bridge route by getting fee data and setting default deadlines.
*
Expand All @@ -114,8 +127,32 @@ export abstract class SynapseModuleSet {
* @param deadline - The deadline to use on the origin chain (default 10 mins).
* @returns The finalized quote with fee data and deadlines.
*/
abstract finalizeBridgeRoute(
async finalizeBridgeRoute(
bridgeRoute: BridgeRoute,
deadline?: BigNumber
): Promise<BridgeQuote>
): Promise<BridgeQuote> {
// Check that route is supported on both chains
const originModule = this.getExistingModule(bridgeRoute.originChainId)
this.getExistingModule(bridgeRoute.destChainId)
invariant(
bridgeRoute.bridgeModuleName === this.bridgeModuleName,
'Invalid bridge module name'
)
const { originQuery, destQuery } = bridgeRoute
// Set origin deadline to 10 mins if not provided
originQuery.deadline = deadline ?? calculateDeadline(TEN_MINUTES)
// Destination deadline is always 1 week
destQuery.deadline = calculateDeadline(ONE_WEEK)
const { feeAmount, feeConfig } = await this.getFeeData(bridgeRoute)
return {
feeAmount,
feeConfig,
routerAddress: originModule.address,
maxAmountOut: destQuery.minAmountOut,
originQuery,
destQuery,
estimatedTime: this.getEstimatedTime(bridgeRoute.originChainId),
bridgeModuleName: bridgeRoute.bridgeModuleName,
}
}
}
44 changes: 11 additions & 33 deletions packages/sdk-router/src/router/routerSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import invariant from 'tiny-invariant'
import { Router } from './router'
import { AddressMap, BigintIsh } from '../constants'
import { DestRequest } from './types'
import { ONE_WEEK, TEN_MINUTES, calculateDeadline } from '../utils/deadlines'
import {
BridgeQuote,
BridgeRoute,
FeeConfig,
SynapseModule,
SynapseModuleSet,
} from '../module'
Expand Down Expand Up @@ -145,40 +144,19 @@ export abstract class RouterSet extends SynapseModuleSet {
}

/**
* @inheritdoc SynapseModuleSet.finalizeBridgeRoute
* @inheritdoc SynapseModuleSet.getFeeData
*/
public async finalizeBridgeRoute(
bridgeRoute: BridgeRoute,
deadline?: BigNumber
): Promise<BridgeQuote> {
const originRouter = this.routers[bridgeRoute.originChainId]
async getFeeData(bridgeRoute: BridgeRoute): Promise<{
feeAmount: BigNumber
feeConfig: FeeConfig
}> {
const destRouter = this.routers[bridgeRoute.destChainId]
invariant(originRouter && destRouter, 'Route not supported')
invariant(
bridgeRoute.bridgeModuleName === this.bridgeModuleName,
'Invalid bridge module name'
)
const { originQuery, destQuery, bridgeToken } = bridgeRoute
// Set origin deadline to 10 mins if not provided
originQuery.deadline = deadline ?? calculateDeadline(TEN_MINUTES)
// Destination deadline is always 1 week
destQuery.deadline = calculateDeadline(ONE_WEEK)
invariant(destRouter, 'Router not found')
// Get fee data: for some Bridge contracts it will depend on the complexity of the bridge action
const { feeAmount, feeConfig } = await destRouter.getBridgeFees(
bridgeToken.token,
originQuery.minAmountOut,
hasComplexBridgeAction(destQuery)
return destRouter.getBridgeFees(
bridgeRoute.bridgeToken.token,
bridgeRoute.originQuery.minAmountOut,
hasComplexBridgeAction(bridgeRoute.destQuery)
)
const estimatedTime = this.getEstimatedTime(bridgeRoute.originChainId)
return {
feeAmount,
feeConfig,
routerAddress: originRouter.address,
maxAmountOut: destQuery.minAmountOut,
originQuery,
destQuery,
estimatedTime,
bridgeModuleName: bridgeRoute.bridgeModuleName,
}
}
}

0 comments on commit 6722fa7

Please sign in to comment.