forked from hicommonwealth/edgeware-supply
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·52 lines (46 loc) · 1.71 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
#!/usr/bin/env node
import { ApiPromise, WsProvider } from '@polkadot/api';
import { bnToBn, stringToU8a } from '@polkadot/util';
import { u128 } from '@polkadot/types';
module.exports = async (req, res) => {
const nodeUrl = 'wss://edgeware.jelliedowl.net';
console.log(`Connecting to API for ${nodeUrl}...`);
let connected;
setTimeout(() => {
if (connected) return;
res.setHeader('content-type', 'text/plain');
res.status(500).send('Connection timed out');
process.exit(1);
}, 2000);
// initialize the api
const api = await ApiPromise.create({
provider: new WsProvider(nodeUrl),
});
connected = true;
const TREASURY_ACCOUNT = stringToU8a('modlpy/trsry'.padEnd(32, '\0'));
//
// get relevant chain data
//
try {
const [issuance, treasury, properties, block] = await Promise.all([
api.query.balances?.totalIssuance(),
api.derive.balances?.account(TREASURY_ACCOUNT),
api.rpc.system.properties(),
]);
const tokenDecimals = properties.tokenDecimals.unwrap();
const issuanceStr = issuance.div(bnToBn(10).pow(bnToBn(tokenDecimals))).toString(10);
const treasuryStr = treasury.freeBalance.div(bnToBn(10).pow(bnToBn(tokenDecimals))).toString(10);
const circulatingStr = issuance.sub(treasury.freeBalance).div(bnToBn(10).pow(bnToBn(tokenDecimals))).toString(10);
res.setHeader('content-type', 'text/plain');
if (!!req.query.circulating) {
res.status(200).send(circulatingStr);
} else if (!!req.query.treasury) {
res.status(200).send(treasuryStr);
} else {
res.status(200).send(issuanceStr);
}
} catch (e) {
res.setHeader('content-type', 'text/plain');
res.status(500).send('Error fetching Edgeware supply data');
}
}