|
| 1 | +import { MarketStatus } from '@chainlink/external-adapter-framework/adapter' |
| 2 | +import { isWeekendNow } from '@chainlink/external-adapter-framework/validation/market-status' |
| 3 | +import { TZDate } from '@date-fns/tz' |
| 4 | +import { FIVE_MINUTES, HALF_HOUR, HOUR, Month, tzDate } from './utils' |
| 5 | + |
| 6 | +const TZ = 'Asia/Seoul' |
| 7 | +const weekend = `516-109:${TZ}` // Friday 16:00 - Monday 9:00 |
| 8 | + |
| 9 | +const HOLIDAY_SCHEDULE = [ |
| 10 | + { |
| 11 | + start: tzDate(2026, Month.May, 1, 9, 0, TZ), |
| 12 | + end: tzDate(2026, Month.May, 1, 16, 0, TZ), |
| 13 | + status: MarketStatus.CLOSED, |
| 14 | + }, |
| 15 | + { |
| 16 | + start: tzDate(2026, Month.May, 5, 9, 0, TZ), |
| 17 | + end: tzDate(2026, Month.May, 5, 16, 0, TZ), |
| 18 | + status: MarketStatus.CLOSED, |
| 19 | + }, |
| 20 | + { |
| 21 | + start: tzDate(2026, Month.May, 25, 9, 0, TZ), |
| 22 | + end: tzDate(2026, Month.May, 25, 16, 0, TZ), |
| 23 | + status: MarketStatus.CLOSED, |
| 24 | + }, |
| 25 | + { |
| 26 | + start: tzDate(2026, Month.Aug, 17, 9, 0, TZ), |
| 27 | + end: tzDate(2026, Month.Aug, 17, 16, 0, TZ), |
| 28 | + status: MarketStatus.CLOSED, |
| 29 | + }, |
| 30 | + { |
| 31 | + start: tzDate(2026, Month.Sep, 24, 9, 0, TZ), |
| 32 | + end: tzDate(2026, Month.Sep, 24, 16, 0, TZ), |
| 33 | + status: MarketStatus.CLOSED, |
| 34 | + }, |
| 35 | + { |
| 36 | + start: tzDate(2026, Month.Sep, 25, 9, 0, TZ), |
| 37 | + end: tzDate(2026, Month.Sep, 25, 16, 0, TZ), |
| 38 | + status: MarketStatus.CLOSED, |
| 39 | + }, |
| 40 | + { |
| 41 | + start: tzDate(2026, Month.Oct, 5, 9, 0, TZ), |
| 42 | + end: tzDate(2026, Month.Oct, 5, 16, 0, TZ), |
| 43 | + status: MarketStatus.CLOSED, |
| 44 | + }, |
| 45 | + { |
| 46 | + start: tzDate(2026, Month.Nov, 19, 9, 0, TZ), |
| 47 | + end: tzDate(2026, Month.Nov, 19, 10, 0, TZ), |
| 48 | + status: MarketStatus.CLOSED, |
| 49 | + }, |
| 50 | + { |
| 51 | + start: tzDate(2026, Month.Nov, 19, 15, 20, TZ), |
| 52 | + end: tzDate(2026, Month.Nov, 19, 16, 20, TZ), |
| 53 | + status: MarketStatus.OPEN, |
| 54 | + }, |
| 55 | + { |
| 56 | + start: tzDate(2026, Month.Dec, 25, 9, 0, TZ), |
| 57 | + end: tzDate(2026, Month.Dec, 25, 16, 0, TZ), |
| 58 | + status: MarketStatus.CLOSED, |
| 59 | + }, |
| 60 | + { |
| 61 | + start: tzDate(2026, Month.Dec, 31, 9, 0, TZ), |
| 62 | + end: tzDate(2026, Month.Dec, 31, 16, 0, TZ), |
| 63 | + status: MarketStatus.CLOSED, |
| 64 | + }, |
| 65 | +] |
| 66 | + |
| 67 | +// Open 09:00 - 15:20 KST Mon-Fri |
| 68 | +export const getStatus = () => { |
| 69 | + const now = TZDate.tz(TZ) |
| 70 | + |
| 71 | + const holiday = HOLIDAY_SCHEDULE.find( |
| 72 | + (s) => now.getTime() >= s.start.getTime() && now.getTime() < s.end.getTime(), |
| 73 | + ) |
| 74 | + if (holiday) { |
| 75 | + return { |
| 76 | + marketStatus: holiday.status, |
| 77 | + statusString: MarketStatus[holiday.status], |
| 78 | + providerIndicatedTimeUnixMs: now.getTime(), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + let status = MarketStatus.CLOSED |
| 83 | + const minutes = now.getHours() * HOUR + now.getMinutes() |
| 84 | + |
| 85 | + if (isWeekendNow(weekend)) { |
| 86 | + status = MarketStatus.CLOSED |
| 87 | + } else if (minutes >= 9 * HOUR && minutes < 15 * HOUR + HALF_HOUR - FIVE_MINUTES * 2) { |
| 88 | + status = MarketStatus.OPEN |
| 89 | + } |
| 90 | + |
| 91 | + return { |
| 92 | + marketStatus: status, |
| 93 | + statusString: MarketStatus[status], |
| 94 | + providerIndicatedTimeUnixMs: now.getTime(), |
| 95 | + } |
| 96 | +} |
0 commit comments