Skip to content

Commit

Permalink
calculateDeadline -> applyOptionalDeadline
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiTimesChi committed Dec 27, 2023
1 parent 6722fa7 commit 686e00a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
10 changes: 7 additions & 3 deletions packages/sdk-router/src/module/synapseModuleSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-router/src/operations/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 }
}
37 changes: 11 additions & 26 deletions packages/sdk-router/src/utils/deadlines.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
11 changes: 5 additions & 6 deletions packages/sdk-router/src/utils/deadlines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 686e00a

Please sign in to comment.