-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_api_test.js
More file actions
268 lines (246 loc) · 6.9 KB
/
simple_api_test.js
File metadata and controls
268 lines (246 loc) · 6.9 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
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
const path = require('path');
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;
const RpcConfig = {
host: "localhost",
port: 50051
}
const RpcAddress = RpcConfig.host + ":" + RpcConfig.port;
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 currencies = [
{
currency: "LTC",
lndChannel: "localhost:10001",
rawCert: readTlsCert("/root/.lnd_ltc/tls.cert"),
},
{
currency: "XSN",
lndChannel: "localhost:10003",
rawCert: readTlsCert("/root/.lnd_xsn/tls.cert"),
}
];
const SampleOrder = {
pairId: "XSN_LTC",
side: 1,
funds: {value: "10000"},
price: {value: "100000"}
};
const getClient = function (Client) {
return new Client(RpcAddress, grpc.credentials.createInsecure());
};
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 isOrderExist(ordersLssd, newOrder) {
if (!newOrder.orderId) return false;
const listOrderReq = {
pairId: "XSN_LTC",
includeOwnOrders: false,
skip: 0,
limit: 100,
};
while (1) {
let orders = [];
let isOk = true;
await new Promise((resolve, reject) => {
ordersLssd.ListOrders( listOrderReq, function(err, res) {
if (isEmpty(res)) return resolve();
if (err || !res.orders || !res.orders.length || !res.orders[0].orderId) {
isOk = false;
}
else {
orders = res.orders;
}
return resolve();
});
});
if (!isOk) return false;
for (let i = 0 ; i < orders.length ; i ++) {
if (orders[i].orderId == newOrder.orderId) {
return orders[i];
}
}
if (orders.length < listOrderReq.limit) break;
listOrderReq.skip += listOrderReq.limit;
}
return false;
}
async function testAddCurrency(currenciesLssd) {
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 testEnableTradingPair(tradingPairLssd) {
await new Promise((resolve, reject) => {
tradingPairLssd.EnableTradingPair( {
pairId: "XSN_LTC"
}, function(err, res) {
if (err) {
testResult("EnableTradingPair(XSN_LTC)", 0);
return reject();
}
testResult("EnableTradingPair(XSN_LTC)", 1);
return resolve();
});
})
}
async function testPlaceAndCancelOrder(ordersLssd) {
const newOrder = SampleOrder;
await new Promise((resolve, reject) => {
ordersLssd.PlaceOrder( newOrder, function(err, res) {
if (err || !res.order) {
testResult("PlaceOrder", 0, err ? err : res);
return reject();
}
const order = res.order;
if (newOrder.pairId === order.pairId && newOrder.side === order.side &&
newOrder.price.value === order.price.value &&
newOrder.funds.value === order.funds.value) {
newOrder.orderId = order.orderId;
testResult("PlaceOrder", 1);
return resolve();
}
else {
testResult("PlaceOrder", 0, res);
return reject();
}
});
});
if (await isOrderExist(ordersLssd, newOrder) !== false) {
testResult("ListOrders", 1);
}
else {
testResult("ListOrders", 0, 'new order not found');
}
await new Promise((resolve, reject) => {
ordersLssd.CancelOrder( {
pairId: newOrder.pairId,
orderId: newOrder.orderId
}, function(err, res) {
return resolve();
});
});
if (await isOrderExist(ordersLssd, newOrder) === false) {
testResult("CancelOrder", 1);
}
else {
testResult("CancelOrder", 0, 'new order not cancelled');
}
}
async function testSubscribers(ordersLssd, swapsLssd) {
// Orders -> PlaceOrder
let newOrder = SampleOrder;
new Promise((resolve, reject) => {
ordersLssd.PlaceOrder( newOrder, function(err, res) {
if (err || !res.order) {
testResult("PlaceOrder", 0, err ? err : res);
return reject();
}
const order = res.order;
if (newOrder.pairId === order.pairId && newOrder.side === order.side &&
newOrder.price.value === order.price.value &&
newOrder.funds.value === order.funds.value) {
newOrder = order;
testResult("PlaceOrder", 1);
return resolve();
}
else {
testResult("PlaceOrder", 0, res);
return reject();
}
});
});
console.log("\x1b[32mPlease wait for SubscribeOrders and SubscribeSwaps\x1b[0m");
// Orders->SubscribeOrders
new Promise((resolve, reject) => {
const orderUpdateStream = ordersLssd.SubscribeOrders({});
orderUpdateStream.on('data', data => {
console.log('order-data', data);
orderUpdateStream.destroy();
});
orderUpdateStream.on('error', err => {
testResult("SubscribeOrders", 0, err);
reject();
});
orderUpdateStream.on('close', () => {
testResult("SubscribeOrders", 1);
resolve();
});
});
// Swap->SubscribeSwaps
new Promise((resolve, reject) => {
const SwapResultStream = swapsLssd.SubscribeSwaps({});
SwapResultStream.on('data', data => {
if(data.success || data.failure) {
testResult("SubscribeSwaps", 1);
}
else {
testResult("SubscribeSwaps", 0, data);
}
console.log(data);
SwapResultStream.destroy();
});
SwapResultStream.on('error', err => {
testResult("SubscribeSwaps", 0, err);
reject();
});
SwapResultStream.on('close', () => {
resolve();
});
});
}
async function bot() {
const currenciesLssd = getClient(currenciesClient);
const tradingPairLssd = getClient(tradingPairsClient);
const ordersLssd = getClient(ordersClient);
const swapsLssd = getClient(swapsClient);
await testAddCurrency(currenciesLssd);
await testEnableTradingPair(tradingPairLssd);
await testPlaceAndCancelOrder(ordersLssd);
await testSubscribers(ordersLssd, swapsLssd);
}
bot();