diff --git a/package.json b/package.json index 477e32c..76926a2 100644 --- a/package.json +++ b/package.json @@ -34,10 +34,12 @@ "@metamask/eth-sig-util": "^4.0.1", "crypto-js": "^4.2.0", "ethereumjs-util": "^7.1.5", + "node-fetch": "2", "redis": "^3.1.2", "web3": "^1.7.4" }, "devDependencies": { + "@types/node-fetch": "^2.6.11", "@types/redis": "^2.8.28", "@typescript-eslint/eslint-plugin": "^6.19.0", "@typescript-eslint/parser": "^6.19.0", diff --git a/src/utils/date.util.ts b/src/utils/date.util.ts index 6dd7c23..f5e7d64 100644 --- a/src/utils/date.util.ts +++ b/src/utils/date.util.ts @@ -28,3 +28,28 @@ export function daysBetween(date1: Date, date2: Date) { const diffInDays = Math.round(diffInMs / ONE_DAY) return diffInDays } + +// get begin of one day +export const getDayBegin = (date: Date): Date => { + const year = date.getFullYear() + const month = date.getMonth() + const day = date.getDate() + return new Date(year, month, day) +} + +// get begin of n day ago +export const getNDayAgo = (n: number, begin: boolean): Date => { + const date = new Date(Date.now() - n * 24 * 60 * 60 * 1000) + if (begin) { + return getDayBegin(date) + } else { + return date + } +} + +// get begin of this month +export const getMonthBegin = (date: Date): Date => { + const year = date.getFullYear() + const month = date.getMonth() + return new Date(year, month, 1) +} \ No newline at end of file diff --git a/src/utils/net.util.ts b/src/utils/net.util.ts index e44b113..db81717 100644 --- a/src/utils/net.util.ts +++ b/src/utils/net.util.ts @@ -1,4 +1,5 @@ import { ZError } from 'common/ZError' +import fetch, { Response, RequestInit } from 'node-fetch' const TIMEOUT_ERROR = new Error('timeout') diff --git a/yarn.lock b/yarn.lock index 396c660..5a8a264 100644 --- a/yarn.lock +++ b/yarn.lock @@ -626,6 +626,14 @@ dependencies: "@types/node" "*" +"@types/node-fetch@^2.6.11": + version "2.6.11" + resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.11.tgz#9b39b78665dae0e82a08f02f4967d62c66f95d24" + integrity sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g== + dependencies: + "@types/node" "*" + form-data "^4.0.0" + "@types/node@*": version "20.11.4" resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.4.tgz#c724a5d6723182af758b91b994209336f4439cb7" @@ -1161,7 +1169,7 @@ color-name@~1.1.4: resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== -combined-stream@^1.0.6, combined-stream@~1.0.6: +combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6: version "1.0.8" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== @@ -1791,6 +1799,15 @@ form-data-encoder@1.7.1: resolved "https://registry.yarnpkg.com/form-data-encoder/-/form-data-encoder-1.7.1.tgz#ac80660e4f87ee0d3d3c3638b7da8278ddb8ec96" integrity sha512-EFRDrsMm/kyqbTQocNvRXMLjc7Es2Vk+IQFx/YW7hkUH1eBl4J1fqiP34l74Yt0pFLCNpc06fkbVk00008mzjg== +form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + form-data@~2.3.2: version "2.3.3" resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6" @@ -2567,7 +2584,7 @@ node-addon-api@^2.0.0: resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-2.0.2.tgz#432cfa82962ce494b132e9d72a15b29f71ff5d32" integrity sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA== -node-fetch@^2.6.12: +node-fetch@2, node-fetch@^2.6.12: version "2.7.0" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d" integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==