From 705a1aaeae540d0f9e595a6f5cbec051b6f3da6a Mon Sep 17 00:00:00 2001 From: Luke Tchang Date: Thu, 23 Nov 2023 10:26:01 -0800 Subject: [PATCH] fix 7am next day bug (#647) --- .changeset/odd-ducks-guess.md | 5 +++ .../src/screening/checks/v1/utils.ts | 21 ++++++++---- .../deposit-screener/test/RULESET_V1.test.ts | 33 ++++++++++++++----- 3 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 .changeset/odd-ducks-guess.md diff --git a/.changeset/odd-ducks-guess.md b/.changeset/odd-ducks-guess.md new file mode 100644 index 000000000..d91e4fa1d --- /dev/null +++ b/.changeset/odd-ducks-guess.md @@ -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 diff --git a/actors/deposit-screener/src/screening/checks/v1/utils.ts b/actors/deposit-screener/src/screening/checks/v1/utils.ts index 78dad51a8..477e5a666 100644 --- a/actors/deposit-screener/src/screening/checks/v1/utils.ts +++ b/actors/deposit-screener/src/screening/checks/v1/utils.ts @@ -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"); }; diff --git a/actors/deposit-screener/test/RULESET_V1.test.ts b/actors/deposit-screener/test/RULESET_V1.test.ts index 553eb381b..436f9bf6e 100644 --- a/actors/deposit-screener/test/RULESET_V1.test.ts +++ b/actors/deposit-screener/test/RULESET_V1.test.ts @@ -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; @@ -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; @@ -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", + }); }); });