Utils

convertDateToUnixTimestamp

type DateFilter = {
  hours?: number;
  days?: number;
};

/**
 * Converts a given date filter to a Unix timestamp.
 * @param {DateFilter} [date] - The date filter object specifying hours or days.
 * @returns {number} The Unix timestamp representing the converted date.
 */
export function convertDateToUnixTimestamp(date?: DateFilter): number {
  if (date?.hours) {
    // Convert hours to seconds and add to current Unix timestamp
    return Math.floor(new Date().getTime() / 1000) + date.hours * 3600;
  } else if (date?.days) {
    // Convert days to seconds and add to current Unix timestamp
    return Math.floor(new Date().getTime() / 1000) + date.days * 24 * 3600;
  } else {
    // If no date is provided, default to one year in the future
    return Math.floor(new Date().getTime() / 1000) + 365 * 24 * 3600;
  }
}

This function takes an optional DateFilter object as input, which specifies either the number of hours or days. It returns a Unix timestamp representing the given date. If no date is provided, it defaults to one year in the future.

convertUnixTimestampToDate

/**
 * Converts a Unix timestamp to a JavaScript Date object.
 * @param {number} unixTimestamp - The Unix timestamp to convert.
 * @returns {Date} The JavaScript Date object representing the converted timestamp.
 */
export function convertUnixTimestampToDate(unixTimestamp: number): Date {
  // Convert Unix timestamp to milliseconds and create a Date object
  return new Date(unixTimestamp * 1000);
}

This function takes a Unix timestamp as input and returns a JavaScript Date object representing the converted timestamp. It multiplies the Unix timestamp by 1000 to convert it from seconds to milliseconds, as JavaScript Date objects operate in milliseconds.

orderToHumanReadable

/**
 * Converts an order object to human-readable format.
 * @param {any} order - The order object to convert.
 * @param {any[]} tokens - An array of token objects containing token information.
 * @returns {Object} An object containing human-readable order information.
 */
export function orderToHumanReadable(order: any, tokens: any[]) {
  const makerToken = tokens.find(
    (token) => token.address === order.maker_asset
  );
  const takerToken = tokens.find(
    (token) => token.address === order.taker_asset
  );
  const makerAmount = order.maker_amount / Math.pow(10, makerToken.decimals);
  const takerAmount = order.taker_amount / Math.pow(10, takerToken.decimals);
  const makerAsset = makerToken.symbol;
  const takerAsset = takerToken.symbol;
  const orderPrice = order.order_price / Math.pow(10, takerToken.decimals);
  const remainingTakerAmount =
    order.remaining_taker_amount / Math.pow(10, takerToken.decimals);
  const remainingMakerAmount =
    order.remaining_maker_amount / Math.pow(10, makerToken.decimals);
  const expiration = convertUnixTimestampToDate(order.expiration);
  return {
    signer: order.signer,
    maker_asset: makerAsset,
    taker_asset: takerAsset,
    maker_amount: makerAmount,
    taker_amount: takerAmount,
    order_price: orderPrice,
    remaining_maker_amount: remainingMakerAmount,
    remaining_taker_amount: remainingTakerAmount,
    expiration: expiration,
    nonce: order.nonce,
    use_solver: order.use_solver,
    partial_fill: order.partial_fill,
    order_hash: order.order_hash,
    status: order.status,
    signature: order.signature,
  };
}

This function takes an order object and an array of token objects as parameters and returns an object containing human-readable order information. It converts amounts from their raw form to their respective token units using token decimals, and it also converts the expiration timestamp to a human-readable date format.

Last updated