-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmarketcap.js
64 lines (56 loc) · 1.98 KB
/
marketcap.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
const {getConfig} = require('./near');
const nearAPI = require('near-api-js');
const config = getConfig(process.env.NODE_ENV || 'production');
const mode = process.env.EXPORT_MODE || 'JSON';
// token account
let brrrToken = 'token.burrow.near';
// set all accounts with locked tokens
const brrrLockedHolders = ['lockup.burrow.near', 'burrow.sputnik-dao.near'];
// token max supply
const maxSupply = Math.pow(10, 9);
const getTokenPrice = async (tokenId) => {
return fetch("https://indexer.ref-finance.net/list-token-price")
.then(res => res.json())
.then(json => json[tokenId].price || 0)
.catch(err => {
console.error(err);
return 0;
});
}
const updateMarketcap = async () => {
let marketCap = {
lockedBalances: [],
circulatingSupply: 0,
lastUpdate: 0
}
let tokenPrice = await getTokenPrice("token.burrow.near");
const near = await nearAPI.connect(config);
const lockedBalances = await Promise.all(
brrrLockedHolders.map(async (address) => {
const account = await near.account(address);
const ft_balance = await account.viewFunction(brrrToken, 'ft_balance_of', {account_id: address})
const parsedBalance = Number(ft_balance)/Math.pow(10,18);
return {
address,
balance: parsedBalance,
value: parsedBalance * tokenPrice
};
})
)
const sumLocked = lockedBalances.reduce((acc, value) => acc+value.balance, 0);
const circulatingSupply = maxSupply-sumLocked;
if(!isNaN(circulatingSupply)) {
marketCap.lockedBalances = lockedBalances;
marketCap.circulatingSupply = circulatingSupply;
marketCap.lastUpdate = new Date().getTime();
}
return(marketCap);
}
updateMarketcap().then((marketCap)=> {
if (mode === "JSON"){
console.log(marketCap)
}
else if (mode === "CS"){
console.log(marketCap.circulatingSupply.toString())
}
});