Skip to content
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
27 changes: 17 additions & 10 deletions api/v1_challenges_info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"fmt"
"time"

"github.com/gofiber/fiber/v2"
Expand All @@ -25,16 +24,25 @@ type ChallengeInfo struct {
}

func getWeeklyPoolWindowStart() time.Time {
now := time.Now().UTC()
return weeklyPoolWindowStartAt(time.Now())
}

if now.Weekday() == time.Monday && now.Hour() < 16 {
year, month, day := now.AddDate(0, 0, -7).Date()
return time.Date(year, month, day, 16, 0, 0, 0, time.UTC)
} else {
// get monday of this week
year, month, day := now.AddDate(0, 0, -int(now.Weekday())+1).Date()
return time.Date(year, month, day, 16, 0, 0, 0, time.UTC)
// weeklyPoolWindowStartAt returns the most recent Monday 16:00 UTC at or
// before `now`: the instant the current weekly reward pool opened.
//
// Go numbers Sunday as 0, so the naive "subtract Weekday, add one" walk to
// Monday landed on the *following* Monday all day Sunday, putting the window
// start in the future and reporting the full pool as remaining every Sunday.
func weeklyPoolWindowStartAt(now time.Time) time.Time {
now = now.UTC()
daysSinceMonday := (int(now.Weekday()) + 6) % 7 // Monday=0 ... Sunday=6
year, month, day := now.AddDate(0, 0, -daysSinceMonday).Date()
start := time.Date(year, month, day, 16, 0, 0, 0, time.UTC)
if now.Before(start) {
// Monday before 16:00: still last week's pool.
start = start.AddDate(0, 0, -7)
}
return start
}

func (app *ApiServer) v1ChallengesInfo(c *fiber.Ctx) error {
Expand All @@ -44,7 +52,6 @@ func (app *ApiServer) v1ChallengesInfo(c *fiber.Ctx) error {
}

weeklyPoolWindowStart := getWeeklyPoolWindowStart()
fmt.Println("weeklyPoolWindowStart", weeklyPoolWindowStart)

sql := `
SELECT
Expand Down
27 changes: 27 additions & 0 deletions api/v1_challenges_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,30 @@ func TestV1ChallengesInfoInvalidWeeklyPoolMinAmount(t *testing.T) {
"error": "weekly_pool_min_amount is invalid",
})
}

func TestWeeklyPoolWindowStartAt(t *testing.T) {
utc := func(y int, m time.Month, d, h int) time.Time {
return time.Date(y, m, d, h, 0, 0, 0, time.UTC)
}
// 2026-09-07 is a Monday.
thisMonday := utc(2026, time.September, 7, 16)
lastMonday := utc(2026, time.August, 31, 16)

cases := []struct {
name string
now time.Time
want time.Time
}{
{"Monday before 16:00 is still last week", utc(2026, time.September, 7, 15), lastMonday},
{"Monday at 16:00 opens the week", thisMonday, thisMonday},
{"Wednesday", utc(2026, time.September, 9, 12), thisMonday},
{"Saturday", utc(2026, time.September, 12, 23), thisMonday},
{"Sunday (Go weekday 0) is the end of the week, not the start of the next", utc(2026, time.September, 13, 21), thisMonday},
{"Sunday just before the next Monday", utc(2026, time.September, 13, 23), thisMonday},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, weeklyPoolWindowStartAt(tc.now))
})
}
}
Loading