-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject-fields.js
192 lines (179 loc) · 4.81 KB
/
inject-fields.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const fs = require ('fs');
const ccxt = require ('../ccxt.js')
// use at your own risk
const tradeKeys = [ 'info',
'timestamp',
'datetime',
'symbol',
'id',
'order',
'type',
'takerOrMaker',
'side',
'price',
'amount',
'cost',
'fee'
]
const marketKeys = [ 'id',
'symbol',
'base',
'quote',
'baseId',
'quoteId',
'active',
'precision',
'limits',
'info'
]
const currencyKeys = [ 'id',
'code',
'name',
'active',
'fee',
'precision',
'limits',
'info'
]
const tickerKeys = [
'symbol',
'info',
'timestamp',
'datetime',
'high',
'low',
'bid',
'bidVolume',
'ask',
'askVolume',
'vwap',
'open',
'close',
'last',
'previousClose',
'change',
'percentage',
'average',
'baseVolume',
'quoteVolume'
]
const orderKeys = [
'info',
'id',
'clientOrderId',
'timestamp',
'datetime',
'lastTradeTimestamp',
'symbol',
'type',
'side',
'price',
'amount',
'cost',
'average',
'filled',
'remaining',
'status',
'trades',
'fee'
]
const things = [ tradeKeys, marketKeys, currencyKeys, tickerKeys, orderKeys ]
// instead of counting by unique keys we could also count by number of shared keys
function runAllExchanges () {
const classNames = fs.readdirSync ('./js')
.filter (file => file.includes ('.js')).map (className => './js/' + className)
for (const className of classNames) {
searchFor (className)
}
}
function searchFor (filename) {
const lines = fs.readFileSync (filename).toString ().split ('\n')
const toInject = []
const flatten = ccxt.flatten (things)
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
let found = null
for (const key of flatten) {
found = line.match (new RegExp ('^\\s+\'' + key + '\':'))
if (found) {
break
}
}
let injected = undefined
if (found) {
const { top, bottom } = extractObject (lines, i)
i = bottom
const sectionLines = lines.slice (top, bottom)
const begin = sectionLines[0]
sectionLines[0] = begin.match (/{.*/)[0]
const end = sectionLines[sectionLines.length - 1]
sectionLines[sectionLines.length - 1] = '}'
const section = sectionLines.join ('\n')
.replace (/\/\/.*$/mg, '')
.replace (/':\s.*,/g, '\': undefined,')
//console.log (section)
try {
eval (`injected = ${section}`)
} catch (e) {
continue
}
let maxCount = 0,
maxThing = []
const injectedKeys = Object.keys (injected)
for (const thing of things) {
let count = 0
for (const key of injectedKeys) {
if (thing.includes (key)) {
count += 1
} else {
count = 0
break
}
}
if (count > maxCount) {
maxCount = count
maxThing = thing
}
}
if (maxCount > maxThing.length / 2) {
sectionLines[0] = begin
sectionLines.length = sectionLines.length - 1
const indent = sectionLines[sectionLines.length - 1].match (/^\s+/)[0].length
for (const missingKey of maxThing.filter (k => !injectedKeys.includes (k))) {
sectionLines.push (' '.repeat (indent) + '\'' + missingKey + '\': ' + (missingKey === 'precision' || missingKey === 'limits' ? ('this.' + missingKey) : 'undefined') + ',')
}
sectionLines.push (end)
toInject.push ({top, bottom, sectionLines})
}
}
}
// iterate through toInject backwards injeceting the stuff using splice
for (let i = toInject.length - 1; i >= 0; i--) {
const injector = toInject[i]
const { top, bottom, sectionLines } = injector
lines.splice (top, bottom - top, ...sectionLines)
}
const output = lines.join ('\n')
fs.writeFileSync (filename, output)
}
function extractObject (lines, i) {
let depth = 1
let top = i
while (depth > 0) {
top -= 1
const line = lines[top]
depth += (line.match (/}/g) || []).length
depth -= (line.match (/{/g) || []).length
}
depth = 1
let bottom = i
while (depth > 0) {
bottom += 1
const line = lines[bottom]
depth += (line.match (/{/g) || []).length
depth -= (line.match (/}/g) || []).length
}
bottom += 1
return { top, bottom }
}
runAllExchanges ()