-
Notifications
You must be signed in to change notification settings - Fork 0
/
BetfairClient.js
210 lines (184 loc) · 7.16 KB
/
BetfairClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const axios = require('axios');
const querystring = require('querystring');
const fs = require('mz/fs');
const https = require('https');
const DB = require('./DB');
const moment = require('moment');
const clone = require('clone');
const log = require('./log');
const BETFAIR_LOGIN = 'https://identitysso.betfair.com/api/certlogin/';
const BETFAIR_KEEPALIVE = 'https://identitysso.betfair.com/api/keepAlive';
const BETFAIR_API = 'https://api.betfair.com/exchange/betting/rest/v1.0/';
const BETFAIR_TIMEFORM = 'https://www.betfair.com/rest/v2/raceCard';
const BETFAIR_HOME = 'https://www.betfair.com/exchange/plus';
const USERAGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36';
const httpsConfig = (process.env.NODE_ENV === 'test') ? {} : {
cert: fs.readFileSync('./secrets/client-2048.crt'),
key: fs.readFileSync('./secrets/client-2048.key')
};
class BetFairClient {
constructor() {
this.config = {
headers: {
'Accept': 'application/json'
},
httpsAgent: new https.Agent(httpsConfig)
};
}
login(credentials) {
const {username, password, appKey} = credentials;
this.config.headers['X-Application'] = appKey;
this.config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
return this._checkIfCurrentSession(username)
.then(session => {
if (session) {
const lastSessionTimeDiff = moment().diff(session.start, 'hours', true);
this.config.headers['Content-Type'] = 'application/json';
this.config.headers['X-Authentication'] = session.token;
this._keepAlive(lastSessionTimeDiff, username);
return session;
}
return this._doLogin(username, password);
});
}
placeOrder(marketId, orderParams, logData) {
if (orderParams.size < 2) {
return this._placeOrderMin(marketId, orderParams, logData);
}
return this._doPlaceOrder(marketId, orderParams, logData);
}
cancelOrder(marketId, cancelOrderParams, logData) {
const {sizeReduction, betId} = cancelOrderParams;
const params = {marketId, instructions: [{betId, sizeReduction}]};
if (!betId) throw new Error('betId is null');
return axios.post(`${BETFAIR_API}cancelOrders/`, params, this.config)
.then(res => this._handleOrderRes(false, res, logData, `cancel order`))
.catch(err => this._handleOrderRes(true, err, logData, `cancel order`));
}
replaceOrder(marketId, replaceOrderParams, logData) {
const {newPrice, betId} = replaceOrderParams;
const params = {marketId, instructions: [{betId, newPrice}]};
if (!betId) throw new Error('betId is null');
return axios.post(`${BETFAIR_API}replaceOrders/`, params, this.config)
.then(res => this._handleOrderRes(false, res, logData, `replace order`))
.catch(err => this._handleOrderRes(true, err, logData, `replace order`));
}
getTimeFormData(marketId, logData) {
const headers = {
'User-Agent': USERAGENT
};
return axios.get(BETFAIR_HOME, {headers})
.then(res => {
const appKey = /"appKey":\s"(.*?)"/g.exec(res.data)[1];
if (!appKey) throw new Error('App Key not found on Betfair!');
headers['X-Application'] = appKey;
return axios.get(BETFAIR_TIMEFORM, {
headers,
params: {
marketId,
dataEntries: '[RACE,TIMEFORM_DATA,RUNNERS,RUNNER_DETAILS]'
}
});
})
.then(res => this._handleOrderRes(false, res, logData, `timeform data`))
}
_placeOrderMin(marketId, orderParams, logData) {
const minOrderParams = clone(orderParams);
const {price, side} = orderParams;
let betId;
minOrderParams.size += 2;
minOrderParams.price = (side === 'BACK') ? 1000 : 1.01;
console.log('minOrderPArams', minOrderParams);
return this._doPlaceOrder(marketId, minOrderParams, logData)
.then(res => {
betId = res.instructionReports[0].betId;
return this.cancelOrder(marketId, {sizeReduction: 2, betId}, logData);
})
.then(res => this.replaceOrder(marketId, {newPrice: price, betId}, logData))
.catch(err => this._handleOrderRes(true, err, logData, `place ${side.toLowerCase()} order:_placeOrderMin`));
}
/**
* @param {Boolean} err The error response
* @param {Object} res The response Object
* @param {Object} logData Context specific log data to merge with responses
* @param {String} logMessage The message to show in the logs
* @description
* @private
*/
_handleOrderRes(err, res, logData, logMessage) {
const result = (Array.isArray(res.data)) ? res.data.pop() : res.data;
if (err) {
let error = (res.response) ? res.response.data : result || res.message || res;
error = (typeof error === 'string') ? {error} : error;
log.error(logMessage, Object.assign({}, logData, error));
return error;
}
if (result.status === 'FAILURE') {
// log.error(logMessage, Object.assign(logData, res.data));
throw res;
}
log.info(logMessage, Object.assign({}, logData, result));
return result;
}
_keepAlive(lastSessionTimeDiff, username) {
let i = 0;
let interval = (lastSessionTimeDiff) ? 3.5 - lastSessionTimeDiff : 3.5;
setInterval(() => {
if (interval !== 3.5 && i === 1) {
interval = 3.5
}
if (i < 1) {
i++;
}
axios.post(BETFAIR_KEEPALIVE, {}, this.config)
.then(res => log.debug('keepalive', {username}))
.catch(err => log.error('keepalive error', err.data || err.message))
}, interval * 60 * 60 * 1000);
}
_doPlaceOrder(marketId, orderParams, logData) {
const {selectionId, side, size, price} = orderParams;
const params = {
marketId,
instructions: [{
selectionId,
side,
orderType: 'LIMIT',
limitOrder: {
size,
price,
persistenceType: 'PERSIST'
}
}]
};
return axios.post(`${BETFAIR_API}placeOrders/`, params, this.config)
.then(res => this._handleOrderRes(false, res, logData, `place ${side.toLowerCase()} order:_doPlaceOrder`))
.catch(err => this._handleOrderRes(true, err, logData, `place ${side.toLowerCase()} order:_doPlaceOrder`));
}
_doLogin(username, password) {
let session;
return axios.post(BETFAIR_LOGIN, querystring.stringify({username, password}), this.config)
.then(res => {
session = {token: res.data.sessionToken, start: new Date()};
return DB;
})
.then(db => db.collection('users').findOneAndUpdate({username}, {$set: {session}}))
.then(() => {
this.config.headers['Content-Type'] = 'application/json';
this.config.headers['X-Authentication'] = session.token;
this._keepAlive(null, username);
return session
});
}
_checkIfCurrentSession(username) {
return DB
.then(db => db.collection('users').findOne({username}))
.then(user => {
if (!user.session) return;
const lastSessionTimeDiff = moment().diff(user.session.start, 'hours', true);
const maxHours = 3.5;
if (lastSessionTimeDiff > maxHours) return;
return user.session;
});
}
}
module.exports = new BetFairClient();