@drift-labs/common
Version:
Common functions for Drift
108 lines • 5.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.S3_LEADERBOARD_CLIENT = exports.getDateRangeFromSelection = exports.s3DateStringToDate = exports.dateToS3DateString = exports.getFileRedisKeyFromParams = void 0;
const _1 = require(".");
const serializableTypes_1 = require("../serializableTypes");
const constants_1 = require("../constants");
// # Redis key for download requests is the downloadRequestParams with colon delimeters.
// # Stringifies the keys alphabetically so we don't have mismatches for identical requests
const getFileRedisKeyFromParams = (downloadRequestParams) => {
return Object.keys(downloadRequestParams)
.sort((a, b) => a.localeCompare(b))
.map((sortedKey) => downloadRequestParams[sortedKey])
.filter((val) => val !== undefined)
.join(':');
};
exports.getFileRedisKeyFromParams = getFileRedisKeyFromParams;
// # Converts a date object into the string format used for S3 files
const dateToS3DateString = (date) => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return `${year}${month < 10 ? '0' + month : month}${day < 10 ? '0' + day : day}`;
};
exports.dateToS3DateString = dateToS3DateString;
// # Converts a string date in S3 file format into a Date object
const s3DateStringToDate = (dateStr) => {
return new Date(parseFloat(dateStr.slice(0, 4)), parseFloat(dateStr.slice(4, 6)) - 1, parseFloat(dateStr.slice(6, 8)));
};
exports.s3DateStringToDate = s3DateStringToDate;
// # Get the from and to dates in S3 file format for a download request
// # For custom opts, month number should be the index (ie: January is index 0)
const getDateRangeFromSelection = (downloadPeriod, customOpts) => {
let from, to;
const now = new Date();
switch (downloadPeriod) {
case 'week':
from = (0, exports.dateToS3DateString)(new Date(now.getTime() - 7 * constants_1.ONE_DAY_MS));
to = (0, exports.dateToS3DateString)(now);
break;
case 'month':
from = (0, exports.dateToS3DateString)(new Date(now.getFullYear(), now.getMonth(), 1));
to = (0, exports.dateToS3DateString)(now);
break;
case '3mo': {
const fromDate = new Date();
fromDate.setMonth(now.getMonth() - 3);
from = (0, exports.dateToS3DateString)(fromDate);
to = (0, exports.dateToS3DateString)(now);
break;
}
case 'ytd':
from = (0, exports.dateToS3DateString)(new Date(now.getFullYear(), 0, 1));
to = (0, exports.dateToS3DateString)(now);
break;
case 'year':
from = (0, exports.dateToS3DateString)(new Date(now.getTime() - 365 * constants_1.ONE_DAY_MS));
to = (0, exports.dateToS3DateString)(now);
break;
case 'custom':
if (!customOpts || (customOpts === null || customOpts === void 0 ? void 0 : customOpts.year) == undefined) {
console.error('Requested custom date range without providing customOpts');
break;
}
if (customOpts.day == undefined && customOpts.month == undefined) {
// request a full year
from = (0, exports.dateToS3DateString)(new Date(customOpts.year, 0, 1));
to = (0, exports.dateToS3DateString)(new Date(customOpts.year, 11, 31));
}
else if (customOpts.day == undefined &&
customOpts.month !== undefined) {
// request a month
from = (0, exports.dateToS3DateString)(new Date(customOpts.year, customOpts.month, 1));
// if December, to date should be 12/31
if (customOpts.month === 11) {
to = (0, exports.dateToS3DateString)(new Date(customOpts.year, customOpts.month, 31));
}
else {
to = (0, exports.dateToS3DateString)(new Date(customOpts.year, customOpts.month + 1, 0));
}
}
else {
// request a day
from = (0, exports.dateToS3DateString)(new Date(customOpts.year, customOpts.month, customOpts.day));
to = (0, exports.dateToS3DateString)(new Date(customOpts.year, customOpts.month, customOpts.day));
}
break;
}
return { from, to };
};
exports.getDateRangeFromSelection = getDateRangeFromSelection;
const getLeaderboardFilename = (orderBy, resolution) => `${orderBy}_${_1.ENUM_UTILS.toStr(resolution)}_leaderboard.json`;
exports.S3_LEADERBOARD_CLIENT = {
getFileName: (orderBy, resolution) => {
return getLeaderboardFilename(orderBy, resolution);
},
getLeaderboardValue: async (env, leaderboardBucketName, orderBy, resolution) => {
const leadboardLocation = env === 'mainnet-beta' ? 's3.eu-west-1' : 's3.us-east-1';
const s3Url = `https://${leaderboardBucketName}.${leadboardLocation}.amazonaws.com/${getLeaderboardFilename(orderBy, resolution)}`;
const result = await (await fetch(s3Url, { cache: 'no-cache' })).json();
const deserializedResult = serializableTypes_1.Serializer.Deserialize.UISerializableLeaderboardResult(result);
return deserializedResult;
},
};
const S3_BUCKET_UTILS = {
S3_LEADERBOARD_CLIENT: exports.S3_LEADERBOARD_CLIENT,
};
exports.default = S3_BUCKET_UTILS;
//# sourceMappingURL=s3Buckets.js.map
;