Games.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import GetTranslation from "../libs/getTranslation.js";
  2. import { getSolutionsWithRelations } from "../libs/getSolutions.js";
  3. import Store from "../state/store.js";
  4. import { getPlatformIorsDetailInfo, getSolutionByLatestIors, getSoulutionBetResult } from "./Markets.js";
  5. export const getLeagues = async () => {
  6. const { polymarket, pinnacle } = Store.get('leagues') ?? { polymarket: [], pinnacle: [] };
  7. const polymarketNames = polymarket.map(item => item.name);
  8. const translatedNames = await GetTranslation(polymarketNames);
  9. const newPolymarket = polymarket.map(item => {
  10. const { name } = item;
  11. const localesName = translatedNames[name] ?? name;
  12. return { ...item, localesName };
  13. });
  14. return { polymarket: newPolymarket, pinnacle };
  15. }
  16. export const setLeaguesRelation = async (relation) => {
  17. const { id, platforms } = relation;
  18. if (!id || !platforms) {
  19. return Promise.reject(new Error('invalid request', { cause: 400 }));
  20. }
  21. const storeRelations = Store.get('leaguesRelations') ?? {};
  22. if (storeRelations[id]) {
  23. return Promise.reject(new Error('relation already exists', { cause: 400 }));
  24. }
  25. storeRelations[id] = relation;
  26. Store.set('leaguesRelations', storeRelations);
  27. return Promise.resolve();
  28. };
  29. export const removeLeaguesRelation = async (id) => {
  30. if (!id) {
  31. return Promise.reject(new Error('invalid request', { cause: 400 }));
  32. }
  33. const storeRelations = Store.get('leaguesRelations') ?? {};
  34. if (!storeRelations[id]) {
  35. return Promise.reject(new Error('relation not found', { cause: 400 }));
  36. }
  37. delete storeRelations[id];
  38. Store.set('leaguesRelations', storeRelations);
  39. return Promise.resolve();
  40. };
  41. export const getLeaguesRelations = async () => {
  42. const storeRelations = Object.values(Store.get('leaguesRelations') ?? {});
  43. const polymarketNames = storeRelations.map(item => item.platforms.polymarket.name);
  44. const translatedNames = await GetTranslation(polymarketNames);
  45. const newRelations = storeRelations.map(item => {
  46. const { platforms: { polymarket, pinnacle } } = item;
  47. const { name } = polymarket;
  48. const localesName = translatedNames[name] ?? name;
  49. return { ...item, platforms: { polymarket: { ...polymarket, localesName }, pinnacle } };
  50. });
  51. return Promise.resolve(newRelations);
  52. };
  53. export const getGames = async () => {
  54. const { polymarket, pinnacle } = Store.get('games') ?? { polymarket: [], pinnacle: [] };
  55. const polymarketNames = [ ...new Set(polymarket.map(item => [item.teamHomeName, item.teamAwayName, item.leagueName]).flat()) ];
  56. const translatedNames = await GetTranslation(polymarketNames);
  57. const newPolymarket = polymarket.map(item => {
  58. const { leagueName, teamHomeName, teamAwayName } = item;
  59. const localesTeamHomeName = translatedNames[teamHomeName] ?? teamHomeName;
  60. const localesTeamAwayName = translatedNames[teamAwayName] ?? teamAwayName;
  61. const localesLeagueName = translatedNames[leagueName] ?? leagueName;
  62. return { ...item, localesTeamHomeName, localesTeamAwayName, localesLeagueName };
  63. }).filter(item => {
  64. const { timestamp } = item;
  65. const now = Date.now();
  66. return (timestamp + 1000 * 60 * 60 * 2) > now;
  67. }).sort((a, b) => a.timestamp - b.timestamp);
  68. return { polymarket: newPolymarket, pinnacle };
  69. }
  70. export const setGamesRelation = async (relation) => {
  71. const { id, platforms, timestamp } = relation;
  72. if (!id || !platforms || !timestamp) {
  73. return Promise.reject(new Error('invalid request', { cause: 400 }));
  74. }
  75. const storeRelations = Store.get('gamesRelations') ?? {};
  76. if (storeRelations[id]) {
  77. return Promise.reject(new Error('relation already exists', { cause: 400 }));
  78. }
  79. storeRelations[id] = relation;
  80. Store.set('gamesRelations', storeRelations);
  81. return Promise.resolve();
  82. }
  83. export const removeGamesRelation = async (id) => {
  84. if (!id) {
  85. return Promise.reject(new Error('invalid request', { cause: 400 }));
  86. }
  87. const storeRelations = Store.get('gamesRelations') ?? {};
  88. if (!storeRelations[id]) {
  89. return Promise.reject(new Error('relation not found', { cause: 400 }));
  90. }
  91. delete storeRelations[id];
  92. Store.set('gamesRelations', storeRelations);
  93. return Promise.resolve();
  94. }
  95. export const getGamesRelations = async () => {
  96. const storeRelations = Object.values(Store.get('gamesRelations') ?? {});
  97. const polymarketNames = [ ...new Set(storeRelations.map(item => {
  98. const { teamHomeName, teamAwayName, leagueName } = item.platforms.polymarket;
  99. return [teamHomeName, teamAwayName, leagueName];
  100. }).flat()) ];
  101. const translatedNames = await GetTranslation(polymarketNames);
  102. const newRelations = storeRelations.map(item => {
  103. const { platforms: { polymarket, pinnacle } } = item;
  104. const { teamHomeName, teamAwayName, leagueName } = polymarket;
  105. const localesTeamHomeName = translatedNames[teamHomeName] ?? teamHomeName;
  106. const localesTeamAwayName = translatedNames[teamAwayName] ?? teamAwayName;
  107. const localesLeagueName = translatedNames[leagueName] ?? leagueName;
  108. return { ...item, platforms: { polymarket: { ...polymarket, localesTeamHomeName, localesTeamAwayName, localesLeagueName }, pinnacle } };
  109. }).sort((a, b) => a.timestamp - b.timestamp);
  110. return Promise.resolve(newRelations);
  111. }
  112. /**
  113. * 获取解决方案
  114. * @param {*} param0
  115. * @returns
  116. */
  117. export const getSolutions = async ({ min_profit_rate = 0 } = {}) => {
  118. const solutions = Store.get('solutions') ?? [];
  119. const solutionsList = solutions.filter(solution => {
  120. const { sol: { win_profit_rate } } = solution;
  121. return win_profit_rate >= min_profit_rate;
  122. }).sort((a, b) => {
  123. return b.sol.win_profit_rate - a.sol.win_profit_rate;
  124. });
  125. const gamesRelations = Store.get('gamesRelations') ?? {};
  126. return getSolutionsWithRelations(solutionsList, gamesRelations, 5);
  127. }
  128. /**
  129. * 获取策略对应的盘口信息
  130. */
  131. export const getSolutionIorsInfo = async (sid) => {
  132. const solution = Store.get('solutions')?.find(item => item.sid == sid);
  133. if (!solution) {
  134. return Promise.reject(new Error('solution not found', { cause: 400 }));
  135. }
  136. const { info: { id }, cpr, sol: { cross_type } } = solution;
  137. const gamesRelations = Store.get('gamesRelations') ?? {};
  138. const gameRelation = gamesRelations[id];
  139. if (!gameRelation) {
  140. return Promise.reject(new Error('game relation not found', { cause: 400 }));
  141. }
  142. const { platforms: { polymarket, pinnacle } } = gameRelation;
  143. const idMap = { polymarket: polymarket.id, pinnacle: pinnacle.id };
  144. const iorsInfo = await Promise.all(cpr.map(item => {
  145. const { k, p } = item;
  146. return getPlatformIorsDetailInfo(k, p, idMap[p]);
  147. }));
  148. return { cpr, iorsInfo, cross_type };
  149. // const accountBalance = await getAccountBalance();
  150. // const solutionInfo = getSolutionByLatestIors(iorsInfo, cross_type);
  151. // return { cpr, iorsInfo, ...solutionInfo };
  152. }
  153. /**
  154. * 根据策略下注
  155. */
  156. export const betSolution = async (sid, stake=0) => {
  157. const solutionIorsInfo = await getSolutionIorsInfo(sid);
  158. const { iorsInfo, cross_type } = solutionIorsInfo;
  159. const solutionInfo = getSolutionByLatestIors(iorsInfo, cross_type);
  160. if (solutionInfo?.error) {
  161. const error = new Error(solutionInfo.error, { cause: 400 });
  162. error.data = solutionInfo.data;
  163. return Promise.reject(error);
  164. }
  165. const betResult = await getSoulutionBetResult({ ...solutionIorsInfo, ...solutionInfo, stake });
  166. return { betResult, ...solutionIorsInfo, ...solutionInfo };
  167. }
  168. /**
  169. * 清理过期关系
  170. */
  171. const cleanGamesRelations = () => {
  172. const now = Date.now();
  173. const storeRelations = Store.get('gamesRelations') ?? [];
  174. Object.keys(storeRelations).forEach(key => {
  175. const relation = storeRelations[key];
  176. const { timestamp } = relation;
  177. if ((timestamp + 1000 * 60 * 60 * 2) < now) {
  178. delete storeRelations[key];
  179. }
  180. });
  181. Store.set('gamesRelations', storeRelations);
  182. }
  183. setInterval(cleanGamesRelations, 1000 * 60);
  184. export default {
  185. getLeagues, setLeaguesRelation, removeLeaguesRelation, getLeaguesRelations,
  186. getGames, setGamesRelation, removeGamesRelation, getGamesRelations,
  187. getSolutions, getSolutionIorsInfo, betSolution,
  188. };