import GetTranslation from "../libs/getTranslation.js"; import { getSolutionsWithRelations } from "../libs/getSolutions.js"; import Store from "../state/store.js"; import { getPlatformIorsDetailInfo, getSolutionByLatestIors, getSoulutionBetResult } from "./Markets.js"; export const getLeagues = async () => { const { polymarket, pinnacle } = Store.get('leagues') ?? { polymarket: [], pinnacle: [] }; const polymarketNames = polymarket.map(item => item.name); const translatedNames = await GetTranslation(polymarketNames); const newPolymarket = polymarket.map(item => { const { name } = item; const localesName = translatedNames[name] ?? name; return { ...item, localesName }; }); return { polymarket: newPolymarket, pinnacle }; } export const setLeaguesRelation = async (relation) => { const { id, platforms } = relation; if (!id || !platforms) { return Promise.reject(new Error('invalid request', { cause: 400 })); } const storeRelations = Store.get('leaguesRelations') ?? {}; if (storeRelations[id]) { return Promise.reject(new Error('relation already exists', { cause: 400 })); } storeRelations[id] = relation; Store.set('leaguesRelations', storeRelations); return Promise.resolve(); }; export const removeLeaguesRelation = async (id) => { if (!id) { return Promise.reject(new Error('invalid request', { cause: 400 })); } const storeRelations = Store.get('leaguesRelations') ?? {}; if (!storeRelations[id]) { return Promise.reject(new Error('relation not found', { cause: 400 })); } delete storeRelations[id]; Store.set('leaguesRelations', storeRelations); return Promise.resolve(); }; export const getLeaguesRelations = async () => { const storeRelations = Object.values(Store.get('leaguesRelations') ?? {}); const polymarketNames = storeRelations.map(item => item.platforms.polymarket.name); const translatedNames = await GetTranslation(polymarketNames); const newRelations = storeRelations.map(item => { const { platforms: { polymarket, pinnacle } } = item; const { name } = polymarket; const localesName = translatedNames[name] ?? name; return { ...item, platforms: { polymarket: { ...polymarket, localesName }, pinnacle } }; }); return Promise.resolve(newRelations); }; export const getGames = async () => { const { polymarket, pinnacle } = Store.get('games') ?? { polymarket: [], pinnacle: [] }; const polymarketNames = [ ...new Set(polymarket.map(item => [item.teamHomeName, item.teamAwayName, item.leagueName]).flat()) ]; const translatedNames = await GetTranslation(polymarketNames); const newPolymarket = polymarket.map(item => { const { leagueName, teamHomeName, teamAwayName } = item; const localesTeamHomeName = translatedNames[teamHomeName] ?? teamHomeName; const localesTeamAwayName = translatedNames[teamAwayName] ?? teamAwayName; const localesLeagueName = translatedNames[leagueName] ?? leagueName; return { ...item, localesTeamHomeName, localesTeamAwayName, localesLeagueName }; }).filter(item => { const { timestamp } = item; const now = Date.now(); return (timestamp + 1000 * 60 * 60 * 2) > now; }).sort((a, b) => a.timestamp - b.timestamp); return { polymarket: newPolymarket, pinnacle }; } export const setGamesRelation = async (relation) => { const { id, platforms, timestamp } = relation; if (!id || !platforms || !timestamp) { return Promise.reject(new Error('invalid request', { cause: 400 })); } const storeRelations = Store.get('gamesRelations') ?? {}; if (storeRelations[id]) { return Promise.reject(new Error('relation already exists', { cause: 400 })); } storeRelations[id] = relation; Store.set('gamesRelations', storeRelations); return Promise.resolve(); } export const removeGamesRelation = async (id) => { if (!id) { return Promise.reject(new Error('invalid request', { cause: 400 })); } const storeRelations = Store.get('gamesRelations') ?? {}; if (!storeRelations[id]) { return Promise.reject(new Error('relation not found', { cause: 400 })); } delete storeRelations[id]; Store.set('gamesRelations', storeRelations); return Promise.resolve(); } export const getGamesRelations = async () => { const storeRelations = Object.values(Store.get('gamesRelations') ?? {}); const polymarketNames = [ ...new Set(storeRelations.map(item => { const { teamHomeName, teamAwayName, leagueName } = item.platforms.polymarket; return [teamHomeName, teamAwayName, leagueName]; }).flat()) ]; const translatedNames = await GetTranslation(polymarketNames); const newRelations = storeRelations.map(item => { const { platforms: { polymarket, pinnacle } } = item; const { teamHomeName, teamAwayName, leagueName } = polymarket; const localesTeamHomeName = translatedNames[teamHomeName] ?? teamHomeName; const localesTeamAwayName = translatedNames[teamAwayName] ?? teamAwayName; const localesLeagueName = translatedNames[leagueName] ?? leagueName; return { ...item, platforms: { polymarket: { ...polymarket, localesTeamHomeName, localesTeamAwayName, localesLeagueName }, pinnacle } }; }).sort((a, b) => a.timestamp - b.timestamp); return Promise.resolve(newRelations); } /** * 获取解决方案 * @param {*} param0 * @returns */ export const getSolutions = async ({ min_profit_rate = 0 } = {}) => { const solutions = Store.get('solutions') ?? []; const solutionsList = solutions.filter(solution => { const { sol: { win_profit_rate } } = solution; return win_profit_rate >= min_profit_rate; }).sort((a, b) => { return b.sol.win_profit_rate - a.sol.win_profit_rate; }); const gamesRelations = Store.get('gamesRelations') ?? {}; return getSolutionsWithRelations(solutionsList, gamesRelations, 5); } /** * 获取策略对应的盘口信息 */ export const getSolutionIorsInfo = async (sid) => { const solution = Store.get('solutions')?.find(item => item.sid == sid); if (!solution) { return Promise.reject(new Error('solution not found', { cause: 400 })); } const { info: { id }, cpr, sol: { cross_type } } = solution; const gamesRelations = Store.get('gamesRelations') ?? {}; const gameRelation = gamesRelations[id]; if (!gameRelation) { return Promise.reject(new Error('game relation not found', { cause: 400 })); } const { platforms: { polymarket, pinnacle } } = gameRelation; const idMap = { polymarket: polymarket.id, pinnacle: pinnacle.id }; const iorsInfo = await Promise.all(cpr.map(item => { const { k, p } = item; return getPlatformIorsDetailInfo(k, p, idMap[p]); })); return { cpr, iorsInfo, cross_type }; // const accountBalance = await getAccountBalance(); // const solutionInfo = getSolutionByLatestIors(iorsInfo, cross_type); // return { cpr, iorsInfo, ...solutionInfo }; } /** * 根据策略下注 */ export const betSolution = async (sid, stake=0) => { const solutionIorsInfo = await getSolutionIorsInfo(sid); const { iorsInfo, cross_type } = solutionIorsInfo; const solutionInfo = getSolutionByLatestIors(iorsInfo, cross_type); return solutionInfo; if (solutionInfo?.error) { const error = new Error(solutionInfo.error, { cause: 400 }); error.data = solutionInfo.data; return Promise.reject(error); } const betResult = await getSoulutionBetResult({ ...solutionIorsInfo, ...solutionInfo, stake }); return { betResult, ...solutionIorsInfo, ...solutionInfo }; } /** * 清理过期关系 */ const cleanGamesRelations = () => { const now = Date.now(); const storeRelations = Store.get('gamesRelations') ?? []; Object.keys(storeRelations).forEach(key => { const relation = storeRelations[key]; const { timestamp } = relation; if ((timestamp + 1000 * 60 * 60 * 2) < now) { delete storeRelations[key]; } }); Store.set('gamesRelations', storeRelations); } setInterval(cleanGamesRelations, 1000 * 60); export default { getLeagues, setLeaguesRelation, removeLeaguesRelation, getLeaguesRelations, getGames, setGamesRelation, removeGamesRelation, getGamesRelations, getSolutions, getSolutionIorsInfo, betSolution, };