wisdom-sdk
Version:
Core business logic and data access layer for prediction markets
128 lines (126 loc) • 4.28 kB
JavaScript
;
// src/utils.ts
var ADMIN_USER_IDS = [
"user_2tjVcbojjJk2bkQd856eNE1Ax0S",
// rozar
"user_2tkBcBEVGanm3LHkg6XK7j91DRj"
// kraken
];
function isAdmin(userId) {
return ADMIN_USER_IDS.includes(userId);
}
function generateUUID() {
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
return crypto.randomUUID();
}
const getRandomBytes = (n) => {
const bytes = new Uint8Array(n);
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
crypto.getRandomValues(bytes);
} else {
for (let i = 0; i < n; i++) {
bytes[i] = Math.floor(Math.random() * 256);
}
}
return bytes;
};
const randomBytes = getRandomBytes(16);
randomBytes[6] = randomBytes[6] & 15 | 64;
randomBytes[8] = randomBytes[8] & 63 | 128;
let hex = "";
for (let i = 0; i < 16; i++) {
hex += randomBytes[i].toString(16).padStart(2, "0");
if (i === 3 || i === 5 || i === 7 || i === 9) {
hex += "-";
}
}
return hex;
}
function calculateOutcomePercentages(outcomes) {
const totalAmount = outcomes.reduce((sum, outcome) => sum + (outcome.amount || 0), 0);
const useFallbackVotes = totalAmount === 0;
const totalVotes = useFallbackVotes ? outcomes.reduce((sum, outcome) => sum + (outcome.votes || 0), 0) : 0;
const outcomesWithPercentages = outcomes.map((outcome) => ({
...outcome,
percentage: useFallbackVotes ? totalVotes > 0 ? Math.round((outcome.votes || 0) / totalVotes * 100) : 0 : totalAmount > 0 ? Math.round((outcome.amount || 0) / totalAmount * 100) : 0
}));
return {
outcomesWithPercentages,
useFallbackVotes
};
}
function getBaseUrl() {
if (process.env.NEXT_PUBLIC_APP_URL) {
return process.env.NEXT_PUBLIC_APP_URL;
}
if (typeof window !== "undefined") {
if (process.env.NODE_ENV === "development") {
return window.location.origin;
}
}
return "https://oppredict.com";
}
function searchMarketText(market, searchText) {
if (!searchText) return true;
const text = `${market.name} ${market.description}`.toLowerCase();
const terms = searchText.toLowerCase().split(/\s+/).filter(Boolean);
return terms.every((term) => text.includes(term));
}
function filterMarkets(markets, options = {}) {
return markets.filter((market) => {
console.log(options.status, market.status);
if (options.status && options.status !== "all" && market.status !== options.status) {
return false;
}
if (options.category && market.category !== options.category) {
return false;
}
if (options.type && options.type !== "all" && market.type !== options.type) {
return false;
}
if (options.creatorId && market.createdBy !== options.creatorId) {
return false;
}
if (options.search && !searchMarketText(market, options.search)) {
return false;
}
return true;
});
}
function sortMarkets(markets, sortBy = "createdAt", sortDirection = "desc") {
return [...markets].sort((a, b) => {
let comparison = 0;
if (sortBy === "createdAt" || sortBy === "endDate") {
const dateA = new Date(a[sortBy] || 0).getTime();
const dateB = new Date(b[sortBy] || 0).getTime();
comparison = dateA - dateB;
} else {
const valA = a[sortBy] || 0;
const valB = b[sortBy] || 0;
comparison = valA - valB;
}
return sortDirection === "asc" ? comparison : -comparison;
});
}
function paginateResults(items, options) {
const limit = options.limit || 20;
const offset = options.offset || 0;
const paginatedItems = items.slice(offset, offset + limit);
return {
items: paginatedItems,
total: items.length,
hasMore: offset + paginatedItems.length < items.length,
nextCursor: offset + paginatedItems.length < items.length ? `${offset + limit}` : void 0
};
}
exports.ADMIN_USER_IDS = ADMIN_USER_IDS;
exports.calculateOutcomePercentages = calculateOutcomePercentages;
exports.filterMarkets = filterMarkets;
exports.generateUUID = generateUUID;
exports.getBaseUrl = getBaseUrl;
exports.isAdmin = isAdmin;
exports.paginateResults = paginateResults;
exports.searchMarketText = searchMarketText;
exports.sortMarkets = sortMarkets;
//# sourceMappingURL=utils.cjs.map
//# sourceMappingURL=utils.cjs.map