-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
341 lines (327 loc) · 11.6 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
const yaml = require('js-yaml');
const fs = require('fs');
const Feed = require('./pricefeed.js');
const table = require('table').table;
const chalk = require('chalk');
const moment = require('moment-timezone');
const Price = require('./lib/Price.js');
const prompt = require('prompt-sync')({sigint: true});
const argv = require('minimist')(process.argv.slice(2));
const Logger= require('./lib/Logger.js');
const {Apis} = require('bitsharesjs-ws');
const {PrivateKey, TransactionBuilder} =require('bitsharesjs');
var privKey = argv['k'];
let pKey = PrivateKey.fromWif(privKey);
let logger= new Logger(argv['d']);
let confYaml = argv['c'];
let apiNode = argv['s'];
let publishFeed = argv['broadcast'];
let usePremium = argv['usepremium'];
let skip_critical = argv['skip_critical'];
try {
(async ()=> {
//console.log(chalk.white('Loading config file...'));
logger.log('Starting pricefeed run');
logger.transient('Loading config file...');
var config = yaml.safeLoad(fs.readFileSync(confYaml, 'utf8'));
logger.info('Config file loaded.');
var feed = new Feed(config);
logger.transient('Initialising pricefeed...');
await feed.init();
logger.info('Pricefeed class initialised.');
logger.info('Setting up pricefeed sources...');
await feed.fetch();
logger.info('Pricefeed sources setup complete.');
if (usePremium) {
feed.calc_premium();
}
logger.info('Calculating prices...');
await feed.derive([]);
logger.info('Prices calculation complete.');
var prices = await feed.get_prices();
printLog(prices);
printPrices(prices);
Apis.instance(apiNode, true).init_promise.then(() => {
logger.log('Connected to API node: '+ apiNode);
let tr = new TransactionBuilder();
for (var symbol in prices) {
let price=prices[symbol];
if(price==undefined) {
logger.warning('No price for '+chalk.white(symbol)+'. '+chalk.red('SKIPPING.'));
continue;
}
let flags = price['flags'];
if ((flags.indexOf('min_change')==-1) && (flags.indexOf('over_max_age')==-1)) {
logger.info(chalk.white(symbol)+' below min change and not old enough. '+chalk.green('SKIPPING.'));
continue;
}
if (flags.indexOf('over_warn_change')>=0) {
if (flags.indexOf('skip_change')==-1) {
let includeprice= prompt(chalk.yellow('WARNING:')+' Price change for '+symbol+' ('+price['priceChange']+') above \'warn_change\'. Include in feed (Y/n)?');
if ((includeprice=='n') || (includeprice=='N')) {
logger.log('Skipping...');
continue;
}
}else{
if (skip_critical) {
logger.log('Skipping...');
continue;
}else{
let includeprice= prompt(chalk.red('CRITICAL:')+' Price change for '+symbol+' ('+price['priceChange']+') above \'skip_change\'. Include in feed (y/N)?');
if ((includeprice!='y') && (includeprice!='Y')) {
logger.log('Skipping...');
continue;
}
}
}
}
let newprice=price.new_feed;
let newcer = newprice.Multiply(price.cef);
let op= {
publisher: feed.producer.id,
asset_id: newprice.base.asset_id,
feed: {
settlement_price: {
base: {
amount: newprice.base.amount,
asset_id: newprice.base.asset_id },
quote: {
amount: newprice.quote.amount,
asset_id: newprice.quote.asset_id }
},
maintenance_collateral_ratio: price.mcr*10,
maximum_short_squeeze_ratio: price.mssr*10,
core_exchange_rate: {
base: {
amount: newcer.base.amount,
asset_id: newcer.base.asset_id },
quote: {
amount: newcer.quote.amount,
asset_id: newcer.quote.asset_id }
}
}
} ;
tr.add_type_operation( 'asset_publish_feed', op);
}
tr.set_required_fees().then(() => {
tr.add_signer(pKey, pKey.toPublicKey().toPublicKeyString());
if (publishFeed) {
logger.log('Publishing feed to blockchain.');
tr.broadcast().then(() => { process.exit();});
}else{
logger.log('\'--broadcast\' flag not set. Not publishing feed.');
process.exit();
}
});
});
})();
} catch (e) {
logger.log(e);
}
function formatPrice(price) {
return chalk.yellow(Number(price).toFixed(9));
}
function highlightlargeDeviation(d, p) {
var perc = ((d - p) / p) * 100;
if (perc < 0) {
return chalk.red(perc.toFixed(2) + '%');
} else {
return chalk.green('+' + perc.toFixed(2) + '%');
}
}
function printLog(prices) {
var tabledata = [['base', 'quote', 'price', 'diff', 'volume', 'source']];
for (var symbol in prices) {
var backing_symbol = prices[symbol]['short_backing_symbol'];
var data = prices[symbol]['log'];
var price = data[symbol];
if (price == undefined) {
continue;
}
for (var didx in price[backing_symbol]) {
var d = price[backing_symbol][didx];
tabledata.push([symbol, backing_symbol, formatPrice(d['price']), highlightlargeDeviation(d['price'], prices[symbol]['price']), d['volume'], JSON.stringify(d['sources'])]);
}
}
let options = {
drawHorizontalLine: (index, size) => {
return index === 0 || index === 1 || index === size;
},
columns: {
0: {
alignment: 'right',
width: 7,
paddingLeft: 1,
paddingRight: 1
},
1: {
alignment: 'right',
width: 7,
paddingLeft: 1,
paddingRight: 1
},
2: {
alignment: 'right',
width: 13,
paddingLeft: 1,
paddingRight: 1
},
3: {
alignment: 'right',
width: 6,
paddingLeft: 1,
paddingRight: 1
},
4: {
alignment: 'right',
width: 24,
paddingLeft: 1,
paddingRight: 1
},
5: {
alignment: 'left',
width: 40,
paddingLeft: 1,
paddingRight: 1
}
}
};
let output = table(tabledata, options);
logger.log('Source details:\r\n'+output);
}
function printPrices(prices) {
var tabledata = [['symbol', 'backing', 'new price', 'cer', 'mean', 'median', 'wgt. avg', 'wgt. std(#)', 'blockchain', 'mssr', 'mcr', 'my last price', 'last update']];
for (var symbol in prices) {
var feed = prices[symbol];
if (feed == undefined) {
continue;
}
var last, age;
let myprice = feed['price'];
let blockchain = new Price(feed['global_feed']['settlement_price']).Float();
if (feed['current_feed'] != undefined) {
last = new Price(feed['current_feed']['settlement_price']).Float();
age = moment.tz(feed['current_feed']['date'], 'UTC').fromNow();
} else {
last = -1;
age = 'Unknown';
}
tabledata.push([
symbol,
feed['short_backing_symbol'],
formatPrice(feed['price']),
formatPrice(feed['cer']),
formatPrice(feed['mean']) + ' (' + priceChange(myprice, feed['mean']) + ')',
formatPrice(feed['median']) + ' (' + priceChange(myprice, feed['median']) + ')',
formatPrice(feed['weighted']) + ' (' + priceChange(myprice, feed['weighted']) + ')',
formatStd(feed['std']) + ' (' + feed['number'] + ')',
formatPrice(blockchain) + ' (' + priceChange(myprice, blockchain) + ')',
feed['mssr'],
feed['mcr'],
formatPrice(last) + ' (' + priceChange(myprice, last) + ')',
age
]);
}
let options = {
drawHorizontalLine: (index, size) => {
return index === 0 || index === 1 || index === size;
},
columns: {
0: {
alignment: 'right',
width: 7,
paddingLeft: 1,
paddingRight: 1
},
1: {
alignment: 'right',
width: 7,
paddingLeft: 1,
paddingRight: 1
},
2: {
alignment: 'right',
width: 13,
paddingLeft: 1,
paddingRight: 1
},
3: {
alignment: 'right',
width: 13,
paddingLeft: 1,
paddingRight: 1
},
4: {
alignment: 'right',
width: 22,
paddingLeft: 1,
paddingRight: 1
},
5: {
alignment: 'right',
width: 22,
paddingLeft: 1,
paddingRight: 1
},
6: {
alignment: 'right',
width: 22,
paddingLeft: 1,
paddingRight: 1
},
7: {
alignment: 'right',
width: 12,
paddingLeft: 1,
paddingRight: 1
},
8: {
alignment: 'right',
width: 22,
paddingLeft: 1,
paddingRight: 1
},
9: {
alignment: 'right',
width: 5,
paddingLeft: 1,
paddingRight: 1
},
10: {
alignment: 'right',
width: 5,
paddingLeft: 1,
paddingRight: 1
},
11: {
alignment: 'right',
width: 22,
paddingLeft: 1,
paddingRight: 1
},
12: {
alignment: 'right',
width: 18,
paddingLeft: 1,
paddingRight: 1
}
}
};
let output = table(tabledata, options);
logger.log('Pricefeed details:\r\n'+output);
}
function priceChange(newp, old) {
if (old == 0) {
return -1;
} else {
let perc = ((newp - old) / old) * 100;
if (perc < 0) {
return chalk.red(perc.toFixed(2) + '%');
} else {
return chalk.green('+' + perc.toFixed(2) + '%');
}
}
}
function formatStd(std) {
return chalk.bold(Number(std).toFixed(2));
}