-
Notifications
You must be signed in to change notification settings - Fork 23
/
exchange_price_feed.js
541 lines (503 loc) · 17.1 KB
/
exchange_price_feed.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*jslint node: true */
'use strict';
const async = require('async');
const request = require('request').defaults({timeout: 10 * 1000});
const eventBus = require('ocore/event_bus.js');
const network = require('ocore/network.js');
const db = require('ocore/db.js');
const storage = require('ocore/storage.js');
const { executeGetter } = require('ocore/formula/evaluation.js');
require("tls").DEFAULT_ECDH_CURVE = "auto"; // fix for Node 8
let rates = {};
let finished_rates;
const decimalsInfo = {};
let updating = false;
function updateBitfinexRates(state, onDone) {
const apiUri = 'https://api.bitfinex.com/v1/pubticker/btcusd';
request(apiUri, function (error, response, body) {
if (!error && response.statusCode == 200) {
let price;
try{
price = parseFloat(JSON.parse(body).last_price);
console.log("new exchange rate: BTC-USD = " + price);
}
catch(e){
console.log('bad response from bitfinex:', e);
return onDone();
}
if (price) {
rates['BTC_USD'] = price;
state.updated = true;
}
}
else {
console.error("Can't get currency rates from bitfinex", error, body);
}
onDone();
});
}
function updateBittrexRates(state, onDone) {
const apiUri = 'https://api.bittrex.com/v3/markets/GBYTE-BTC/ticker';
request(apiUri, function (error, response, body) {
if (!error && response.statusCode == 200) {
let price;
try{
price = parseFloat(JSON.parse(body).lastTradeRate);
console.log("new exchange rate: GBYTE-BTC = " + price);
}
catch(e){
console.log('bad response from bittrex:', e);
return onDone();
}
if (price) {
rates['GBYTE_BTC'] = price;
if (rates['BTC_USD']) {
rates['GBYTE_USD'] = price * rates['BTC_USD'];
}
state.updated = true;
}
}
else {
console.error("Can't get currency rates from bittrex", error, body);
}
onDone();
});
}
async function updateGbyteRates(state, onDone) {
if (process.env.devnet)
return onDone();
if (network.isCatchingUp())
return onDone();
rates['GBYTE_USD'] = await executeGetter(db, process.env.testnet ? 'CFJTSWILG4FJGJJAN7II7FHP2TAFBB57' : 'MBTF5GG44S3ARJHIZH3DEAB4DGUCHCF6', 'get_price', ['x', 9, 4]);
if (rates['BTC_USD'])
rates['GBYTE_BTC'] = rates['GBYTE_USD'] / rates['BTC_USD'];
state.updated = true;
onDone();
}
function updateOstableRates(state, onDone) {
const apiUri = 'https://data.ostable.org/api/v1/summary';
request(apiUri, function (error, response, body) {
if (!error && response.statusCode == 200) {
let arrCoinInfos;
try {arrCoinInfos = JSON.parse(body);} catch(e){}
if (!arrCoinInfos) {
console.log('bad rates from ostable data api');
return onDone();
}
arrCoinInfos.forEach(coinInfo => {
if (!coinInfo.last_price || coinInfo.quote_id !== 'base' || coinInfo.base_id === 'base')
return;
console.log("new exchange rate: " + coinInfo.market_name + " = " + coinInfo.last_price);
if (rates['GBYTE_USD']) {
rates[coinInfo.base_id +'_USD'] = rates['GBYTE_USD'] * coinInfo.last_price;
}
state.updated = true;
});
arrCoinInfos.forEach(coinInfo => {
if (!coinInfo.last_price || coinInfo.quote_id === 'base' || coinInfo.base_id === 'base')
return;
console.log("new exchange rate: " + coinInfo.market_name + " = " + coinInfo.last_price);
if (rates[coinInfo.quote_id +'_USD']) {
rates[coinInfo.base_id +'_USD'] = rates[coinInfo.quote_id +'_USD'] * coinInfo.last_price;
state.updated = true;
}
});
}
else {
console.error("Can't get currency rates from ostable data api", error, body);
}
onDone();
});
}
function updateOstableReferralsRates(state, onDone) {
const apiUri = 'https://referrals.ostable.org/prices';
request(apiUri, function (error, response, body) {
if (!error && response.statusCode == 200) {
let arrCoinInfos;
try {arrCoinInfos = JSON.parse(body).data;} catch(e){}
if (!arrCoinInfos) {
console.log('bad rates from ostable referrals api');
return onDone();
}
for (var asset in arrCoinInfos) {
if (!asset || asset === 'base')
continue;
rates[asset +'_USD'] = arrCoinInfos[asset];
state.updated = true;
}
}
else {
console.error("Can't get currency rates from ostable referrals api", error, body);
}
onDone();
});
}
function requestAsync(url) {
return new Promise((resolve, reject) => {
request(url, (error, response, body) => {
if (error)
return reject(error);
if (response.statusCode != 200)
return reject("non-200 status code " + response.statusCode);
resolve(body);
});
});
}
const nativeSymbols = {
Ethereum: 'ETH',
BSC: 'BNB',
Polygon: 'MATIC',
Kava: 'KAVA',
};
const coingeckoChains = {
Ethereum: 'ethereum',
BSC: 'binance-smart-chain',
Polygon: 'polygon-pos',
Kava: 'kava',
};
const fetchCryptocompareExchangeRate = async (in_currency, out_currency) => {
let data = await requestAsync(`https://min-api.cryptocompare.com/data/price?fsym=${in_currency}&tsyms=${out_currency}`);
data = JSON.parse(data);
if (!data[out_currency])
throw new Error(`no ${out_currency} in response ${JSON.stringify(data)}`);
return data[out_currency];
}
async function fetchERC20ExchangeRate(chain, token_address, quote) {
if (token_address === '0x4DBCdF9B62e891a7cec5A2568C3F4FAF9E8Abe2b') // USDC rinkeby
token_address = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
if (token_address === '0xbF7A7169562078c96f0eC1A8aFD6aE50f12e5A99') // BAT rinkeby
token_address = '0x0D8775F648430679A709E98d2b0Cb6250d2887EF';
if (token_address === '0xeD24FC36d5Ee211Ea25A80239Fb8C4Cfd80f12Ee') // BUSD testnet
token_address = '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56';
let data = await requestAsync(`https://api.coingecko.com/api/v3/coins/${chain}/contract/${token_address.toLowerCase()}`);
data = JSON.parse(data);
const prices = data.market_data.current_price;
quote = quote.toLowerCase();
if (!prices[quote])
throw new Error(`no ${quote} in response ${JSON.stringify(data)}`);
return prices[quote];
}
async function updateImportedAssetsRates(state, onDone) {
if (network.isCatchingUp())
return onDone();
const import_factory_aa = 'KFAJZYLH6T3W2U6LNQJYIWXJVSBB24FN';
storage.readAAStateVars(import_factory_aa, 'import_', 'import_', 0, async vars => {
for (let var_name in vars) {
const { asset, asset_decimals, home_network, home_asset } = vars[var_name];
const chain = coingeckoChains[home_network];
if (!chain) {
console.error('unknown network ' + home_network);
continue;
}
decimalsInfo[asset] = asset_decimals; // cache for updateOswapPoolTokenRates()
try {
if (home_asset === '0x0000000000000000000000000000000000000000')
rates[asset + '_USD'] = await fetchCryptocompareExchangeRate(nativeSymbols[home_network], 'USD');
else
rates[asset + '_USD'] = await fetchERC20ExchangeRate(chain, home_asset, 'USD');
state.updated = true;
}
catch (e) {
console.error('failed to fetch the rate of', home_asset, 'on', home_network, e);
}
}
onDone();
});
}
async function updateOswapTokenRate(state, onDone) {
if (process.env.devnet)
return onDone();
if (network.isCatchingUp())
return onDone();
const oswap_token_aa = process.env.testnet ? 'IGUTWKORU2CVHHFUFY3OG7LQKKLCRJSA' : 'OSWAPWKOXZKJPYWATNK47LRDV4UN4K7H';
const price = await executeGetter(db, oswap_token_aa, 'get_price', []);
const { asset } = await storage.readAAStateVar(oswap_token_aa, 'constants');
rates[asset + '_USD'] = price * rates['GBYTE_USD'];
state.updated = true;
onDone();
}
async function updateOswapPoolTokenRates(state, onDone) {
if (network.isCatchingUp())
return onDone();
const pool_factory_aa = process.env.testnet ? 'PFNAFDKV6HKKFIEB2R2ZE4IAPSDNNIGX' : 'B22543LKSS35Z55ROU4GDN26RT6MDKWU';
const pools = {};
const vars = await storage.readAAStateVars(pool_factory_aa, 'pools.', 'pools.', 0);
const db = require('ocore/db.js');
for (let var_name in vars) {
const [prefix, pool_address, key] = var_name.split('.');
pools[pool_address] = pools[pool_address] || {};
pools[pool_address][key] = vars[var_name];
}
// several passes to find prices of tokens not paired directly with known tokens, such as in pools GBYTE-A, A-B
for (let i = 0; i < 3; i++)
await scanPools();
onDone();
async function scanPools() {
for (let pool_address in pools) {
try {
const { asset, asset0, asset1 } = pools[pool_address];
if (!asset || !asset0 || !asset1) {
console.error('pool assets missing', pool_address);
continue;
}
if (rates[asset + '_USD']) // already known (2nd pass)
continue;
const price0 = getAssetUSDPrice(asset0);
const price1 = getAssetUSDPrice(asset1);
if (!price0 && !price1) {
console.error('both prices missing for pool assets', pool_address);
continue;
}
const balances = await storage.readAABalances(db, pool_address);
if (!balances[asset0] || !balances[asset1]) {
console.error('pool balances empty', pool_address);
continue;
}
const balance0_in_display_units = await getAssetAmount(balances[asset0], asset0);
const balance1_in_display_units = await getAssetAmount(balances[asset1], asset1);
let asset0value = balance0_in_display_units * price0;
let asset1value = balance1_in_display_units * price1;
if (asset0value && !asset1value) {
asset1value = asset0value; // dollar values of both assets are always equal in 50/50 pools
rates[asset1 + '_USD'] = asset1value / balance1_in_display_units;
console.log('setting ' + asset1 + ' rate to ' + asset1value + ' / ' + balance1_in_display_units);
}
else if (!asset0value && asset1value) {
asset0value = asset1value; // dollar values of both assets are always equal in 50/50 pools
rates[asset0 + '_USD'] = asset0value / balance0_in_display_units;
console.log('setting ' + asset0 + ' rate to ' + asset0value + ' / ' + balance0_in_display_units);
}
else if (!asset0value && !asset1value) // should be already skipped
throw Error("none of the values is known")
const total_pool_value = asset0value + asset1value;
const supply = await storage.readAAStateVar(pool_address, 'supply');
if (!supply) {
console.error('pool asset supply empty', pool_address);
continue;
}
rates[asset + '_USD'] = total_pool_value / supply;
state.updated = true;
}
catch (e) {
console.error('failed to fetch the rate for', pool_address, 'pool', e);
}
}
}
}
async function updateOswapV2PoolTokenRates(state, onDone) {
if (network.isCatchingUp())
return onDone();
const pool_factory_aas = ['OQLU4HOAIVJ32SDVBJA6AKD52OVTHAOF', 'MODBFVX2J2TRPQUK7XFTFQK73AB64NF3'];
let factoryVars = {};
for (let pool_factory_aa of pool_factory_aas) {
const vars = await storage.readAAStateVars(pool_factory_aa);
factoryVars = { ...factoryVars, ...vars };
}
const db = require('ocore/db.js');
const pools = {};
for (let var_name in factoryVars) {
const pool_address = var_name.replace('pool_', '');
const pool = factoryVars[var_name];
pool.asset = pool.pool_asset;
pools[pool_address] = pool;
}
// several passes to find prices of tokens not paired directly with known tokens, such as in pools GBYTE-A, A-B
for (let i = 0; i < 3; i++)
await scanPools();
onDone();
async function scanPools() {
for (let pool_address in pools) {
try {
const { asset, x_asset, y_asset } = pools[pool_address];
if (!asset || !x_asset || !y_asset) {
console.error('pool assets missing', pool_address);
continue;
}
if (rates[asset + '_USD']) // already known (2nd pass)
continue;
let x_price = getAssetUSDPrice(x_asset);
let y_price = getAssetUSDPrice(y_asset);
if (!x_price && !y_price) {
console.error('both prices missing for pool assets', pool_address);
continue;
}
const balances = await storage.readAABalances(db, pool_address);
if (!balances[x_asset] || !balances[y_asset]) {
console.error('pool balances empty', pool_address);
continue;
}
const x_decimals = await getDecimals(x_asset);
const y_decimals = await getDecimals(y_asset);
const x_balance_in_display_units = await getAssetAmount(balances[x_asset], x_asset);
const y_balance_in_display_units = await getAssetAmount(balances[y_asset], y_asset);
let x_asset_value = x_balance_in_display_units * x_price;
let y_asset_value = y_balance_in_display_units * y_price;
if (x_price && !y_price) {
const pxy = await executeGetter(db, pool_address, 'get_price', ['x', x_decimals, y_decimals]);
y_price = x_price / pxy;
y_asset_value = y_balance_in_display_units * y_price;
rates[y_asset + '_USD'] = y_asset_value / y_balance_in_display_units;
console.log('setting ' + y_asset + ' rate to ' + y_asset_value + ' / ' + y_balance_in_display_units);
}
else if (!x_price && y_price) {
const pxy = await executeGetter(db, pool_address, 'get_price', ['x', x_decimals, y_decimals]);
x_price = y_price * pxy;
x_asset_value = x_balance_in_display_units * x_price;
rates[x_asset + '_USD'] = x_asset_value / x_balance_in_display_units;
console.log('setting ' + x_asset + ' rate to ' + x_asset_value + ' / ' + x_balance_in_display_units);
}
else if (!x_price && !y_price) // should be already skipped
throw Error("none of the values is known")
const total_pool_value = x_asset_value + y_asset_value;
const lp_shares = await storage.readAAStateVar(pool_address, 'lp_shares');
if (!lp_shares) {
console.error('lp_shares empty', pool_address);
continue;
}
rates[asset + '_USD'] = total_pool_value / lp_shares.issued;
state.updated = true;
}
catch (e) {
console.error('failed to fetch the rate for', pool_address, 'pool', e);
}
}
}
}
async function getDecimals(asset) {
const asset_registry = 'O6H6ZIFI57X3PLTYHOCVYPP5A553CYFQ';
let decimals = 0; // default for unregistered assets
if (asset === 'base')
decimals = 9;
else if (typeof decimalsInfo[asset] === 'number')
decimals = decimalsInfo[asset];
else {
const desc_hash = await storage.readAAStateVar(asset_registry, "current_desc_" + asset);
if (desc_hash) {
const dec = await storage.readAAStateVar(asset_registry, 'decimals_' + desc_hash);
if (typeof dec === 'number') {
decimals = dec;
decimalsInfo[asset] = decimals;
}
}
}
return decimals;
}
async function getAssetAmount(balance, asset) {
const decimals = await getDecimals(asset);
return balance / (10 ** decimals);
}
function getAssetUSDPrice(asset){
if (asset === 'base') asset = 'GBYTE';
if (rates[asset + '_USD'])
return rates[asset + '_USD'];
}
function updateFreebeRates(state, onDone) {
const apiUri = 'https://blackbytes.io/last';
request(apiUri, function (error, response, body) {
if (!error && response.statusCode == 200) {
let price;
try{
price = parseFloat(JSON.parse(body).price_bytes);
console.log("new exchange rate: GBB-GBYTE = " + price);
}
catch(e){
console.log('bad response from freebe:', e);
return onDone();
}
if (rates['GBYTE_USD'] && price) {
rates['GBB_GBYTE'] = price;
rates['GBB_USD'] = rates['GBYTE_USD'] * price;
state.updated = true;
}
if (rates['GBYTE_BTC'] && price) {
rates['GBB_BTC'] = rates['GBYTE_BTC'] * price;
state.updated = true;
}
}
else {
console.error("Can't get currency rates from freebe", error, body);
}
onDone();
});
}
function updateBTC_20200701Rates(state, onDone) {
// transactions.json is more up-to-date than ticker.json
const apiUri = 'https://cryptox.pl/api/BTC_20200701BTC/transactions.json';
request(apiUri, function (error, response, body) {
if (!error && response.statusCode == 200) {
let price;
try{
price = parseFloat(JSON.parse(body)[0].price);
console.log("new exchange rate: BTC_20200701-BTC = " + price);
}
catch(e){
console.log('bad response from cryptox:', e);
return onDone();
}
if (rates['BTC_USD'] && price) {
rates['ZVuuh5oWAJnISvtOFdzHAa7QTl/CG7T2KDfAGB4qSxk=_USD'] = rates['BTC_USD'] * price;
state.updated = true;
}
}
else {
console.error("Can't get currency rates from cryptox", error, body);
}
onDone();
});
}
function updateRates(){
if (updating)
return console.log('already updating rates, will skip');
updating = true;
rates = {}; // reset
let state = {updated: false};
async.series([
function(cb){
updateBitfinexRates(state, cb);
},
function(cb){
updateGbyteRates(state, cb);
},
// function(cb){
// updateOstableRates(state, cb);
// },
function(cb){
updateOstableReferralsRates(state, cb);
},
function(cb){
updateImportedAssetsRates(state, cb);
},
function(cb){
updateOswapTokenRate(state, cb);
},
function(cb){
updateOswapV2PoolTokenRates(state, cb);
},
function(cb){
updateOswapPoolTokenRates(state, cb);
},
function(cb){
updateFreebeRates(state, cb);
},
// function(cb){
// updateBTC_20200701Rates(state, cb);
// },
], function(){
console.log(rates);
finished_rates = rates;
network.setExchangeRates(rates);
if (state.updated)
broadcastNewRates();
updating = false;
});
}
function broadcastNewRates(){
network.sendAllInboundJustsaying('exchange_rates', finished_rates);
}
eventBus.on('client_logged_in', function(ws){
if (Object.keys(rates).length > 0)
network.sendJustsaying(ws, 'exchange_rates', finished_rates || rates);
});
updateRates();
setInterval(updateRates, 1000 * 60 * 5);
//exports.rates = rates;