FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
edge-react-gui/scripts/ratesCacheReplay.ts at develop · EdgeApp/edge-react-gui · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
EdgeApp
/
edge-react-gui
Public
Notifications
You must be signed in to change notification settings
Fork
293
Star
519
Code
Issues
51
Pull requests
144
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
edge-react-gui
/
scripts
/
ratesCacheReplay.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
317 lines (293 loc) · 10.4 KB
Breadcrumbs
edge-react-gui
/
scripts
/
ratesCacheReplay.ts
Copy path
File metadata and controls
317 lines (293 loc) · 10.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// ---------------------------------------------------------------------------
// ratesCacheReplay
//
// Replays a logged exchange-rate cache blob (the `***Exchange Rate Cache***`
// section captured in the support logs, or any JSON matching that shape)
// against the rates server: it rebuilds the `v3/rates` queries the app would
// have sent for the pairs in the blob, sends them, and reports what the server
// returns for every requested pair — so a rate problem seen in a user's logs
// can be reproduced and inspected directly.
//
// Query construction mirrors `convertToRatesParams` in
// `src/actions/ExchangeRateActions.ts` — keep the two in sync. Only the
// subscribed pairs present in the blob (`cryptoPairs` / `fiatPairs`) are
// replayed; wallet-derived and historical pairs the app adds at runtime are
// not part of the cache and so are not reproduced here.
//
// Each requested pair is reported as one of:
// <rate> the server returned a rate
// NO RATE the server returned the pair but declined to price it
// MISSING the server omitted the pair from its response entirely
//
// Usage:
// node -r sucrase/register scripts/ratesCacheReplay.ts <blob.json>
// pbpaste | node -r sucrase/register scripts/ratesCacheReplay.ts
// node -r sucrase/register scripts/ratesCacheReplay.ts <blob.json> --server https://rates4.edge.app
// node -r sucrase/register scripts/ratesCacheReplay.ts <blob.json> --dry-run
//
// Input may be the bare cache JSON or a log excerpt containing it — the first
// complete JSON object in the text is used. `--server` picks which rates host
// to hit (default rates3; point it at rates4 to compare hosts). `--dry-run`
// prints the query bodies without sending them.
// ---------------------------------------------------------------------------
import
*
as
fs
from
'fs'
// Mirrors RATES_SERVER_MAX_QUERY_SIZE in ExchangeRateActions.ts:
const
RATES_SERVER_MAX_QUERY_SIZE
=
100
// Default v3 rate server (see RATES_SERVERS in src/util/network.ts):
const
DEFAULT_RATES_SERVER
=
'https://rates3.edge.app'
// Mirrors removeIsoPrefix in src/util/utils.ts:
const
removeIsoPrefix
=
(
currencyCode
:
string
)
:
string
=>
currencyCode
.
replace
(
'iso:'
,
''
)
interface
CryptoAsset
{
pluginId
:
string
tokenId
?:
string
|
null
}
interface
CryptoFiatPair
{
asset
:
CryptoAsset
targetFiat
:
string
isoDate
?:
string
expiration
:
number
}
interface
FiatFiatPair
{
fiatCode
:
string
targetFiat
:
string
isoDate
?:
string
expiration
:
number
}
interface
RateCacheBlob
{
cryptoPairs
?:
CryptoFiatPair
[
]
fiatPairs
?:
FiatFiatPair
[
]
}
interface
RatesQuery
{
targetFiat
:
string
crypto
:
Array
<
{
isoDate
:
string
;
asset
:
CryptoAsset
}
>
fiat
:
Array
<
{
isoDate
:
string
;
fiatCode
:
string
}
>
}
interface
RatesResponse
{
crypto
?:
Array
<
{
asset
?:
CryptoAsset
;
rate
?:
number
|
null
}
>
fiat
?:
Array
<
{
fiatCode
?:
string
;
rate
?:
number
|
null
}
>
}
type
Outcome
=
'resolved'
|
'no-rate'
|
'missing'
interface
PairResult
{
label
:
string
outcome
:
Outcome
rate
?:
number
|
null
}
/** Identity for matching a response entry back to the pair we asked for. */
const
cryptoKey
=
(
asset
:
CryptoAsset
)
:
string
=>
`
${
asset
.
pluginId
}
:
${
asset
.
tokenId
??
''
}
`
/**
* Group the cached pairs by targetFiat and chunk them into query bodies,
* exactly as convertToRatesParams does in the app.
*/
function
cacheToQueries
(
blob
:
RateCacheBlob
,
isoNow
:
string
)
:
RatesQuery
[
]
{
const
cryptoPairs
=
blob
.
cryptoPairs
??
[
]
const
fiatPairs
=
blob
.
fiatPairs
??
[
]
const
grouped
=
new
Map
<
string
,
{
crypto
:
CryptoFiatPair
[
]
;
fiat
:
FiatFiatPair
[
]
}
>
(
)
const
bucket
=
(
targetFiat
:
string
)
:
{
crypto
:
CryptoFiatPair
[
]
;
fiat
:
FiatFiatPair
[
]
}
=>
{
let
entry
=
grouped
.
get
(
targetFiat
)
if
(
entry
==
null
)
{
entry
=
{
crypto
:
[
]
,
fiat
:
[
]
}
grouped
.
set
(
targetFiat
,
entry
)
}
return
entry
}
for
(
const
pair
of
cryptoPairs
)
bucket
(
pair
.
targetFiat
)
.
crypto
.
push
(
pair
)
for
(
const
pair
of
fiatPairs
)
bucket
(
pair
.
targetFiat
)
.
fiat
.
push
(
pair
)
const
queries
:
RatesQuery
[
]
=
[
]
for
(
const
[
targetFiat
,
{
crypto
,
fiat
}
]
of
grouped
.
entries
(
)
)
{
const
remainingCrypto
=
[
...
crypto
]
const
remainingFiat
=
[
...
fiat
]
while
(
remainingCrypto
.
length
>
0
||
remainingFiat
.
length
>
0
)
{
const
cryptoChunk
=
remainingCrypto
.
splice
(
0
,
RATES_SERVER_MAX_QUERY_SIZE
)
const
fiatChunk
=
remainingFiat
.
splice
(
0
,
RATES_SERVER_MAX_QUERY_SIZE
)
queries
.
push
(
{
targetFiat
:
removeIsoPrefix
(
targetFiat
)
,
crypto
:
cryptoChunk
.
map
(
pair
=>
(
{
isoDate
:
pair
.
isoDate
==
null
?
isoNow
:
new
Date
(
pair
.
isoDate
)
.
toISOString
(
)
,
asset
:
pair
.
asset
}
)
)
,
fiat
:
fiatChunk
.
map
(
pair
=>
(
{
isoDate
:
pair
.
isoDate
==
null
?
isoNow
:
new
Date
(
pair
.
isoDate
)
.
toISOString
(
)
,
fiatCode
:
removeIsoPrefix
(
pair
.
fiatCode
)
}
)
)
}
)
}
}
return
queries
}
/**
* Pull the first complete JSON object out of arbitrary text, so a pasted log
* excerpt (with surrounding lines) works as well as bare JSON.
*/
function
extractFirstJsonObject
(
text
:
string
)
:
string
{
const
start
=
text
.
indexOf
(
'{'
)
if
(
start
<
0
)
throw
new
Error
(
'No JSON object found in input'
)
let
depth
=
0
let
inString
=
false
let
escaped
=
false
for
(
let
i
=
start
;
i
<
text
.
length
;
i
++
)
{
const
ch
=
text
[
i
]
if
(
inString
)
{
if
(
escaped
)
escaped
=
false
else
if
(
ch
===
'\\'
)
escaped
=
true
else
if
(
ch
===
'"'
)
inString
=
false
continue
}
if
(
ch
===
'"'
)
inString
=
true
else
if
(
ch
===
'{'
)
depth
++
else
if
(
ch
===
'}'
)
{
depth
--
if
(
depth
===
0
)
return
text
.
slice
(
start
,
i
+
1
)
}
}
throw
new
Error
(
'Unbalanced JSON object in input'
)
}
/**
* Match every pair we asked for against what the server actually returned.
*/
function
matchResults
(
query
:
RatesQuery
,
response
:
RatesResponse
)
:
PairResult
[
]
{
const
cryptoByKey
=
new
Map
<
string
,
{
rate
?:
number
|
null
}
>
(
)
for
(
const
entry
of
response
.
crypto
??
[
]
)
{
if
(
entry
.
asset
!=
null
)
cryptoByKey
.
set
(
cryptoKey
(
entry
.
asset
)
,
entry
)
}
const
fiatByCode
=
new
Map
<
string
,
{
rate
?:
number
|
null
}
>
(
)
for
(
const
entry
of
response
.
fiat
??
[
]
)
{
if
(
entry
.
fiatCode
!=
null
)
fiatByCode
.
set
(
entry
.
fiatCode
,
entry
)
}
const
results
:
PairResult
[
]
=
[
]
for
(
const
requested
of
query
.
crypto
)
{
const
label
=
requested
.
asset
.
tokenId
==
null
?
requested
.
asset
.
pluginId
:
`
${
requested
.
asset
.
pluginId
}
:
${
requested
.
asset
.
tokenId
}
`
const
found
=
cryptoByKey
.
get
(
cryptoKey
(
requested
.
asset
)
)
if
(
found
==
null
)
results
.
push
(
{
label
,
outcome
:
'missing'
}
)
else
if
(
found
.
rate
==
null
)
results
.
push
(
{
label
,
outcome
:
'no-rate'
}
)
else
results
.
push
(
{
label
,
outcome
:
'resolved'
,
rate
:
found
.
rate
}
)
}
for
(
const
requested
of
query
.
fiat
)
{
const
label
=
`
${
requested
.
fiatCode
}
(fiat)`
const
found
=
fiatByCode
.
get
(
requested
.
fiatCode
)
if
(
found
==
null
)
results
.
push
(
{
label
,
outcome
:
'missing'
}
)
else
if
(
found
.
rate
==
null
)
results
.
push
(
{
label
,
outcome
:
'no-rate'
}
)
else
results
.
push
(
{
label
,
outcome
:
'resolved'
,
rate
:
found
.
rate
}
)
}
return
results
}
function
printResults
(
results
:
PairResult
[
]
)
:
void
{
const
width
=
results
.
reduce
(
(
max
,
r
)
=>
Math
.
max
(
max
,
r
.
label
.
length
)
,
0
)
for
(
const
result
of
results
)
{
const
value
=
result
.
outcome
===
'resolved'
?
String
(
result
.
rate
)
:
result
.
outcome
.
toUpperCase
(
)
console
.
log
(
`
${
result
.
label
.
padEnd
(
width
)
}
${
value
}
`
)
}
}
async
function
main
(
)
:
Promise
<
void
>
{
const
argv
=
process
.
argv
.
slice
(
2
)
let
server
=
DEFAULT_RATES_SERVER
let
dryRun
=
false
let
file
:
string
|
undefined
for
(
let
i
=
0
;
i
<
argv
.
length
;
i
++
)
{
const
arg
=
argv
[
i
]
if
(
arg
===
'--dry-run'
)
dryRun
=
true
else
if
(
arg
===
'--server'
)
server
=
argv
[
++
i
]
else
if
(
!
arg
.
startsWith
(
'--'
)
)
file
=
arg
}
const
raw
=
file
!=
null
&&
file
!==
'-'
?
fs
.
readFileSync
(
file
,
'utf8'
)
:
fs
.
readFileSync
(
0
,
'utf8'
)
const
blob
=
JSON
.
parse
(
extractFirstJsonObject
(
raw
)
)
as
RateCacheBlob
const
queries
=
cacheToQueries
(
blob
,
new
Date
(
)
.
toISOString
(
)
)
const
cryptoCount
=
blob
.
cryptoPairs
?.
length
??
0
const
fiatCount
=
blob
.
fiatPairs
?.
length
??
0
console
.
log
(
`
${
queries
.
length
}
quer
${
queries
.
length
===
1
?
'y'
:
'ies'
}
from
${
cryptoCount
}
crypto +
${
fiatCount
}
fiat cached pairs`
)
if
(
dryRun
)
{
for
(
const
query
of
queries
)
console
.
log
(
JSON
.
stringify
(
query
)
)
return
}
const
totals
=
{
resolved
:
0
,
'no-rate'
:
0
,
missing
:
0
}
let
failedQueries
=
0
for
(
const
[
index
,
query
]
of
queries
.
entries
(
)
)
{
console
.
log
(
`\nquery
${
index
}
targetFiat=
${
query
.
targetFiat
}
${
query
.
crypto
.
length
}
crypto +
${
query
.
fiat
.
length
}
fiat ->
${
server
}
/v3/rates`
)
const
started
=
Date
.
now
(
)
let
response
try
{
response
=
await
fetch
(
`
${
server
}
/v3/rates`
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
query
)
}
)
}
catch
(
error
)
{
failedQueries
++
console
.
log
(
` request failed:
${
String
(
error
)
}
`
)
continue
}
const
elapsed
=
Date
.
now
(
)
-
started
const
text
=
await
response
.
text
(
)
if
(
!
response
.
ok
)
{
failedQueries
++
console
.
log
(
` HTTP
${
response
.
status
}
(
${
elapsed
}
ms):
${
text
.
slice
(
0
,
300
)
}
`
)
continue
}
let
parsed
:
RatesResponse
try
{
parsed
=
JSON
.
parse
(
text
)
}
catch
(
error
)
{
failedQueries
++
console
.
log
(
` HTTP 200 (
${
elapsed
}
ms) but unparseable body:
${
text
.
slice
(
0
,
300
)
}
`
)
continue
}
const
results
=
matchResults
(
query
,
parsed
)
console
.
log
(
` HTTP
${
response
.
status
}
(
${
elapsed
}
ms)`
)
printResults
(
results
)
const
counts
=
{
resolved
:
0
,
'no-rate'
:
0
,
missing
:
0
}
for
(
const
result
of
results
)
counts
[
result
.
outcome
]
++
for
(
const
key
of
Object
.
keys
(
counts
)
as
Outcome
[
]
)
totals
[
key
]
+=
counts
[
key
]
console
.
log
(
` ->
${
counts
.
resolved
}
resolved,
${
counts
[
'no-rate'
]
}
no rate,
${
counts
.
missing
}
missing`
)
}
console
.
log
(
`\nTOTAL:
${
totals
.
resolved
}
resolved,
${
totals
[
'no-rate'
]
}
no rate,
${
totals
.
missing
}
missing across
${
queries
.
length
}
quer
${
queries
.
length
===
1
?
'y'
:
'ies'
}
${
failedQueries
>
0
?
` (
${
failedQueries
}
failed)`
:
''
}
`
)
if
(
totals
.
missing
>
0
||
totals
[
'no-rate'
]
>
0
||
failedQueries
>
0
)
{
process
.
exitCode
=
1
}
}
main
(
)
.
catch
(
(
error
:
unknown
)
=>
{
console
.
error
(
String
(
error
)
)
process
.
exitCode
=
1
}
)
Back
|
FazBrowse Home
|
New Git URL