UNPKG

@meshsdk/mesh-csl

Version:

Cardano Off-chain Code APIs built on cardano-serialization-lib

33 lines (32 loc) 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unixTimeToEnclosingSlot = exports.slotToBeginUnixTime = exports.SLOT_CONFIG_NETWORK = void 0; exports.SLOT_CONFIG_NETWORK = { Mainnet: { zeroTime: 1596059091000, zeroSlot: 4492800, slotLength: 1000 }, Preview: { zeroTime: 1666656000000, zeroSlot: 0, slotLength: 1000 }, Preprod: { zeroTime: 1654041600000 + 1728000000, zeroSlot: 86400, slotLength: 1000, }, /** Customizable slot config (Initialized with 0 values). */ Custom: { zeroTime: 0, zeroSlot: 0, slotLength: 0 }, }; const slotToBeginUnixTime = (slot, slotConfig) => { const msAfterBegin = (slot - slotConfig.zeroSlot) * slotConfig.slotLength; return slotConfig.zeroTime + msAfterBegin; }; exports.slotToBeginUnixTime = slotToBeginUnixTime; // slotToBeginUnixTime and slotToEndUnixTime are identical when slotLength == 1. So we don't need to worry about this now. // function slotToEndUnixTime(slot: Slot, slotConfig: SlotConfig): UnixTime { // return slotToBeginUnixTime(slot, slotConfig) + (slotConfig.slotLength - 1); // } const unixTimeToEnclosingSlot = (unixTime, slotConfig) => { const timePassed = unixTime - slotConfig.zeroTime; const slotsPassed = Math.floor(timePassed / slotConfig.slotLength); return slotsPassed + slotConfig.zeroSlot; }; exports.unixTimeToEnclosingSlot = unixTimeToEnclosingSlot; // const timeNow = new Date().getTime(); // console.log(timeNow); // console.log(unixTimeToEnclosingSlot(timeNow, SLOT_CONFIG_NETWORK.Preprod));