@@ -129,6 +129,10 @@ function nowInSeconds(): number {
129129 return Math . floor ( Date . now ( ) / 1000 ) ;
130130}
131131
132+ function nowInMilliseconds ( ) : number {
133+ return Date . now ( ) ;
134+ }
135+
132136function round ( value : number ) : number {
133137 return Number ( value . toFixed ( 2 ) ) ;
134138}
@@ -242,10 +246,11 @@ function checkContributorLimit(
242246 */
243247export function calculateProgress (
244248 campaign : CampaignRecord ,
245- at = nowInSeconds ( ) ,
249+ at = nowInMilliseconds ( ) ,
246250 pledgeCount ?: number ,
247251) : CampaignProgress {
248- const deadlineReached = at >= campaign . deadline ;
252+ const deadlineAt = campaign . deadline * 1000 ;
253+ const deadlineReached = at > deadlineAt ;
249254 const canClaim =
250255 campaign . claimedAt === undefined &&
251256 deadlineReached &&
@@ -270,7 +275,7 @@ export function calculateProgress(
270275 percentFunded : round ( ( campaign . pledgedAmount / campaign . targetAmount ) * 100 ) ,
271276 remainingAmount : round ( Math . max ( 0 , campaign . targetAmount - campaign . pledgedAmount ) ) ,
272277 pledgeCount : pledgeCount ?? getActivePledgeCount ( campaign . id ) ,
273- hoursLeft : round ( Math . max ( 0 , campaign . deadline - at ) / 3600 ) ,
278+ hoursLeft : round ( Math . max ( 0 , deadlineAt - at ) / 3600000 ) ,
274279 canPledge,
275280 canClaim,
276281 canRefund,
@@ -389,7 +394,7 @@ if (options?.searchQuery && options.searchQuery.trim()) {
389394 }
390395
391396 if ( options ?. status ) {
392- const now = Math . floor ( Date . now ( ) / 1000 ) ;
397+ const now = nowInMilliseconds ( ) ;
393398 switch ( options . status ) {
394399 case 'claimed' :
395400 whereClauses . push ( `claimed_at IS NOT NULL` ) ;
@@ -399,12 +404,12 @@ if (options?.searchQuery && options.searchQuery.trim()) {
399404 break ;
400405 case 'failed' :
401406 whereClauses . push (
402- `campaigns.claimed_at IS NULL AND campaigns.pledged_amount < campaigns.target_amount AND campaigns.deadline <= ?` ,
407+ `campaigns.claimed_at IS NULL AND campaigns.pledged_amount < campaigns.target_amount AND campaigns.deadline * 1000 < ?` ,
403408 ) ;
404409 params . push ( now ) ;
405410 break ;
406411 case 'open' :
407- whereClauses . push ( `claimed_at IS NULL AND pledged_amount < target_amount AND deadline > ?` ) ;
412+ whereClauses . push ( `claimed_at IS NULL AND pledged_amount < target_amount AND deadline * 1000 >= ?` ) ;
408413 params . push ( now ) ;
409414 break ;
410415 }
@@ -469,8 +474,13 @@ if (options?.searchQuery && options.searchQuery.trim()) {
469474 const { pledge_count : _pledgeCount , ...campaignRow } = row ;
470475 void _pledgeCount ;
471476
472- const now = Math . floor ( Date . now ( ) / 1000 ) ;
473- if ( campaignRow . claimed_at === null && campaignRow . pledged_amount < campaignRow . target_amount && now >= campaignRow . deadline && campaignRow . failed_at === null ) {
477+ const now = nowInMilliseconds ( ) ;
478+ if (
479+ campaignRow . claimed_at === null &&
480+ campaignRow . pledged_amount < campaignRow . target_amount &&
481+ now > campaignRow . deadline * 1000 &&
482+ campaignRow . failed_at === null
483+ ) {
474484 campaignRow . failed_at = campaignRow . deadline ;
475485 db . prepare ( `UPDATE campaigns SET failed_at = ? WHERE id = ?` ) . run ( campaignRow . deadline , campaignRow . id ) ;
476486 }
@@ -498,8 +508,13 @@ export function getCampaign(campaignId: string): CampaignRecord | undefined {
498508 | undefined ;
499509
500510 if ( row ) {
501- const now = Math . floor ( Date . now ( ) / 1000 ) ;
502- if ( row . claimed_at === null && row . pledged_amount < row . target_amount && now >= row . deadline && row . failed_at === null ) {
511+ const now = nowInMilliseconds ( ) ;
512+ if (
513+ row . claimed_at === null &&
514+ row . pledged_amount < row . target_amount &&
515+ now > row . deadline * 1000 &&
516+ row . failed_at === null
517+ ) {
503518 row . failed_at = row . deadline ;
504519 db . prepare ( `UPDATE campaigns SET failed_at = ? WHERE id = ?` ) . run ( row . deadline , row . id ) ;
505520 }
@@ -972,16 +987,16 @@ export function reconcileOnChainPledge(
972987 * @param at - Unix timestamp (seconds) used to classify campaign statuses; defaults to now.
973988 * @returns A {@link GlobalStats} object with total campaigns, per-status counts, total pledged, and unique contributor count.
974989 */
975- export function getGlobalStats ( at = nowInSeconds ( ) ) : GlobalStats {
990+ export function getGlobalStats ( at = nowInMilliseconds ( ) ) : GlobalStats {
976991 const db = getDb ( ) ;
977992 const row = db
978993 . prepare (
979994 `SELECT
980995 COUNT(*) AS total_campaigns,
981996 SUM(CASE WHEN claimed_at IS NOT NULL THEN 1 ELSE 0 END) AS claimed_count,
982997 SUM(CASE WHEN claimed_at IS NULL AND pledged_amount >= target_amount THEN 1 ELSE 0 END) AS funded_count,
983- SUM(CASE WHEN claimed_at IS NULL AND pledged_amount < target_amount AND deadline <= ? THEN 1 ELSE 0 END) AS failed_count,
984- SUM(CASE WHEN claimed_at IS NULL AND pledged_amount < target_amount AND deadline > ? THEN 1 ELSE 0 END) AS open_count,
998+ SUM(CASE WHEN claimed_at IS NULL AND pledged_amount < target_amount AND deadline * 1000 < ? THEN 1 ELSE 0 END) AS failed_count,
999+ SUM(CASE WHEN claimed_at IS NULL AND pledged_amount < target_amount AND deadline * 1000 >= ? THEN 1 ELSE 0 END) AS open_count,
9851000 COALESCE(SUM(pledged_amount), 0) AS total_pledged
9861001 FROM campaigns` ,
9871002 )
0 commit comments