diff --git a/packages/sdk-router/src/module/synapseModuleSet.ts b/packages/sdk-router/src/module/synapseModuleSet.ts index 781ccbd508..7c7b06ea2e 100644 --- a/packages/sdk-router/src/module/synapseModuleSet.ts +++ b/packages/sdk-router/src/module/synapseModuleSet.ts @@ -4,7 +4,11 @@ import invariant from 'tiny-invariant' import { BigintIsh } from '../constants' import { BridgeQuote, BridgeRoute, FeeConfig } from './types' import { SynapseModule } from './synapseModule' -import { ONE_WEEK, TEN_MINUTES, calculateDeadline } from '../utils/deadlines' +import { + ONE_WEEK, + TEN_MINUTES, + applyOptionalDeadline, +} from '../utils/deadlines' export abstract class SynapseModuleSet { abstract readonly bridgeModuleName: string @@ -140,9 +144,9 @@ export abstract class SynapseModuleSet { ) const { originQuery, destQuery } = bridgeRoute // Set origin deadline to 10 mins if not provided - originQuery.deadline = deadline ?? calculateDeadline(TEN_MINUTES) + originQuery.deadline = applyOptionalDeadline(deadline, TEN_MINUTES) // Destination deadline is always 1 week - destQuery.deadline = calculateDeadline(ONE_WEEK) + destQuery.deadline = applyOptionalDeadline(undefined, ONE_WEEK) const { feeAmount, feeConfig } = await this.getFeeData(bridgeRoute) return { feeAmount, diff --git a/packages/sdk-router/src/operations/swap.ts b/packages/sdk-router/src/operations/swap.ts index 7f74947077..4f6e59b3b9 100644 --- a/packages/sdk-router/src/operations/swap.ts +++ b/packages/sdk-router/src/operations/swap.ts @@ -5,7 +5,7 @@ import { BigintIsh } from '../constants' import { Query, SwapQuote } from '../module' import { handleNativeToken } from '../utils/handleNativeToken' import { SynapseSDK } from '../sdk' -import { getOriginDeadline } from '../utils/deadlines' +import { TEN_MINUTES, applyOptionalDeadline } from '../utils/deadlines' /** * Performs a swap through a Synapse Router. @@ -61,6 +61,6 @@ export async function swapQuote( if (query.minAmountOut.isZero()) { throw Error('No queries found for this route') } - query.deadline = getOriginDeadline(deadline) + query.deadline = applyOptionalDeadline(deadline, TEN_MINUTES) return { routerAddress, maxAmountOut, query } } diff --git a/packages/sdk-router/src/utils/deadlines.test.ts b/packages/sdk-router/src/utils/deadlines.test.ts index 52bcc506bb..f9418f4231 100644 --- a/packages/sdk-router/src/utils/deadlines.test.ts +++ b/packages/sdk-router/src/utils/deadlines.test.ts @@ -1,45 +1,30 @@ import { BigNumber } from '@ethersproject/bignumber' -import { - calculateDeadline, - getDestinationDeadline, - getOriginDeadline, -} from './deadlines' +import { applyOptionalDeadline, calculateDeadline } from './deadlines' describe('deadlines', () => { // Something good happened on this day Date.now = jest.fn(() => Date.parse('2021-08-29')) - it('calculates correct deadlines', () => { - const seconds = 1337 - const deadline = calculateDeadline(seconds) - const now = Math.floor(Date.now() / 1000) - expect(deadline.toNumber()).toBe(now + seconds) - }) - - describe('getOriginDeadline', () => { - it('returns the deadline if it is defined', () => { - const deadline = BigNumber.from(1337) - expect(getOriginDeadline(deadline)).toBe(deadline) - }) - - it('Uses 10 minutes if deadline is undefined', () => { - const deadline = getOriginDeadline() + describe('calculateDeadline', () => { + it('calculates correct deadlines', () => { + const seconds = 1337 + const deadline = calculateDeadline(seconds) const now = Math.floor(Date.now() / 1000) - expect(deadline.toNumber()).toBe(now + 10 * 60) + expect(deadline.toNumber()).toBe(now + seconds) }) }) - describe('getDestinationDeadline', () => { + describe('applyOptionalDeadline', () => { it('returns the deadline if it is defined', () => { const deadline = BigNumber.from(1337) - expect(getDestinationDeadline(deadline)).toBe(deadline) + expect(applyOptionalDeadline(deadline, 1234)).toBe(deadline) }) - it('Uses 1 week if deadline is undefined', () => { - const deadline = getDestinationDeadline() + it('applies the default period if the deadline is undefined', () => { + const deadline = applyOptionalDeadline(undefined, 1234) const now = Math.floor(Date.now() / 1000) - expect(deadline.toNumber()).toBe(now + 7 * 24 * 60 * 60) + expect(deadline.toNumber()).toBe(now + 1234) }) }) }) diff --git a/packages/sdk-router/src/utils/deadlines.ts b/packages/sdk-router/src/utils/deadlines.ts index f2e77f3682..2723b080a0 100644 --- a/packages/sdk-router/src/utils/deadlines.ts +++ b/packages/sdk-router/src/utils/deadlines.ts @@ -8,10 +8,9 @@ export const calculateDeadline = (seconds: number) => { return BigNumber.from(Math.floor(Date.now() / 1000) + seconds) } -export const getOriginDeadline = (deadline?: BigNumber) => { - return deadline ?? calculateDeadline(TEN_MINUTES) -} - -export const getDestinationDeadline = (deadline?: BigNumber) => { - return deadline ?? calculateDeadline(ONE_WEEK) +export const applyOptionalDeadline = ( + deadline: BigNumber | undefined, + defaultPeriod: number +): BigNumber => { + return deadline ?? calculateDeadline(defaultPeriod) }