-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
206 lines (165 loc) · 6.4 KB
/
main.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
const { Apis } = require('bitsharesjs-ws');
const fs = require('fs');
const { markets } = require('./config');
let assets;
/*TODO: 24h price change
7d price change
last bucket base volume
last bucket base in usd volume
last bucket close price in base
lase bucket close price in usd*/
const populateAssets = async (assets) => {
try {
const result = await Apis.instance().db_api().exec('lookup_asset_symbols', [assets]);
return result.reduce((model, asset) => {
model[asset.symbol] = asset;
return model;
}, {})
} catch (e) {
console.log('Error lookup', e);
}
};
const precisedCount = (cnt, prec) => cnt / 10 ** prec;
// It returns open and close prices for given bucket with base and quote precisions
const getPricesFromBucket = (basePrecision, quotePrecision, bucket) => {
const closeCountBase = precisedCount(bucket.close_base, basePrecision);
const closeCountQuote = precisedCount(bucket.close_quote, quotePrecision);
const openCountBase = precisedCount(bucket.open_base, basePrecision);
const openCountQuote = precisedCount(bucket.open_quote, quotePrecision);
return {
open: openCountBase / openCountQuote,
close: closeCountBase / closeCountQuote
}
};
// It returns USD prices { last, median } for entered Base asset and bucket of history
const getUsdPrices = (basePrecision, usdLastBucket) => {
const usdPrecision = assets['USD'].precision;
const {
open: usdOpenPrice,
close: usdClosePrice
} = getPricesFromBucket(basePrecision, usdPrecision, usdLastBucket);
const medianPrice = (usdOpenPrice + usdClosePrice) / 2;
return {
last: usdClosePrice,
median: medianPrice
}
};
// Returns volume in base, last price and 24h change for base - quote market
const get24HourData = (base, quote) => {
const bucketSize = 3600;
const endDate = new Date();
const startDate = new Date(endDate - (1000 * 60 * 60 * 24 * 7));
const startDateISO = startDate.toISOString().slice(0, -5);
const endDateISO = endDate.toISOString().slice(0, -5);
return Apis.instance().history_api().exec(
'get_market_history',
[base.id, quote.id, bucketSize, startDateISO, endDateISO]
).then((data) => {
if (!data.length) {
return false;
}
const volume = data.reduce((vol, itm) => itm.base_volume + vol, 0);
const firstBucket = data[0];
const lastBucket = data[data.length - 1];
const firstBucketPrices = getPricesFromBucket(base.precision, quote.precision, firstBucket);
const lastBucketPrices = getPricesFromBucket(base.precision, quote.precision, lastBucket);
console.log(base, quote);
console.log(firstBucketPrices);
console.log(lastBucketPrices);
console.log(volume);
return {
baseVolume: volume,
price: 0
}
});
};
const getRawStats = async (base, quotes) => {
const bucketSize = 86400;
const endDate = new Date();
const startDate = new Date(endDate - (1000 * 60 * 60 * 24 * 7));
const endDateISO = endDate.toISOString().slice(0, -5);
const startDateISO = startDate.toISOString().slice(0, -5);
// We are adding USD to the beginning of quotes, to calculate USD volumes
quotes.unshift('USD');
const baseToQuotes = {};
const promises = Promise.all(quotes.map((quote) => {
return get24HourData(assets[base], assets[quote]).then((data) => {
return { quote, data }
});
}));
await promises;
process.exit(-1);
try {
const basePrecision = assets[base].precision;
const results = await promises;
const usdResult = results.shift().data;
const usdLastBucket = usdResult[usdResult.length - 1];
const usdPrices = getUsdPrices(basePrecision, usdLastBucket);
results.forEach((result) => {
const lastBucket = result.data[result.data.length - 1];
const firstBucket = result.data[0];
if (lastBucket) {
const quotePrecision = assets[result.quote].precision;
const firstBucketPrices = getPricesFromBucket(basePrecision, quotePrecision, firstBucket);
const lastBucketPrices = getPricesFromBucket(basePrecision, quotePrecision, lastBucket);
const lastPriceInUsd = lastBucketPrices.close / usdPrices.last;
const baseVolume = precisedCount(parseInt(lastBucket.base_volume), basePrecision);
const usdVolume = baseVolume * usdPrices.median;
console.log('Now calculating stats for ', base, result.quote, lastBucket);
console.log(baseVolume, usdVolume);
const quoteStatsResult = {
usdPrice: lastPriceInUsd,
basePrice: lastBucketPrices.close,
baseVolume,
usdVolume
};
process.exit(-1);
console.log(lastBucket);
console.log('Stats for BTS:', result.quote, quoteStats);
} else {
baseToQuotes[result.quote] = false;
}
});
return baseToQuotes;
} catch(e) {
console.log('Error', e);
}
};
/*
У нас есть список маркетов Base -> Quotes
1. Забираем стату за 24h часовыми бакетами (заодно забрав бакет с usd)
2. Выводим стату за 24h
3. Забираем стату за 7 дней дневными бакетами
4. Выводим изменение цены за 7 дней
*/
// Returns Usd data for given market (24h)
const getUsdData = (base) => {
const usdAsset = assets['USD'];
return Apis.instance().history_api().exec('')
}
Apis.instance("wss://bitshares.openledger.info/ws", true).init_promise.then(async (res) => {
try {
assets = JSON.parse(fs.readFileSync('./assets.json', 'utf8'));
console.log('Assets restored');
} catch (e) {
let marketAssets = Object.keys(markets);
let othersAssets = marketAssets.reduce((result, market) => {
return result.concat(markets[market]);
}, []);
assets = await populateAssets([...marketAssets,...othersAssets]);
fs.writeFile('assets.json', JSON.stringify(assets), 'utf8', () => console.log('Wrote assets'));
}
const usdData = await getUsdData('BTS');
const test24 = await get24HourData('BTS', 'OPEN.EOS');
// const usd24 = calculate from usdData and test24
//const test7d = await get7dData('BTS', 'OPEN.EOS');
const testStats = await getRawStats('BTS', markets['BTS']);
console.log('RAW', testStats);
return;
try {
const ticker = await Apis.instance().db_api().exec('get_ticker', ['BTS', 'CNY']);
console.log(ticker);
} catch(e) {
console.log(e);
}
});