-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworth.js
139 lines (122 loc) · 4.84 KB
/
worth.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
import fetch from 'node-fetch';
import { config } from './config.js';
import {
AssetError,
throwProperly,
UpstreamError
} from './errors.js';
import logger from './logger.cjs';
const NOW_NODES = config.NOW_NODES;
const NN_URL = config.NN_URL;
const NN_SUFFIX = config.NN_SUFFIX;
const BOOKS = config.BOOKS;
const BA_PREFIX = config.BA_PREFIX;
const COIN_NAMES = config.COIN_NAMES;
const SCALE = config.SCALE;
// Iterate over address map to find amounts owned
export async function getAmounts(addresses) {
try {
if (!addresses) throw new ArgError();
const amounts = new Map();
logger.info("Getting amounts owned for all addresses...");
for (const [key, val] of addresses) {
if (!BOOKS[key]) continue;
const fullURL = `${BOOKS[key]}${NN_URL}${val}${NN_SUFFIX}`;
const response = await fetch(fullURL, {
method: 'GET',
headers: {
'api-key': NOW_NODES,
},
}).catch(e => { throw new UpstreamError });
const responseBody = await response.json();
try {
if (!COIN_NAMES.includes(key)) {
logger.info(`getAmounts: Asset ${key} is not supported`);
continue;
}
const parsedBalance = Number(responseBody.balance);
const formattedBalance = parsedBalance / Math.pow(10, SCALE[key]);
logger.info(`${key} balance: ${formattedBalance}`);
const balance = String(formattedBalance);
amounts.set(key, balance);
} catch { throw new UpstreamError() }
}
if (amounts.size === 0) throw new AssetError(amounts.values().next().value);
logger.info("Finished getting amounts owned");
return amounts;
} catch(e) { throwProperly(e) }
}
// Get amount owned by an address for a specified asset
export async function getSingleAmount(asset, address) {
try {
if (arguments.length !== 2) throw new ArgError();
let apiResponse = {};
let amount = "";
logger.info(`Getting amount of ${asset} owned by address ${address}`);
if (!BOOKS[asset]) throw new AssetError(asset);
const fullURL = `${BOOKS[asset]}${NN_URL}${address}${NN_SUFFIX}`;
const response = await fetch(fullURL, {
method: 'GET',
headers: {
'api-key': NOW_NODES,
},
}).catch(e => { throw new UpstreamError() });
const responseBody = await response.json();
try {
if (!COIN_NAMES.includes(asset)) throw new AssetError(asset);
const parsedBalance = Number(responseBody.balance);
const formattedBalance = parsedBalance / Math.pow(10, SCALE[asset]);
amount = String(formattedBalance);
} catch { throw new UpstreamError() }
apiResponse["balance"] = amount;
logger.info(`Found amount owned: ${amount}`);
return apiResponse;
} catch(e) { throwProperly(e) }
}
// Convert asset balance to USD
export async function toFiat(asset, balance) {
try {
if (arguments.length !== 2) throw new ArgError();
let fiatAmount = {};
const ast = asset.toUpperCase();
let bal = Number(balance);
const fullURL = `${BA_PREFIX}${ast}-USD`;
logger.info(`Converting ${balance} ${asset} to usd...`);
const response = await fetch(fullURL, {
method: 'GET',
}).catch(e => {
throw new UpstreamError();
});
if (response.status === 400) throw new AssetError(asset);
const responseBody = await response.json();
try {
const price = responseBody.last_trade_price;
const fiatBalance = bal * price;
fiatAmount["usd"] = String(fiatBalance);
logger.info(`Converted to ${fiatBalance} usd`);
return fiatAmount;
} catch {
throw new UpstreamError();
}
} catch(e) { throwProperly(e) }
}
// Return the sum of all retrieved amounts in USD
export async function netWorth(balances) {
try {
if (!balances) throw new ArgError();
let response = {};
let net = 0.0;
logger.info("Calculating net worth from passed amounts...");
for (const [asset, balance] of balances) {
const fiat = await toFiat(asset, balance);
if (!fiat) throw new UpstreamError();
Object.values(fiat).forEach(value => {
let num = Number(value);
net += num;
});
}
response["net"] = String(net);
logger.info(`Net worth is ${net} usd`);
return response;
} catch(e) { throwProperly(e) }
}