-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
66 lines (58 loc) · 2.24 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
const config = require('./configuration');
const fetch = require('node-fetch');
// === Gets all positions ======================================================
const getAllPositions = async (deviceID) => {
try{
const url = `https://api-v0.blockfolio.com/rest/get_all_positions/${deviceID}`;
const reqOptions = {
method: 'GET'
};
const response = await fetch(url, reqOptions);
const parsedResponse = await response.json();
return parsedResponse;
} catch(err) {
const errorString = JSON.stringify(err);
console.error(`Failed retrieving all positions. Error: ${errorString}`);
}
};
// === Converts to euro =======================================================
const convertToEuro = async (amountInUSD) => {
try{
const url = `http://api.fixer.io/latest?base=USD&symbols=EUR`;
const reqOptions = {
method: 'GET'
};
const response = await fetch(url, reqOptions);
const parsedResponse = await response.json();
const euroRate = parsedResponse.rates.EUR;
const valueInEUR = (amountInUSD*euroRate).toFixed(2);
return valueInEUR;
} catch(err) {
const errorString = JSON.stringify(err);
console.error(`Failed converting to euro. Error: ${errorString}`);
}
};
// === Executes the request ====================================================
(async function() {
try{
const showType = process.argv[2].toLowerCase(); // Can be USD, EUR. BTC for now
const allPositions = await getAllPositions(config.deviceID);
const portfolioValueInBTC = allPositions.portfolio.btcValue;
const portfolioValueInUSD = allPositions.portfolio.usdValue;
const parsedValueInBTC = parseFloat(portfolioValueInBTC).toFixed(4);
const parsedValueInUSD = parseFloat(portfolioValueInUSD).toFixed(2);
const parsedValueInEUR = await convertToEuro(parsedValueInUSD);
if(showType==='btc'){
console.log(parsedValueInBTC);
} else if(showType==='usd'){
console.log(`$${parsedValueInUSD}`)
} else if(showType==='eur'){
console.log(`€${parsedValueInEUR}`)
} else { //fallback USD
console.log(parsedValueInBTC);
}
} catch(err){
console.error(`Please provide a currency you want to display your results in.
Available options are EUR,USD,BTC.`)
}
})();