Games.js 8.4 KB

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