flyzto 2 долоо хоног өмнө
parent
commit
15049ec225

+ 21 - 13
pinnacle/libs/pinnacleClient.js

@@ -29,16 +29,18 @@ const pinnacleWebOptions = {
   },
 }
 
-export const pinnacleRequest = async (options) => {
+export const pinnacleRequest = async (options, channel) => {
 
   const { url, ...optionsRest } = options;
   const username = process.env.PINNACLE_USERNAME;
   const password = process.env.PINNACLE_PASSWORD;
-  if (!url || !username || !password) {
-    throw new Error("url、username、password is required");
+  if (!url || !channel && (!username || !password)) {
+    throw new Error("url、username、password、channel is required");
   }
 
-  const authHeader = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
+  Logs.outDev('pinnacle request', url, channel, username, password);
+
+  const authHeader = channel ? `Basic ${channel}` : `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
   const axiosConfig = { ...axiosDefaultOptions, ...optionsRest, url, baseURL: "https://api.pinnacle888.com" };
   Object.assign(axiosConfig.headers, {
     "Authorization": authHeader,
@@ -61,11 +63,11 @@ export const pinnacleRequest = async (options) => {
  * @param {*} params
  * @returns
  */
-export const pinnacleGet = async (url, params) => {
+export const pinnacleGet = async (url, params, channel) => {
   return pinnacleRequest({
     url,
     params
-  })
+  }, channel)
   .catch(err => {
     const source = { url, params };
     if (err?.response?.data) {
@@ -83,7 +85,7 @@ export const pinnacleGet = async (url, params) => {
  * @param {*} data
  * @returns
  */
-export const pinnaclePost = async (url, data) => {
+export const pinnaclePost = async (url, data, channel) => {
   return pinnacleRequest({
     url,
     method: 'POST',
@@ -91,7 +93,7 @@ export const pinnaclePost = async (url, data) => {
       'Content-Type': 'application/json',
     },
     data
-  });
+  }, channel);
 }
 
 /**
@@ -108,7 +110,7 @@ const cleanUndefined = (obj) => {
 /**
  * 获取直盘线
  */
-export const getLineInfo = async (info = {}) => {
+export const getLineInfo = async (info = {}, channel) => {
   const {
     leagueId, eventId, betType, handicap, team, side,
     specialId, contestantId,
@@ -121,10 +123,12 @@ export const getLineInfo = async (info = {}) => {
     data = { specialId, contestantId, oddsFormat };
   }
   data = cleanUndefined(data);
-  return pinnacleGet(url, data)
+  return pinnacleGet(url, data, channel)
   .then(ret => ({ info: ret, line: data }))
   .catch(err => {
     Logs.errDev('get line info error', err);
+    err.data = err.response.data;
+    err.cause = err.response.status;
     return Promise.reject(err);
   });
 }
@@ -139,7 +143,7 @@ export const getAccountBalance = async () => {
 /**
  * 下注
  */
-export const placeOrder = async ({ info, line, stakeSize }) => {
+export const placeOrder = async ({ info, line, stakeSize }, channel) => {
   // return Promise.resolve({info, line, stakeSize});
   const uuid = randomUUID()
   if (line.specialId) {
@@ -154,7 +158,7 @@ export const placeOrder = async ({ info, line, stakeSize }) => {
       contestantId: info.contestantId,
     });
     Logs.outDev('pinnacle place order data', data);
-    return pinnaclePost('/v4/bets/special', { bets: [data] })
+    return pinnaclePost('/v4/bets/special', { bets: [data] }, channel)
     .then(ret => ret.bets?.[0] ?? ret)
     .then(ret => {
       Logs.outDev('pinnacle place order', ret, uuid);
@@ -163,6 +167,8 @@ export const placeOrder = async ({ info, line, stakeSize }) => {
     .catch(err => {
       Logs.outDev('pinnacle place order error', err.response.data, uuid);
       Logs.errDev(err);
+      err.data = err.response.data;
+      err.cause = err.response.status;
       return Promise.reject(err);
     });
   }
@@ -185,7 +191,7 @@ export const placeOrder = async ({ info, line, stakeSize }) => {
       handicap: line.handicap,
     });
     Logs.outDev('pinnacle place order data', data);
-    return pinnaclePost('/v4/bets/place', data)
+    return pinnaclePost('/v4/bets/place', data, channel)
     .then(ret => {
       Logs.outDev('pinnacle place order', ret, uuid);
       return ret;
@@ -193,6 +199,8 @@ export const placeOrder = async ({ info, line, stakeSize }) => {
     .catch(err => {
       Logs.outDev('pinnacle place order error', err.response.data, uuid);
       Logs.errDev(err);
+      err.data = err.response.data;
+      err.cause = err.response.status;
       return Promise.reject(err);
     });
   }

+ 9 - 3
server/main.js

@@ -37,21 +37,21 @@ app.use((req, res, next) => {
     }
     return res.status(400).json({ statusCode: 400, code: -1, message: msg ?? 'Bad Request', data });
   }
-  res.unauthorized = (data,msg) => {
+  res.unauthorized = (data, msg) => {
     if (!msg && typeof data === 'string') {
       msg = data;
       data = undefined;
     }
     return res.status(401).json({ statusCode: 401, code: -1,  message: msg ?? 'Unauthorized', data });
   }
-    res.notFound = (data,msg) => {
+  res.notFound = (data, msg) => {
     if (!msg && typeof data === 'string') {
       msg = data;
       data = undefined;
     }
     return res.status(404).json({ statusCode: 404, code: -1,  message: msg ?? 'Not Found', data });
   }
-  res.serverError = (data,msg) => {
+  res.serverError = (data, msg) => {
     if (!msg && typeof data === 'string') {
       msg = data;
       data = undefined;
@@ -69,6 +69,12 @@ app.use((req, res, next) => {
     if (err.cause == 400) {
       return res.badRequest(err.data, err.message);
     }
+    else if (err.cause == 401) {
+      return res.unauthorized(err.data, err.message);
+    }
+    else if (err.cause == 404) {
+      return res.notFound(err.data, err.message);
+    }
     else {
       return res.serverError(err.data, err.message);
     }

+ 7 - 7
server/models/Markets.js

@@ -288,14 +288,14 @@ const getPolymarketIorDetailInfo = async (info) => {
 /**
  * 获取pinnacle盘口详细信息
  */
-const getPinnacleIorDetailInfo = async (info) => {
-  return getLineInfo(info);
+const getPinnacleIorDetailInfo = async (info, channel) => {
+  return getLineInfo(info, channel);
 }
 
 /**
  * 获取平台盘口详细信息
  */
-export const getPlatformIorsDetailInfo = async (ior, platform, id) => {
+export const getPlatformIorsDetailInfo = async (ior, platform, id, channel) => {
   const info = await getPlatformIorInfo(ior, platform, id);
   if (!info) {
     return Promise.reject(new Error('platform ior info not found', { cause: 400 }));
@@ -305,7 +305,7 @@ export const getPlatformIorsDetailInfo = async (ior, platform, id) => {
       return getPolymarketIorDetailInfo(info);
     },
     pinnacle() {
-      return getPinnacleIorDetailInfo(info);
+      return getPinnacleIorDetailInfo(info, channel);
     }
   }
   return getInfo[platform]?.();
@@ -314,15 +314,15 @@ export const getPlatformIorsDetailInfo = async (ior, platform, id) => {
 /**
  * 平台盘口下注
  */
-export const placePlatformOrder = async (ior, platform, id, stake=0) => {
-  const iorInfo = await getPlatformIorsDetailInfo(ior, platform, id);
+export const placePlatformOrder = async (ior, platform, id, stake=0, channel) => {
+  const iorInfo = await getPlatformIorsDetailInfo(ior, platform, id, channel);
   const betInfo = { ...iorInfo, stakeSize: stake };
   const placeOrder = {
     polymarket() {
       return polymarketPlaceOrder(betInfo);
     },
     pinnacle() {
-      return pinnaclePlaceOrder(betInfo);
+      return pinnaclePlaceOrder(betInfo, channel);
     }
   }
   return placeOrder[platform]?.();

+ 13 - 5
server/models/PartnerGate.js

@@ -63,12 +63,20 @@ const getSolutionWithIors = async(params) => {
  * @returns
  */
 const betPinnacle = async (params) => {
-  const { id, ior, stake=0 } = params;
-  const iorInfo = await placePlatformOrder(ior, 'pinnacle', id, stake);
-  if (!iorInfo) {
-    return Promise.reject(new Error('ior info not found'));
+  const { id, ior, stake=0, channel } = params;
+  if (typeof channel !== 'string' || channel.length === 0) {
+    return Promise.reject(new Error('channel is required', { cause: 400 }));
   }
-  return iorInfo;
+  return placePlatformOrder(ior, 'pinnacle', id, stake, channel)
+  .then(ret => {
+    if (ret.errorCode) {
+      const error = new Error('place order error');
+      error.data = ret;
+      error.cause = 400;
+      return Promise.reject(error);
+    }
+    return ret;
+  });
 }
 
 export const gate = async (data) => {