Skip to content

Commit

Permalink
normalize timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteenkamp89 committed Jan 3, 2025
1 parent 34053f3 commit b6d63c0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
5 changes: 0 additions & 5 deletions api/_transfer-with-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
13 changes: 5 additions & 8 deletions api/swap/auth/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions api/swap/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ 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()),
});

export type authSwapQueryParams = Infer<typeof authSwapQueryParamsSchema>;

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 = {
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit b6d63c0

Please sign in to comment.