forked from jerusalemc/ripple-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchXRP.js
217 lines (173 loc) · 5.42 KB
/
watchXRP.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
211
212
213
214
215
216
217
// looking for profit between exchanges!!
'use strict';
const ripple = require('ripple-lib');
var request = require('request');
var axios = require('axios');
var BigNumber = require('bignumber.js');
const RippleAPI = ripple.RippleAPI; // require('ripple-lib')
var RippleOrderBook = require('./libs/ripple-orderbook').OrderBook;
const api = new RippleAPI({server: 'wss://s2.ripple.com:443'});
// var api = new RippleAPI({server: 'wss://s1.ripple.com'});
const address = 'rrrrrrrrrrrrrrrrrrrrBZbvji';
const NAMES = {
bitstamp: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
rippleChina: 'razqQKzJRdB4UxFPWf5NEpEG3WMkmwgcXA',
rippleFox: 'rKiCet8SdvWxPXnAgYarFUXMh1zCPz432Y',
gatehubFifth: 'rchGBxcD1A1C2tdxF6papQYZ8kjRKMYcL',
gatehubFifthETH: 'rcA8X3TVMST1n3CJeAdGk1RdRCHii7N2h',
gatehub: 'rhub8VRN55s94qWKDv6jmDy1pUykJzF3wq',
rippleCN: 'rnuF96W4SZoCJmbHYBFoJZpR8eCaxNvekK',
xiaobu: 'rBqC5LRFE93v8jX5Hdu5K9qKAMziwLrKMb',
rippleSignapore: 'r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH',
}
/*
Variables
*/
// ask, bid (ETH)
const prices = {
fox: [null, null],
china: [null, null],
btsd: [null, null],
}
// ask eth(means I buy from the seller), bid eth(means I sell to the buyers)
const fees = {
fox: [0.001, 0.003], // amortized withdraw fee
china: [0.001, 0.003], // amortized withdraw fee
btsd: [0.003, 0.003], // amortized withdraw fee
}
/*
Retrieve prices
*/
function calcAverage(offers, amount, isAsk) {
let count = 0;
let total = 0;
for(let i = 0; i < offers.length && count < amount; i++) {
const offer = offers[i];
//console.log(offer);
const eth = isAsk ? parseFloat(offer.taker_gets_funded) : parseFloat(offer.taker_pays_funded);
const cny = isAsk ? parseFloat(offer.taker_pays_funded) : parseFloat(offer.taker_gets_funded);
count += eth;
total += cny;
}
return total / count;
}
function calcAverageBtsd(offers, amount) {
let count = 0;
let total = 0;
for(let i = 0; i < offers.length && count < amount; i++) {
const offer = offers[i];
//console.log(offer);
const xrp = parseFloat(offer[1]);
const cny = parseFloat(offer[0]) * xrp;
count += xrp;
total += cny;
}
return total / count;
}
/*
Compare prices
*/
let lastCheck = null;
let lastNotification = null;
function comparePrices(a, b, minProfit = 0.025) {
lastCheck = Date.now();
if (!prices[a][1] || !prices[b][0]) {
return;
}
// console.log('***')
// console.log(a.toString() + prices[a][0].toString())
// console.log(a.toString() + prices[a][1].toString())
// console.log(b.toString() + prices[b][0].toString())
// console.log(b.toString() + prices[b][1].toString())
// console.log('@@@')
const costSellABuyB = prices[a][1] * fees[a][1] + prices[b][0] * fees[b][1];
const profitSellABuyB = (prices[a][1] - prices[b][0] - costSellABuyB) / prices[b][0];
// console.log(`卖了${a},买${b}`, profitSellABuyB * 100);
if (lastNotification && (Date.now() - lastNotification < 1000 * 60 * 10)) {
// 5分钟内提醒过了
return;
}
if (profitSellABuyB > minProfit) {
lastNotification = Date.now();
var fs = require('fs');
var stream = fs.createWriteStream("./send_message/result_xrp.txt");
stream.once('open', function (fd) {
stream.write(`[XRP] ${(profitSellABuyB * 100).toFixed(2)}% profit!! Sell ${a} at ¥${prices[a][1].toFixed(2)}, and buy ${b} at ¥${prices[b][0].toFixed(2)}!!`);
stream.end();
});
}
return profitSellABuyB;
}
function initRippleListeners(issuer, pricePtr) {
const asksFox = RippleOrderBook.createOrderBook(api, {
currency_pays: 'CNY',
issuer_pays: issuer,
currency_gets: 'XRP',
});
const bidsFox = RippleOrderBook.createOrderBook(api, {
currency_pays: 'XRP',
currency_gets: 'CNY',
issuer_gets: issuer,
});
asksFox.on('model', (offers) => {
const average = calcAverage(offers, 20000, true);
prices[pricePtr][0] = average * 1000000;
});
bidsFox.on('model', (offersB) => {
const average = calcAverage(offersB, 20000, false);
prices[pricePtr][1] = average * 1000000;
comparePrices(pricePtr, 'btsd');
comparePrices('btsd', pricePtr);
});
}
function listenBtsd() {
btsdListeners();
const interval = setInterval(btsdListeners, 60000);
}
async function btsdListeners() {
try {
const result = await axios.get('http://www.btc38.com/trade/getTradeList30.php?coinname=XRP&mk_type=CNY');
const data = result.data;
if (!data) return;
const averageSell = calcAverageBtsd(data.sellStr, 20000);
const averageBuy = calcAverageBtsd(data.buyStr, 20000);
prices['btsd'][0] = averageSell;
prices['btsd'][1] = averageBuy;
// console.log('平均sell', averageSell);
// console.log('平均buy', averageBuy);
} catch (ex) {
console.log(ex);
}
}
async function listenRipple() {
try {
const result = await api.connect();
api.on('error', (errorCode, errorMessage, data) => {
console.log(errorCode + ': ' + errorMessage);
});
console.log('Connected');
} catch(ex) {
console.log('error', ex);
}
try {
initRippleListeners(NAMES.rippleFox, 'fox');
// initRippleListeners(NAMES.rippleChina, 'china');
} catch(ex) {
console.log(' errored', ex);
}
/*
Order book
*/
}
listenBtsd();
listenRipple();
setInterval(() => {
if (Date.now() - lastCheck > 1000 * 60 * 3) {
// something happened;
console.log('Reconnect??');
api.disconnect();
api.connect();
lastCheck = Date.now();
}
}, 60000)
api.connect();