Skip to content

Commit

Permalink
fix 7am next day bug (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
luketchang authored Nov 23, 2023
1 parent 08e6d2e commit 705a1aa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-ducks-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nocturne-xyz/deposit-screener": patch
---

fix 7am next day US timezone delay rule, calculation for 7am was overshooting in morning case
21 changes: 14 additions & 7 deletions actors/deposit-screener/src/screening/checks/v1/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ export function isCreatedAfterTornadoCashSanction(

export const timeUntil7AMNextDayInSeconds = (): number => {
const currentTime = moment().tz("America/New_York");
const sevenAMNextDay = currentTime
.clone()
.add(1, "days")
.hour(7)
.minute(0)
.second(0);

let sevenAMTarget: moment.Moment;
if (currentTime.hour() < 7) {
// If time is between midnight and 7 AM, set target to 7 AM of the same day
sevenAMTarget = currentTime.clone().hour(7).minute(0).second(0);
} else {
// If time is between 9:30 PM and midnight, set target to 7 AM of the next day
sevenAMTarget = currentTime
.clone()
.add(1, "days")
.hour(7)
.minute(0)
.second(0);
}
// Calculate the duration in seconds
return sevenAMNextDay.diff(currentTime, "seconds");
return sevenAMTarget.diff(currentTime, "seconds");
};
33 changes: 24 additions & 9 deletions actors/deposit-screener/test/RULESET_V1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import { getLatestSnapshotFolder } from "./utils";
import { makeLogger } from "@nocturne-xyz/offchain-utils";
import moment from "moment-timezone";
import * as sinon from "sinon";
import {
FIVE_ETHER,
timeUntil7AMNextDayInSeconds,
} from "../src/screening/checks/v1/utils";
import { FIVE_ETHER } from "../src/screening/checks/v1/utils";

describe("RULESET_V1", () => {
let server: RedisMemoryServer;
Expand Down Expand Up @@ -261,7 +258,7 @@ describe("RULESET_V1", () => {
});
});

it("adds delay if sleeping US timezone", async () => {
it("adds delay if sleeping US timezone evening", async () => {
const largeDeposit = formDepositInfo(APPROVE_ADDRESSES.AZTEC_3);
largeDeposit.value = FIVE_ETHER;

Expand All @@ -275,15 +272,33 @@ describe("RULESET_V1", () => {
const clock = sinon.useFakeTimers(newTime);
clock.setSystemTime(newTime);

const expectedExtraDelay = timeUntil7AMNextDayInSeconds();
const result = await ruleset.check(largeDeposit);

expect(result).to.deep.equal({
timeSeconds: 7200 + expectedExtraDelay,
timeSeconds: 7200 + 3600 * 9,
type: "Delay",
});
});

it("adds delay if sleeping US timezone next morning", async () => {
const largeDeposit = formDepositInfo(APPROVE_ADDRESSES.AZTEC_3);
largeDeposit.value = FIVE_ETHER;

// // Restore the test-specific clock at the end of the test, afterAll will restore sinon global
// clock.restore();
// We need separately init'ed sinon for this special case
sinon.restore();

// Set the new time and capture a brand new clock instance
const newTime = moment
.tz("2023-11-21 01:00:00", "America/New_York")
.valueOf();
const clock = sinon.useFakeTimers(newTime);
clock.setSystemTime(newTime);

const result = await ruleset.check(largeDeposit);

expect(result).to.deep.equal({
timeSeconds: 7200 + 3600 * 6,
type: "Delay",
});
});
});

0 comments on commit 705a1aa

Please sign in to comment.