From b6d63c09919be3fb57eff3eae8ef17d8dba0cdb0 Mon Sep 17 00:00:00 2001 From: Gerhard Steenkamp Date: Fri, 3 Jan 2025 21:08:53 +0200 Subject: [PATCH] normalize timestamps --- api/_transfer-with-auth.ts | 5 ----- api/swap/auth/_utils.ts | 13 +++++-------- api/swap/auth/index.ts | 6 +++--- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/api/_transfer-with-auth.ts b/api/_transfer-with-auth.ts index f9ee26df6..fab13ea9e 100644 --- a/api/_transfer-with-auth.ts +++ b/api/_transfer-with-auth.ts @@ -122,8 +122,3 @@ export async function getTransferWithAuthTypedData(params: { }, }; } - -export function convertMaybeMillisecondsToSeconds(timestamp: number): number { - const isMilliseconds = timestamp > 1_000_000_000; // rough approximation - return isMilliseconds ? Math.floor(timestamp / 1000) : Math.floor(timestamp); -} diff --git a/api/swap/auth/_utils.ts b/api/swap/auth/_utils.ts index df13c0d4e..dcc459428 100644 --- a/api/swap/auth/_utils.ts +++ b/api/swap/auth/_utils.ts @@ -3,10 +3,7 @@ import { DepositEntryPointContract, OriginSwapEntryPointContract, } from "../../_dexes/types"; -import { - convertMaybeMillisecondsToSeconds, - getTransferWithAuthTypedData, -} from "../../_transfer-with-auth"; +import { getTransferWithAuthTypedData } from "../../_transfer-with-auth"; import { getDepositTypedData, getSwapAndDepositTypedData, @@ -25,8 +22,8 @@ export async function buildAuthTxPayload({ submissionFees, }: { crossSwapQuotes: CrossSwapQuotes; - authDeadline: number; // maybe milliseconds - authStart?: number; // maybe milliseconds + authDeadline: number; + authStart?: number; submissionFees?: { amount: BigNumberish; recipient: string; @@ -68,8 +65,8 @@ export async function buildAuthTxPayload({ // random non-sequesntial nonce const nonce = utils.hexlify(utils.randomBytes(32)); - const validAfter = convertMaybeMillisecondsToSeconds(authStart); - const validBefore = convertMaybeMillisecondsToSeconds(authDeadline); + const validAfter = authStart; + const validBefore = authDeadline; if (originSwapQuote) { if (!originSwapEntryPoint) { diff --git a/api/swap/auth/index.ts b/api/swap/auth/index.ts index 40b6aefc0..21b919b66 100644 --- a/api/swap/auth/index.ts +++ b/api/swap/auth/index.ts @@ -14,6 +14,7 @@ import { InvalidParamError } from "../../_errors"; import { QuoteFetchStrategies } from "../../_dexes/utils"; import { buildAuthTxPayload } from "./_utils"; import { GAS_SPONSOR_ADDRESS } from "../../relay/_utils"; +import * as sdk from "@across-protocol/sdk"; export const authSwapQueryParamsSchema = type({ authDeadline: optional(positiveIntStr()), @@ -21,8 +22,7 @@ export const authSwapQueryParamsSchema = type({ export type authSwapQueryParams = Infer; -const DEFAULT_AUTH_DEADLINE = - Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 365; // 1 year +const DEFAULT_AUTH_DEADLINE = sdk.utils.getCurrentTime() + 60 * 60 * 24 * 365; // 1 year // For auth-based flows, we have to use the `SpokePoolPeriphery` as an entry point const quoteFetchStrategies: QuoteFetchStrategies = { @@ -53,7 +53,7 @@ const handler = async ( authSwapQueryParamsSchema ); const authDeadline = Number(_authDeadline ?? DEFAULT_AUTH_DEADLINE); - const authStart = Number(_authStart ?? Date.now()); + const authStart = Number(_authStart ?? sdk.utils.getCurrentTime()); if (authDeadline < Math.floor(Date.now() / 1000)) { throw new InvalidParamError({