-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (152 loc) · 5.67 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
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
import express from 'express';
import helmet from 'helmet';
import { default as pinoHTTP } from 'pino-http';
import * as resolve from './resolve.js';
import * as worth from './worth.js';
import logger from './logger.cjs';
import { config } from './config.js';
import { show } from './errors.js';
const PORT = config.PORT;
export const app = express();
// Return all data (domain, addresses, amounts owned, fiat values, net worth)
app.get('/domain/:domain', async (req, res, next) => {
try {
let response = {};
let domain = req.params.domain;
const resolver = await resolve.init(domain);
const avatar = await resolve.resolveAvatar(resolver);
const coinTypes = await resolve.getCoinTypes(domain);
const addrs = await resolve.resolveAddrs(coinTypes, resolver);
const amounts = await worth.getAmounts(addrs);
const net = await worth.netWorth(amounts);
response["domain"] = domain;
response["avatar"] = avatar;
Object.assign(response, net);
let assets = [];
for (const [asset, addr] of addrs) {
const name = { "name": asset };
const address = { "address": addr };
const amount = await worth.getSingleAmount(asset, addr);
const fiat = await worth.toFiat(asset, amount.balance);
let assetFull = Object.assign({}, name, address, amount, fiat);
assets.push(assetFull);
}
response["assets"] = assets;
return res.json(response);
} catch(e) {
next(e);
}
});
// Return all supported address records for a given domain
app.get('/domain/:domain/address', async (req, res, next) => {
try {
let domain = req.params.domain;
const resolver = await resolve.init(domain);
const coinTypes = await resolve.getCoinTypes(domain);
const addrs = await resolve.resolveAddrs(coinTypes, resolver);
return res.json(Object.fromEntries(addrs));
} catch(e) {
next(e);
}
});
// Return amounts owned for all supported address records
app.get('/domain/:domain/amount', async (req, res, next) => {
try {
let domain = req.params.domain;
const resolver = await resolve.init(domain);
const coinTypes = await resolve.getCoinTypes(domain);
const addrs = await resolve.resolveAddrs(coinTypes, resolver);
const amounts = await worth.getAmounts(addrs);
return res.json(Object.fromEntries(amounts));
} catch(e) {
next(e);
}
});
// Return only the avatar for a given domain
app.get('/domain/:domain/avatar', async (req, res, next) => {
try {
let response = {};
let domain = req.params.domain;
const resolver = await resolve.init(domain);
const avatar = await resolve.resolveAvatar(resolver);
response["avatar"] = avatar;
return res.json(response);
} catch(e) {
next(e);
}
});
// Return sum of balances in local currency for all supported addresses
app.get('/domain/:domain/net', async (req, res, next) => {
try {
let domain = req.params.domain;
const resolver = await resolve.init(domain);
const coinTypes = await resolve.getCoinTypes(domain);
const addrs = await resolve.resolveAddrs(coinTypes, resolver);
const amounts = await worth.getAmounts(addrs);
const net = await worth.netWorth(amounts);
return res.json(net);
} catch(e) {
next(e);
}
});
// Return a domain's address, amount owned, and fiat value for one asset
app.get('/domain/:domain/:asset', async (req, res, next) => {
try {
let domain = req.params.domain;
let asset = req.params.asset;
const resolver = await resolve.init(domain);
const addr = await resolve.resolveSingleAddr(asset, resolver);
const amount = await worth.getSingleAmount(asset, addr.address);
const fiat = await worth.toFiat(asset, amount.balance);
const response = Object.assign({}, addr, amount, fiat);
return res.json(response);
} catch(e) {
next(e);
}
});
// Return address of a specified asset for a given domain
app.get('/domain/:domain/:asset/address', async (req, res, next) => {
try {
let domain = req.params.domain;
let asset = req.params.asset;
const resolver = await resolve.init(domain);
const addr = await resolve.resolveSingleAddr(asset, resolver);
return res.json(addr);
} catch(e) {
next(e);
}
});
// Return amount owned of a specified asset for a given domain
app.get('/domain/:domain/:asset/amount', async (req, res, next) => {
try {
let domain = req.params.domain;
let asset = req.params.asset;
const resolver = await resolve.init(domain);
const addr = await resolve.resolveSingleAddr(asset, resolver);
const amount = await worth.getSingleAmount(asset, addr.address);
return res.json(amount);
} catch(e) {
next(e);
}
});
// Return value in local currency of amount owned for one asset
app.get('/domain/:domain/:asset/fiat', async (req, res, next) => {
try {
let domain = req.params.domain;
let asset = req.params.asset;
const resolver = await resolve.init(domain);
const addr = await resolve.resolveSingleAddr(asset, resolver);
const amount = await worth.getSingleAmount(asset, addr.address);
const fiat = await worth.toFiat(asset, amount.balance);
return res.json(fiat);
} catch(e) {
next(e);
}
});
app.get('*', (req, res) => {
res.status(400).send("Route not allowed");
});
app.use(pinoHTTP());;
app.use(helmet());
app.use(show);
app.listen(PORT, () => logger.info(`Server started on port ${PORT}`));