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
|
const https = require('https')
const URL = 'https://www.binance.com/bapi/composite/v1/public/marketing/symbol/list'
const roundSmall = (n) => {
const log10 = n ? Math.floor(Math.log10(n)) : 0, div = log10 < 0 ? Math.pow(10, 1 - log10) : 100;
return Math.round(n * div) / div;
}
const formatResponse = token => ({
'Rank': token.rank,
'Name': token.name,
'Full name': token.fullName,
'Price': '€'+(token.price<0.1?roundSmall(token.price):token.price.toFixed(2)),
'Last 24h': token.dayChange.toFixed(2)+'%',
'Market Cap': '€'+Math.round(token.marketCap),
'Circulating': token.totalSupply ? Math.round(token.circulatingSupply/token.totalSupply*100)+'%' : '-',
'Total': token.maxSupply ? Math.round(token.totalSupply/token.maxSupply*100)+'%' : '-'
})
const getResponse = (url) => https.get(url, res => {
let response = ''
res.on('data', data => { response += data })
res.on('end', () => { handleResponse(response) })
res.on('error', err => { console.error(err) })
})
const handleResponse = (response) => {
const list = JSON.parse(response)
const processed = list.data
.filter(token => token.rank && token.rank <= 40)
.map(formatResponse)
render(processed)
}
const render = (processed) => {
const paddings = Object.keys(processed[0]).map(key => {
const longest = processed.sort((a,b) => b[key].length - a[key].length)[0][key]
return longest.length > key.length ? longest.length : key.length
})
const header = '| '+Object.keys(processed[0]).map((el, i) => el.padStart(paddings[i])).join(' | ')+' |'
const line = '+'+Array(header.length-1).join('-')+'+'
console.log(line+'\n\x1b[1m'+header+'\x1b[0m\n'+line)
processed
.sort((a, b) => a.Rank - b.Rank)
.forEach(row => {
const color = `\x1b[${Number(row['Last 24h'].slice(0, -1))<0?31:32}m`
console.log('| '+color+Object.values(row).map((el, i) => el.toString().padStart(paddings[i])).join('\x1b[0m | '+color)+'\x1b[0m |')
})
console.log(line)
}
getResponse(URL)
|