PartnerGate.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import Logs from "../libs/logs.js";
  2. import { receivePartnerData } from "./Partner.js";
  3. import eventSolutions from '../triangle/eventSolutions.js';
  4. import { placePlatformOrder } from "./Markets.js";
  5. /**
  6. * 精确浮点数字
  7. * @param {number} number
  8. * @param {number} x
  9. * @returns {number}
  10. */
  11. const fixFloat = (number, x=3) => {
  12. return parseFloat(number.toFixed(x));
  13. }
  14. /**
  15. * 根据赔率获取策略
  16. * @param {*} params
  17. * @returns
  18. */
  19. const getSolutionWithIors = async(params) => {
  20. const { iors, cross_type, base_stake } = params;
  21. if (typeof params !== 'object' || params === null || Array.isArray(params)) {
  22. return Promise.reject(new Error('params must be an object'));
  23. }
  24. if (!Array.isArray(iors) || iors.length < 2) {
  25. return Promise.reject(new Error('iors must be an array and length must be greater than 2'));
  26. }
  27. iors.forEach(item => {
  28. if (typeof item.v !== 'number' || !Number.isFinite(item.v)) {
  29. return Promise.reject(new Error('iors must be an array of numbers'));
  30. }
  31. if (item.v <= 1) {
  32. return Promise.reject(new Error('iors must be an array of numbers greater than 1'));
  33. }
  34. });
  35. if (typeof cross_type !== 'string' || cross_type.length === 0) {
  36. return Promise.reject(new Error('cross_type must be a non-empty string'));
  37. }
  38. if (typeof base_stake !== 'number' || !Number.isFinite(base_stake) || base_stake <= 0) {
  39. return Promise.reject(new Error('base_stake must be a positive number'));
  40. }
  41. const base_index = iors.reduce((minIdx, cur, idx) => cur.v < iors[minIdx].v ? idx : minIdx, 0);
  42. if (iors.length === 2) {
  43. iors.push({ v: 1 });
  44. }
  45. const betInfo = {
  46. cross_type,
  47. base_index,
  48. base_stake,
  49. odds_side_a: fixFloat(iors[0].v - 1),
  50. odds_side_b: fixFloat(iors[1].v - 1),
  51. odds_side_c: fixFloat(iors[2].v - 1),
  52. };
  53. const sol = eventSolutions(betInfo, true);
  54. return sol;
  55. }
  56. /**
  57. * 下注Pinnacle
  58. * @param {*} data
  59. * @returns
  60. */
  61. const betPinnacle = async (params) => {
  62. const { id, ior, stake=0 } = params;
  63. const iorInfo = await placePlatformOrder(ior, 'pinnacle', id, stake);
  64. if (!iorInfo) {
  65. return Promise.reject(new Error('ior info not found'));
  66. }
  67. return iorInfo;
  68. }
  69. export const gate = async (data) => {
  70. return receivePartnerData(data).
  71. then(({ action, params }) => {
  72. switch (action) {
  73. case 'iors.solution':
  74. return getSolutionWithIors(params);
  75. case 'bet.pinnacle':
  76. return betPinnacle(params);
  77. default:
  78. return Promise.reject(new Error('invalid action'));
  79. }
  80. });
  81. }
  82. export default { gate };