-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
241 lines (220 loc) · 5.85 KB
/
bot.js
File metadata and controls
241 lines (220 loc) · 5.85 KB
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
const dotenv = require('dotenv');
const grpc = require('grpc');
const lineReaderSync = require('line-reader-sync');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('lssdrpc.proto');
const lssdrpc = grpc.loadPackageDefinition(packageDefinition).lssdrpc;
const currenciesClient = lssdrpc.currencies;
const tradingPairsClient = lssdrpc.tradingPairs;
const ordersClient = lssdrpc.orders;
const swapsClient = lssdrpc.swaps;
dotenv.config({
path: './.env',
});
const env = process.env;
const max = 100000000;
const RpcConfig = {
host: env['LSSD_HOST'],
port: parseInt(env['LSSD_PORT'])
}
const RpcAddress = RpcConfig.host + ":" + RpcConfig.port;
const currencies = [
{
currency: "LTC",
lndChannel: env['LTC_LND_CHANNEL'],
rawCert: readTlsCert(env['LTC_CERT_PATH']),
},
{
currency: "XSN",
lndChannel: env['XSN_LND_CHANNEL'],
rawCert: readTlsCert(env['XSN_CERT_PATH']),
},
{
currency: "BTC",
lndChannel: env['BTC_LND_CHANNEL'],
rawCert: readTlsCert(env['BTC_CERT_PATH']),
}
];
let totalPlacedOrder = 0;
const PlaceOrderLimit = env['PLACE_ORDER_LIMIT'];
const getClient = function (Client) {
return new Client(RpcAddress, grpc.credentials.createInsecure());
};
const currenciesLssd = getClient(currenciesClient);
const tradingPairLssd = getClient(tradingPairsClient);
const ordersLssd = getClient(ordersClient);
const swapsLssd = getClient(swapsClient);
function readTlsCert(path) {
let cert = "";
lrs = new lineReaderSync(path);
while(true){
const line = lrs.readline();
if (line === null) {
break;
}
if (cert) cert += "\n";
cert += line;
}
return cert;
}
const testResult = function(text, result, msg) {
if (result) {
console.log(text + ": \x1b[32mpassed\x1b[0m");
}
else {
console.log(text + ": \x1b[31mnot passed\x1b[0m");
if (msg) console.log(msg);
/*
console.log("\x1b[31mPlase Try Again!\x1b[0m");
process.exit();*/
}
}
function isEmpty(obj) {
for(const prop in obj) {
if(obj.hasOwnProperty(prop)) {
return false;
}
}
return true;
}
async function getNewOrder(side) {
if (side == 0) {
return {
pairId: env['PAIRID'],
side: 0,
funds: {value: ((Math.floor(Math.random() * max) % 3) + 13).toString()},
price: {value: ((Math.floor(Math.random() * max) % 30000) + 170000).toString()},
};
}
else {
return {
pairId: env['PAIRID'],
side: 1,
funds: {value: ((Math.floor(Math.random() * max) % 4000) + 6000).toString()},
price: {value: ((Math.floor(Math.random() * max) % 30000) + 170000).toString()},
};
}
/* Can Add Algo Here
const newOrder = {
pairId: env['PAIRID'],
side: parseInt(env['ORDER_SIDE']),
funds: {value: "10000"},
price: {value: "100000"}
};
const listOrderReq = {
pairId: env['PAIRID'],
includeOwnOrders: false,
skip: 0,
limit: 100,
};
while (1) {
let orders = [];
await new Promise((resolve, reject) => {
ordersLssd.ListOrders( listOrderReq, function(err, res) {
if (!err && !isEmpty(res) && res.orders)
{
orders = res.orders;
}
return resolve();
});
});
for (let i = 0 ; i < orders.length ; i ++) {
if (!orders[i].side && orders[i].side != parseInt(env['ORDER_SIDE'])) {
continue;
}
// And find Min and Max here
}
if (orders.length < listOrderReq.limit) break;
listOrderReq.skip += listOrderReq.limit;
}
return newOrder;
*/
}
async function AddCurrency() {
await Promise.all(currencies.map( async item => {
return new Promise((resolve, reject) => {
currenciesLssd.AddCurrency(item, function(err, res) {
if (err) {
testResult("AddCurrency(" + item.currency + ")", 0);
return reject();
}
testResult("AddCurrency(" + item.currency + ")", 1);
return resolve();
});
})
}));
}
async function EnableTradingPair() {
await new Promise((resolve, reject) => {
tradingPairLssd.EnableTradingPair( {
pairId: env['PAIRID']
}, function(err, res) {
if (err) {
testResult("EnableTradingPair", 0);
return reject();
}
testResult("EnableTradingPair", 1);
return resolve();
});
})
}
async function PlaceOrder(PlaceOrderLimit) {
if (PlaceOrderLimit == 0) {
console.log("\x1b[32mfinished!\x1b[0m");
process.exit();
}
let buyorsell = 1;
if (env['BUY_OR_SELL'] === 'both') {
buyorsell = (PlaceOrderLimit % 2 + 2) % 2;
}
else if (env['BUY_OR_SELL'] === 'buy') {
buyorsell = 0;
}
else if (env['BUY_OR_SELL'] === 'sell') {
buyorsell = 1;
}
const newOrder = await getNewOrder(buyorsell);
ordersLssd.PlaceOrder( newOrder, function(err, res) {
if (err) {
testResult("PlaceOrder(" + totalPlacedOrder + ")", 0, err);
console.log('Check your swap server, please.');
process.exit();
}
if (res.failure) {
testResult("PlaceOrder(" + totalPlacedOrder + ")", 0, res);
}
else {
testResult("PlaceOrder(" + totalPlacedOrder + ")", 1);
totalPlacedOrder ++;
}
console.log(res);
return PlaceOrder(PlaceOrderLimit - 1);
});
}
async function Subscribe() {
const orderUpdateStream = ordersLssd.SubscribeOrders({});
orderUpdateStream.on('data', data => {
console.log('orders - ', data);
});
orderUpdateStream.on('error', err => {
testResult("SubscribeOrders", 0, err);
});
orderUpdateStream.on('close', () => {
});
const SwapResultStream = swapsLssd.SubscribeSwaps({});
SwapResultStream.on('data', data => {
console.log('swaps - ', data);
});
SwapResultStream.on('error', err => {
testResult("SubscribeSwaps", 0, err);
});
SwapResultStream.on('close', () => {
});
}
async function bot() {
await AddCurrency();
await EnableTradingPair();
PlaceOrder(PlaceOrderLimit);
Subscribe();
}
bot();