-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
323 lines (304 loc) · 11.3 KB
/
index.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const async = require('async');
const Utils = require('./common/Utils');
const { transactionCoin, transactionCoinName, jubiProcedure, yunbiProcedure } = require('./common/Config');
const $ajax = require('./common/Ajax');
const JBO = require('./lib/jubi');
const YBO = require('./lib/yubi');
const querystring = require('querystring');
let totalAmount = 0; // 总金额
let JB_Obj = {
CNY: 0,
count: 0,
buy: 0,
sell: 0,
currentPrice: 0,
serviceAmount: 0
}
let YB_Obj = {
CNY: 0,
count: 10,
buy: 0,
sell: 0,
currentPrice: 0,
serviceAmount: 0
}
// 交易次数
let transactionCount = 0;
// 单笔交易数量
let transactionCoinNumber = 0.1;
// 单笔手续费用
let serviceAmount = 0;
// 类型
let type = '';
function restart() {
console.log('5秒后开始下一笔交易...');
console.log('');
console.log('');
console.log('');
setTimeout(function () {
start();
}, 5000);
}
// 交易
let transaction = function(callBack) {
if(type == 'JBT') {
// JB_Obj.count = Utils.FloatSub(JB_Obj.count, transactionCoinNumber);
// JB_Obj.CNY = Utils.FloatSub(Utils.FloatAdd(JB_Obj.CNY, Utils.FloatMul(JB_Obj.buy , transactionCoinNumber)), JB_Obj.serviceAmount);
//
// YB_Obj.count = Utils.FloatAdd(YB_Obj.count, transactionCoinNumber);
// YB_Obj.CNY = Utils.FloatSub(Utils.FloatSub(YB_Obj.CNY, Utils.FloatMul(YB_Obj.sell , transactionCoinNumber)), YB_Obj.serviceAmount);
// setTimeout(function () {
// callBack(null);
// }, 1000)
JBO.createOrder({amount: transactionCoinNumber, price: JB_Obj.buy, type: 'sell', coin: transactionCoin}, function (res) {
if(res.data && res.data.result) {
console.log('聚币交易完成....');
}else {
console.log('聚币交易失败...' + res.data.code);
}
})
YBO.createOrder({market: `${transactionCoin}cny`, side: 'buy', volume: transactionCoinNumber, price: YB_Obj.sell, ord_type: 'limit'}, function (res) {
if(res.data) {
}
})
}else if(type == 'YBT') {
YB_Obj.count = Utils.FloatSub(YB_Obj.count, transactionCoinNumber);
YB_Obj.CNY = Utils.FloatSub(Utils.FloatAdd(YB_Obj.CNY, Utils.FloatMul(YB_Obj.buy , transactionCoinNumber)), YB_Obj.serviceAmount);
JB_Obj.count = Utils.FloatAdd(JB_Obj.count, transactionCoinNumber);
JB_Obj.CNY = Utils.FloatSub(Utils.FloatSub(JB_Obj.CNY, Utils.FloatMul(JB_Obj.sell , transactionCoinNumber)), JB_Obj.serviceAmount);
setTimeout(function () {
callBack(null);
}, 1000)
}else {
callBack('该笔交易不符合规则...');
}
}
// 设置JB价格
let setJBPrice = function(callBack) {
var timer = false;
JBO.getCurrentPrice(function (res) {
timer = true;
if(res.retCode == 0) {
let ret = res.data;
JB_Obj.currentPrice = ret['last'];
JB_Obj.buy = ret['buy'];
JB_Obj.sell = ret['sell'];
console.log(JB_Obj.currentPrice + '__聚币价格');
callBack(null);
}else {
callBack('获取聚币网价格错误');
}
});
// 5秒超时
setTimeout(function () {
if(!timer) {
console.log('聚币');
callBack(-2);
}
}, 5000);
}
// 获取聚币个人信息
let setJBProfile = function (callBack) {
var timer = false;
JBO.getProfileInfo(function (res) {
timer = true;
if(res.retCode == 0) {
let ret = res.data;
console.log(ret);
// JB_Obj.CNY = ret['cny_balance'];
// JB_Obj.count = ret[`${transactionCoin}_balance`];
// callBack(null);
}else {
callBack('获取聚币个人信息有误...');
}
});
// 5秒超时
setTimeout(function () {
if(!timer) {
callBack(-2);
}
}, 5000);
}
// 获取云币个人信息
let setYBProfile = function (callBack) {
var timer = false;
YBO.getProfileInfo(function (res) {
timer = true;
if(res.retCode == 0) {
let ret = res.data;
let result = getCurrentCoinInfo(ret['accounts']);
console.log(result);
if(Object.keys(result).length == 2) {
YB_Obj.CNY = result['cny'];
YB_Obj.count = result[transactionCoin];
callBack(null);
}else {
callBack('获取云币个人信息失败...');
}
}else {
callBack('获取聚币个人信息有误...');
}
});
// 5秒超时
setTimeout(function () {
if(!timer) {
callBack(-2);
}
}, 5000);
}
function getCurrentCoinInfo(items) {
var result = {};
if(items.length > 0) {
for(var i = 0; i < items.length; i++) {
if(items[i]['currency'] == 'cny') {
result['cny'] = items[i]['balance'];
continue;
}
if(items[i]['currency'] == transactionCoin) {
result[transactionCoin] = items[i]['balance'];
continue;
}
if(Object.keys(result).length == 2) {
break;
}
}
}
return result;
}
// 设置YB交易价格
let setYBPrice = function(callBack) {
var timer = false;
YBO.getCurrentPrice(function (res) {
timer = true;
if(res.retCode == 0) {
let ret = res.data['ticker'];
YB_Obj.currentPrice = ret['last'];
YB_Obj.buy = ret['buy'];
YB_Obj.sell = ret['sell'];
console.log(YB_Obj.currentPrice + '_云币价格');
callBack(null);
}else {
callBack('获取云币价格错误...');
}
})
setTimeout(function () {
if(!timer) {
callBack(-2);
}
}, 5000);
}
// 数据展示
let showInfo = function (callBack) {
let jbCoinAmount = Utils.FloatMul(JB_Obj.currentPrice , JB_Obj.count);
let ybCoinAmount = Utils.FloatMul(YB_Obj.currentPrice , YB_Obj.count);
let coinTotal = Utils.FloatAdd(jbCoinAmount, ybCoinAmount);
totalAmount = Utils.FloatAdd(Utils.FloatAdd(JB_Obj.CNY, YB_Obj.CNY) , coinTotal);
console.log('--------------------------------------------------------------------------------------------------------------');
console.log(`总金额:${ totalAmount } 当前交易次数:${ transactionCount }`);
console.log(`活动总金额: ${ Utils.FloatAdd(JB_Obj.CNY, YB_Obj.CNY) }`);
console.log(`平台:JB网---类型:${ transactionCoinName } ---当前价格:${ JB_Obj.currentPrice }---当前币数目:${JB_Obj.count}---剩余活动资金:${JB_Obj.CNY}---买一:${JB_Obj.buy}---卖一:${JB_Obj.sell}`);
console.log(`平台:YB网---类型:${ transactionCoinName } ---当前价格:${ YB_Obj.currentPrice }---当前币数目:${YB_Obj.count}---剩余活动资金:${YB_Obj.CNY}---买一:${YB_Obj.buy}---卖一:${YB_Obj.sell}`);
console.log('--------------------------------------------------------------------------------------------------------------');
callBack(null);
}
// 数据分析
let analysis = function (callBack) {
if(JB_Obj.buy > YB_Obj.sell) {
console.log('聚币卖,云币买...');
if(JB_Obj.count > 1 && YB_Obj.count > 1 && YB_Obj.CNY > Utils.FloatMul(YB_Obj.sell, transactionCoinNumber)) {
let spreadPrice = Utils.FloatMul(Utils.FloatSub(JB_Obj.buy, YB_Obj.sell), transactionCoinNumber);
let jbServiceAmount = Utils.FloatMul(Utils.FloatMul(JB_Obj.buy, transactionCoinNumber), jubiProcedure);
let ybServiceAmount = Utils.FloatMul(Utils.FloatMul(YB_Obj.sell, transactionCoinNumber), yunbiProcedure);
let poundage = Utils.FloatAdd(jbServiceAmount, ybServiceAmount);
JB_Obj.serviceAmount = jbServiceAmount;
YB_Obj.serviceAmount = ybServiceAmount;
console.log('差价:' + spreadPrice + '---手续费:' + poundage);
if(spreadPrice > poundage) {
type = 'JBT';
callBack(null);
}else {
type = '';
callBack('该笔交易无法盈利...');
}
}else {
type = '';
// -1代表暂停交易,余额不足或者币不足,请人工处理
callBack(-1);
}
}else if(JB_Obj.sell < YB_Obj.buy) {
console.log('云币卖,聚币买...');
if( JB_Obj.count > 1 && YB_Obj.count > 1 && JB_Obj.CNY > Utils.FloatMul(JB_Obj.sell, transactionCoinNumber)) {
let spreadPrice = Utils.FloatMul(Utils.FloatSub(YB_Obj.buy, JB_Obj.sell), transactionCoinNumber);
let jbServiceAmount = Utils.FloatMul(Utils.FloatMul(JB_Obj.sell, transactionCoinNumber), jubiProcedure);
let ybServiceAmount = Utils.FloatMul(Utils.FloatMul(YB_Obj.buy, transactionCoinNumber), yunbiProcedure);
let poundage = Utils.FloatAdd(jbServiceAmount, ybServiceAmount);
JB_Obj.serviceAmount = jbServiceAmount;
YB_Obj.serviceAmount = ybServiceAmount;
console.log('差价:' + spreadPrice + '---手续费:' + poundage);
if(spreadPrice > poundage) {
type = 'YBT';
callBack(null);
}else {
type = '';
callBack('该笔交易无法盈利...');
}
}else {
type = '';
// -1代表暂停交易,余额不足或者币不足,请人工处理
callBack(-1);
}
}else {
type = '';
callBack('该笔交易无法盈利...');
}
}
function start() {
console.log('交易开始....');
async.parallel([ setYBPrice, setJBPrice, setJBProfile, setYBProfile ], function (err, results) {
if(err) {
if(err == -2) {
console.log('获取接口超时, 请重启程序....');
process.exit();
}else {
console.log(err);
restart();
}
}else {
async.series([ showInfo, analysis ,transaction ], function (err, results) {
if(err) {
if(err == -1) {
console.log('暂停交易,余额不足或者币不足,请人工及时处理...');
}else {
console.log(err);
restart();
}
}else {
transactionCount ++;
console.log('恭喜你, 该笔交易成功!');
restart();
}
})
}
})
}
// 开始交易
console.log('启动交易程序....');
// start();
// 256加密
// YBO.getProfileInfo(function (res) {
// console.log(res);
// })
// 下单接口
YBO.createOrder({ market:'gxscny', side:'buy', volume:'0.5', price:'24.0', ord_type:'limit'}, function (res) {
console.log(res);
})
// 获取个人信息
// JBO.getProfileInfo(function (res) {
// console.log(res);
// })
// setJBPrice(function () {
// console.log(JB_Obj.buy);
// JBO.createOrder({ amount: 1, price: JB_Obj.buy, type: 'sell', coin: transactionCoin }, function (res) {
// console.log(res);
// })
// });