typeDateFilter= { 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. */exportfunctionconvertDateToUnixTimestamp(date?:DateFilter):number {if (date?.hours) {// Convert hours to seconds and add to current Unix timestampreturnMath.floor(newDate().getTime() /1000) +date.hours *3600; } elseif (date?.days) {// Convert days to seconds and add to current Unix timestampreturnMath.floor(newDate().getTime() /1000) +date.days *24*3600; } else {// If no date is provided, default to one year in the futurereturnMath.floor(newDate().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. */exportfunctionconvertUnixTimestampToDate(unixTimestamp:number):Date {// Convert Unix timestamp to milliseconds and create a Date objectreturnnewDate(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. */exportfunctionorderToHumanReadable(order:any, tokens:any[]) {constmakerToken=tokens.find( (token) =>token.address ===order.maker_asset );consttakerToken=tokens.find( (token) =>token.address ===order.taker_asset );constmakerAmount=order.maker_amount /Math.pow(10,makerToken.decimals);consttakerAmount=order.taker_amount /Math.pow(10,takerToken.decimals);constmakerAsset=makerToken.symbol;consttakerAsset=takerToken.symbol;constorderPrice=order.order_price /Math.pow(10,takerToken.decimals);constremainingTakerAmount=order.remaining_taker_amount /Math.pow(10,takerToken.decimals);constremainingMakerAmount=order.remaining_maker_amount /Math.pow(10,makerToken.decimals);constexpiration=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.