Skip to content

Commit 990b3f2

Browse files
committed
refactor: 💡 Time utils now use Bignumberish instead of number
1 parent 9cd994d commit 990b3f2

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

‎src/utils/time.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BigNumberish } from 'ethers';
12
import { DateTime } from 'luxon';
23

34
/** Calculation bases */
@@ -44,8 +45,12 @@ export const validateDurationFormat = (str: string): DurationFormat => {
4445
* @param {(string | number)} str
4546
* @returns {number}
4647
*/
47-
export const parseSeconds = (str: string | number): number => {
48+
export const parseSeconds = (str: string | number | bigint): bigint => {
4849
if (typeof str === 'number') {
50+
return BigInt(str);
51+
}
52+
53+
if (typeof str === 'bigint') {
4954
return str;
5055
}
5156

@@ -82,9 +87,17 @@ export const parseSeconds = (str: string | number): number => {
8287
throw new Error('Unknown duration type');
8388
}
8489

85-
return Math.ceil(parsed);
90+
return BigInt(Math.ceil(parsed));
8691
};
8792

93+
/**
94+
* Parses expiration time
95+
* @param {string | BigNumberish} expire Given expiration time
96+
* @returns {bigint}
97+
*/
98+
export const parseExpire = (expire: string | BigNumberish): bigint =>
99+
typeof expire === 'string' ? BigInt(nowSec()) + parseSeconds(expire) : parseSeconds(expire);
100+
88101
/**
89102
* Converts milliseconds to seconds
90103
*
@@ -103,7 +116,7 @@ export const nowSec = () => Math.round(DateTime.now().toSeconds());
103116
/**
104117
* Checks expiration time
105118
*
106-
* @param {number} expire
119+
* @param {BigNumberish} expire
107120
* @returns {boolean}
108121
*/
109-
export const isExpired = (expire: number): boolean => nowSec() + 1 > expire;
122+
export const isExpired = (expire: BigNumberish): boolean => BigInt(nowSec()) + 1n > BigInt(expire);

0 commit comments

Comments
 (0)