{"version":3,"sources":["../../src/utils/orderExpiration.ts"],"sourcesContent":["import { BigDecimalish, toBigInt } from '@nadohq/utils';\n\n// All valid \"special\" order expiration types\nexport type OrderExpirationType = 'default' | 'ioc' | 'fok' | 'post_only';\n\n// Encodes all aspects of an order expiration number\nexport interface OrderExpirationConfig {\n  type: OrderExpirationType;\n  // The expiration timestamp in UNIX seconds\n  expirationTime: number;\n  // If true, the order can only reduce the size of an existing position. Works only with IOC & FOK\n  reduceOnly?: boolean;\n}\n\n// 2 MSBs of u64\nconst EXPIRATION_TYPE_TO_MS2B: Record<OrderExpirationType, bigint> = {\n  default: 0n,\n  ioc: 1n,\n  fok: 2n,\n  post_only: 3n,\n};\n\n/**\n * Special order types, such as immediate-or-cancel, are encoded into the expiration field.\n * This is a utility to create the proper timestamp needed\n *\n * @param config\n */\nexport function getExpirationTimestamp(config: OrderExpirationConfig): bigint {\n  const expirationWithType =\n    toBigInt(config.expirationTime) |\n    (EXPIRATION_TYPE_TO_MS2B[config.type] << 62n);\n\n  if (!config.reduceOnly) {\n    return expirationWithType;\n  }\n\n  // 3rd MSB denotes the boolean value of reduce-only\n  return expirationWithType | (1n << 61n);\n}\n\n/**\n * Parses the expiration timestamp into its subcomponents:\n * - expiration type\n * - expiration timestamp in UNIX seconds\n * - value of the reduce only flag\n *\n * @param rawExpiration\n */\nexport function parseRawExpirationTimestamp(\n  rawExpiration: BigDecimalish,\n): Required<OrderExpirationConfig> {\n  const bigIntRawExpiration = toBigInt(rawExpiration);\n  const largestTwoBits = bigIntRawExpiration >> 62n;\n  const reduceOnlyBitValue = (bigIntRawExpiration >> 61n) & 1n;\n\n  const expirationType = (() => {\n    if (largestTwoBits === EXPIRATION_TYPE_TO_MS2B.default) {\n      return 'default';\n    }\n    if (largestTwoBits === EXPIRATION_TYPE_TO_MS2B.ioc) {\n      return 'ioc';\n    }\n    if (largestTwoBits === EXPIRATION_TYPE_TO_MS2B.fok) {\n      return 'fok';\n    }\n    if (largestTwoBits === EXPIRATION_TYPE_TO_MS2B.post_only) {\n      return 'post_only';\n    }\n    throw new Error(\n      `Could not detect order expiration type. Raw expiration ${bigIntRawExpiration.toString()}`,\n    );\n  })();\n  const bigIntExpiration = bigIntRawExpiration - (largestTwoBits << 62n);\n\n  return {\n    type: expirationType,\n    expirationTime: Number(bigIntExpiration.toString()),\n    reduceOnly: reduceOnlyBitValue === 1n,\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAwC;AAexC,IAAM,0BAA+D;AAAA,EACnE,SAAS;AAAA,EACT,KAAK;AAAA,EACL,KAAK;AAAA,EACL,WAAW;AACb;AAQO,SAAS,uBAAuB,QAAuC;AAC5E,QAAM,yBACJ,uBAAS,OAAO,cAAc,IAC7B,wBAAwB,OAAO,IAAI,KAAK;AAE3C,MAAI,CAAC,OAAO,YAAY;AACtB,WAAO;AAAA,EACT;AAGA,SAAO,qBAAsB,MAAM;AACrC;AAUO,SAAS,4BACd,eACiC;AACjC,QAAM,0BAAsB,uBAAS,aAAa;AAClD,QAAM,iBAAiB,uBAAuB;AAC9C,QAAM,qBAAsB,uBAAuB,MAAO;AAE1D,QAAM,kBAAkB,MAAM;AAC5B,QAAI,mBAAmB,wBAAwB,SAAS;AACtD,aAAO;AAAA,IACT;AACA,QAAI,mBAAmB,wBAAwB,KAAK;AAClD,aAAO;AAAA,IACT;AACA,QAAI,mBAAmB,wBAAwB,KAAK;AAClD,aAAO;AAAA,IACT;AACA,QAAI,mBAAmB,wBAAwB,WAAW;AACxD,aAAO;AAAA,IACT;AACA,UAAM,IAAI;AAAA,MACR,0DAA0D,oBAAoB,SAAS,CAAC;AAAA,IAC1F;AAAA,EACF,GAAG;AACH,QAAM,mBAAmB,uBAAuB,kBAAkB;AAElE,SAAO;AAAA,IACL,MAAM;AAAA,IACN,gBAAgB,OAAO,iBAAiB,SAAS,CAAC;AAAA,IAClD,YAAY,uBAAuB;AAAA,EACrC;AACF;","names":[]}