Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIT-3935 - Auto-Top-Up - Fix 'is same day' calculation #17

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"start": "pnpm build && node ./dist/worker/worker.js",
"prepare": "husky",
"ci_validate": "node ./dist/bin/validateJSONRecipients.js",
"validate": "pnpm tsc && node ./dist/bin/validateJSONRecipients.js"
"validate": "pnpm build && node ./dist/bin/validateJSONRecipients.js"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was incorrectly running commit checks on the last built artifact instead of the current code in the repo.

},
"packageManager": "[email protected]",
"keywords": [],
Expand Down
4 changes: 2 additions & 2 deletions packages/lit-task-auto-top-up/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@
"dev": "echo 'This will use stub when #371 is fixed.' && pnpm build && echo 'Built the package for local use! Carry on.'"
},
"dependencies": {
"@date-fns/tz": "^1.1.2",
"@hokify/agenda": "^6.3.0",
"@lit-protocol/contracts-sdk": "^6.4.0",
"awaity": "^1.0.0",
"bs58": "^5.0.0",
"consola": "^3.2.3",
"date-and-time": "2.4.1",
"date-fns": "^4.1.0",
"lodash": "^4.17.21",
"multiformats": "9.7.1",
"verror": "^1.10.1",
"viem": "^2.9.5",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/date-and-time": "0.13.0",
"chai": "^5.1.0"
}
}
12 changes: 6 additions & 6 deletions packages/lit-task-auto-top-up/src/Classes/TaskHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TZDate } from '@date-fns/tz';
import { Job } from '@hokify/agenda';
import awaity from 'awaity'; // Awaity is a cjs package, breaks `import` with named imports in ESM
import { ConsolaInstance } from 'consola';
import date from 'date-and-time';
import { addDays, isSameDay } from 'date-fns';
import VError from 'verror';

import { mintCapacityCreditNFT } from '../actions/mintCapacityCreditNFT';
Expand Down Expand Up @@ -34,7 +35,7 @@ export class TaskHandler {
const tokens = await this.fetchExistingTokens({ recipientAddress });
const { noUsableTokensTomorrow, unexpiredTokens } = this.getExistingTokenDetails({
tokens,
today: new Date(),
today: TZDate.tz('UTC'),
});

if (noUsableTokensTomorrow) {
Expand Down Expand Up @@ -63,7 +64,7 @@ export class TaskHandler {
}

getExistingTokenDetails({ today, tokens }: { today: Date; tokens: CapacityToken[] }) {
const tomorrow = date.addDays(today, 1);
const tomorrow = addDays(today, 1);

// Only mint a new token if the recipient...
// 1. Has no NFTs at all
Expand All @@ -80,9 +81,8 @@ export class TaskHandler {
if (isExpired) {
return true;
}
return (
date.isSameDay(new Date(timestamp), tomorrow) || date.isSameDay(new Date(timestamp), today)
);
const tokenExpiresDate = new TZDate(timestamp, 'UTC');
return isSameDay(tokenExpiresDate, tomorrow) || isSameDay(tokenExpiresDate, today);
});
return {
noUsableTokensTomorrow,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { expect } from 'chai';
import { createConsola } from 'consola';

import { tokenFixtures, NOW, Expired, UnexpiredButInvalid, Unexpired } from './tokenFixtures';
import { isSameDay } from 'date-fns';

import {
tokenFixtures,
NOW,
Expired,
UnexpiredButInvalid,
Unexpired,
MIDNIGHT_TODAY,
} from './tokenFixtures';
import { TaskHandler } from '../../src/index';
import { CapacityToken } from '../../src/types/types';
import { COMPLETE_CONFIG } from '../config';
Expand Down Expand Up @@ -44,6 +52,11 @@ function testAndAssert({
return true;
}
describe('Expired Tokens', () => {
describe('midnight today', () => {
it('should be a date set to midnight of the SAME DAY as the date provided', () => {
expect(isSameDay(NOW, MIDNIGHT_TODAY)).equal(true);
});
});
describe('noUsableTokensTomorrow', () => {
it('should be `true` when there are no tokens at all', () => {
testAndAssert({});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import date from 'date-and-time';
import { TZDate } from '@date-fns/tz';
import { startOfDay, addDays, subDays, addWeeks, addHours } from 'date-fns';

import { CapacityToken } from '../../src/types/types';

Expand All @@ -24,21 +25,26 @@ interface TokenFixtures {
unexpired: Record<Unexpired, CapacityToken>;
unexpiredButInvalid: Record<UnexpiredButInvalid, CapacityToken>;
}
function getMidnightToday(now: Date) {
const midnightDate = new Date(now);
midnightDate.setHours(0, 0, 0, 0);
return midnightDate;
}

export const NOW = new Date(1712157737877);
export const MIDNIGHT_TODAY = getMidnightToday(NOW);
export const DAY_BEFORE_YESTERDAY = date.addDays(MIDNIGHT_TODAY, -2);
export const YESTERDAY = date.addDays(MIDNIGHT_TODAY, -1);
export const LATER_TODAY = date.addHours(NOW, 2);
export const TOMORROW = date.addDays(MIDNIGHT_TODAY, 1);
export const DAY_AFTER_TOMORROW = date.addDays(MIDNIGHT_TODAY, 2);
export const NEXT_WEEK = date.addDays(MIDNIGHT_TODAY, 7);
export const TWO_WEEKS_FROM_NOW = date.addDays(MIDNIGHT_TODAY, 14);
export const NOW = new TZDate(1712157737877, 'UTC');
export const MIDNIGHT_TODAY = startOfDay(NOW);
export const YESTERDAY = subDays(MIDNIGHT_TODAY, 1);
export const DAY_BEFORE_YESTERDAY = subDays(MIDNIGHT_TODAY, 2);
export const LATER_TODAY = addHours(NOW, 2);
export const TOMORROW = addDays(MIDNIGHT_TODAY, 1);
export const DAY_AFTER_TOMORROW = addDays(MIDNIGHT_TODAY, 2);
export const NEXT_WEEK = addWeeks(MIDNIGHT_TODAY, 1);
export const TWO_WEEKS_FROM_NOW = addWeeks(MIDNIGHT_TODAY, 2);

// console.log('NOW', NOW.toISOString());
// console.log('MIDNIGHT_TODAY', MIDNIGHT_TODAY.toISOString());
// console.log('DAY_BEFORE_YESTERDAY', DAY_BEFORE_YESTERDAY.toISOString());
// console.log('YESTERDAY', YESTERDAY.toISOString());
// console.log('LATER_TODAY', LATER_TODAY.toISOString());
// console.log('TOMORROW', TOMORROW.toISOString());
// console.log('DAY_AFTER_TOMORROW', DAY_AFTER_TOMORROW.toISOString());
// console.log('NEXT_WEEK', NEXT_WEEK.toISOString());
// console.log('TWO_WEEKS_FROM_NOW', TWO_WEEKS_FROM_NOW.toISOString());

export const tokenFixtures: TokenFixtures = {
expired: {
Expand Down
41 changes: 28 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading