forked from WietseWind/xrp-ledgerstats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseLedgerFile.js
More file actions
141 lines (132 loc) · 5.33 KB
/
parseLedgerFile.js
File metadata and controls
141 lines (132 loc) · 5.33 KB
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
process.stdout.write('\033c')
const numeral = require('numeral')
const fs = require('fs')
const { table } = require('table')
const instructions = () => {
console.log('Error: please enter the (fetched) ledger number (after the command to execute)')
console.log('You can find the ledger number by checking the ./data/ folder (without the `.json` part)')
console.log('')
console.log(' eg. npm run stats 32570')
console.log('')
console.log('If you didn\'t fetch this ledger data first:')
console.log(' npm run fetch')
console.log('')
process.exit(0)
}
if (process.argv.length < 3) {
instructions()
}
let filename = parseInt(process.argv[2].split('.')[0]) + '.json'
let exportJson = {
meta: {},
top100Balance: 0,
accountPercentageBalance: [],
accountNumberBalanceRange: []
}
if (fs.existsSync(__dirname + '/data/' + filename)) {
console.log('Reading ledger stats:', filename)
let data = JSON.parse(fs.readFileSync(__dirname + '/data/' + filename))
data.balances.sort((a, b) => { return (a.b > b.b) ? -1 : ((b.b > a.b) ? 1 : 0) })
let numberOfAccounts = data.balances.length
console.log(' -- Accounts: ', numberOfAccounts)
exportJson.meta.numberAccounts = numberOfAccounts
console.log(' -- Ledger close time: ', data.stats.close_time_human)
exportJson.meta.ledgerClosedAt = data.stats.close_time_human
console.log(' -- Ledger hash: ', data.stats.hash)
exportJson.meta.ledgerHash = data.stats.hash
console.log(' -- Ledger index: ', data.stats.ledger_index)
exportJson.meta.ledgerIndex = data.stats.ledger_index
console.log(' -- Total XRP existing: ', numeral(data.stats.total_coins).format('0,0.000000'))
exportJson.meta.existingXRP = data.stats.total_coins
console.log('')
console.log('Calculating spendable XRP sum')
let balanceSum = data.balances.reduce((a, b) => { return a + b.b }, 0)
console.log(' -- Accounts balance sum: ', numeral(balanceSum).format('0,0.000000'))
exportJson.top100Balance = balanceSum
console.log('')
console.log('Stats 🎉')
console.log('')
console.log(' -- Top 100')
let top100sum = data.balances.slice(0, 100).reduce((a, b) => { return a + b.b }, 0)
console.log(' > Balance sum ', numeral(top100sum).format('0,0.000000'))
console.log(' > Balance percentage (vs. Total XRP existing) ', numeral(top100sum / data.stats.total_coins * 100).format('0.00') + ' %')
console.log(' > Balance percentage (vs. Accounts balance sum)', numeral(top100sum / balanceSum * 100).format('0.00') + ' %')
console.log('')
console.log(' -- Percentage of accounts with balance starting at...')
let percentages = [ 0.01, 0.1, 0.2, 0.5, 1, 2, 3, 4, 5, 10 ]
let pctAccountsBalance = [ [ 'Percentage', '# Accounts', 'Balance equals (or greater than)' ] ]
percentages.forEach(p => {
let n = Math.round(numberOfAccounts / 100 * p)
let e = data.balances.slice(0, n).reverse()[0].b
pctAccountsBalance.push([
p + ' %',
numeral(n).format('0,0.'),
numeral(e).format('0,0.000000') + ' XRP'
])
exportJson.accountPercentageBalance.push({
percentage: p,
numberAccounts: n,
balanceEqGt: e
})
})
console.log(table(pctAccountsBalance, {
columns: {
0: { alignment: 'right' },
1: { alignment: 'right' },
2: { alignment: 'right' }
}
}))
// numberOfAccounts
console.log('')
console.log(' -- Number of accounts and sum of balance range')
let balanceranges = [ 1000000000, 500000000, 100000000, 20000000, 10000000, 5000000, 1000000, 500000, 100000, 75000, 50000, 25000, 10000, 5000, 1000, 500, 20, 0 ]
let noAccountsBalanceRange = [ [ '# Accounts', 'Balance from', '... To', 'Sum (XRP)' ] ]
let sliceFrom = 0
let lastBalanceRange = 0
let XbalanceSum = 0
balanceranges.forEach(b => {
let a = 0
let f = b
let t = lastBalanceRange
let s = 0
for (let i = sliceFrom; i < numberOfAccounts; i++) {
if (data.balances[i].b < f) {
sliceFrom = i
break;
} else {
s += data.balances[i].b
a++
}
}
noAccountsBalanceRange.push([
numeral(a).format('0,0.'),
numeral(f).format('0,0.'),
lastBalanceRange === 0 ? '∞' : numeral(t).format('0,0.'),
numeral(Math.round(s)).format('0,0.000000')
])
exportJson.accountNumberBalanceRange.push({
numberAccounts: a,
balanceFrom: f,
balanceTo: t,
balanceSum: s
})
lastBalanceRange = b
})
console.log(table(noAccountsBalanceRange, {
columns: {
0: { alignment: 'right' },
1: { alignment: 'right' },
3: { alignment: 'right' }
}
}))
console.log('')
console.log('')
console.log('---')
console.log('Writing stats...')
let exportFilename = __dirname + '/data/' + filename.replace(/.json$/, '.stats.json')
fs.writeFileSync(exportFilename, JSON.stringify(exportJson), 'utf8');
console.log(' > Stats written as JSON object to: ', exportFilename)
console.log('')
} else {
instructions()
}