import axios from "axios"; import { HttpsProxyAgent } from "https-proxy-agent"; import { randomUUID } from 'crypto'; import Logs from "./logs.js"; const axiosDefaultOptions = { baseURL: "", url: "", method: "GET", headers: {}, params: {}, data: {}, timeout: 10000, }; export const pinnacleRequest = async (options, channel) => { const { url, ...optionsRest } = options; const username = process.env.PINNACLE_USERNAME; const password = process.env.PINNACLE_PASSWORD; if (!url || !channel && (!username || !password)) { throw new Error("url、username、password、channel is required"); } 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, "Accept": "application/json", }); const proxy = process.env.NODE_HTTP_PROXY; if (proxy) { axiosConfig.proxy = false; axiosConfig.httpsAgent = new HttpsProxyAgent(proxy); } Logs.outDev('pinnacle request', url, axiosConfig, { channel, username, password }); return axios(axiosConfig).then(res => { return res.data; }); } /** * Pinnacle API Get请求 * @param {*} url * @param {*} params * @returns */ export const pinnacleGet = async (url, params, channel) => { return pinnacleRequest({ url, params }, channel) .catch(err => { Logs.errDev('pinnacle get error', err); if (err.response?.data) { err.data = err.response.data; err.cause = err.response.status; } return Promise.reject(err); }); } /** * Pinnacle API Post请求 * @param {*} url * @param {*} data * @returns */ export const pinnaclePost = async (url, data, channel) => { return pinnacleRequest({ url, method: 'POST', headers: { 'Content-Type': 'application/json', }, data }, channel); } /** * 清理对象中的undefined值 * @param {*} obj * @returns {Object} */ const cleanUndefined = (obj) => { return Object.fromEntries( Object.entries(obj).filter(([, v]) => v !== undefined) ); } /** * 获取直盘线 */ export const getLineInfo = async ({ info = {}, channel }) => { const { leagueId, eventId, betType, handicap, team, side, specialId, contestantId, periodNumber=0, oddsFormat='Decimal', sportId=29 } = info; let url = '/v2/line/'; let data = { sportId, leagueId, eventId, betType, handicap, periodNumber, team, side, oddsFormat }; if (specialId) { url = '/v2/line/special'; data = { specialId, contestantId, oddsFormat }; } data = cleanUndefined(data); return pinnacleGet(url, data, channel) .then(ret => ({ info: ret, line: data })) .catch(err => { Logs.outDev('get line info error', err.data); Logs.errDev(err); return Promise.reject(err); }); } /** * 获取账户余额 */ export const getAccountBalance = async () => { return pinnacleGet('/v1/client/balance'); } /** * 下注 */ export const placeOrder = async ({ info, line, stakeSize }, channel) => { // return Promise.resolve({info, line, stakeSize}); const uuid = randomUUID() if (line.specialId) { const data = cleanUndefined({ oddsFormat: line.oddsFormat, uniqueRequestId: uuid, acceptBetterLine: true, stake: stakeSize, winRiskStake: 'RISK', lineId: info.lineId, specialId: info.specialId, contestantId: info.contestantId, }); Logs.outDev('pinnacle place order data', data); return pinnaclePost('/v4/bets/special', { bets: [data] }, channel) .then(ret => ret.bets?.[0] ?? ret) .then(ret => { Logs.outDev('pinnacle place order', ret, uuid); return ret; }) .catch(err => { Logs.outDev('pinnacle place order error', err.data, uuid); Logs.errDev(err); return Promise.reject(err); }); } else { const data = cleanUndefined({ oddsFormat: line.oddsFormat, uniqueRequestId: uuid, acceptBetterLine: true, stake: stakeSize, winRiskStake: 'RISK', lineId: info.lineId, altLineId: info.altLineId, fillType: 'NORMAL', sportId: line.sportId, eventId: line.eventId, periodNumber: line.periodNumber, betType: line.betType, team: line.team, side: line.side, handicap: line.handicap, }); Logs.outDev('pinnacle place order data', data); return pinnaclePost('/v4/bets/place', data, channel) .then(ret => { Logs.outDev('pinnacle place order', ret, uuid); return ret; }) .catch(err => { Logs.outDev('pinnacle place order error', err.data, uuid); Logs.errDev(err); return Promise.reject(err); }); } }