-
Notifications
You must be signed in to change notification settings - Fork 1
/
2-get-accounts-info.js
162 lines (143 loc) · 4.04 KB
/
2-get-accounts-info.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
/**
* @author UMU618 <[email protected]>
* @copyright MEET.ONE 2018
* @description Use block-always-using-brace npm-coding-style.
*/
'use strict'
const DEFAULT_OUTPUT_FILE_NAME_PREFIX = '2-info@'
let inputPath = ''
let lineNumber = 1
let url = ''
let outputPath = ''
// parse arguments
{
const CONST = require('./const.js')
const po = require('commander')
po
.version('0.1.0')
.arguments('<input> [line-number]', 'File that contains account names.')
.action(function (input, line) {
inputPath = input
if (line) {
lineNumber = parseInt(line)
if (lineNumber < 1) {
lineNumber = 1
}
}
})
.option('-u, --url <TEXT>'
, 'the http/https URL where nodeos is running. Default to '
+ CONST.LOCAL.URL)
.option('-k, --kylin', 'Equal to --url ' + CONST.KYLIN.URL)
.option('-m, --mainnet', 'Equal to --url ' + CONST.MAINNET.URL)
.option('-s, --sidechain', 'Equal to --url ' + CONST.SIDECHAIN.URL)
.option('-o, --output <FILE>', 'Write to FILE, will be appended!')
.option('-p, --output-prefix <NAME>', 'Output filename prefix')
.on('--help', function () {
console.log('')
console.log('Examples:')
console.log(' ' + process.argv0 + ' ' + process.argv[1]
+ ' --url http://kylin.fn.eosbixin.com')
})
.parse(process.argv)
if (!inputPath) {
po.outputHelp()
process.exit(-1)
}
console.log('Input file: ' + inputPath)
console.log('Line number(1 base): ' + lineNumber)
if (po.url) {
url = po.url
if (po.kylin) {
console.log('--kylin is overridden, use ' + url)
}
if (po.mainnet) {
console.log('--mainnet is overridden, use ' + url)
}
} else if (po.kylin) {
url = CONST.KYLIN.URL
} else if (po.mainnet) {
url = CONST.MAINNET.URL
} else if (po.sidechain) {
url = CONST.SIDECHAIN.URL
} else {
url = CONST.LOCAL.URL
}
let u = new URL(url)
url = u.origin
console.log('URL: ' + url)
if (po.output) {
outputPath = po.output
} else {
if (po.outputPrefix) {
outputPath = po.outputPrefix
} else if (po.kylin) {
outputPath = CONST.KYLIN.NAME + '-'
} else if (po.mainnet) {
outputPath = CONST.MAINNET.NAME + '-'
} else if (po.sidechain) {
outputPath = CONST.SIDECHAIN.NAME + '-'
} else if (po.url) {
outputPath = u.hostname + '-'
} else {
outputPath = CONST.LOCAL.NAME + '-'
}
const moment = require('moment')
outputPath += DEFAULT_OUTPUT_FILE_NAME_PREFIX
+ moment().format('YYYY-MM-DD[T]HH-mm-ss.SSS[Z]ZZ') + '.txt'
}
console.log('Output file: ' + outputPath)
}
const EosApi = require('eosjs-api')
const options = {
httpEndpoint: url
, verbose: false
, logger: {
log: null //console.log
, error: console.error
}
, fetchConfiguration: {}
}
const eos = EosApi(options)
const fs = require('fs')
const all = fs.readFileSync(inputPath, 'utf8')
const lines = all.split(/\n/)
const lineCount = lines[lines.length - 1]
? lines.length
: lines.length - 1
console.log(`Line count: ${lineCount}`)
const ws = fs.createWriteStream(outputPath
, { flags: 'a', encoding: 'utf8', autoClose: true }
)
let retry = 0
GetAccountKeys()
async function GetAccountKeys() {
while (lineNumber <= lineCount) {
// line index = line number - 1
let accountName = lines[lineNumber - 1]
if (retry == 0) {
console.log('[' + lineNumber + '] ' + accountName)
} else {
console.log('[' + lineNumber + '] Retry on ' + accountName + ' for '
+ retry + ' times...')
}
await eos.getAccount(accountName).then((res) => {
retry = 0
if (res) {
let buffer = JSON.stringify(res) + '\n'
ws.write(buffer)
} else {
console.log('[' + lineNumber + '] Error on ' + accountName)
ws.write('{"accountName": "' + accountName + '"}')
}
lineNumber++
}, (err) => {
// retry
if (++retry > 3) {
throw new Error('[' + lineNumber + '] Retry failed on ' + accountName)
}
})
}
console.log('Done!')
}