forked from sei-protocol/sei-chain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprices.js
74 lines (68 loc) · 2.42 KB
/
prices.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
require('log-timestamp');
const axios = require('axios');
const coinUtils = require('./utils').utils;
// Note that ust seems to only support conversions to usd, so we need to
// do the following conversion to get x:ust:
// x:usd / ust:usd
var getCoinGeckoPrice = async (marketID) => {
let coinPrice;
let ustPrice;
try {
const coinPriceUrl = coinUtils.loadCoinGeckoQuery(marketID);
const ustPriceUrl = coinUtils.loadCoinGeckoQuery('ust');
const coinPriceFetchAsync = axios.get(coinPriceUrl);
const ustPriceFetchAsync = axios.get(ustPriceUrl);
const coinPriceFetch = await coinPriceFetchAsync;
const ustPriceFetch = await ustPriceFetchAsync;
coinPrice = coinPriceFetch.data[coinUtils.loadCoinGeckoMarket(marketID)].usd;
ustPrice = ustPriceFetch.data[coinUtils.loadCoinGeckoMarket('ust')].usd;
} catch (e) {
console.log(e);
console.log(`could not fetch ${marketID} price from Coin Gecko`);
return;
}
return parseFloat(coinPrice) / parseFloat(ustPrice);
};
var getBinancePrice = async (marketID) => {
if (!coinUtils.marketSupportedByBinance(marketID)) {
throw new Error(`${marketID} not supported in Binance`);
}
if (coinUtils.marketHasDirectUSTConversion(marketID)) {
return getBinanceUSTPrice(marketID);
}
return calculateBinanceUSTPrice(marketID);
};
// Directly query binance for price of x:ust
var getBinanceUSTPrice = async (marketID) => {
let priceFetch;
try {
let url = coinUtils.loadBinanceQuery(marketID, 'ust');
priceFetch = await axios.get(url);
} catch (e) {
console.log(e);
throw new Error(`could not fetch ${marketID} price from binance`);
}
return priceFetch.data.lastPrice;
};
// Calculate the price of x:ust by getting the prices based on usdt
// (similar to Coingecko computation)
var calculateBinanceUSTPrice = async (marketID) => {
let coinPriceFetch;
let ustPriceFetch;
try {
let coinPriceUrl = coinUtils.loadBinanceQuery(marketID);
let ustPriceUrl = coinUtils.loadBinanceQuery('ust');
let coinPriceFetchAsync = axios.get(coinPriceUrl);
let ustPriceFetchAsync = axios.get(ustPriceUrl);
coinPriceFetch = await coinPriceFetchAsync;
ustPriceFetch = await ustPriceFetchAsync;
} catch (e) {
console.log(e);
throw new Error(`could not fetch ${marketID} price from binance`)
}
return coinPriceFetch.data.lastPrice / ustPriceFetch.data.lastPrice;
};
module.exports.prices = {
getBinancePrice,
getCoinGeckoPrice,
};