forked from WietseWind/xrp-ledgerstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchLedger.js
106 lines (95 loc) · 3.33 KB
/
fetchLedger.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
process.stdout.write('\033c')
const numeral = require('numeral')
const fs = require('fs')
const JSONStream = require('JSONStream')
const WebSocket = require('ws')
let endpoint = 'wss://s1.ripple.com'
if (process.argv.length > 3) {
endpoint = process.argv[3]
if (!endpoint.match(/^ws[s]*:\/\//)) {
endpoint = 'wss://' + endpoint
}
}
console.log('Connecting to rippled running at:', endpoint)
console.log('')
const ws = new WebSocket(endpoint)
let ledger = null
let calls = 0
let records = 0
let lastMarker = ''
let transformStream
let outputStream
const send = (requestJson) => {
calls++
ws.send(JSON.stringify(requestJson))
}
ws.on('open', function open () {
let requestLedgerIndex = 'closed'
if (process.argv.length > 2) {
if (process.argv[2].match(/^[0-9]+$/)) {
requestLedgerIndex = parseInt(process.argv[2])
}
}
send({ command: 'ledger', ledger_index: requestLedgerIndex })
})
let req = { command: 'ledger_data', ledger: null, type: 'account', limit: 20000 }
ws.on('message', function incoming (data) {
const r = JSON.parse(data)
if (ledger === null) {
if (typeof r.error_message === 'undefined') {
ledger = r.result.ledger
req.ledger = ledger.hash
console.log('Now fetching XRP ledger', ledger.ledger_index)
console.log('')
console.log(' -- Ledger close time: ', ledger.close_time_human)
console.log(' -- Ledger hash: ', ledger.hash)
console.log(' -- Total XRP existing: ', numeral(parseInt(ledger.total_coins) / 1000000).format('0,0.000000'))
console.log('')
let filename = ledger.ledger_index + '.json'
let stats = {
hash: ledger.hash,
ledger_index: parseInt(ledger.ledger_index),
close_time_human: ledger.close_time_human,
total_coins: parseInt(ledger.total_coins) / 1000000
}
transformStream = JSONStream.stringify('{\n "stats": ' + JSON.stringify(stats) + ',\n "balances": [\n ', ',\n ', '\n ]\n}\n')
outputStream = fs.createWriteStream(__dirname + '/data/' + filename)
transformStream.pipe(outputStream)
outputStream.on('finish', function handleFinish () {
console.log('')
console.log('Done! wrote records:', records, 'to:', './data/' + filename)
console.log('')
console.log('Now you can retrieve the stats for this ledger by running:')
console.log(' npm run stats ' + ledger.ledger_index)
console.log('')
process.exit(0)
})
send(req)
} else {
console.log('Error from rippled:', r.error_message)
ws.close()
}
} else {
if (r.status && r.status === 'success' && r.type && r.type === 'response') {
if (r.result.state !== null) {
r.result.state.forEach((i) => {
records++
transformStream.write({ a: i.Account, b: parseInt(i.Balance) / 1000000 })
})
}
process.stdout.write(' > Retrieved ' + records + ' accounts in ' + calls + ' calls to rippled...' + "\r");
if (typeof r.result.marker === 'undefined' || r.result.marker === null || r.result.marker === lastMarker) {
// No new marker
console.log('')
transformStream.end()
} else {
// Continue
req.marker = r.result.marker
lastMarker = req.marker
send(req)
}
} else {
throw new Error('Response error...')
}
}
})