|
| 1 | +/** |
| 2 | + * Time utilities for Cardano blockchain time operations. |
| 3 | + * Provides type-safe conversions between slots and Unix time. |
| 4 | + * |
| 5 | + * @module Time |
| 6 | + * @since 2.0.0 |
| 7 | + */ |
| 8 | + |
| 9 | +import type * as Network from "../Network.js" |
| 10 | +import type * as Slot from "./Slot.js" |
| 11 | +import * as SlotConfig from "./SlotConfig.js" |
| 12 | +import * as UnixTime from "./UnixTime.js" |
| 13 | + |
| 14 | +export * as Slot from "./Slot.js" |
| 15 | +export * as SlotConfig from "./SlotConfig.js" |
| 16 | +export * as UnixTime from "./UnixTime.js" |
| 17 | + |
| 18 | +/** |
| 19 | + * Convert a slot number to Unix time (in milliseconds). |
| 20 | + * |
| 21 | + * @param slot - The slot number to convert |
| 22 | + * @param slotConfig - The network's slot configuration |
| 23 | + * @returns Unix timestamp in milliseconds |
| 24 | + * |
| 25 | + * @category Conversion |
| 26 | + * @since 2.0.0 |
| 27 | + * |
| 28 | + * @example |
| 29 | + * ```typescript |
| 30 | + * import * as Time from "@evolution-sdk/core/Time" |
| 31 | + * |
| 32 | + * const slot = 12345678n |
| 33 | + * const config = Time.SlotConfig.SLOT_CONFIG_NETWORK.Mainnet |
| 34 | + * const unixTime = Time.slotToUnixTime(slot, config) |
| 35 | + * console.log(unixTime) // Unix time in milliseconds |
| 36 | + * ``` |
| 37 | + */ |
| 38 | +export const slotToUnixTime = (slot: Slot.Slot, slotConfig: SlotConfig.SlotConfig): UnixTime.UnixTime => { |
| 39 | + const msAfterBegin = (slot - slotConfig.zeroSlot) * BigInt(slotConfig.slotLength) |
| 40 | + return slotConfig.zeroTime + msAfterBegin |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Convert Unix time (in milliseconds) to the enclosing slot number. |
| 45 | + * Uses floor division to find the slot that contains the given time. |
| 46 | + * |
| 47 | + * @param unixTime - Unix timestamp in milliseconds |
| 48 | + * @param slotConfig - The network's slot configuration |
| 49 | + * @returns The slot number that contains this Unix time |
| 50 | + * |
| 51 | + * @category Conversion |
| 52 | + * @since 2.0.0 |
| 53 | + * |
| 54 | + * @example |
| 55 | + * ```typescript |
| 56 | + * import * as Time from "@evolution-sdk/core/Time" |
| 57 | + * |
| 58 | + * const unixTime = 1596059091000n // Mainnet Shelley start |
| 59 | + * const config = Time.SlotConfig.SLOT_CONFIG_NETWORK.Mainnet |
| 60 | + * const slot = Time.unixTimeToSlot(unixTime, config) |
| 61 | + * console.log(slot) // 4492800n |
| 62 | + * ``` |
| 63 | + */ |
| 64 | +export const unixTimeToSlot = (unixTime: UnixTime.UnixTime, slotConfig: SlotConfig.SlotConfig): Slot.Slot => { |
| 65 | + const timePassed = unixTime - slotConfig.zeroTime |
| 66 | + const slotsPassed = timePassed / BigInt(slotConfig.slotLength) |
| 67 | + return slotsPassed + slotConfig.zeroSlot |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Get the current slot number for a network. |
| 72 | + * |
| 73 | + * @param network - The network to get current slot for |
| 74 | + * @returns Current slot number |
| 75 | + * |
| 76 | + * @category Utility |
| 77 | + * @since 2.0.0 |
| 78 | + * |
| 79 | + * @example |
| 80 | + * ```typescript |
| 81 | + * import * as Time from "@evolution-sdk/core/Time" |
| 82 | + * |
| 83 | + * const currentSlot = Time.getCurrentSlot("Mainnet") |
| 84 | + * console.log(currentSlot) // Current mainnet slot |
| 85 | + * ``` |
| 86 | + */ |
| 87 | +export const getCurrentSlot = (network: Network.Network): Slot.Slot => { |
| 88 | + const config = SlotConfig.SLOT_CONFIG_NETWORK[network] |
| 89 | + const now = UnixTime.now() |
| 90 | + return unixTimeToSlot(now, config) |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Check if a slot is in the future relative to current time. |
| 95 | + * |
| 96 | + * @param slot - The slot to check |
| 97 | + * @param slotConfig - The network's slot configuration |
| 98 | + * @returns True if the slot is in the future |
| 99 | + * |
| 100 | + * @category Utility |
| 101 | + * @since 2.0.0 |
| 102 | + */ |
| 103 | +export const isSlotInFuture = (slot: Slot.Slot, slotConfig: SlotConfig.SlotConfig): boolean => { |
| 104 | + const now = UnixTime.now() |
| 105 | + const slotTime = slotToUnixTime(slot, slotConfig) |
| 106 | + return slotTime > now |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Check if a slot is in the past relative to current time. |
| 111 | + * |
| 112 | + * @param slot - The slot to check |
| 113 | + * @param slotConfig - The network's slot configuration |
| 114 | + * @returns True if the slot is in the past |
| 115 | + * |
| 116 | + * @category Utility |
| 117 | + * @since 2.0.0 |
| 118 | + */ |
| 119 | +export const isSlotInPast = (slot: Slot.Slot, slotConfig: SlotConfig.SlotConfig): boolean => { |
| 120 | + const now = UnixTime.now() |
| 121 | + const slotTime = slotToUnixTime(slot, slotConfig) |
| 122 | + return slotTime < now |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Get the slot at a specific offset from now (in milliseconds). |
| 127 | + * |
| 128 | + * @param offsetMs - Offset in milliseconds (positive for future, negative for past) |
| 129 | + * @param network - The network |
| 130 | + * @returns Slot number at the offset time |
| 131 | + * |
| 132 | + * @category Utility |
| 133 | + * @since 2.0.0 |
| 134 | + * |
| 135 | + * @example |
| 136 | + * ```typescript |
| 137 | + * import * as Time from "@evolution-sdk/core/Time" |
| 138 | + * |
| 139 | + * // Get slot 5 minutes from now |
| 140 | + * const futureSlot = Time.getSlotAt(5 * 60 * 1000, "Mainnet") |
| 141 | + * |
| 142 | + * // Get slot 10 minutes ago |
| 143 | + * const pastSlot = Time.getSlotAt(-10 * 60 * 1000, "Mainnet") |
| 144 | + * ``` |
| 145 | + */ |
| 146 | +export const getSlotAt = (offsetMs: number, network: Network.Network): Slot.Slot => { |
| 147 | + const config = SlotConfig.SLOT_CONFIG_NETWORK[network] |
| 148 | + const targetTime = UnixTime.now() + BigInt(offsetMs) |
| 149 | + return unixTimeToSlot(targetTime, config) |
| 150 | +} |
0 commit comments